Commit 0794e784 by Scott

Coding style (db posts/users)

parent f5aaabc6
...@@ -20,145 +20,159 @@ ...@@ -20,145 +20,159 @@
More about this license: http://www.question2answer.org/license.php 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 if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../'); header('Location: ../');
exit; exit;
} }
function qa_db_uservote_set($postid, $userid, $vote) /**
/* * Set the vote for $userid on $postid to $vote in the database
Set the vote for $userid on $postid to $vote in the database * @param $postid
*/ * @param $userid
{ * @param $vote
$vote=max(min(($vote), 1), -1); */
function qa_db_uservote_set($postid, $userid, $vote)
qa_db_query_sub( {
'INSERT INTO ^uservotes (postid, userid, vote, flag) VALUES (#, #, #, 0) ON DUPLICATE KEY UPDATE vote=#', $vote = max(min(($vote), 1), -1);
$postid, $userid, $vote, $vote
); qa_db_query_sub(
} 'INSERT INTO ^uservotes (postid, userid, vote, flag) VALUES (#, #, #, 0) ON DUPLICATE KEY UPDATE vote=#',
$postid, $userid, $vote, $vote
);
function qa_db_uservote_get($postid, $userid) }
/*
Get the vote for $userid on $postid from the database (or NULL if none)
*/ /**
{ * Get the vote for $userid on $postid from the database (or NULL if none)
return qa_db_read_one_value(qa_db_query_sub( * @param $postid
'SELECT vote FROM ^uservotes WHERE postid=# AND userid=#', * @param $userid
$postid, $userid * @return mixed|null
), true); */
} function qa_db_uservote_get($postid, $userid)
{
return qa_db_read_one_value(qa_db_query_sub(
function qa_db_userflag_set($postid, $userid, $flag) 'SELECT vote FROM ^uservotes WHERE postid=# AND userid=#',
/* $postid, $userid
Set the flag for $userid on $postid to $flag (true or false) in the database ), true);
*/ }
{
$flag=$flag ? 1 : 0;
/**
* Set the flag for $userid on $postid to $flag (true or false) in the database
* @param $postid
* @param $userid
* @param $flag
*/
function qa_db_userflag_set($postid, $userid, $flag)
{
$flag = $flag ? 1 : 0;
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
* @param $postid
*/
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
* @param $postid
*/
function qa_db_post_recount_votes($postid)
{
if (qa_should_update_counts())
qa_db_query_sub( qa_db_query_sub(
'INSERT INTO ^uservotes (postid, userid, vote, flag) VALUES (#, #, 0, #) ON DUPLICATE KEY UPDATE flag=#', '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, $userid, $flag, $flag $postid, $postid
); );
} }
function qa_db_userflags_clear_all($postid) /**
/* * Recalculate the cached count of flags for $postid in the database
Clear all flags for $postid in the database * @param $postid
*/ */
{ function qa_db_post_recount_flags($postid)
{
if (qa_should_update_counts())
qa_db_query_sub( qa_db_query_sub(
'UPDATE ^uservotes SET flag=0 WHERE postid=#', '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, $postid
); );
} }
function qa_db_post_recount_votes($postid) /**
/* * Returns all non-zero votes on post $postid from the database as an array of [userid] => [vote]
Recalculate the cached count of upvotes, downvotes and netvotes for $postid in the database * @param $postid
*/ * @return array
{ */
if (qa_should_update_counts()) function qa_db_uservote_post_get($postid)
qa_db_query_sub( {
'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=#', return qa_db_read_all_assoc(qa_db_query_sub(
$postid, $postid 'SELECT userid, vote FROM ^uservotes WHERE postid=# AND vote!=0',
); $postid
} ), 'userid', 'vote');
}
function qa_db_post_recount_flags($postid)
/* /**
Recalculate the cached count of flags for $postid in the database * Returns all the postids from the database for posts that $userid has voted on or flagged
*/ * @param $userid
{ * @return array
if (qa_should_update_counts()) */
qa_db_query_sub( function qa_db_uservoteflag_user_get($userid)
'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 return qa_db_read_all_values(qa_db_query_sub(
); 'SELECT postid FROM ^uservotes WHERE userid=# AND (vote!=0) OR (flag!=0)',
} $userid
));
}
function qa_db_uservote_post_get($postid)
/*
Returns all non-zero votes on post $postid from the database as an array of [userid] => [vote] /**
*/ * Return information about all the non-zero votes and/or flags on the posts in postids, including user handles for internal user management
{ * @param $postids
* @return array
*/
function qa_db_uservoteflag_posts_get($postids)
{
if (QA_FINAL_EXTERNAL_USERS) {
return qa_db_read_all_assoc(qa_db_query_sub( return qa_db_read_all_assoc(qa_db_query_sub(
'SELECT userid, vote FROM ^uservotes WHERE postid=# AND vote!=0', 'SELECT postid, userid, vote, flag FROM ^uservotes WHERE postid IN (#) AND ((vote!=0) OR (flag!=0))',
$postid $postids
), 'userid', 'vote'); ));
} } else {
return qa_db_read_all_assoc(qa_db_query_sub(
'SELECT postid, handle, vote, flag FROM ^uservotes LEFT JOIN ^users ON ^uservotes.userid=^users.userid WHERE postid IN (#) AND ((vote!=0) OR (flag!=0))',
function qa_db_uservoteflag_user_get($userid) $postids
/*
Returns all the postids from the database for posts that $userid has voted on or flagged
*/
{
return qa_db_read_all_values(qa_db_query_sub(
'SELECT postid FROM ^uservotes WHERE userid=# AND (vote!=0) OR (flag!=0)',
$userid
)); ));
} }
}
function qa_db_uservoteflag_posts_get($postids)
/* /**
Return information about all the non-zero votes and/or flags on the posts in postids, including user handles for internal user management * 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.
if (QA_FINAL_EXTERNAL_USERS) */
return qa_db_read_all_assoc(qa_db_query_sub( function qa_db_uservote_remove_own($postid)
'SELECT postid, userid, vote, flag FROM ^uservotes WHERE postid IN (#) AND ((vote!=0) OR (flag!=0))', {
$postids 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
);
else }
return qa_db_read_all_assoc(qa_db_query_sub(
'SELECT postid, handle, vote, flag FROM ^uservotes LEFT JOIN ^users ON ^uservotes.userid=^users.userid WHERE postid IN (#) AND ((vote!=0) OR (flag!=0))',
$postids
));
}
/**
* 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
);
}
/*
Omit PHP closing tag to help avoid accidental output
*/
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment