BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

poll_vote.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. #declare(strict_types=1);
  3. if (!isset($_POST['topicid']) || !is_number($_POST['topicid'])) {
  4. error(0, true);
  5. }
  6. $TopicID = $_POST['topicid'];
  7. if (!empty($_POST['large'])) {
  8. $Size = 750;
  9. } else {
  10. $Size = 140;
  11. }
  12. if (!$ThreadInfo = $Cache->get_value("thread_$TopicID".'_info')) {
  13. $DB->query("
  14. SELECT
  15. t.Title,
  16. t.ForumID,
  17. t.IsLocked,
  18. t.IsSticky,
  19. COUNT(fp.id) AS Posts,
  20. t.LastPostAuthorID,
  21. ISNULL(p.TopicID) AS NoPoll
  22. FROM forums_topics AS t
  23. JOIN forums_posts AS fp ON fp.TopicID = t.ID
  24. LEFT JOIN forums_polls AS p ON p.TopicID = t.ID
  25. WHERE t.ID = '$TopicID'
  26. GROUP BY fp.TopicID");
  27. if (!$DB->has_results()) {
  28. error();
  29. }
  30. $ThreadInfo = $DB->next_record(MYSQLI_ASSOC);
  31. if (!$ThreadInfo['IsLocked'] || $ThreadInfo['IsSticky']) {
  32. $Cache->cache_value("thread_$TopicID".'_info', $ThreadInfo, 0);
  33. }
  34. }
  35. $ForumID = $ThreadInfo['ForumID'];
  36. if (!list($Question, $Answers, $Votes, $Featured, $Closed) = $Cache->get_value("polls_$TopicID")) {
  37. $DB->query("
  38. SELECT
  39. Question,
  40. Answers,
  41. Featured,
  42. Closed
  43. FROM forums_polls
  44. WHERE TopicID = '$TopicID'");
  45. list($Question, $Answers, $Featured, $Closed) = $DB->next_record(MYSQLI_NUM, array(1));
  46. $Answers = unserialize($Answers);
  47. $DB->query("
  48. SELECT Vote, COUNT(UserID)
  49. FROM forums_polls_votes
  50. WHERE TopicID = '$TopicID'
  51. AND Vote != '0'
  52. GROUP BY Vote");
  53. $VoteArray = $DB->to_array(false, MYSQLI_NUM);
  54. $Votes = [];
  55. foreach ($VoteArray as $VoteSet) {
  56. list($Key,$Value) = $VoteSet;
  57. $Votes[$Key] = $Value;
  58. }
  59. for ($i = 1, $il = count($Answers); $i <= $il; ++$i) {
  60. if (!isset($Votes[$i])) {
  61. $Votes[$i] = 0;
  62. }
  63. }
  64. $Cache->cache_value("polls_$TopicID", array($Question, $Answers, $Votes, $Featured, $Closed), 0);
  65. }
  66. if ($Closed) {
  67. error(403,true);
  68. }
  69. if (!empty($Votes)) {
  70. $TotalVotes = array_sum($Votes);
  71. $MaxVotes = max($Votes);
  72. } else {
  73. $TotalVotes = 0;
  74. $MaxVotes = 0;
  75. }
  76. if (!isset($_POST['vote']) || !is_number($_POST['vote'])) {
  77. ?>
  78. <span class="error">Please select an option.</span><br />
  79. <form class="vote_form" name="poll" id="poll" action="">
  80. <input type="hidden" name="action" value="poll" />
  81. <input type="hidden" name="auth" value="<?=$LoggedUser['AuthKey']?>" />
  82. <input type="hidden" name="large" value="<?=display_str($_POST['large'])?>" />
  83. <input type="hidden" name="topicid" value="<?=$TopicID?>" />
  84. <?php for ($i = 1, $il = count($Answers); $i <= $il; $i++) { ?>
  85. <input type="radio" name="vote" id="answer_<?=$i?>" value="<?=$i?>" />
  86. <label for="answer_<?=$i?>"><?=display_str($Answers[$i])?></label><br />
  87. <?php } ?>
  88. <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 />
  89. <input type="button" onclick="ajax.post('index.php', 'poll', function(response) { $('#poll_container').raw().innerHTML = response });" value="Vote" />
  90. </form>
  91. <?
  92. } else {
  93. authorize();
  94. $Vote = $_POST['vote'];
  95. if (!isset($Answers[$Vote]) && $Vote != 0) {
  96. error(0,true);
  97. }
  98. //Add our vote
  99. $DB->query("
  100. INSERT IGNORE INTO forums_polls_votes
  101. (TopicID, UserID, Vote)
  102. VALUES
  103. ($TopicID, " . $LoggedUser['ID'] . ", $Vote)");
  104. if ($DB->affected_rows() == 1 && $Vote != 0) {
  105. $Cache->begin_transaction("polls_$TopicID");
  106. $Cache->update_row(2, array($Vote => '+1'));
  107. $Cache->commit_transaction(0);
  108. $Votes[$Vote]++;
  109. $TotalVotes++;
  110. $MaxVotes++;
  111. }
  112. if ($Vote != 0) {
  113. $Answers[$Vote] = '=> '.$Answers[$Vote];
  114. }
  115. ?>
  116. <ul class="poll nobullet">
  117. <?
  118. if ($ForumID != STAFF_FORUM) {
  119. for ($i = 1, $il = count($Answers); $i <= $il; $i++) {
  120. if (!empty($Votes[$i]) && $TotalVotes > 0) {
  121. $Ratio = $Votes[$i] / $MaxVotes;
  122. $Percent = $Votes[$i] / $TotalVotes;
  123. } else {
  124. $Ratio = 0;
  125. $Percent = 0;
  126. }
  127. ?>
  128. <li><?=display_str($Answers[$i])?> (<?=number_format($Percent * 100, 2)?>%)</li>
  129. <li class="graph">
  130. <span class="center_poll" style="width: <?=round($Ratio * $Size)?>px;"></span>
  131. </li>
  132. <?
  133. }
  134. } else {
  135. //Staff forum, output voters, not percentages
  136. $DB->query("
  137. SELECT GROUP_CONCAT(um.Username SEPARATOR ', '),
  138. fpv.Vote
  139. FROM users_main AS um
  140. JOIN forums_polls_votes AS fpv ON um.ID = fpv.UserID
  141. WHERE TopicID = $TopicID
  142. GROUP BY fpv.Vote");
  143. $StaffVotes = $DB->to_array();
  144. foreach ($StaffVotes as $StaffVote) {
  145. list($StaffString, $StaffVoted) = $StaffVote;
  146. ?>
  147. <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>
  148. <?
  149. }
  150. }
  151. ?>
  152. </ul>
  153. <br /><strong>Votes:</strong> <?=number_format($TotalVotes)?>
  154. <?
  155. }