changeset 161:50032763bc79

Clean up the code a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 27 Oct 2013 03:21:17 +0200
parents 0980e705dea0
children cc02c1d6808c
files usrajax.php
diffstat 1 files changed, 26 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/usrajax.php	Sun Oct 27 03:05:49 2013 +0200
+++ b/usrajax.php	Sun Oct 27 03:21:17 2013 +0200
@@ -10,31 +10,44 @@
 require "msession.inc.php";
 
 
+//
+// "Submit" one vote into the database
+//
 function stSubmitOneVote($voter_id, $entry_id, $vote)
 {
+  // Check if the entry_id is actually valid
+  $sql = stPrepareSQL("SELECT * FROM entries WHERE id=%d", $entry_id);
+  if (($entry = stFetchSQL($sql)) === false)
+    return FALSE;
+
+  // Check if the compo is valid for the entry
+  $sql = stPrepareSQL("SELECT * FROM compos WHERE id=%d", $entry["compo_id"]);
+  if (($compo = stFetchSQL($sql)) === false || $compo["voting"] == 0)
+    return FALSE;
+
+  // Check if the vote already exists
   $sql = stPrepareSQL("SELECT id FROM votes WHERE voter_id=%d AND entry_id=%d",
     $voter_id, $entry_id);
 
   if (($res = stFetchSQLColumn($sql)) === false)
   {
+    // Didn't exist, insert it
     $sql = stPrepareSQL(
       "INSERT INTO votes (voter_id,entry_id,value) VALUES (%d,%d,%d)",
       $voter_id, $entry_id, $vote);
-
-    if (stExecSQL($sql) === false)
-      return FALSE;
   }
   else
   {
+    // Existed, thusly update
     $sql = stPrepareSQL(
       "UPDATE votes SET value=%d WHERE voter_id=%d AND entry_id=%d",
       $vote, $voter_id, $eid);
-
-    if (stExecSQL($sql) === false)
-      return FALSE;
   }
 
-  return TRUE;
+  if (stExecSQL($sql) === false)
+    return FALSE;
+  else
+    return TRUE;
 }
 
 
@@ -50,17 +63,20 @@
   exit;
 }
 
+//
+// Initialize
+//
 stSetupCacheControl();
 
-// Initiate SQL database connection
 if (!stConnectSQLDB())
   die("Could not connect to SQL database.");
 
-// Fetch non-"hardcoded" settings from SQL database
 stReloadSettings();
 
 
-// XMLHttp responses
+//
+// Handle the request
+//
 $action = "ERROR";
 if (stChkRequestItem("action") && stChkRequestItem("type"))
 {