qa-app-votes.php 9.45 KB
Newer Older
Gideon Greenspan committed
1 2 3
<?php

/*
Gideon Greenspan committed
4
	Question2Answer by Gideon Greenspan and contributors
Gideon Greenspan committed
5 6 7

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-app-votes.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Handling incoming votes (application level)


	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 29 30 31 32 33 34 35 36 37 38
	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
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}


	function qa_vote_error_html($post, $vote, $userid, $topage)
/*
	Check if $userid can vote on $post, on the page $topage.
	Return an HTML error to display if there was a problem, or false if it's OK.
*/
	{
Gideon Greenspan committed
39
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
40

Gideon Greenspan committed
41 42 43
		// The 'login', 'confirm', 'limit', 'userblock' and 'ipblock' permission errors are reported to the user here.
		// Others ('approve', 'level') prevent the buttons being clickable in the first place, in qa_get_vote_view(...)

Gideon Greenspan committed
44 45
		require_once QA_INCLUDE_DIR.'qa-app-users.php';
		require_once QA_INCLUDE_DIR.'qa-app-limits.php';
Scott Vivian committed
46

Gideon Greenspan committed
47 48 49 50 51 52
		if (
			is_array($post) &&
			( ($post['basetype']=='Q') || ($post['basetype']=='A') ) &&
			qa_opt(($post['basetype']=='Q') ? 'voting_on_qs' : 'voting_on_as') &&
			( (!isset($post['userid'])) || (!isset($userid)) || ($post['userid']!=$userid) )
		) {
Gideon Greenspan committed
53
			$permiterror=qa_user_post_permit_error(($post['basetype']=='Q') ? 'permit_vote_q' : 'permit_vote_a', $post, QA_LIMIT_VOTES);
Scott Vivian committed
54

Gideon Greenspan committed
55 56
			$errordownonly=(!$permiterror) && ($vote<0);
			if ($errordownonly)
Gideon Greenspan committed
57
				$permiterror=qa_user_post_permit_error('permit_vote_down', $post);
Scott Vivian committed
58

Gideon Greenspan committed
59 60 61 62
			switch ($permiterror) {
				case 'login':
					return qa_insert_login_links(qa_lang_html('main/vote_must_login'), $topage);
					break;
Scott Vivian committed
63

Gideon Greenspan committed
64 65 66
				case 'confirm':
					return qa_insert_login_links(qa_lang_html($errordownonly ? 'main/vote_down_must_confirm' : 'main/vote_must_confirm'), $topage);
					break;
Scott Vivian committed
67

Gideon Greenspan committed
68 69 70
				case 'limit':
					return qa_lang_html('main/vote_limit');
					break;
Scott Vivian committed
71

Gideon Greenspan committed
72 73 74
				default:
					return qa_lang_html('users/no_permission');
					break;
Scott Vivian committed
75

Gideon Greenspan committed
76 77 78
				case false:
					return false;
			}
Scott Vivian committed
79

Gideon Greenspan committed
80 81 82 83
		} else
			return qa_lang_html('main/vote_not_allowed'); // voting option should not have been presented (but could happen due to options change)
	}

Scott Vivian committed
84

Gideon Greenspan committed
85 86 87 88 89 90
	function qa_vote_set($post, $userid, $handle, $cookieid, $vote)
/*
	Actually set (application level) the $vote (-1/0/1) by $userid (with $handle and $cookieid) on $postid.
	Handles user points, recounting and event reports as appropriate.
*/
	{
Gideon Greenspan committed
91
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
92

Gideon Greenspan committed
93 94 95 96 97
		require_once QA_INCLUDE_DIR.'qa-db-points.php';
		require_once QA_INCLUDE_DIR.'qa-db-hotness.php';
		require_once QA_INCLUDE_DIR.'qa-db-votes.php';
		require_once QA_INCLUDE_DIR.'qa-db-post-create.php';
		require_once QA_INCLUDE_DIR.'qa-app-limits.php';
Scott Vivian committed
98

Gideon Greenspan committed
99 100 101 102 103
		$vote=(int)min(1, max(-1, $vote));
		$oldvote=(int)qa_db_uservote_get($post['postid'], $userid);

		qa_db_uservote_set($post['postid'], $userid, $vote);
		qa_db_post_recount_votes($post['postid']);
Scott Vivian committed
104

Gideon Greenspan committed
105
		$postisanswer=($post['basetype']=='A');
Scott Vivian committed
106

Gideon Greenspan committed
107 108 109 110
		if ($postisanswer) {
			qa_db_post_acount_update($post['parentid']);
			qa_db_unupaqcount_update();
		}
Scott Vivian committed
111

Gideon Greenspan committed
112
		$columns=array();
Scott Vivian committed
113

Gideon Greenspan committed
114 115 116 117 118
		if ( ($vote>0) || ($oldvote>0) )
			$columns[]=$postisanswer ? 'aupvotes' : 'qupvotes';

		if ( ($vote<0) || ($oldvote<0) )
			$columns[]=$postisanswer ? 'adownvotes' : 'qdownvotes';
Scott Vivian committed
119

Gideon Greenspan committed
120
		qa_db_points_update_ifuser($userid, $columns);
Scott Vivian committed
121

Gideon Greenspan committed
122
		qa_db_points_update_ifuser($post['userid'], array($postisanswer ? 'avoteds' : 'qvoteds', 'upvoteds', 'downvoteds'));
Scott Vivian committed
123

Gideon Greenspan committed
124 125
		if ($post['basetype']=='Q')
			qa_db_hotness_update($post['postid']);
Scott Vivian committed
126

Gideon Greenspan committed
127 128 129 130 131 132
		if ($vote<0)
			$event=$postisanswer ? 'a_vote_down' : 'q_vote_down';
		elseif ($vote>0)
			$event=$postisanswer ? 'a_vote_up' : 'q_vote_up';
		else
			$event=$postisanswer ? 'a_vote_nil' : 'q_vote_nil';
Scott Vivian committed
133

Gideon Greenspan committed
134 135
		qa_report_event($event, $userid, $handle, $cookieid, array(
			'postid' => $post['postid'],
136
			'userid' => $post['userid'],
Gideon Greenspan committed
137 138 139 140
			'vote' => $vote,
			'oldvote' => $oldvote,
		));
	}
Scott Vivian committed
141 142


Gideon Greenspan committed
143 144 145 146 147 148
	function qa_flag_error_html($post, $userid, $topage)
/*
	Check if $userid can flag $post, on the page $topage.
	Return an HTML error to display if there was a problem, or false if it's OK.
*/
	{
Gideon Greenspan committed
149
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
150

Gideon Greenspan committed
151 152 153
		// The 'login', 'confirm', 'limit', 'userblock' and 'ipblock' permission errors are reported to the user here.
		// Others ('approve', 'level') prevent the flag button being shown, in qa_page_q_post_rules(...)

Gideon Greenspan committed
154 155 156 157 158 159 160 161 162 163
		require_once QA_INCLUDE_DIR.'qa-db-selects.php';
		require_once QA_INCLUDE_DIR.'qa-app-options.php';
		require_once QA_INCLUDE_DIR.'qa-app-users.php';
		require_once QA_INCLUDE_DIR.'qa-app-limits.php';

		if (
			is_array($post) &&
			qa_opt('flagging_of_posts') &&
			( (!isset($post['userid'])) || (!isset($userid)) || ($post['userid']!=$userid) )
		) {
Scott Vivian committed
164

Gideon Greenspan committed
165
			switch (qa_user_post_permit_error('permit_flag', $post, QA_LIMIT_FLAGS)) {
Gideon Greenspan committed
166 167 168
				case 'login':
					return qa_insert_login_links(qa_lang_html('question/flag_must_login'), $topage);
					break;
Scott Vivian committed
169

Gideon Greenspan committed
170 171 172
				case 'confirm':
					return qa_insert_login_links(qa_lang_html('question/flag_must_confirm'), $topage);
					break;
Scott Vivian committed
173

Gideon Greenspan committed
174 175 176
				case 'limit':
					return qa_lang_html('question/flag_limit');
					break;
Scott Vivian committed
177

Gideon Greenspan committed
178 179 180
				default:
					return qa_lang_html('users/no_permission');
					break;
Scott Vivian committed
181

Gideon Greenspan committed
182 183 184
				case false:
					return false;
			}
Scott Vivian committed
185

Gideon Greenspan committed
186 187 188
		} else
			return qa_lang_html('question/flag_not_allowed'); // flagging option should not have been presented
	}
Scott Vivian committed
189

Gideon Greenspan committed
190 191 192 193 194 195 196 197

	function qa_flag_set_tohide($oldpost, $userid, $handle, $cookieid, $question)
/*
	Set (application level) a flag by $userid (with $handle and $cookieid) on $oldpost which belongs to $question.
	Handles recounting, admin notifications and event reports as appropriate.
	Returns true if the post should now be hidden because it has accumulated enough flags.
*/
	{
Gideon Greenspan committed
198
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
199

Gideon Greenspan committed
200 201
		require_once QA_INCLUDE_DIR.'qa-db-votes.php';
		require_once QA_INCLUDE_DIR.'qa-app-limits.php';
Gideon Greenspan committed
202
		require_once QA_INCLUDE_DIR.'qa-db-post-update.php';
Scott Vivian committed
203

Gideon Greenspan committed
204 205
		qa_db_userflag_set($oldpost['postid'], $userid, true);
		qa_db_post_recount_flags($oldpost['postid']);
Gideon Greenspan committed
206
		qa_db_flaggedcount_update();
Scott Vivian committed
207

Gideon Greenspan committed
208 209 210 211
		switch ($oldpost['basetype']) {
			case 'Q':
				$event='q_flag';
				break;
Scott Vivian committed
212

Gideon Greenspan committed
213 214 215 216 217 218 219 220
			case 'A':
				$event='a_flag';
				break;

			case 'C':
				$event='c_flag';
				break;
		}
Scott Vivian committed
221

Gideon Greenspan committed
222
		$post=qa_db_select_with_pending(qa_db_full_post_selectspec(null, $oldpost['postid']));
Scott Vivian committed
223

Gideon Greenspan committed
224 225 226 227 228 229 230
		qa_report_event($event, $userid, $handle, $cookieid, array(
			'postid' => $oldpost['postid'],
			'oldpost' => $oldpost,
			'flagcount' => $post['flagcount'],
			'questionid' => $question['postid'],
			'question' => $question,
		));
Scott Vivian committed
231

Gideon Greenspan committed
232 233 234 235 236 237 238 239 240 241
		return ($post['flagcount']>=qa_opt('flagging_hide_after')) && !$post['hidden'];
	}


	function qa_flag_clear($oldpost, $userid, $handle, $cookieid)
/*
	Clear (application level) a flag on $oldpost by $userid (with $handle and $cookieid).
	Handles recounting and event reports as appropriate.
*/
	{
Gideon Greenspan committed
242
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
243

Gideon Greenspan committed
244 245
		require_once QA_INCLUDE_DIR.'qa-db-votes.php';
		require_once QA_INCLUDE_DIR.'qa-app-limits.php';
Gideon Greenspan committed
246
		require_once QA_INCLUDE_DIR.'qa-db-post-update.php';
Scott Vivian committed
247

Gideon Greenspan committed
248 249
		qa_db_userflag_set($oldpost['postid'], $userid, false);
		qa_db_post_recount_flags($oldpost['postid']);
Gideon Greenspan committed
250
		qa_db_flaggedcount_update();
Scott Vivian committed
251

Gideon Greenspan committed
252 253 254 255
		switch ($oldpost['basetype']) {
			case 'Q':
				$event='q_unflag';
				break;
Scott Vivian committed
256

Gideon Greenspan committed
257 258 259 260 261 262 263 264
			case 'A':
				$event='a_unflag';
				break;

			case 'C':
				$event='c_unflag';
				break;
		}
Scott Vivian committed
265

Gideon Greenspan committed
266 267 268 269 270
		qa_report_event($event, $userid, $handle, $cookieid, array(
			'postid' => $oldpost['postid'],
			'oldpost' => $oldpost,
		));
	}
Scott Vivian committed
271 272


Gideon Greenspan committed
273 274 275 276 277 278
	function qa_flags_clear_all($oldpost, $userid, $handle, $cookieid)
/*
	Clear (application level) all flags on $oldpost by $userid (with $handle and $cookieid).
	Handles recounting and event reports as appropriate.
*/
	{
Gideon Greenspan committed
279
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
280

Gideon Greenspan committed
281 282
		require_once QA_INCLUDE_DIR.'qa-db-votes.php';
		require_once QA_INCLUDE_DIR.'qa-app-limits.php';
Gideon Greenspan committed
283
		require_once QA_INCLUDE_DIR.'qa-db-post-update.php';
Scott Vivian committed
284

Gideon Greenspan committed
285 286
		qa_db_userflags_clear_all($oldpost['postid']);
		qa_db_post_recount_flags($oldpost['postid']);
Gideon Greenspan committed
287
		qa_db_flaggedcount_update();
Gideon Greenspan committed
288 289 290 291 292

		switch ($oldpost['basetype']) {
			case 'Q':
				$event='q_clearflags';
				break;
Scott Vivian committed
293

Gideon Greenspan committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
			case 'A':
				$event='a_clearflags';
				break;

			case 'C':
				$event='c_clearflags';
				break;
		}

		qa_report_event($event, $userid, $handle, $cookieid, array(
			'postid' => $oldpost['postid'],
			'oldpost' => $oldpost,
		));
	}


/*
	Omit PHP closing tag to help avoid accidental output
*/