From: Chris Fulljames Date: Sun, 31 May 2026 13:15:17 +0000 (-0400) Subject: Remove responses, move points to options X-Git-Url: https://littlesong.place/gitweb/?a=commitdiff_plain;h=e6643e7815847359d5667135356d644b51325d78;p=poll.git Remove responses, move points to options --- diff --git a/index.php b/index.php index 53f6c92..c62c032 100755 --- a/index.php +++ b/index.php @@ -48,28 +48,20 @@ function get_options($qid) function get_results($qid) { $q = query_db( - "SELECT * FROM options WHERE qid = ?", + "SELECT * FROM options WHERE qid = ? ORDER BY points DESC", [ $qid ])->fetchAll(); $results = []; - foreach ($q as $opt) - { - // Get all points for option - $qq = query_db( - "SELECT SUM(points) FROM responses WHERE oid = ?", - [ $opt['oid'] ])->fetch(); - $results[$opt['name']] = $qq['SUM(points)'] ?? 0; - } - asort($results); - return array_reverse($results, $preserve_keys=true); + foreach ($q as $row) $results[$row['name']] = $row['points']; + return $results; } -function get_num_responses($qid) +function get_total_points($qid) { $q = query_db( - "SELECT COUNT(*) FROM responses INNER JOIN options USING (oid) WHERE qid = ?", + "SELECT SUM(points) FROM options WHERE qid = ?", [ $qid ])->fetch(); - return $q["COUNT(*)"]; + return $q["SUM(points)"]; } function get_end_date($qid) @@ -82,21 +74,9 @@ function get_end_date($qid) function add_vote($qid, $opt, $points) { - $q = query_db( - "SELECT oid FROM options WHERE qid = ? AND name = ?", - [$qid, $opt])->fetch(); - - if ($q) - { - $oid = $q['oid']; - query_db( - "INSERT INTO responses(oid, points) VALUES (?, ?)", - [$oid, $points])->fetch(); - } - else - { - error_log($opt); - } + query_db( + "UPDATE options SET points = points + ? WHERE qid = ? AND name = ?", + [$points, $qid, $opt])->fetch(); } function create_new_poll($title, $options, $enddate) @@ -164,7 +144,7 @@ if (isset($_POST['qid'])) { foreach ($_POST as $key => $value) { - $prefix = "opt-"; + $prefix = "opt-"; if (str_starts_with($key, $prefix)) { $opt = substr($key, strlen($prefix)); @@ -183,7 +163,7 @@ if (isset($_POST['qid'])) if (isset($qid) && isset($_GET['count'])) { // Check of more users have joined header('Content-type: application/json'); - if ($_GET['count'] != get_num_responses($qid)) echo '{"reload": true}'; + if ($_GET['count'] != get_total_points($qid)) echo '{"reload": true}'; else echo '{"reload": false}'; exit; } @@ -271,7 +251,7 @@ if (isset($qid) && isset($_GET['count'])) {