answer.php 3.14 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-ajax-answer.php
	Description: Server-side response to Ajax create answer requests


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
23 24 25
require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'app/limits.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
Scott committed
26 27


Scott committed
28
// Load relevant information about this question
Scott committed
29

Scott committed
30 31
$questionid = qa_post_text('a_questionid');
$userid = qa_get_logged_in_userid();
Scott committed
32

Scott committed
33 34 35 36
list($question, $childposts) = qa_db_select_with_pending(
	qa_db_full_post_selectspec($userid, $questionid),
	qa_db_full_child_posts_selectspec($userid, $questionid)
);
Scott committed
37 38


Scott committed
39
// Check if the question exists, is not closed, and whether the user has permission to do this
Scott committed
40

Scott committed
41 42 43 44 45 46 47
if (@$question['basetype'] == 'Q' && !isset($question['closedbyid']) && !qa_user_post_permit_error('permit_post_a', $question, QA_LIMIT_ANSWERS)) {
	require_once QA_INCLUDE_DIR . 'app/captcha.php';
	require_once QA_INCLUDE_DIR . 'app/format.php';
	require_once QA_INCLUDE_DIR . 'app/post-create.php';
	require_once QA_INCLUDE_DIR . 'app/cookies.php';
	require_once QA_INCLUDE_DIR . 'pages/question-view.php';
	require_once QA_INCLUDE_DIR . 'pages/question-submit.php';
Scott committed
48 49


Scott committed
50
	// Try to create the new answer
Scott committed
51

Scott committed
52 53 54
	$usecaptcha = qa_user_use_captcha(qa_user_level_for_post($question));
	$answers = qa_page_q_load_as($question, $childposts);
	$answerid = qa_page_q_add_a_submit($question, $answers, $usecaptcha, $in, $errors);
Scott committed
55

Scott committed
56
	// If successful, page content will be updated via Ajax
Scott committed
57

Scott committed
58 59
	if (isset($answerid)) {
		$answer = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $answerid));
Scott committed
60

Scott committed
61 62
		$question = $question + qa_page_q_post_rules($question, null, null, $childposts); // array union
		$answer = $answer + qa_page_q_post_rules($answer, $question, $answers, null);
Scott committed
63

Scott committed
64
		$usershtml = qa_userids_handles_html(array($answer), true);
Scott committed
65

Scott committed
66
		$a_view = qa_page_q_answer_view($question, $answer, false, $usershtml, false);
Scott committed
67

Scott committed
68 69
		$themeclass = qa_load_theme_class(qa_get_site_theme(), 'ajax-answer', null, null);
		$themeclass->initialize();
Scott committed
70

Scott committed
71
		echo "QA_AJAX_RESPONSE\n1\n";
Scott committed
72 73


Scott committed
74
		// Send back whether the 'answer' button should still be visible
Scott committed
75

Scott committed
76
		echo (int)qa_opt('allow_multi_answers') . "\n";
Scott committed
77 78


Scott committed
79
		// Send back the count of answers
Scott committed
80

Scott committed
81
		$countanswers = $question['acount'] + 1;
Scott committed
82

83
		if ($countanswers == 1) {
Scott committed
84
			echo qa_lang_html('question/1_answer_title') . "\n";
85
		} else {
Scott committed
86
			echo qa_lang_html_sub('question/x_answers_title', $countanswers) . "\n";
87
		}
Scott committed
88 89


Scott committed
90
		// Send back the HTML
Scott committed
91

Scott committed
92
		$themeclass->a_list_item($a_view);
Scott committed
93

Scott committed
94
		return;
Scott committed
95
	}
Scott committed
96
}
Scott committed
97 98


Scott committed
99
echo "QA_AJAX_RESPONSE\n0\n"; // fall back to non-Ajax submission if there were any problems