-
Notifications
You must be signed in to change notification settings - Fork 2
/
grader.php
executable file
·389 lines (351 loc) · 13.7 KB
/
grader.php
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
<!DOCTYPE html><?php
require_once "tools.php";
require_once "grader_rubrics.php";
if (!$isstaff) {
http_response_code(403);
die('staff use only');
}
$_review = array();
function get_review($quizid) {
if (isset($_review[$quizid])) return $_review[$quizid];
if (FALSE && file_exists("cache/$quizid-review.json")) {
$ans = json_decode(file_get_contents("cache/$quizid-review.json"), true);
} else {
$ans = array();
histogram($quizid, $ans);
}
if (file_exists("log/$quizid/regrades.log")) {
$fh = fopen("log/$quizid/regrades.log", "r");
while(($line = fgets($fh)) != FALSE) {
$rgent = json_decode($line, true);
if (!isset($ans["$rgent[task]-regrade"]))
$ans["$rgent[task]-regrade"] = array();
if ($rgent['add']) {
$ans["$rgent[task]-regrade"][$rgent['student']] = true;
} else {
unset($ans["$rgent[task]-regrade"][$rgent['student']]);
}
}
fclose($fh);
foreach($ans as $key=>$val) if (substr($key, 8) == '-regrade') {
$slug = substr($key,0,8);
if (!isset($ans[$slug])) $ans[$slug] = array();
foreach($val as $user=>$dump) {
if (!in_array($user, $ans[$slug])) $ans[$slug][] = $user;
}
}
}
$_review[$quizid] = $ans;
return $ans;
}
/**
* Given a quiz and one of its questions, return an array.
* keys are user-submitted text
* values are arrays with three keys:
* "matches":{"quiz key":weight, "quiz key":weight, ...}
* "users":["mst3k", ...]
* "decided": (null or number)
*/
function get_blanks($quizid, $q) {
$slug = $q['slug'];
$rev = get_review($quizid);
if (!isset($rev["$slug-answers"])) return array();
$ext = array();
if (file_exists("log/$q[quizid]/key_$slug.json"))
$ext = json_decode(file_get_contents("log/$q[quizid]/key_$slug.json"), true);
$ans = array();
foreach($rev["$slug-answers"] as $txt=>$users) {
$match = array();
foreach($q['key'] as $key) {
$k = $key['text'];
if(($k[0] == '/') ? preg_match($k, $txt) : $k == $txt)
$match[$k] = $key['points'];
}
$ans[$txt] = array(
'users' => $users,
'matches' => $match,
'decided' => (isset($ext[$txt]) ? $ext[$txt] : null),
);
}
return $ans;
}
function null_first($a,$b) {
if ($a === null) return $b === null ? 0 : -1;
return $b === null ? 1 : 0;
}
/**
* Given a quiz and one of its questions, return an array.
* keys are users
* values are either null (if not reviewed) or an array with keys
* "feedback":"TA entered text" or "" (if no feedback text)
* "grade":number (new grade) or null (no change)
*/
function get_comments($quizid, $slug) {
$rev = get_review($quizid);
if (!isset($rev[$slug])) return array();
$whom = $rev[$slug];
$ans = array();
foreach($whom as $k) $ans[$k] = null;
if (file_exists("log/$quizid/adjustments_$slug.csv")) {
$fh = fopen("log/$quizid/adjustments_$slug.csv", "r");
while (($row = fgetcsv($fh)) !== FALSE) {
$ans[$row[0]] = array(
"grade" => is_numeric($row[1]) ? floatval($row[1]) : null,
"feedback" => $row[2],
);
}
}
foreach($ans as $k=>$v) {
if (isset($rev["$slug-regrade"][$k])) $ans[$k] = null;
}
uasort($ans, 'null_first');
return $ans;
}
function show_blanks($quizid, $q, $mq) {
$slug = $q['slug'];
if ($mq['text']) echo "<div class='multiquestion'>$mq[text]";
showQuestion($q, $quizid, '', 'none', false, $mq['text'], array(''), true, true, false, true);
if ($mq['text']) echo '</div>';
$anum = 0;
foreach(get_blanks($quizid, $q) as $opt => $details) {
$anum += 1;
echo "<div class='multiquestion";
if (isset($details['decided'])) echo " submitted";
echo "' id='q-$anum'>Reply: <code style='font-size:150%; border: thin solid gray'>";
echo htmlentities($opt)."</code> – ".count($details['users'])." replies";
$score = 0;
foreach($details['matches'] as $key=>$weight) {
echo "<br/>matches key($weight): <code style='font-size:150%; border: thin solid gray'>".htmlentities($key)."</code>";
if ($weight > $score) $score = $weight;
}
if (isset($details['decided'])) $score = $details['decided'];
echo "<p>Points: <input type='text' id='a-$anum' value='$score' onchange='setKey(\"$anum\",".json_encode(str_replace("'","'",$opt)).")' onkeydown='pending($\"$anum\")'/>";
if (!isset($details['decided']))
echo "<input type='button' onclick='setKey(\"$anum\",".json_encode(str_replace("'","'",$opt)).")' id='delme-$anum' value='no reply needed'/>";
echo "</p>";
echo "</div>";
}
}
function show_comments($quizid, $q, $mq) {
$qobj = qparse($quizid);
$hist = histogram($qobj);
foreach(get_comments($quizid, $q['slug']) as $user=>$details) {
$sobj = aparse($qobj, $user);
grade($qobj, $sobj); // annotate with score
echo "<div class='multiquestion";
if (isset($details)) echo " submitted";
echo "' id='q-$user'>$mq[text]";
showQuestion($q, $quizid, $user, $user, $qobj['comments']
,$mq['text']
,isset($sobj[$q['slug']]) ? $sobj[$q['slug']]
: array('answer'=>array(),'comments'=>'')
,true
,$hist
,true
,false
);
$score = isset($sobj[$q['slug']]['score']) ? $sobj[$q['slug']]['score'] : 0;
if ($q['points']) $score /= $q['points'];
$rawscore = $score;
$feedback = '';
if (isset($details['grade'])) $score = $details['grade'];
if (isset($details['feedback'])) $feedback = $details['feedback'];
echo "<p>Points: <input type='text' id='a-$user' value='$score' onchange='setComment(\"$user\")' rawscore='$rawscore' onkeydown='pending(\"$user\")'/></p>";
echo "<div class='tinput'><span>Feedback:</span><textarea id='r-$user' onchange='setComment(\"$user\")' onkeydown='pending(\"$user\")'";
echo ">";
echo htmlentities($feedback);
echo "</textarea></div>";
if (!isset($details))
echo "<input type='button' onclick='setComment(\"$user\")' id='delme-$user' value='no reply needed'/>";
echo '</div>';
}
?><script>
document.querySelectorAll('textarea').forEach(x => {
x.style.height = 'auto';
x.style.height = x.scrollHeight+'px';
});
</script><?php
}
?>
<html>
<head>
<title>Grade <?=$metadata['quizname']?> <?=isset($_GET['qid']) ? $_GET['qid'] : ''?></title>
<link rel="icon" type="image/svg+xml" href="favicon.svg">
<link rel="stylesheet" href="style.css">
<script type="text/javascript" src="katex/katex.min.js"></script>
<link rel="stylesheet" href="katex/katex.min.css">
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
document.querySelectorAll('span.mymath').forEach(x =>
katex.render(x.textContent, x,
{throwOnError:false, displayMode:false})
)
document.querySelectorAll('div.mymath').forEach(x =>
katex.render(x.textContent, x,
{throwOnError:false, displayMode:true})
)
});
</script>
<script type="text/javascript">//<!--
var quizid = <?=json_encode(isset($_GET['qid']) ? $_GET['qid'] : null)?>;
var slug = <?=json_encode(isset($_GET['slug']) ? $_GET['slug'] : null)?>;
function pending(num) {
if (document.getElementById('q-'+num).className != "multiquestion submitting")
document.getElementById('q-'+num).className = "multiquestion submitting";
}
function setKey(id, val) {
document.getElementById('q-'+id).className = 'multiquestion submitting';
let v = Number(document.getElementById('a-'+id).value);
if (isNaN(v) || v<0 || v>1) {
document.getElementById('q-'+id).className = 'multiquestion disconnected';
return
}
let datum = {
'kind':'key',
'quiz':quizid,
'slug':slug,
'key':val,
'val':v,
}
console.log(datum);
ajaxSend(datum, id);
let tmp = document.getElementById('delme-'+id)
if (tmp) tmp.remove();
}
function setComment(id) {
document.getElementById('q-'+id).className = 'multiquestion submitting';
let v = document.getElementById('a-'+id).value;
if (v != '') {
v = Number(v)
if (isNaN(v) || v<0 || v>1) {
document.getElementById('q-'+id).className = 'multiquestion disconnected';
return
}
}
if (v == Number(document.getElementById('a-'+id).getAttribute('rawscore')))
v = '';
let r = document.getElementById('r-'+id).value;
let datum = {
'kind':'reply',
'quiz':quizid,
'slug':slug,
'user':id,
'score':v,
'reply':r,
}
console.log(datum);
ajaxSend(datum, id);
let tmp = document.getElementById('delme-'+id)
if (tmp) tmp.remove();
}
function ajaxSend(data, id) {
var xhr = new XMLHttpRequest();
if (!("withCredentials" in xhr)) {
return null;
}
xhr.open("POST", "grader_listener.php", true);
xhr.withCredentials = true;
xhr.setRequestHeader("Content-type", 'application/json');
xhr.onerror = function() {
console.log("auto-check for new data broken");
}
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
console.log("done", xhr);
if (xhr.status == 200) {
document.getElementById('q-'+id).className = "multiquestion submitted";
console.log("response: " + xhr.responseText);
}
}
}
xhr.send(JSON.stringify(data));
}
//--></script>
</head>
<body><?php
if (isset($_GET['qid']) && !isset(($qobj = qparse($_GET['qid']))['error'])) {
$questions = array();
$mqs = array();
foreach($qobj['q'] as $mq) foreach($mq['q'] as $q) {
$questions[$q['slug']] = $q;
$mqs[$q['slug']] = $mq;
}
if (isset($_GET['slug']) && isset($questions[$_GET['slug']])) {
if ($_GET['kind'] == 'blank') {
show_blanks($_GET['qid'], $questions[$_GET['slug']], $mqs[$_GET['slug']]);
} else if ($_GET['kind'] == 'comment') {
show_comments($_GET['qid'], $questions[$_GET['slug']], $mqs[$_GET['slug']]);
} else if ($_GET['kind'] == 'rubric') {
show_rubric($_GET['qid'], $questions[$_GET['slug']], $mqs[$_GET['slug']]);
} else {
echo "To do: show \"$_GET[kind]\" view for $qobj[slug] question $_GET[slug]\n";
}
} else {
$rev = get_review($qobj['slug']);
?><table><thead>
<tr><th>Kind</th><th>Hash</th><th>Done</th><th>Text</th></tr>
</thead><tbody>
<?php
/*
$qnum = 0;
foreach($questions as $num=>$q) {
$qnum += 1;
if (isset($rev["$q[slug]-answers"])) {
echo "<tr><td>Question $qnum blank</td></tr>";
}
if (isset($rev["$q[slug]"])) {
echo "<tr><td>Question $qnum comments</td></tr>";
}
}
*/
echo "<script>console.log(".json_encode(array_keys($rev)).")</script>";
foreach($rev as $slug=>$val) if (substr($slug,8) == '-pending') {
$slug = substr($slug,0,8);
$done = count($rev["$slug-graded"]);
$left = count($val);
$of = $done + $left;
echo "<tr><td>rubric</td><td><a href='?qid=$_GET[qid]&slug=$slug&kind=rubric'>$slug</a></td><td";
if ($left == 0) echo ' class="submitted"';
echo ">$done of $of";
echo "</td><td>".$questions[$slug]['text']."</td></tr>\n";
}
foreach($rev as $slug=>$val) if (substr($slug,8) == '-answers') {
$slug = substr($slug,0,8);
echo "<tr><td>blank</td><td><a href='?qid=$_GET[qid]&slug=$slug&kind=blank'>$slug</a></td><td";
$of = count($val);
$sheet = get_blanks($qobj['slug'], $questions[$slug]);
$left = 0;
foreach($sheet as $obj)
if (!isset($obj['decided'])) $left += 1;
if ($left == 0) echo ' class="submitted"';
echo ">".($of-$left)." of $of";
echo "</td><td>".$questions[$slug]['text']."</td></tr>\n";
}
foreach($rev as $slug=>$val) if (substr($slug,8) == '-regrade') {
$num = count($val);
if (!$num) continue;
$slug = substr($slug,0,8);
echo "<tr><td>regrade</td><td><a href='?qid=$_GET[qid]&slug=$slug&kind=comment'>$slug</a></td><td>0 of $num</td><td>".$questions[$slug]['text']."</td></tr>\n";
}
foreach($rev as $slug=>$val) if (strlen($slug) == 8) {
echo "<tr><td>comment</td><td><a href='?qid=$_GET[qid]&slug=$slug&kind=comment'>$slug</a></td><td";
$of = count($val);
$sheet = get_comments($qobj['slug'], $slug);
$left = 0;
foreach($sheet as $uid=>$obj)
if (!is_array($obj)) $left += 1;
if ($left == 0) echo ' class="submitted"';
echo ">".($of-$left)." of $of";
echo "</td><td>".$questions[$slug]['text']."</td></tr>\n";
}
?></tbody></table><?php
}
} else {
foreach(glob('questions/*.md') as $i=>$name) {
$name = basename($name,".md");
$qobj = qparse($name);
if ($qobj['due'] >= time()) continue;
echo "<br/><a href='grader.php?qid=$name'>$name: $qobj[title]</a>";
}
}
?></body></html>