Gazelle/sections/forums/poll_vote.php

171 lines
4.7 KiB
PHP
Raw Normal View History

2011-03-28 14:21:28 +00:00
<?
2013-04-30 18:18:07 +00:00
if (!isset($_POST['topicid']) || !is_number($_POST['topicid'])) {
2013-10-30 08:01:19 +00:00
error(0, true);
2013-04-30 18:18:07 +00:00
}
2011-03-28 14:21:28 +00:00
$TopicID = $_POST['topicid'];
if (!empty($_POST['large'])) {
$Size = 750;
} else {
$Size = 140;
}
2013-10-30 08:01:19 +00:00
if (!$ThreadInfo = $Cache->get_value("thread_$TopicID".'_info')) {
2013-05-04 08:00:48 +00:00
$DB->query("
SELECT
t.Title,
t.ForumID,
t.IsLocked,
t.IsSticky,
COUNT(fp.id) AS Posts,
t.LastPostAuthorID,
ISNULL(p.TopicID) AS NoPoll
2011-03-28 14:21:28 +00:00
FROM forums_topics AS t
2013-05-04 08:00:48 +00:00
JOIN forums_posts AS fp ON fp.TopicID = t.ID
2013-10-30 08:01:19 +00:00
LEFT JOIN forums_polls AS p ON p.TopicID = t.ID
2011-03-28 14:21:28 +00:00
WHERE t.ID = '$TopicID'
GROUP BY fp.TopicID");
2013-07-10 00:08:53 +00:00
if (!$DB->has_results()) {
2013-04-30 18:18:07 +00:00
die();
}
2011-03-28 14:21:28 +00:00
$ThreadInfo = $DB->next_record(MYSQLI_ASSOC);
if (!$ThreadInfo['IsLocked'] || $ThreadInfo['IsSticky']) {
2013-10-30 08:01:19 +00:00
$Cache->cache_value("thread_$TopicID".'_info', $ThreadInfo, 0);
2011-03-28 14:21:28 +00:00
}
}
$ForumID = $ThreadInfo['ForumID'];
2013-10-30 08:01:19 +00:00
if (!list($Question, $Answers, $Votes, $Featured, $Closed) = $Cache->get_value("polls_$TopicID")) {
2013-05-04 08:00:48 +00:00
$DB->query("
SELECT
Question,
Answers,
Featured,
Closed
FROM forums_polls
2013-10-30 08:01:19 +00:00
WHERE TopicID = '$TopicID'");
2011-03-28 14:21:28 +00:00
list($Question, $Answers, $Featured, $Closed) = $DB->next_record(MYSQLI_NUM, array(1));
$Answers = unserialize($Answers);
2013-05-04 08:00:48 +00:00
$DB->query("
SELECT Vote, COUNT(UserID)
FROM forums_polls_votes
2013-05-27 08:00:58 +00:00
WHERE TopicID = '$TopicID'
AND Vote != '0'
2013-05-04 08:00:48 +00:00
GROUP BY Vote");
2011-03-28 14:21:28 +00:00
$VoteArray = $DB->to_array(false, MYSQLI_NUM);
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
$Votes = array();
foreach ($VoteArray as $VoteSet) {
2013-02-22 08:00:24 +00:00
list($Key,$Value) = $VoteSet;
2011-03-28 14:21:28 +00:00
$Votes[$Key] = $Value;
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
for ($i = 1, $il = count($Answers); $i <= $il; ++$i) {
if (!isset($Votes[$i])) {
$Votes[$i] = 0;
}
}
2013-10-30 08:01:19 +00:00
$Cache->cache_value("polls_$TopicID", array($Question, $Answers, $Votes, $Featured, $Closed), 0);
2011-03-28 14:21:28 +00:00
}
if ($Closed) {
error(403,true);
}
if (!empty($Votes)) {
$TotalVotes = array_sum($Votes);
$MaxVotes = max($Votes);
} else {
$TotalVotes = 0;
$MaxVotes = 0;
}
if (!isset($_POST['vote']) || !is_number($_POST['vote'])) {
?>
<span class="error">Please select an option.</span><br />
2013-01-15 08:00:37 +00:00
<form class="vote_form" name="poll" id="poll" action="">
2012-09-19 08:00:35 +00:00
<input type="hidden" name="action" value="poll" />
2011-03-28 14:21:28 +00:00
<input type="hidden" name="auth" value="<?=$LoggedUser['AuthKey']?>" />
2012-09-19 08:00:35 +00:00
<input type="hidden" name="large" value="<?=display_str($_POST['large'])?>" />
2011-03-28 14:21:28 +00:00
<input type="hidden" name="topicid" value="<?=$TopicID?>" />
2013-10-30 08:01:19 +00:00
<? for ($i = 1, $il = count($Answers); $i <= $il; $i++) { ?>
2011-03-28 14:21:28 +00:00
<input type="radio" name="vote" id="answer_<?=$i?>" value="<?=$i?>" />
<label for="answer_<?=$i?>"><?=display_str($Answers[$i])?></label><br />
2013-10-30 08:01:19 +00:00
<? } ?>
2013-06-15 08:00:45 +00:00
<br /><input type="radio" name="vote" id="answer_0" value="0" /> <label for="answer_0">Blank&#8202;&mdash;&#8202;Show the results!</label><br /><br />
2013-10-30 08:01:19 +00:00
<input type="button" onclick="ajax.post('index.php', 'poll', function(response) { $('#poll_container').raw().innerHTML = response });" value="Vote" />
2011-03-28 14:21:28 +00:00
</form>
<?
} else {
authorize();
$Vote = $_POST['vote'];
2013-05-04 08:00:48 +00:00
if (!isset($Answers[$Vote]) && $Vote != 0) {
error(0,true);
}
2011-03-28 14:21:28 +00:00
//Add our vote
2013-10-30 08:01:19 +00:00
$DB->query("
INSERT IGNORE INTO forums_polls_votes
(TopicID, UserID, Vote)
VALUES
($TopicID, " . $LoggedUser['ID'] . ", $Vote)");
2011-03-28 14:21:28 +00:00
if ($DB->affected_rows() == 1 && $Vote != 0) {
2013-10-30 08:01:19 +00:00
$Cache->begin_transaction("polls_$TopicID");
2011-03-28 14:21:28 +00:00
$Cache->update_row(2, array($Vote => '+1'));
$Cache->commit_transaction(0);
$Votes[$Vote]++;
$TotalVotes++;
$MaxVotes++;
}
if ($Vote != 0) {
$Answers[$Vote] = '=> '.$Answers[$Vote];
}
?>
<ul class="poll nobullet">
<?
2013-04-30 18:18:07 +00:00
if ($ForumID != STAFF_FORUM) {
2011-03-28 14:21:28 +00:00
for ($i = 1, $il = count($Answers); $i <= $il; $i++) {
if (!empty($Votes[$i]) && $TotalVotes > 0) {
2013-04-30 18:18:07 +00:00
$Ratio = $Votes[$i] / $MaxVotes;
$Percent = $Votes[$i] / $TotalVotes;
2011-03-28 14:21:28 +00:00
} else {
2013-04-30 18:18:07 +00:00
$Ratio = 0;
$Percent = 0;
2013-02-22 08:00:24 +00:00
}
2011-03-28 14:21:28 +00:00
?>
2013-10-30 08:01:19 +00:00
<li><?=display_str($Answers[$i])?> (<?=number_format($Percent * 100, 2)?>%)</li>
2011-03-28 14:21:28 +00:00
<li class="graph">
<span class="left_poll"></span>
2013-04-30 18:18:07 +00:00
<span class="center_poll" style="width: <?=round($Ratio * $Size)?>px;"></span>
2011-03-28 14:21:28 +00:00
<span class="right_poll"></span>
</li>
2013-10-30 08:01:19 +00:00
<?
}
2011-03-28 14:21:28 +00:00
} else {
//Staff forum, output voters, not percentages
2013-05-04 08:00:48 +00:00
$DB->query("
SELECT GROUP_CONCAT(um.Username SEPARATOR ', '),
fpv.Vote
FROM users_main AS um
JOIN forums_polls_votes AS fpv ON um.ID = fpv.UserID
WHERE TopicID = $TopicID
GROUP BY fpv.Vote");
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
$StaffVotes = $DB->to_array();
2013-04-30 18:18:07 +00:00
foreach ($StaffVotes as $StaffVote) {
2011-03-28 14:21:28 +00:00
list($StaffString, $StaffVoted) = $StaffVote;
?>
2013-10-30 08:01:19 +00:00
<li><a href="forums.php?action=change_vote&amp;threadid=<?=$TopicID?>&amp;auth=<?=$LoggedUser['AuthKey']?>&amp;vote=<?=(int)$StaffVoted?>"><?=display_str(empty($Answers[$StaffVoted]) ? 'Blank' : $Answers[$StaffVoted])?></a> - <?=$StaffString?></li>
2011-03-28 14:21:28 +00:00
<?
}
}
?>
</ul>
<br /><strong>Votes:</strong> <?=number_format($TotalVotes)?>
<?
}