]> littlesong.place Git - poll.git/commitdiff
Remove responses, move points to options
authorChris Fulljames <christian.fulljames@gmail.com>
Sun, 31 May 2026 13:15:17 +0000 (09:15 -0400)
committerChris Fulljames <christian.fulljames@gmail.com>
Sun, 31 May 2026 13:15:17 +0000 (09:15 -0400)
index.php
schema.sql

index 53f6c927f64499e344af10777d166e4aad29ef1a..c62c0324998a9339d07bd5d4aca1f83e2c4cda54 100755 (executable)
--- 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'])) {
     <?php if (isset($qid) && $view == "results"): ?>
     <script>
     async function reloadIfChanged() {
-        const url = "/?qid=<?= $qid ?>&count=<?= get_num_responses($qid) ?>";
+        const url = "/?qid=<?= $qid ?>&count=<?= get_total_points($qid) ?>";
         const response = await fetch(url);
         const result = await response.json();
         if (result.reload) location.reload();
index c5fa308fc74aacaeffed0c9661267ec5c7f8659b..6a765abe6597a762a26db6c61f19a24852131fab 100644 (file)
@@ -10,20 +10,10 @@ CREATE TABLE options (
     oid INTEGER NOT NULL PRIMARY KEY,
     qid INTEGER NOT NULL,
     name TEXT NOT NULL,
+    points INTEGER NOT NULL DEFAULT 0,
 
     FOREIGN KEY(qid) REFERENCES questions(qid) ON DELETE CASCADE
 );
 DROP INDEX IF EXISTS options_by_qid;
 CREATE INDEX options_by_qid ON options(qid);
 
-DROP TABLE IF EXISTS responses;
-CREATE TABLE responses (
-    rid INTEGER NOT NULL PRIMARY KEY,
-    oid INTEGER NOT NULL,
-    points INTEGER NOT NULL,
-
-    FOREIGN KEY(oid) REFERENCES options(oid) ON DELETE CASCADE
-);
-DROP INDEX IF EXISTS responses_by_oid;
-CREATE INDEX responses_by_oid ON responses(oid);
-