comparison usrajax.php @ 1087:4c76b4994414

Somewhat refactor usrajax and voting.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 26 Jan 2017 00:38:06 +0200
parents 7da8bde9b7be
children 95b74632cfe2
comparison
equal deleted inserted replaced
1086:4a95cd4fa341 1087:4c76b4994414
79 79
80 stReloadSettings(); 80 stReloadSettings();
81 81
82 $voteKeyId = stGetSessionItem("key_id"); 82 $voteKeyId = stGetSessionItem("key_id");
83 83
84 //
85 // Check vote key validity
86 //
87 $sql = stPrepareSQL("SELECT * FROM votekeys WHERE id=%d", $voteKeyId);
88 if (($key = stFetchSQL($sql)) === false)
89 {
90 stError("Votekey does not exist.");
91 }
92 else
93 {
94 // Validate login based on current vote key mode
95 switch (stGetSetting("voteKeyMode"))
96 {
97 case VOTE_ACTIVATE:
98 if ($key["active"] == 0)
99 stError("Votekey is not active.");
100 break;
101
102 case VOTE_ASSIGN:
103 $sql = stPrepareSQL("SELECT id FROM attendees WHERE key_id=%d", $key["id"]);
104 if (stFetchSQL($sql) === false)
105 stError("Votekey is not assigned to any user.");
106 break;
107 }
108 }
84 109
85 // 110 //
86 // Handle the request 111 // Handle the request
87 // 112 //
88 switch (stGetRequestItem("action")) 113 switch (stGetRequestItem("action"))
89 { 114 {
90 case "set": 115 case "set":
91 // 116 //
92 // Set vote, if voting is enabled 117 // Set vote, if voting is enabled
93 // 118 //
119 $ajax = TRUE;
94 if (!stChkSetting("allowVoting")) 120 if (!stChkSetting("allowVoting"))
95 stError("Voting is not enabled."); 121 stError("Voting is not enabled.");
96 else 122 else
97 if (stChkRequestItem("entry_id", $entry_id, 123 if (stChkRequestItem("entry_id", $entry_id,
98 array(CHK_TYPE, VT_INT, "Invalid data.")) && 124 array(CHK_TYPE, VT_INT, "Invalid data.")) &&
111 stDBCommitTransaction(); 137 stDBCommitTransaction();
112 } 138 }
113 break; 139 break;
114 140
115 case "submit": 141 case "submit":
142 //
143 // Submit all votes, if voting is enabled
144 //
145 $ajax = FALSE;
116 if (!stChkSetting("allowVoting")) 146 if (!stChkSetting("allowVoting"))
117 stError("Voting is not enabled."); 147 stError("Voting is not enabled.");
118 else 148 else
149 foreach (stExecSQL("SELECT * FROM compos WHERE visible<>0 AND voting<>0") as $compo)
119 { 150 {
120 foreach (stExecSQL("SELECT * FROM compos WHERE visible<>0 AND voting<>0") as $compo) 151 stDBBeginTransaction();
152 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry)
121 { 153 {
122 stDBBeginTransaction(); 154 if (stCheckVoteValue("ventry".$entry["id"], $value))
123 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry)
124 { 155 {
125 if (stCheckVoteValue("ventry".$entry["id"], $value)) 156 if (!stUpdateVote($voteKeyId, $entry["id"], $value))
126 { 157 stError("Could not set vote for compo #".$compo["id"].", entry #".$entry["id"]);
127 if (!stUpdateVote($voteKeyId, $entry["id"], $value))
128 stError("Could not set vote for compo #".$compo["id"].", entry #".$entry["id"]);
129 }
130 } 158 }
131 stDBCommitTransaction();
132 } 159 }
133 160 stDBCommitTransaction();
134 if ($errorSet)
135 {
136 stSetSessionItem("mode", "error");
137 stSetSessionItem("error", $errorMsgs);
138 }
139 else
140 stSetSessionItem("mode", "done");
141
142 header("Location: ".stGetRequestItem("goto", "vote"));
143 } 161 }
162 stSetSessionItem("mode", "done");
144 break; 163 break;
145 164
146 default: 165 default:
147 stSetStatus(902, "Operation not supported."); 166 stSetStatus(902, "Operation not supported.");
148 break; 167 break;
149 } 168 }
150 169
170
151 if ($errorSet) 171 if ($errorSet)
152 { 172 {
153 ob_clean(); 173 ob_clean();
154 stDumpAJAXStatusErrors(); 174 stSetSessionItem("mode", "error");
175 stSetSessionItem("error", $errorMsgs);
176 }
177
178 if ($ajax)
179 {
180 if ($errorSet)
181 stDumpAJAXStatusErrors();
182 }
183 else
184 {
185 header("Location: ".stGetRequestItem("goto", "vote"));
155 } 186 }
156 187
157 ob_end_flush(); 188 ob_end_flush();
158 ?> 189 ?>