qa-ajax-answer.php 3.31 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-ajax-answer.php
	Version: See define()s at top of qa-include/qa-base.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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28
	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
*/

	require_once QA_INCLUDE_DIR.'qa-app-users.php';
	require_once QA_INCLUDE_DIR.'qa-app-limits.php';
Gideon Greenspan committed
29
	require_once QA_INCLUDE_DIR.'qa-db-selects.php';
Gideon Greenspan committed
30 31


Gideon Greenspan committed
32
//	Load relevant information about this question
Scott Vivian committed
33

Gideon Greenspan committed
34 35
	$questionid=qa_post_text('a_questionid');
	$userid=qa_get_logged_in_userid();
Scott Vivian committed
36

Gideon Greenspan committed
37 38 39 40 41
	list($question, $childposts)=qa_db_select_with_pending(
		qa_db_full_post_selectspec($userid, $questionid),
		qa_db_full_child_posts_selectspec($userid, $questionid)
	);

Gideon Greenspan committed
42

Gideon Greenspan committed
43 44 45
//	Check if the question exists, is not closed, and whether the user has permission to do this

	if ((@$question['basetype']=='Q') && (!isset($question['closedbyid'])) && !qa_user_post_permit_error('permit_post_a', $question, QA_LIMIT_ANSWERS)) {
Gideon Greenspan committed
46 47 48 49 50 51 52 53
		require_once QA_INCLUDE_DIR.'qa-app-captcha.php';
		require_once QA_INCLUDE_DIR.'qa-app-format.php';
		require_once QA_INCLUDE_DIR.'qa-app-post-create.php';
		require_once QA_INCLUDE_DIR.'qa-app-cookies.php';
		require_once QA_INCLUDE_DIR.'qa-page-question-view.php';
		require_once QA_INCLUDE_DIR.'qa-page-question-submit.php';


Gideon Greenspan committed
54
	//	Try to create the new answer
Scott Vivian committed
55

Gideon Greenspan committed
56 57 58
		$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 Vivian committed
59

Gideon Greenspan committed
60
	//	If successful, page content will be updated via Ajax
Gideon Greenspan committed
61

Gideon Greenspan committed
62 63
		if (isset($answerid)) {
			$answer=qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $answerid));
Scott Vivian committed
64

Gideon Greenspan committed
65 66
			$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 Vivian committed
67

Gideon Greenspan committed
68
			$usershtml=qa_userids_handles_html(array($answer), true);
Scott Vivian committed
69

Gideon Greenspan committed
70
			$a_view=qa_page_q_answer_view($question, $answer, false, $usershtml, false);
Scott Vivian committed
71

Gideon Greenspan committed
72
			$themeclass=qa_load_theme_class(qa_get_site_theme(), 'ajax-answer', null, null);
Scott Vivian committed
73

Gideon Greenspan committed
74
			echo "QA_AJAX_RESPONSE\n1\n";
Gideon Greenspan committed
75

Scott Vivian committed
76

Gideon Greenspan committed
77
		//	Send back whether the 'answer' button should still be visible
Scott Vivian committed
78

Gideon Greenspan committed
79
			echo (int)qa_opt('allow_multi_answers')."\n";
Gideon Greenspan committed
80

Scott Vivian committed
81

Gideon Greenspan committed
82
		//	Send back the count of answers
Scott Vivian committed
83

Gideon Greenspan committed
84
			$countanswers=$question['acount']+1;
Gideon Greenspan committed
85

Gideon Greenspan committed
86 87 88 89
			if ($countanswers==1)
				echo qa_lang_html('question/1_answer_title')."\n";
			else
				echo qa_lang_html_sub('question/x_answers_title', $countanswers)."\n";
Gideon Greenspan committed
90 91


Gideon Greenspan committed
92
		//	Send back the HTML
Gideon Greenspan committed
93

Gideon Greenspan committed
94
			$themeclass->a_list_item($a_view);
Gideon Greenspan committed
95

Gideon Greenspan committed
96
			return;
Gideon Greenspan committed
97 98
		}
	}
Scott Vivian committed
99

Gideon Greenspan committed
100 101 102

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

Scott Vivian committed
103

Gideon Greenspan committed
104 105 106
/*
	Omit PHP closing tag to help avoid accidental output
*/