votes.php 4.77 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: Database-level access to votes tables


	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
	exit;
}


/**
 * Set the vote for $userid on $postid to $vote in the database
30 31 32
 * @param int $postid
 * @param mixed $userid
 * @param int $vote
Scott committed
33 34 35 36 37 38
 */
function qa_db_uservote_set($postid, $userid, $vote)
{
	$vote = max(min(($vote), 1), -1);

	qa_db_query_sub(
Scott committed
39
		'INSERT INTO ^uservotes (postid, userid, vote, flag, votecreated) VALUES (#, #, #, 0, NOW()) ON DUPLICATE KEY UPDATE vote=#, voteupdated=NOW()',
Scott committed
40 41 42 43 44 45 46
		$postid, $userid, $vote, $vote
	);
}


/**
 * Get the vote for $userid on $postid from the database (or NULL if none)
47 48 49
 * @param int $postid
 * @param mixed $userid
 * @return string|null
Scott committed
50 51 52 53 54 55 56 57 58 59 60 61
 */
function qa_db_uservote_get($postid, $userid)
{
	return qa_db_read_one_value(qa_db_query_sub(
		'SELECT vote FROM ^uservotes WHERE postid=# AND userid=#',
		$postid, $userid
	), true);
}


/**
 * Set the flag for $userid on $postid to $flag (true or false) in the database
62 63 64
 * @param int $postid
 * @param mixed $userid
 * @param bool $flag
Scott committed
65 66 67
 */
function qa_db_userflag_set($postid, $userid, $flag)
{
68
	$flag = (int)$flag;
Scott committed
69 70 71 72 73 74 75 76 77 78

	qa_db_query_sub(
		'INSERT INTO ^uservotes (postid, userid, vote, flag) VALUES (#, #, 0, #) ON DUPLICATE KEY UPDATE flag=#',
		$postid, $userid, $flag, $flag
	);
}


/**
 * Clear all flags for $postid in the database
79
 * @param int $postid
Scott committed
80 81 82 83 84 85 86 87 88 89 90 91
 */
function qa_db_userflags_clear_all($postid)
{
	qa_db_query_sub(
		'UPDATE ^uservotes SET flag=0 WHERE postid=#',
		$postid
	);
}


/**
 * Recalculate the cached count of upvotes, downvotes and netvotes for $postid in the database
92
 * @param int $postid
Scott committed
93 94 95
 */
function qa_db_post_recount_votes($postid)
{
96
	if (qa_should_update_counts()) {
Scott committed
97
		qa_db_query_sub(
Scott committed
98 99
			'UPDATE ^posts AS x, (SELECT COALESCE(SUM(GREATEST(0,vote)),0) AS upvotes, -COALESCE(SUM(LEAST(0,vote)),0) AS downvotes FROM ^uservotes WHERE postid=#) AS a SET x.upvotes=a.upvotes, x.downvotes=a.downvotes, x.netvotes=a.upvotes-a.downvotes WHERE x.postid=#',
			$postid, $postid
Scott committed
100
		);
101
	}
Scott committed
102
}
Scott committed
103 104


Scott committed
105 106
/**
 * Recalculate the cached count of flags for $postid in the database
107
 * @param int $postid
Scott committed
108 109 110
 */
function qa_db_post_recount_flags($postid)
{
111
	if (qa_should_update_counts()) {
Scott committed
112
		qa_db_query_sub(
Scott committed
113 114
			'UPDATE ^posts AS x, (SELECT COALESCE(SUM(IF(flag, 1, 0)),0) AS flagcount FROM ^uservotes WHERE postid=#) AS a SET x.flagcount=a.flagcount WHERE x.postid=#',
			$postid, $postid
Scott committed
115
		);
116
	}
Scott committed
117 118 119 120 121
}


/**
 * Returns all non-zero votes on post $postid from the database as an array of [userid] => [vote]
122
 * @param int $postid
Scott committed
123 124 125 126 127 128 129 130 131 132 133 134 135
 * @return array
 */
function qa_db_uservote_post_get($postid)
{
	return qa_db_read_all_assoc(qa_db_query_sub(
		'SELECT userid, vote FROM ^uservotes WHERE postid=# AND vote!=0',
		$postid
	), 'userid', 'vote');
}


/**
 * Returns all the postids from the database for posts that $userid has voted on or flagged
136
 * @param mixed $userid
Scott committed
137 138 139 140 141
 * @return array
 */
function qa_db_uservoteflag_user_get($userid)
{
	return qa_db_read_all_values(qa_db_query_sub(
Scott committed
142
		'SELECT postid FROM ^uservotes WHERE userid=# AND (vote!=0 OR flag!=0)',
Scott committed
143 144 145 146 147 148 149
		$userid
	));
}


/**
 * Return information about all the non-zero votes and/or flags on the posts in postids, including user handles for internal user management
150
 * @param array $postids
Scott committed
151 152 153 154 155
 * @return array
 */
function qa_db_uservoteflag_posts_get($postids)
{
	if (QA_FINAL_EXTERNAL_USERS) {
Scott committed
156
		return qa_db_read_all_assoc(qa_db_query_sub(
Scott committed
157
			'SELECT postid, userid, vote, flag, votecreated, voteupdated FROM ^uservotes WHERE postid IN (#) AND (vote!=0 OR flag!=0)',
Scott committed
158 159 160 161
			$postids
		));
	} else {
		return qa_db_read_all_assoc(qa_db_query_sub(
Scott committed
162
			'SELECT postid, handle, vote, flag, votecreated, voteupdated FROM ^uservotes LEFT JOIN ^users ON ^uservotes.userid=^users.userid WHERE postid IN (#) AND (vote!=0 OR flag!=0)',
Scott committed
163
			$postids
Scott committed
164 165
		));
	}
Scott committed
166 167 168 169 170 171 172 173 174 175 176 177 178
}


/**
 * Remove all votes assigned to a post that had been cast by the owner of the post.
 * @param int $postid The post ID from which the owner's votes will be removed.
 */
function qa_db_uservote_remove_own($postid)
{
	qa_db_query_sub(
		'DELETE uv FROM ^uservotes uv JOIN ^posts p ON uv.postid=p.postid AND uv.userid=p.userid WHERE uv.postid=#', $postid
	);
}