]> littlesong.place Git - poll.git/commitdiff
Prevent voting after close
authorChris Fulljames <christianfulljames@gmail.com>
Fri, 29 May 2026 21:13:32 +0000 (17:13 -0400)
committerChris Fulljames <christianfulljames@gmail.com>
Fri, 29 May 2026 21:13:32 +0000 (17:13 -0400)
index.php

index 5642e002722c492339cd5b07ffa05df1d3c69aa3..0cb3a07090a8d70face8046379f41aada96ea02f 100644 (file)
--- a/index.php
+++ b/index.php
@@ -124,17 +124,38 @@ if (isset($_POST['title']) and isset($_POST['options']))
     exit;
 }
 
+$ended = false;
+if (isset($qid))
+{
+    $enddate = get_end_date($qid);
+    if ($enddate)
+    {
+        $nowdate = gmdate("Y-m-d\TH:i");
+        $ended = $nowdate > $enddate;
+    }
+
+    if ($ended && $view == "vote")
+    {
+        // Poll ended - redirect to results
+        header("Location: ".poll_url($qid, "results"));
+        exit;
+    }
+}
+
 // Voting
 if (isset($_POST['qid']))
 {
-    foreach ($_POST as $key => $value)
+    if (!$ended)
     {
-        $prefix =  "opt-";
-        if (str_starts_with($key, $prefix))
+        foreach ($_POST as $key => $value)
         {
-            $opt = substr($key, strlen($prefix));
-            $opt = str_replace("_", " ", $opt);
-            add_vote($_POST['qid'], $opt, (int) $value);
+            $prefix =  "opt-";
+            if (str_starts_with($key, $prefix))
+            {
+                $opt = substr($key, strlen($prefix));
+                $opt = str_replace("_", " ", $opt);
+                add_vote($_POST['qid'], $opt, (int) $value);
+            }
         }
     }
 
@@ -152,17 +173,6 @@ if (isset($qid) && isset($_GET['count'])) {
     exit;
 }
 
-$ended = false;
-if (isset($qid))
-{
-    $enddate = get_end_date($qid);
-    if ($enddate)
-    {
-        $nowdate = gmdate("Y-m-d\TH:i");
-        $ended = $nowdate > $enddate;
-    }
-}
-
 ?>
 <!DOCTYPE html>
 <html>
@@ -177,7 +187,7 @@ if (isset($qid))
         border: 1px solid #cca;
         padding: 10px;
         background-color: #ffd;
-        color: #434;
+        color: #443;
     }
     input, textarea {
         font-family: sans-serif;
@@ -186,20 +196,24 @@ if (isset($qid))
         width: 100%;
         margin-bottom: 0.5em;
     }
+    textarea {
+        resize: vertical;
+    }
     input[type=submit] {
         background: #fdf;
-        color: #434;
+        color: #443;
         border: 0;
         cursor: pointer;
         border-radius: 5px;
-        padding: 5px;
+        padding: 10px;
         font-weight: bold;
         margin: 10px 0px;
     }
     input[disabled] {
         background: none;
         border: none;
-        color: #434;
+        color: #443;
+        width: unset;
     }
     input[type=number] {
         width: unset;
@@ -218,7 +232,8 @@ if (isset($qid))
     li[draggable=true] {
         cursor: grab;
         padding: 0.8em;
-        border: 1px solid #cca;
+        border: 1px solid #443;
+        border-radius: 5px;
     }
     hr {
         border: none;
@@ -249,21 +264,14 @@ if (isset($qid))
 <?php #########################################################################
 # VOTE
 
-if (isset($qid) && $view == "vote" && !$ended): ?>
+if (isset($qid) && $view == "vote"): ?>
 
     <h2><?= get_title($qid) ?></h2>
 <?php if ($enddate): ?>
     <p>Closes: <input type="datetime-local" value="<?= $enddate ?>" disabled> (UTC)</p>
 <?php endif ?>
-    <!--
-    <p>
-        The option with the most points wins!
-        Give your favorite option 5 points, second favorite 4 points, etc.
-        Or give them all 5 points if you really can't decide.
-    </p> 
-    -->
-    
     <hr>
+    <p>Drag-and-drop the options to order them from favorite (top) to least favorite (bottom).</p> 
     <form method="post">
         <input name="qid" type="hidden" value="<?= $qid ?>">
         <ul>
@@ -281,6 +289,7 @@ if (isset($qid) && $view == "vote" && !$ended): ?>
         <?php endforeach ?>
         </ul>
         <input type="submit" value="Cast Vote!">
+        <small>(Please vote only once)</small>
     </form>
 
     <script>
@@ -320,7 +329,7 @@ if (isset($qid) && $view == "vote" && !$ended): ?>
 
     <hr>
     <br><a href="<?= poll_url($qid, "results") ?>">Show Results</a>
-    <br><br><a href="/">New Poll</a>
+    <a href="/">New Poll</a>
 
 <?php #########################################################################
 # RESULTS
@@ -329,7 +338,7 @@ elseif (isset($qid)): ?>
     <h2><?= get_title($qid) ?></h2>
 
 <?php if ($ended): ?>
-    <p>Poll has ended.</p>
+    <small>Poll has ended.</small>
 <?php endif ?>
 
     <ul>