diff vote.inc.php @ 5:76c3b89d7b11

Improve voting, clean up the code, etc.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 05 Dec 2012 08:08:58 +0200
parents 934ab7d8c244
children ea0f98a0bed8
line wrap: on
line diff
--- a/vote.inc.php	Tue Dec 04 21:28:11 2012 +0200
+++ b/vote.inc.php	Wed Dec 05 08:08:58 2012 +0200
@@ -1,191 +1,117 @@
 <?
-$mode = stGetRequestItem("mode", "start");
-$showAuthors = stGetSetting("showVoteAuthors", FALSE);
-
-stGetCompoList(FALSE);
-
-
-function stPrintFormData($button, $mode = "start")
+function stGetSessionItem($name, $default = "")
 {
-  global $compos;
-
-  echo
-  "<form name=\"vote\" action=\"vote\" method=\"post\">\n".
-  " <input type=\"submit\" value=\"".chentities($button)."\" />\n";
-
-  stPrintFormHiddenInput("mode", $mode);
-  stPrintFormHiddenInput("key", stGetRequestItem("key"));
-
-  foreach ($compos as $id => $compo)
-  {
-    foreach ($compo["entries"] as $eid => $entry)
-    {
-      stPrintFormHiddenInput("entry".$eid, stGetRequestItem("entry".$eid));
-    }
-  }
-
-  echo "</form>\n";
+  return isset($_SESSION[$name]) ? trim($_SESSION[$name]) : $default;
 }
 
 
 // Check if voting is enabled
 if (!stChkSetting("allowVoting"))
 {
-?>
-<h1>Sorry, voting disabled!</h1>
-<p>
-Voting functionality not available at this time.
-</p>
-<?
+  echo
+  "<h1>Sorry, voting disabled!</h1>\n".
+  "<p>Voting functionality not available at this time.</p>\n";
 }
 else
-if ($mode == "start")
 {
-?>
-<h1>Way Too Simple Vote System</h1>
-
-<form name="vote" action="vote" method="post">
- <input type="hidden" name="mode" value="check">
- <table class="misc">
-<?
-
-stPrintFormTextInput("Vote key:", "(that series of characters)", 30, 30, "key", "autocomplete=\"off\"");
-echo "</table>\n";
-
-foreach ($compos as $id => $compo)
-if (count($compo["entries"]) > 0)
-{
-  echo
-    " <table class=\"misc\">\n".
-    "  <tr><th colspan=\"3\">".chentities($compo["name"])."</th></tr>\n".
-    "  <tr>\n".
-    "   <th class=\"vtitle\">Title</th>\n".
-    ($showAuthors ? "   <th class=\"vauthor\">Author</th>\n" : "").
-    "   ";
-
-  for ($i = stGetSetting("voteMin", -2); $i <= stGetSetting("voteMax", 2); $i++)
+  stVoteSessionAuth();
+  $mode = stGetSessionItem("status", 0);
+  
+  if ($mode < 0)
+  {
+    echo "<h1>Oh noes, an error!</h1>\n".
+    "<ul>\n".stGetSessionItem("message", "")."</ul>\n".
+    "<form name=\"vote\" action=\"vote\" method=\"post\">\n".
+    " <input type=\"submit\" value=\" Go back \" />\n".
+    "</form>\n";
+    stSetVoteStatus(0);
+  }
+  else
+  if ($mode == 0)
   {
     echo
-    "<th class=\"vvalue\">".$i."</th>";
+    "<h1>Voting system</h1>\n".
+    "<form name=\"vote\" action=\"dovote.php\" method=\"post\">\n".
+    stGetFormHiddenInput("mode", "key")."\n".
+    " <div class=\"votectrl\">\n".
+    "  Enter your vote key: ".
+    stGetFormTextInput(30, 30, "key", "key", "", stGetSessionItem("key"), "autocomplete=\"off\"").
+    "  <input type=\"submit\" value=\" Login \" />".
+    " </div>\n";
+    "</form>\n";
   }
-  echo "\n".
-    "  </tr>\n";
+  else
+  if ($mode == 1)
+  {
+    $showAuthors = stGetSetting("showVoteAuthors");
+    stGetCompoList(FALSE);
 
-  foreach ($compo["entries"] as $eid => $entry)
-  {
     echo
-      "  <tr>\n".
-      "   <td class=\"vtitle\">".$entry["name"]."</td>\n".
-      ($showAuthors ? "   <td class=\"vauthor\">".$entry["author"]."</td>\n" : "").
-      "   ";
+    "<h1>Voting system</h1>\n".
+    "<form name=\"vote\" action=\"dovote.php\" method=\"post\">\n".
+    stGetFormHiddenInput("mode", "check")."\n".
+    stGetFormHiddenInput("key", stGetSessionItem("key"))."\n".
+    " <div class=\"votectrl\">\n".
+    "  <input type=\"submit\" value=\" Submit votes! \" />".
+    " </div>\n";
+
+    foreach ($compos as $id => $compo)
+    if (count($compo["entries"]) > 0)
+    {
+      echo
+        " <table class=\"vote\">\n".
+        "  <tr><th colspan=\"3\">".chentities($compo["name"])."</th></tr>\n".
+        "  <tr>\n".
+        "   <th class=\"vtitle\">Title</th>\n".
+        ($showAuthors ? "   <th class=\"vauthor\">Author</th>\n" : "").
+        "   ";
 
-    for ($i = stGetSetting("voteMin", -2); $i <= stGetSetting("voteMax", 2); $i++)
-    {
-      $name = "entry".$eid;
-      $checked = stChkRequestItem($name) ? stGetRequestItem($name) : 0;
+      for ($i = stGetSetting("voteMin"); $i <= stGetSetting("voteMax"); $i++)
+      {
+        echo
+        "<th class=\"vvalue\">".$i."</th>";
+      }
+      echo "\n".
+        "  </tr>\n";
+
+      $row = 0;
+      foreach ($compo["entries"] as $eid => $entry)
+      {
+        echo
+          "  <tr class=\"".($row % 2 == 1 ? "rodd" : "reven")."\">\n".
+          "   <td class=\"vtitle\">".$entry["name"]."</td>\n".
+          ($showAuthors ? "   <td class=\"vauthor\">".$entry["author"]."</td>\n" : "").
+          "   ";
+
+        for ($i = stGetSetting("voteMin"); $i <= stGetSetting("voteMax"); $i++)
+        {
+          $name = "entry".$eid;
+          echo
+            "<td class=\"vvalue\"><input type=\"radio\" name=\"".$name."\" ".
+            ($i == stGetSessionItem($name, 0) ? "checked=\"checked\" " : "")."value=\"".$i."\" /></td>";
+        }
+
+        echo
+          "\n".
+          "  </tr>\n";
+
+        $row++;
+      }
       echo
-        "<td class=\"vvalue\"><input type=\"radio\" name=\"".$name."\" ".
-        ($i == $checked ? "checked=\"checked\" " : "")."value=\"".$i."\" /></td>";
+        " </table>\n";
     }
 
     echo
-      "\n".
-      "  </tr>\n";
-  }
-  echo
-    " </table>\n";
-}
-?>
- <input type="submit" value="Vote!" /><br />
-</form>
-<?
-}
-else
-if ($mode == "check")
-{
-  // Check received data
-  if (stChkDataItem("key") ||
-    strlen(stGetRequestItem("key")) != stGetSetting("votekeylen", 8))
-  {
-    stError("Invalid or empty vote key, please check.");
+    "<div class=\"votectrl\"><input type=\"submit\" value=\" Submit votes! \" /></div>\n";
+    echo
+    "</form>\n";
   }
   else
-  {
-    // Check if the key exists and is active
-    $sql = stPrepareSQL(
-      "SELECT * FROM voters WHERE key=%S AND enabled<>0",
-      "key");
-
-    if (($voter = stFetchSQL($sql)) === FALSE)
-      stError("Vote key does not exist, perhaps you typed it incorrectly?");
-  }
-
-  // Check the submitted vote values
-  foreach ($compos as $id => $compo)
-  if (count($compo["entries"]) > 0)
-  {
-    foreach ($compo["entries"] as $eid => $entry)
-    {
-      $vote = stGetRequestItem("entry".$eid);
-      if ($vote < stGetSetting("voteMin", -2) || $vote > stGetSetting("voteMax", 2))
-      {
-        stError("One or more vote value was out of bounds. Trying to cheat, eh?");
-        break;
-      }
-    }
-  }
-
-  // Ookkay...
-  if ($errorSet)
-  {
-    echo "<p>Following errors occured:</p>\n".
-    "<ul>\n".$errorMsg."</ul>\n";
-    stPrintFormData("Go back");
-  }
-  else
+  if ($mode == 2)
   {
-    foreach ($compos as $id => $compo)
-    if (count($compo["entries"]) > 0)
-    {
-      foreach ($compo["entries"] as $eid => $entry)
-      {
-        $vote = stGetRequestItem("entry".$eid);
-        $sql = stPrepareSQL("SELECT id FROM votes WHERE voter_id=%d AND entry_id=%d",
-          $voter["id"], $eid);
-          
-        if (stFetchSQLColumn($sql) === false)
-        {
-          $sql = stPrepareSQL(
-            "INSERT INTO votes (voter_id,entry_id,value) VALUES (%d,%d,%d)",
-            $voter["id"], $eid, $vote);
-
-          if (stExecSQL($sql) === false)
-            break;
-        }
-        else
-        {
-          $sql = stPrepareSQL(
-            "UPDATE votes SET value=%d WHERE voter_id=%d AND entry_id=%d",
-            $vote, $voter["id"], $eid);
-
-          if (stExecSQL($sql) === false)
-            break;
-        }
-      }
-    }
-
-    if ($errorSet)
-    {
-      echo "<h1>An error occured.</h1>\n";
-      echo "<p>Following errors occured:</p>\n".
-      "<ul>\n".$errorMsg."</ul>\n";
-      stPrintFormData("Go back");
-    }
-    else
-    {
-      echo "<h1>Voting successful</h1>\n";
-      echo "<p>Now go FAP some more! Or whatever.</p>\n";
-    }
+    echo "<h1>Yay, you have voted!</h1>\n".
+    "<p>Now go FAP some more! And make a demo about it.</p>";
+    stSessionEnd(TRUE);
   }
 }
 ?>
\ No newline at end of file