Gazelle/sections/ajax/takevote.php

216 lines
6.1 KiB
PHP
Raw Normal View History

2012-10-27 08:00:09 +00:00
<?
authorize();
$GroupID = $_REQUEST['groupid'];
if (!is_number($GroupID)) {
echo 'Invalid Group';
die();
}
// What groups has this guy voted?
2012-11-02 08:00:18 +00:00
$UserVotes = Votes::get_user_votes($LoggedUser['ID']);
2012-10-27 08:00:09 +00:00
// What are the votes for this group?
2012-11-02 08:00:18 +00:00
$GroupVotes = Votes::get_group_votes($GroupID);
2012-10-27 08:00:09 +00:00
$UserID = $LoggedUser['ID'];
if ($_REQUEST['do'] == 'vote') {
if (isset($UserVotes[$GroupID]) || !check_perms('site_album_votes')) {
echo 'noaction';
die();
}
if ($_REQUEST['vote'] != 'up' && $_REQUEST['vote'] != 'down') {
echo 'badvote';
die();
}
2013-05-14 08:00:34 +00:00
$Type = ($_REQUEST['vote'] == 'up') ? 'Up' : 'Down';
2013-02-22 08:00:24 +00:00
2012-10-28 08:00:19 +00:00
// Update the two votes tables if needed
2013-05-14 08:00:34 +00:00
$DB->query("
INSERT IGNORE INTO users_votes (UserID, GroupID, Type)
VALUES ($UserID, $GroupID, '$Type')");
2012-10-28 08:00:19 +00:00
if ($DB->affected_rows() == 0) {
echo 'noaction';
die();
}
2013-02-22 08:00:24 +00:00
2012-10-27 08:00:09 +00:00
// Update the group's cache key
$GroupVotes['Total'] += 1;
2013-05-14 08:00:34 +00:00
if ($Type == 'Up') {
2012-10-27 08:00:09 +00:00
$GroupVotes['Ups'] += 1;
}
2013-09-06 08:00:41 +00:00
$Cache->cache_value("votes_$GroupID", $GroupVotes);
2013-02-22 08:00:24 +00:00
2012-10-27 08:00:09 +00:00
// If the group has no votes yet, we need an insert, otherwise an update
// so we can cut corners and use the magic of INSERT...ON DUPLICATE KEY UPDATE...
// to accomplish both in one query
2013-09-06 08:00:41 +00:00
$DB->query("
INSERT INTO torrents_votes
(GroupID, Total, Ups, Score)
VALUES
($GroupID, 1, ".($Type == 'Up' ? 1 : 0).", 0)
ON DUPLICATE KEY UPDATE
Total = Total + 1,
Score = IFNULL(binomial_ci(Ups".($Type == 'Up' ? '+1' : '').", Total), 0)".
($Type == 'Up' ? ', Ups = Ups + 1' : ''));
2013-02-22 08:00:24 +00:00
2012-10-27 08:00:09 +00:00
$UserVotes[$GroupID] = array('GroupID' => $GroupID, 'Type' => $Type);
2013-02-22 08:00:24 +00:00
2012-10-27 08:00:09 +00:00
// Update this guy's cache key
$Cache->cache_value('voted_albums_'.$LoggedUser['ID'], $UserVotes);
2013-02-22 08:00:24 +00:00
2012-10-27 08:00:09 +00:00
// Update the paired cache keys for "people who liked"
2013-05-30 08:00:30 +00:00
// First update this album's paired votes. If this keys is magically not set,
// our life just got a bit easier. We're only tracking paired votes on upvotes.
2012-10-27 08:00:09 +00:00
if ($Type == 'Up') {
2013-09-06 08:00:41 +00:00
$VotePairs = $Cache->get_value("vote_pairs_$GroupID", true);
2013-04-13 08:00:19 +00:00
if ($VotePairs !== false) {
2012-10-27 08:00:09 +00:00
foreach ($UserVotes as $Vote) {
if ($Vote['GroupID'] == $GroupID) {
continue;
}
// Go through each of his other votes, incrementing the
// corresponding keys in this groups vote_pairs array
if (isset($VotePairs[$Vote['GroupID']])) {
$VotePairs[$Vote['GroupID']]['Total'] += 1;
if ($Vote['Type'] == 'Up') {
$VotePairs[$Vote['GroupID']]['Ups'] += 1;
}
} else {
2013-09-06 08:00:41 +00:00
$VotePairs[$Vote['GroupID']] = array(
'GroupID' => $Vote['GroupID'],
'Total' => 1,
'Ups' => ($Type == 'Up') ? 1 : 0);
2012-10-27 08:00:09 +00:00
}
}
}
2013-09-06 08:00:41 +00:00
$Cache->cache_value("vote_pairs_$GroupID", $VotePairs);
2012-10-27 08:00:09 +00:00
}
// Now do the paired votes keys for all of this guy's other votes
foreach ($UserVotes as $VGID => $Vote) {
if ($Vote['Type'] != 'Up') {
// We're only track paired votes on upvotes
continue;
}
2012-10-29 08:00:20 +00:00
if ($VGID == $GroupID) {
continue;
}
2012-10-27 08:00:09 +00:00
// Again, if the cache key is not set, move along
2013-09-06 08:00:41 +00:00
$VotePairs = $Cache->get_value("vote_pairs_$VGID", true);
2013-04-13 08:00:19 +00:00
if ($VotePairs !== false) {
2012-10-27 08:00:09 +00:00
// Go through all of the other albums paired to this one, and update
// this group's entry in their vote_pairs keys
if (isset($VotePairs[$GroupID])) {
$VotePairs[$GroupID]['Total']++;
if ($Type == 'Up') {
$VotePairs[$GroupID]['Ups']++;
}
} else {
2013-09-06 08:00:41 +00:00
$VotePairs[$GroupID] = array(
'GroupID' => $GroupID,
'Total' => 1,
'Ups' => ($Type == 'Up') ? 1 : 0);
2012-10-27 08:00:09 +00:00
}
2013-09-06 08:00:41 +00:00
$Cache->cache_value("vote_pairs_$VGID", $VotePairs);
2012-10-27 08:00:09 +00:00
}
}
2013-02-22 08:00:24 +00:00
2012-10-27 08:00:09 +00:00
echo 'success';
} elseif ($_REQUEST['do'] == 'unvote') {
if (!isset($UserVotes[$GroupID])) {
echo 'noaction';
die();
}
$Type = $UserVotes[$GroupID]['Type'];
2013-02-22 08:00:24 +00:00
2013-05-14 08:00:34 +00:00
$DB->query("
DELETE FROM users_votes
2013-09-06 08:00:41 +00:00
WHERE UserID = $UserID
AND GroupID = $GroupID");
2013-02-22 08:00:24 +00:00
2012-10-27 08:00:09 +00:00
// Update personal cache key
unset($UserVotes[$GroupID]);
$Cache->cache_value('voted_albums_'.$LoggedUser['ID'], $UserVotes);
2013-02-22 08:00:24 +00:00
2012-10-27 08:00:09 +00:00
// Update the group's cache key
$GroupVotes['Total'] -= 1;
2013-05-14 08:00:34 +00:00
if ($Type == 'Up') {
2012-10-27 08:00:09 +00:00
$GroupVotes['Ups'] -= 1;
}
2013-09-06 08:00:41 +00:00
$Cache->cache_value("votes_$GroupID", $GroupVotes);
2012-10-27 08:00:09 +00:00
2013-05-30 08:00:30 +00:00
$DB->query('
2013-05-14 08:00:34 +00:00
UPDATE torrents_votes
2013-09-06 08:00:41 +00:00
SET
Total = GREATEST(0, Total - 1),
Score = IFNULL(binomial_ci(GREATEST(0, Ups'.($Type == 'Up' ? '-1' : '').'), GREATEST(0, Total)), 0)'.
2013-05-14 08:00:34 +00:00
($Type == 'Up' ? ', Ups = GREATEST(0, Ups - 1)' : '')."
WHERE GroupID=$GroupID");
2012-10-27 08:00:09 +00:00
// Update paired cache keys
2013-05-30 08:00:30 +00:00
// First update this album's paired votes. If this keys is magically not set,
// our life just got a bit easier. We're only tracking paired votes on upvotes.
2012-10-27 08:00:09 +00:00
if ($Type == 'Up') {
2012-10-29 08:00:20 +00:00
$VotePairs = $Cache->get_value('vote_pairs_'.$GroupID, true);
2013-04-13 08:00:19 +00:00
if ($VotePairs !== false) {
2012-10-27 08:00:09 +00:00
foreach ($UserVotes as $Vote) {
if (isset($VotePairs[$Vote['GroupID']])) {
if ($VotePairs[$Vote['GroupID']]['Total'] == 0) {
// Something is screwy
$Cache->delete_value('vote_pairs_'.$GroupID);
continue;
}
$VotePairs[$Vote['GroupID']]['Total'] -= 1;
if ($Vote['Type'] == 'Up') {
$VotePairs[$Vote['GroupID']]['Ups'] -= 1;
}
} else {
// Something is screwy, kill the key and move on
$Cache->delete_value('vote_pairs_'.$GroupID);
break;
}
}
}
$Cache->cache_value('vote_pairs_'.$GroupID, $VotePairs);
}
2013-02-22 08:00:24 +00:00
2012-10-27 08:00:09 +00:00
// Now do the paired votes keys for all of this guy's other votes
foreach ($UserVotes as $VGID => $Vote) {
if ($Vote['Type'] != 'Up') {
// We're only track paired votes on upvotes
continue;
}
2012-10-29 08:00:20 +00:00
if ($VGID == $GroupID) {
continue;
}
2012-10-27 08:00:09 +00:00
// Again, if the cache key is not set, move along
2012-10-29 08:00:20 +00:00
$VotePairs = $Cache->get_value('vote_pairs_'.$VGID, true);
2013-04-13 08:00:19 +00:00
if ($VotePairs !== false) {
2012-10-27 08:00:09 +00:00
if (isset($VotePairs[$GroupID])) {
if ($VotePairs[$GroupID]['Total'] == 0) {
// Something is screwy
$Cache->delete_value('vote_pairs_'.$VGID);
continue;
}
$VotePairs[$GroupID]['Total'] -= 1;
if ($Type == 'Up') {
$VotePairs[$GroupID]['Ups'] -= 1;
}
$Cache->cache_value('vote_pairs_'.$VGID, $VotePairs);
} else {
// Something is screwy, kill the key and move on
$Cache->delete_value('vote_pairs_'.$VGID);
}
}
}
2013-02-22 08:00:24 +00:00
2012-10-27 08:00:09 +00:00
// Let the script know what happened
if ($Type == 'Up') {
echo 'success-up';
} else {
echo 'success-down';
}
}
2013-04-13 08:00:19 +00:00
?>