votes.php 9.81 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
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	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.

	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
22
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
23
	header('Location: ../../');
Scott committed
24 25 26 27 28 29 30
	exit;
}


/**
 * 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.
31 32 33 34 35
 * @param array $post
 * @param int $vote
 * @param mixed $userid
 * @param string $topage
 * @return bool|string
Scott committed
36 37 38 39 40 41 42 43 44 45 46
 */
function qa_vote_error_html($post, $vote, $userid, $topage)
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

	// 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(...)

	require_once QA_INCLUDE_DIR . 'app/users.php';
	require_once QA_INCLUDE_DIR . 'app/limits.php';

47 48 49 50 51 52 53
	if ($post['hidden']) {
		return qa_lang_html('main/vote_disabled_hidden');
	}
	if ($post['queued']) {
		return qa_lang_html('main/vote_disabled_queued');
	}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	switch($post['basetype'])
	{
		case 'Q':
			$allowVoting = qa_opt('voting_on_qs');
			break;
		case 'A':
			$allowVoting = qa_opt('voting_on_as');
			break;
		case 'C':
			$allowVoting = qa_opt('voting_on_cs');
			break;
		default:
			$allowVoting = false;
			break;
	}

	if (!$allowVoting || (isset($post['userid']) && isset($userid) && $post['userid'] == $userid)) {
71 72 73
		// voting option should not have been presented (but could happen due to options change)
		return qa_lang_html('main/vote_not_allowed');
	}
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

	$permiterror = qa_user_post_permit_error(($post['basetype'] == 'Q') ? 'permit_vote_q' : 'permit_vote_a', $post, QA_LIMIT_VOTES);

	$errordownonly = !$permiterror && $vote < 0;
	if ($errordownonly) {
		$permiterror = qa_user_post_permit_error('permit_vote_down', $post);
	}

	switch ($permiterror) {
		case false:
			return false;
			break;

		case 'login':
			return qa_insert_login_links(qa_lang_html('main/vote_must_login'), $topage);
			break;

		case 'confirm':
			return qa_insert_login_links(qa_lang_html($errordownonly ? 'main/vote_down_must_confirm' : 'main/vote_must_confirm'), $topage);
			break;

		case 'limit':
			return qa_lang_html('main/vote_limit');
			break;

		default:
			return qa_lang_html('users/no_permission');
			break;
	}
Scott committed
103 104 105 106 107 108
}


/**
 * 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.
109 110 111 112 113 114
 * @param array $post
 * @param mixed $userid
 * @param string $handle
 * @param string $cookieid
 * @param int $vote
 * @return mixed
Scott committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
 */
function qa_vote_set($post, $userid, $handle, $cookieid, $vote)
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

	require_once QA_INCLUDE_DIR . 'db/points.php';
	require_once QA_INCLUDE_DIR . 'db/hotness.php';
	require_once QA_INCLUDE_DIR . 'db/votes.php';
	require_once QA_INCLUDE_DIR . 'db/post-create.php';
	require_once QA_INCLUDE_DIR . 'app/limits.php';

	$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']);

132 133 134 135 136
	if (!in_array($post['basetype'], array('Q', 'A', 'C'))) {
		return;
	}

	$prefix = strtolower($post['basetype']);
Scott committed
137

138
	if ($prefix === 'a') {
Scott committed
139 140
		qa_db_post_acount_update($post['parentid']);
		qa_db_unupaqcount_update();
Scott committed
141 142
	}

Scott committed
143 144
	$columns = array();

145
	if ($vote > 0 || $oldvote > 0) {
146
		$columns[] = $prefix . 'upvotes';
147
	}
Scott committed
148

149
	if ($vote < 0 || $oldvote < 0) {
150
		$columns[] = $prefix . 'downvotes';
151
	}
Scott committed
152 153 154

	qa_db_points_update_ifuser($userid, $columns);

155
	qa_db_points_update_ifuser($post['userid'], array($prefix . 'voteds', 'upvoteds', 'downvoteds'));
Scott committed
156

157
	if ($prefix === 'q') {
Scott committed
158
		qa_db_hotness_update($post['postid']);
159
	}
Scott committed
160

161
	if ($vote < 0) {
162
		$event = $prefix . '_vote_down';
163
	} elseif ($vote > 0) {
164
		$event = $prefix . '_vote_up';
165
	} else {
166
		$event = $prefix . '_vote_nil';
167
	}
Scott committed
168 169 170 171 172 173 174 175 176 177 178 179 180

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


/**
 * 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.
181 182 183 184
 * @param array $post
 * @param mixed $userid
 * @param string $topage
 * @return bool|string|array
Scott committed
185 186 187 188 189 190 191 192 193 194 195 196 197
 */
function qa_flag_error_html($post, $userid, $topage)
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

	// 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(...)

	require_once QA_INCLUDE_DIR . 'db/selects.php';
	require_once QA_INCLUDE_DIR . 'app/options.php';
	require_once QA_INCLUDE_DIR . 'app/users.php';
	require_once QA_INCLUDE_DIR . 'app/limits.php';

198
	if (is_array($post) && qa_opt('flagging_of_posts') &&
Scott committed
199 200 201 202 203 204
		(!isset($post['userid']) || !isset($userid) || $post['userid'] != $userid)
	) {
		switch (qa_user_post_permit_error('permit_flag', $post, QA_LIMIT_FLAGS)) {
			case 'login':
				return qa_insert_login_links(qa_lang_html('question/flag_must_login'), $topage);
				break;
Scott committed
205

Scott committed
206 207
			case 'confirm':
				return qa_insert_login_links(qa_lang_html('question/flag_must_confirm'), $topage);
Scott committed
208 209
				break;

Scott committed
210 211
			case 'limit':
				return qa_lang_html('question/flag_limit');
Scott committed
212 213
				break;

Scott committed
214 215
			default:
				return qa_lang_html('users/no_permission');
Scott committed
216
				break;
Scott committed
217 218 219

			case false:
				return false;
Scott committed
220
		}
221
	} else {
Scott committed
222
		return qa_lang_html('question/flag_not_allowed'); // flagging option should not have been presented
223
	}
Scott committed
224 225 226 227 228 229 230
}


/**
 * 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.
231 232 233 234 235
 * @param array $oldpost
 * @param mixed $userid
 * @param string $handle
 * @param string $cookieid
 * @param array $question
Scott committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
 * @return bool
 */
function qa_flag_set_tohide($oldpost, $userid, $handle, $cookieid, $question)
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

	require_once QA_INCLUDE_DIR . 'db/votes.php';
	require_once QA_INCLUDE_DIR . 'app/limits.php';
	require_once QA_INCLUDE_DIR . 'db/post-update.php';

	qa_db_userflag_set($oldpost['postid'], $userid, true);
	qa_db_post_recount_flags($oldpost['postid']);
	qa_db_flaggedcount_update();

	switch ($oldpost['basetype']) {
		case 'Q':
			$event = 'q_flag';
			break;

		case 'A':
			$event = 'a_flag';
			break;

		case 'C':
			$event = 'c_flag';
			break;
Scott committed
262 263
	}

Scott committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	$post = qa_db_select_with_pending(qa_db_full_post_selectspec(null, $oldpost['postid']));

	qa_report_event($event, $userid, $handle, $cookieid, array(
		'postid' => $oldpost['postid'],
		'oldpost' => $oldpost,
		'flagcount' => $post['flagcount'],
		'questionid' => $question['postid'],
		'question' => $question,
	));

	return $post['flagcount'] >= qa_opt('flagging_hide_after') && !$post['hidden'];
}


/**
 * Clear (application level) a flag on $oldpost by $userid (with $handle and $cookieid).
 * Handles recounting and event reports as appropriate.
281 282 283 284
 * @param array $oldpost
 * @param mixed $userid
 * @param string $handle
 * @param string $cookieid
Scott committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
 * @return mixed
 */
function qa_flag_clear($oldpost, $userid, $handle, $cookieid)
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

	require_once QA_INCLUDE_DIR . 'db/votes.php';
	require_once QA_INCLUDE_DIR . 'app/limits.php';
	require_once QA_INCLUDE_DIR . 'db/post-update.php';

	qa_db_userflag_set($oldpost['postid'], $userid, false);
	qa_db_post_recount_flags($oldpost['postid']);
	qa_db_flaggedcount_update();

	switch ($oldpost['basetype']) {
		case 'Q':
			$event = 'q_unflag';
			break;

		case 'A':
			$event = 'a_unflag';
			break;

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

Scott committed
313 314 315 316 317 318 319 320 321 322
	qa_report_event($event, $userid, $handle, $cookieid, array(
		'postid' => $oldpost['postid'],
		'oldpost' => $oldpost,
	));
}


/**
 * Clear (application level) all flags on $oldpost by $userid (with $handle and $cookieid).
 * Handles recounting and event reports as appropriate.
323 324 325 326
 * @param array $oldpost
 * @param mixed $userid
 * @param string $handle
 * @param string $cookieid
Scott committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
 * @return mixed
 */
function qa_flags_clear_all($oldpost, $userid, $handle, $cookieid)
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

	require_once QA_INCLUDE_DIR . 'db/votes.php';
	require_once QA_INCLUDE_DIR . 'app/limits.php';
	require_once QA_INCLUDE_DIR . 'db/post-update.php';

	qa_db_userflags_clear_all($oldpost['postid']);
	qa_db_post_recount_flags($oldpost['postid']);
	qa_db_flaggedcount_update();

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

		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,
	));
}