-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
408 lines (337 loc) · 13.4 KB
/
index.php
File metadata and controls
408 lines (337 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
require_once "../../config.php";
// The Tsugi PHP API Documentation is available at:
// http://do1.dr-chuck.com/tsugi/phpdoc/namespaces/Tsugi.html
use \Tsugi\Core\Settings;
use \Tsugi\Core\LTIX;
use \Tsugi\Core\Cache;
// No parameter means we require CONTEXT, USER, and LINK
$LTI = LTIX::requireData();
// Model
$p = $CFG->dbprefix;
$old_code = Settings::linkGet('code', '');
if ( $USER->instructor) {
if (isset($_POST['action']) && strcmp($_POST['action'], "results_window") == 0) {
echo(file_get_contents("views/child_window.html"));
return;
}
if (isset($_POST['type']) && strcmp($_POST['type'], "multiple-choice") == 0) {
// create poll in DB
$title = null;
if (isset($_POST['title'])) { $title=$_POST['title']; }
$stmt = $PDOX->queryDie("INSERT INTO {$p}iclicker_polls (context_id, modified, title) VALUES (:UI, NOW(),:TI)",
array(
':UI' => $CONTEXT->id,
':TI' => $title
)
);
if ($stmt->success) { $poll_id = $PDOX->lastInsertId(); }
else {
error_log("TODO: failed to obtain lastInsertId from PDOX. Should probably handle appropriately...");
throw new Exception("lastInsertId not obtained from PDOX");
}
// insert choices into DB
$choice_ids = array();
foreach ($_POST['choice'] as $choice) {
$stmt = $PDOX->queryDie("INSERT INTO {$p}iclicker_choices (choice_hash, choice_value)
VALUES (PASSWORD(:VAL), :VAL)
ON DUPLICATE KEY UPDATE choice_id=LAST_INSERT_ID(choice_id), choice_value=:VAL",
array(
':VAL' => $choice
)
);
if ($stmt->success) { $choice_ids[$PDOX->lastInsertId()] = $choice; }
else {
error_log("TODO: failed to obtain lastInsertId from PDOX. Should probably handle appropriately...");
throw new Exception("lastInsertId not obtained from PDOX");
}
}
// associate poll and choices in DB
foreach (array_keys($choice_ids) as $id) {
$stmt = $PDOX->queryDie("INSERT INTO {$p}iclicker_pollchoices (poll_id, choice_id) VALUES (:PID,:CID)",
array(
':PID' => $poll_id,
':CID' => $id
)
);
if (!$stmt->success) { throw new Exception("cannot associate poll and choices"); }
}
// if choices should be ordered
if (isset($_POST['ordered'])) {
// mark poll as ordered
$PDOX->queryDie("UPDATE {$p}iclicker_polls SET ordered=1 WHERE poll_id=".$poll_id);
// store ordering in DB
$i = 0;
foreach (array_keys($choice_ids) as $id) {
$stmt = $PDOX->queryDie("INSERT INTO {$p}iclicker_order (poll_id, choice_id, position) VALUES (:PID,:CID,:POS)",
array(
':PID'=> $poll_id,
':CID' => $id,
':POS' => $i
)
);
if (!$stmt->success) { throw new Exception("Failed to record position"); }
$i++;
}
}
if (isset($_POST['action']) && strcmp($_POST['action'], "save") == 0) {
$stmt = $PDOX->queryDie("UPDATE iclicker_polls SET pending = 1 WHERE poll_id=".$poll_id);
echo(json_encode(array("action"=>"save","result"=>true, "poll_id"=>$poll_id, "date"=>date('Y-m-d H:i:s'))));
return;
}
// process form data, be sure to sanatize input!
// Need to check that owner doesn't have more than one active poll (or do I want to support this?)
$stmt = $PDOX->queryDie("INSERT INTO {$p}iclicker_active (poll_id, context_id) VALUES (:PID, :CID)
ON DUPLICATE KEY UPDATE poll_id=:PID",
array(
':PID' => $poll_id,
':CID' => $CONTEXT->id
)
);
// $stmt = $PDOX->queryDie("UPDATE iclicker_polls SET active=1");
if (!$stmt->success) {
error_log("TODO: unable to move to active. What to do...");
throw new Exception("failed to insert into active table");
}
$entryData = array(
'category' => 'polls',
'type' => $_POST['type'],
'poll' => $poll_id,
'choices' => $choice_ids,
'order' => 'random',
'when' => time()
);
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send(json_encode($entryData));
echo(json_encode(array('poll_id'=>$poll_id, 'date'=>date('Y-m-d H:i:s'))));
return;
}
else if (isset($_POST['action']) && strcmp($_POST['action'],"get_active") == 0) {
$stmt = $PDOX->queryDie("SELECT p.poll_id, p.title, p.modified FROM iclicker_active a INNER JOIN iclicker_polls p ON a.poll_id=p.poll_id WHERE a.context_id=:CID",
array(
':CID' => $CONTEXT->id
)
);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) { echo(""); return;}
$stmt = $PDOX->queryDie("SELECT COUNT(*) FROM {$p}iclicker_responses WHERE poll_id=".$row['poll_id']);
$count = $stmt->fetch(PDO::FETCH_ASSOC);
$row['count'] = $count['COUNT(*)'];
echo(json_encode($row));
return;
}
else if (isset($_POST['action']) && strcmp($_POST['action'],"retract_poll") == 0) {
if (!isset($_POST['poll_id'])) { echo("incomplete API call; missing parameter"); return; }
$PDOX->queryDie("UPDATE iclicker_polls SET completed=NOW() WHERE poll_id=".$_POST['poll_id']);
$stmt = $PDOX->queryDie("DELETE FROM {$p}iclicker_active WHERE context_id=".$CONTEXT->id);
if ($stmt->rowCount() > 0) {
echo("true");
} else {
echo('no poll to retract');
}
$entryData = array(
'category' => 'retract',
'when' => time()
);
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send(json_encode($entryData));
return;
}
else if (isset($_POST['action']) && strcmp($_POST['action'],"get_archive") == 0) {
if (isset($_POST['range']) && $_POST['range'] != "undefined") {
$stmt = $PDOX->queryDie("SELECT p.poll_id, p.title, p.completed AS date, COUNT(r.user_id) AS count, SUM(r.choice_id=p.answer_id) AS correct
FROM {$p}iclicker_polls p LEFT JOIN {$p}iclicker_responses r
ON p.poll_id=r.poll_id WHERE context_id=:CID AND p.pending IS NULL
GROUP BY p.poll_id ORDER BY modified
DESC LIMIT ".$_POST['range'].", 5", array(':CID'=>$CONTEXT->id));
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo(json_encode($row));
} else {
$stmt = $PDOX->queryDie("SELECT p.poll_id, p.title, p.completed AS date, COUNT(r.user_id) AS count, SUM(r.choice_id=p.answer_id) AS correct
FROM {$p}iclicker_polls p LEFT JOIN {$p}iclicker_responses r
ON p.poll_id=r.poll_id WHERE context_id=:CID AND p.pending IS NULL
GROUP BY p.poll_id ORDER BY modified
DESC LIMIT 5", array(':CID'=>$CONTEXT->id));
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo(json_encode($row));
}
return;
}
else if (isset($_POST['action']) && strcmp($_POST['action'],"get_poll") == 0) {
if (!isset($_POST['poll_id'])) { echo("incomplete API call; missing parameter"); return; }
$stmt=$PDOX->queryDie("SELECT c.choice_value AS value, COUNT(*) AS count FROM iclicker_responses r
JOIN iclicker_choices c ON r.choice_id=c.choice_id
WHERE poll_id=".$_POST['poll_id']." GROUP BY c.choice_value");
$responses = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt=$PDOX->queryDie("SELECT c.choice_value AS value FROM iclicker_pollchoices pc JOIN iclicker_choices c ON pc.choice_id=c.choice_id WHERE pc.poll_id=".$_POST['poll_id']);
$choices = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt = $PDOX->queryDie("SELECT * FROM iclicker_polls WHERE poll_id=".$_POST['poll_id']);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$row['responses'] = $responses;
$row['choices'] = $choices;
echo(json_encode($row));
return;
}
else if (isset($_POST['action']) && strcmp($_POST['action'],"copy_poll") == 0) {
if (!isset($_POST['poll_id'])) { echo("incomplete API call; missing parameter"); return; }
$stmt = $PDOX->queryDie("SELECT poll_id, title as ':t', answer_id as ':a', ordered as ':o' FROM iclicker_polls WHERE poll_id=".$_POST['poll_id']);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$old_poll_id = $row['poll_id'];
unset($row['poll_id']);
$row[':CID'] = $CONTEXT->id;
if (strcmp($row[':t'], "") == 0)
$row[':t'] = null;
$stmt = $PDOX->queryDie("INSERT INTO iclicker_polls (title, context_id, answer_id, ordered, modified, pending) VALUES ( :t, :CID, :a, :o, NOW(), 1)", $row);
$new_poll_id = $PDOX->lastInsertId();
$stmt = $PDOX->queryDie("SELECT choice_id FROM iclicker_pollchoices WHERE poll_id=".$old_poll_id);
$choices = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($choices as $choice) {
$stmt = $PDOX->queryDie("INSERT INTO iclicker_pollchoices (poll_id, choice_id) VALUES (:p, :c)", array(':p'=>$new_poll_id,':c'=>$choice['choice_id']));
}
echo(json_encode(array('poll_id'=>$new_poll_id)));
return;
}
else if (isset($_POST['action']) && strcmp($_POST['action'],"get_pending") == 0) {
$stmt = $PDOX->queryDie("SELECT poll_id,title,modified as date FROM iclicker_polls WHERE context_id=:CID AND pending=1",array(":CID"=>$CONTEXT->id));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$response = array("action"=>"get_pending", "result"=>$rows);
echo(json_encode($response));
return;
}
else if (isset($_POST['action']) && strcmp($_POST['action'],"make_active") == 0) {
if (!isset($_POST['poll_id'])) { echo("incomplete API call; missing parameter"); return; }
$stmt = $PDOX->queryDie("INSERT INTO {$p}iclicker_active (poll_id, context_id) VALUES (:PID, :CID)
ON DUPLICATE KEY UPDATE poll_id=:PID",
array(
':PID' => $_POST['poll_id'],
':CID' => $CONTEXT->id
)
);
$PDOX->queryDie("UPDATE iclicker_polls SET pending=NULL WHERE poll_id=".$_POST['poll_id']);
$stmt = $PDOX->queryDie("SELECT c.choice_id, c.choice_value FROM iclicker_pollchoices pc
JOIN iclicker_choices c ON pc.choice_id=c.choice_id
WHERE pc.poll_id=".$_POST['poll_id']);
$choices = $stmt->fetchAll(PDO::FETCH_ASSOC);
$choice_ids = array();
foreach ($choices as $choice) {
$choice_ids[$choice['choice_id']] = $choice['choice_value'];
}
$entryData = array(
'category' => 'polls',
'type' => "multiple-choice",
'poll' => $_POST['poll_id'],
'choices' => $choice_ids,
'order' => 'random',
'when' => time()
);
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send(json_encode($entryData));
echo(json_encode($entryData));
return;
}
else if (isset($_POST['action']) && strcmp($_POST['action'],"delete_poll") == 0) {
if (!isset($_POST['poll_id'])) { echo("incomplete API call; missing parameter"); return; }
$PDOX->queryDie("DELETE FROM iclicker_polls WHERE poll_id=".$_POST['poll_id']." AND context_id=".$CONTEXT->id);
$PDOX->queryDie("DELETE FROM iclicker_pollchoices WHERE poll_id=".$_POST['poll_id']);
echo('true');
return;
}
} else { // Student
// iclicker API values, action specifies response
if (isset($_POST['action']) && strcmp($_POST['action'], "get_active") == 0) {
$cacheloc = 'iclicker_active';
$row = Cache::check($cacheloc, $CONTEXT->id);
// if $row not found in cache
if ( $row == false ) {
$stmt = $PDOX->queryDie("SELECT poll_id FROM {$p}iclicker_active WHERE context_id=:CID",
array(
':CID' => $CONTEXT->id
)
);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
}
// if no active poll, echo empty response
if (!$row['poll_id']) {
echo('');
return;
}
// if poll is ordered, get choices and postions
if (isset($row['ordered'])) {
$stmt = $PDOX->queryDie("SELECT pc.poll_id, c.choice_id, c.choice_value, o.position
FROM iclicker_pollchoices pc INNER JOIN iclicker_choices c
ON pc.choice_id = c.choice_id JOIN iclicker_order o
ON c.choice_id = o.choice_id AND pc.poll_id = o.poll_id
WHERE pc.poll_id = :PID",
array(
':PID' => $row['poll_id']
)
);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
}
// if not ordered, get choices
else {
$stmt = $PDOX->queryDie("SELECT pc.poll_id, c.choice_id, c.choice_value
FROM iclicker_pollchoices pc INNER JOIN iclicker_choices c
ON pc.choice_id = c.choice_id
WHERE pc.poll_id = :PID",
array(
':PID' => $row['poll_id']
)
);
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// return response
echo(json_encode($row));
return;
}
else if (isset($_POST['action']) && strcmp($_POST['action'], "vote") == 0) {
$stmt = $PDOX->queryDie("SELECT * from iclicker_active WHERE poll_id=:PID",array(':PID'=>$_POST['poll_id']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) {
echo "expired poll";
return;
}
$stmt = $PDOX->queryDie("INSERT INTO {$p}iclicker_responses (user_id, poll_id, choice_id) VALUES (:UID,:PID,:CID)
ON DUPLICATE KEY UPDATE choice_id = :CID",
array(
':UID' => $USER->id,
':PID' => $_POST['poll_id'],
':CID' => $_POST['choice_id']
)
);
if (!$stmt->success) { echo("false"); throw new Exception("unable to record response"); return; }
echo("true");
return;
}
else if (isset($_POST['action']) && strcmp($_POST['action'], "get_vote") == 0) {
$stmt = $PDOX->queryDie("SELECT choice_id FROM {$p}iclicker_responses WHERE user_id=:UID AND poll_id=:PID",
array(
':UID' => $USER->id,
':PID' => $_POST['poll_id']
)
);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo(json_encode($row));
return;
}
}
// View
$OUTPUT->header();
$OUTPUT->bodyStart();
$OUTPUT->flashMessages();
if ( $USER->instructor ) {
include "views/instructor.php";
} else {
include "views/student.php";
}
echo <<<EOT
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] +
':35729/livereload.js?snipver=1"></' + 'script>')</script>
EOT;
$OUTPUT->footer();