comparison usrajax.php @ 161:50032763bc79

Clean up the code a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 27 Oct 2013 03:21:17 +0200
parents 5b92f130ba87
children cc02c1d6808c
comparison
equal deleted inserted replaced
160:0980e705dea0 161:50032763bc79
8 require "mconfig.inc.php"; 8 require "mconfig.inc.php";
9 require "msite.inc.php"; 9 require "msite.inc.php";
10 require "msession.inc.php"; 10 require "msession.inc.php";
11 11
12 12
13 //
14 // "Submit" one vote into the database
15 //
13 function stSubmitOneVote($voter_id, $entry_id, $vote) 16 function stSubmitOneVote($voter_id, $entry_id, $vote)
14 { 17 {
18 // Check if the entry_id is actually valid
19 $sql = stPrepareSQL("SELECT * FROM entries WHERE id=%d", $entry_id);
20 if (($entry = stFetchSQL($sql)) === false)
21 return FALSE;
22
23 // Check if the compo is valid for the entry
24 $sql = stPrepareSQL("SELECT * FROM compos WHERE id=%d", $entry["compo_id"]);
25 if (($compo = stFetchSQL($sql)) === false || $compo["voting"] == 0)
26 return FALSE;
27
28 // Check if the vote already exists
15 $sql = stPrepareSQL("SELECT id FROM votes WHERE voter_id=%d AND entry_id=%d", 29 $sql = stPrepareSQL("SELECT id FROM votes WHERE voter_id=%d AND entry_id=%d",
16 $voter_id, $entry_id); 30 $voter_id, $entry_id);
17 31
18 if (($res = stFetchSQLColumn($sql)) === false) 32 if (($res = stFetchSQLColumn($sql)) === false)
19 { 33 {
34 // Didn't exist, insert it
20 $sql = stPrepareSQL( 35 $sql = stPrepareSQL(
21 "INSERT INTO votes (voter_id,entry_id,value) VALUES (%d,%d,%d)", 36 "INSERT INTO votes (voter_id,entry_id,value) VALUES (%d,%d,%d)",
22 $voter_id, $entry_id, $vote); 37 $voter_id, $entry_id, $vote);
23
24 if (stExecSQL($sql) === false)
25 return FALSE;
26 } 38 }
27 else 39 else
28 { 40 {
41 // Existed, thusly update
29 $sql = stPrepareSQL( 42 $sql = stPrepareSQL(
30 "UPDATE votes SET value=%d WHERE voter_id=%d AND entry_id=%d", 43 "UPDATE votes SET value=%d WHERE voter_id=%d AND entry_id=%d",
31 $vote, $voter_id, $eid); 44 $vote, $voter_id, $eid);
32
33 if (stExecSQL($sql) === false)
34 return FALSE;
35 } 45 }
36 46
37 return TRUE; 47 if (stExecSQL($sql) === false)
48 return FALSE;
49 else
50 return TRUE;
38 } 51 }
39 52
40 53
41 54
42 // Check if we are allowed to execute 55 // Check if we are allowed to execute
48 61
49 header("Location: ".stGetSetting("defaultPage")); 62 header("Location: ".stGetSetting("defaultPage"));
50 exit; 63 exit;
51 } 64 }
52 65
66 //
67 // Initialize
68 //
53 stSetupCacheControl(); 69 stSetupCacheControl();
54 70
55 // Initiate SQL database connection
56 if (!stConnectSQLDB()) 71 if (!stConnectSQLDB())
57 die("Could not connect to SQL database."); 72 die("Could not connect to SQL database.");
58 73
59 // Fetch non-"hardcoded" settings from SQL database
60 stReloadSettings(); 74 stReloadSettings();
61 75
62 76
63 // XMLHttp responses 77 //
78 // Handle the request
79 //
64 $action = "ERROR"; 80 $action = "ERROR";
65 if (stChkRequestItem("action") && stChkRequestItem("type")) 81 if (stChkRequestItem("action") && stChkRequestItem("type"))
66 { 82 {
67 $action = $_REQUEST["action"]; 83 $action = $_REQUEST["action"];
68 $type = $_REQUEST["type"]; 84 $type = $_REQUEST["type"];