comparison vote.inc.php @ 0:8019b357cc03

Initial import.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 04 Dec 2012 19:07:18 +0200
parents
children 934ab7d8c244
comparison
equal deleted inserted replaced
-1:000000000000 0:8019b357cc03
1 <?
2 $mode = stGetRequestItem("mode", "start");
3
4 stGetCompoList(FALSE);
5
6
7 function stPrintFormData($button, $mode = "start")
8 {
9 global $compos;
10
11 echo
12 "<form name=\"vote\" action=\"vote\" method=\"post\">\n".
13 " <input type=\"submit\" value=\"".chentities($button)."\" />\n";
14
15 stPrintFormHiddenInput("mode", $mode);
16 stPrintFormHiddenInput("key", stGetRequestItem("key"));
17
18 foreach ($compos as $id => $compo)
19 {
20 foreach ($compo["entries"] as $eid => $entry)
21 {
22 stPrintFormHiddenInput("entry".$eid, stGetRequestItem("entry".$eid));
23 }
24 }
25
26 echo "</form>\n";
27 }
28
29
30 // Check if voting is enabled
31 if (!stChkSetting("allowVoting"))
32 {
33 ?>
34 <h1>Sorry, voting disabled!</h1>
35 <p>
36 Voting functionality not available at this time.
37 </p>
38 <?
39 }
40 else
41 if ($mode == "start")
42 {
43 ?>
44 <h1>Way Too Simple Vote System</h1>
45
46 <form name="vote" action="vote" method="post">
47 <input type="hidden" name="mode" value="check">
48 <table class="misc">
49 <?
50 stPrintFormTextInput("Vote key:", "(that series of characters)", 30, 30, "key", "autocomplete=\"off\"");
51 echo "</table>\n";
52
53 foreach ($compos as $id => $compo)
54 if (count($compo["entries"]) > 0)
55 {
56 echo
57 " <table class=\"misc\">\n".
58 " <tr><th colspan=\"3\">".chentities($compo["name"])."</th></tr>\n".
59 " <tr>\n".
60 " <th>Title</th>\n".
61 " <th>Author</th>\n".
62 " <th>Actions</th>\n".
63 " </tr>\n";
64
65 foreach ($compo["entries"] as $eid => $entry)
66 {
67 echo
68 " <tr>\n".
69 " <td>".$entry["name"]."</td>\n".
70 " <td>".$entry["author"]."</td>\n".
71 " <td>\n";
72
73 for ($i = stGetSetting("voteMin", -2); $i <= stGetSetting("voteMax", 2); $i++)
74 {
75 $name = "entry".$eid;
76 $checked = stChkRequestItem($name) ? stGetRequestItem($name) : 0;
77 echo
78 " <input type=\"radio\" name=\"".$name."\" ".
79 ($i == $checked ? "checked=\"checked\" " : "").
80 "value=\"".$i."\"><label for=\"".$name."\">".$i."</label>\n";
81 }
82
83 echo
84 " </td>\n".
85 " </tr>\n";
86 }
87 echo
88 " </table>\n";
89 }
90 ?>
91 <input type="submit" value="Vote!" /><br />
92 </form>
93 <?
94 }
95 else
96 if ($mode == "check")
97 {
98 // Check received data
99 if (stChkDataItem("key") ||
100 strlen(stGetRequestItem("key")) != stGetSetting("votekeylen", 8))
101 {
102 stError("Invalid or empty vote key, please check.");
103 }
104 else
105 {
106 // Check if the key exists and is active
107 $sql = stPrepareSQL(
108 "SELECT * FROM voters WHERE key=%S AND enabled<>0",
109 "key");
110
111 if (($voter = stFetchSQL($sql)) === FALSE)
112 stError("Vote key does not exist, perhaps you typed it incorrectly?");
113 }
114
115 // Check the submitted vote values
116 foreach ($compos as $id => $compo)
117 if (count($compo["entries"]) > 0)
118 {
119 foreach ($compo["entries"] as $eid => $entry)
120 {
121 $vote = stGetRequestItem("entry".$eid);
122 if ($vote < stGetSetting("voteMin", -2) || $vote > stGetSetting("voteMax", 2))
123 {
124 stError("One or more vote value was out of bounds. Trying to cheat, eh?");
125 break;
126 }
127 }
128 }
129
130 // Ookkay...
131 if ($errorSet)
132 {
133 echo "<p>Following errors occured:</p>\n".
134 "<ul>\n".$errorMsg."</ul>\n";
135 stPrintFormData("Go back");
136 }
137 else
138 {
139 foreach ($compos as $id => $compo)
140 if (count($compo["entries"]) > 0)
141 {
142 foreach ($compo["entries"] as $eid => $entry)
143 {
144 $vote = stGetRequestItem("entry".$eid);
145 $sql = stPrepareSQL("SELECT id FROM votes WHERE voter_id=%d AND entry_id=%d",
146 $voter["id"], $eid);
147
148 if (stFetchSQLColumn($sql) === false)
149 {
150 $sql = stPrepareSQL(
151 "INSERT INTO votes (voter_id,entry_id,value) VALUES (%d,%d,%d)",
152 $voter["id"], $eid, $vote);
153
154 if (stExecSQL($sql) === false)
155 break;
156 }
157 else
158 {
159 $sql = stPrepareSQL(
160 "UPDATE votes SET value=%d WHERE voter_id=%d AND entry_id=%d",
161 $vote, $voter["id"], $eid);
162
163 if (stExecSQL($sql) === false)
164 break;
165 }
166 }
167 }
168
169 if ($errorSet)
170 {
171 echo "<h1>An error occured.</h1>\n";
172 echo "<p>Following errors occured:</p>\n".
173 "<ul>\n".$errorMsg."</ul>\n";
174 stPrintFormData("Go back");
175 }
176 else
177 {
178 echo "<h1>Voting successful</h1>\n";
179 echo "<p>Now go FAP some more! Or whatever.</p>\n";
180 }
181 }
182 }
183 ?>