forked from maxdougherty/snap_autograder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AGAgent.js
508 lines (433 loc) · 17.8 KB
/
AGAgent.js
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
var current_iframe = window.frameElement;
//num_iframes = window.parent.document.getElementsByTagName("iframe").length;
var num_iframes = parent.document.getElementsByClassName('problem-header').length;
var iframes = parent.document.getElementsByTagName("iframe");
var id_problem = 0;
for (i = 0; i < num_iframes; i++) {
if (iframes[i] === current_iframe) {
id_problem = i;
//problem_iframe = iframes[i];
}
}
var showPoints = false;
// console.log(showPoints);
function isEDXurl() {
var url = window.location.href;
if (url.indexOf("edx") !== -1) {
return true;
} else {
return false;
}
}
/*if (isEDX) {
console.log(current_iframe.parentNode.parentNode.parentNode.parentNode.parentNode.nextElementSibling.children[1]);
var edX_check_button = current_iframe.parentNode.parentNode.parentNode.parentNode.parentNode.nextElementSibling.children[1];
current_iframe.parentNode.parentNode.parentNode.style.width = "100%";
}*/
/* Removes the previously saved AG_state. Runs the tests in
* AGTest().
* Called by 'click' event on autograder_button.
*/
function runAGTest(snapWorld, taskID, outputLog) {
// Create a new gradingLog if none is specified.
var numAttempts = setNumAttempts(taskID);
outputLog = outputLog || new FeedbackLog(snapWorld, taskID, numAttempts);
// Populate, run, and evaluate the tests specified in AGTest()
// These tests specified by the Course Designer.
//outputLog.numAttempts += 1;
//console.log(outputLog);
var test_log = AGTest(outputLog);
if(!test_log.runSnapTests()) {
test_log.scoreLog();
}
// test_log.scoreLog();
// populateFeedback(test_log);
}
/* After loading the XML, check if the current XML is a known
* state, restore the gradingLog if it is.
* @return {gradingLog}
* TODO: Trigger AGStart when a Snap file is loaded.
*/
function AGStart(snapWorld, taskID) {
//Grab HTML divs
var menu_button = document.getElementById("onclick-menu");
var grade_button = document.getElementById("autograding_button");
//Get the current Snap XML string
var ide = snapWorld.children[0];
var curr_xml = ide.serializer.serialize(ide.stage);
//Retrieve previously graded Snap XML strings (if in sessionStorage).
var c_prev_xml = sessionStorage.getItem(taskID + "_c_test_state");
var prev_xml = sessionStorage.getItem(taskID + "_test_state");
var outputLog;
if (!graded) {
AG_bar_nograde();
return
}
//If the current XML matches the stored correct XML
if (isSameSnapXML(c_prev_xml, curr_xml)) {
//Restore the AG status bar to a graded state
var outputLog = JSON.parse(sessionStorage.getItem(taskID + "_c_test_log"));
outputLog.snapWorld = snapWorld;
if (outputLog.allCorrect === true) {
AG_bar_graded(outputLog);
} else {
AG_bar_semigraded(outputLog);
}
return outputLog;
}
//If the current XML matches the last stored
if (isSameSnapXML(prev_xml, curr_xml)) {
//Restore the AG status bar to a graded state
var outputLog = JSON.parse(sessionStorage.getItem(taskID + "_test_log"));
outputLog.snapWorld = snapWorld;
AG_bar_semigraded(outputLog);
return outputLog;
} else {
//Restore the AG status bar to a graded state
//If no previous state is recognized, return new {gradingLog}.
var numAttempts = setNumAttempts(taskID);
outputLog = new FeedbackLog(snapWorld, taskID, 'test of new feedback', numAttempts);
AG_bar_ungraded(outputLog);
return outputLog;
}
}
/* Checks to see if the Snap! XML has changed and updates the
* AG status bar. If Snap! is restored to its former state
* the grading log and status bar are also restored.
* @return {gradingLog} outputLog
* Note:
* - Should only be called from a "mouseup" event.
*/
function AGUpdate(snapWorld, taskID) {
//TODO: Are there any optional parameters that may be useful?
//Grabs HTML divs
var menu_button = document.getElementById("onclick-menu");
var grade_button = document.getElementById("autograding_button");
//Get the current Snap XML string
var ide = snapWorld.children[0];
var curr_xml = ide.serializer.serialize(ide.stage);
//Retrieve previously graded Snap XML strings (if in sessionStorage).
var c_prev_xml = sessionStorage.getItem(taskID + "_c_test_state");
var c_prev_log = sessionStorage.getItem(taskID + "_c_test_log");
var prev_xml = sessionStorage.getItem(taskID + "_test_state");
var prev_log = sessionStorage.getItem(taskID + "_test_log");
//var last_xml = sessionStorage.getItem(taskID + "_last_submitted_state");
//Retrieve previous grade logs (if in sessionStorage). As {String}s
if (!prev_xml || !curr_xml) {
//console.log(prev_xml);
//console.log(curr_xml);
console.log('AGUpdate: Either prev_xml or curr_xml do not exist.');
}
//menu bar grays out options that are not available
//(ex. current state is same as best attempt) and restores the button state
grayOutButtons(snapWorld, taskID);
var outputLog;
if (!graded) {
AG_bar_nograde();
return
}
//If current XML is different from prev_xml
if (c_prev_xml && isSameSnapXML(c_prev_xml, curr_xml)) {
//Restore the AG status bar to a graded state
// TODO: Write a good comment
// TODO: Give gradeLog ability to recover log data and xml string
console.log('AGUpdate: Thinks this is the "correct" XML.');
sessionStorage.setItem(taskID + "_test_log", c_prev_log);
sessionStorage.setItem(taskID + "_test_state", curr_xml);
//document.getElementById("different-feedback").innerHTML = "";
//Retrieve the correct test log from sessionStorage
outputLog = JSON.parse(c_prev_log);
outputLog.snapWorld = snapWorld;
if (outputLog.allCorrect === true) {
AG_bar_graded(outputLog);
} else {
AG_bar_semigraded(outputLog);
}
/*if (isEDX) {
setTimeout(function() {
parent.document.getElementsByClassName('overlay-button')[id_problem].style.display = "none";
}, 500);
}*/
} else if (prev_xml && isSameSnapXML(prev_xml, curr_xml, true)) {
//Restore the AG status bar to a graded state
console.log('AGUpdate: Thinks this is just the "last" XML.');
//Retrieve the previous test log from sessionStorage
//document.getElementById("different-feedback").innerHTML = "";
outputLog = JSON.parse(prev_log);
outputLog.snapWorld = snapWorld;
AG_bar_semigraded(outputLog);
/*if (isEDX) {
setTimeout(function() {
parent.document.getElementsByClassName('overlay-button')[id_problem].style.display = "none";
}, 500);
}*/
} else {
// console.log("AGUpdate: Button should be ungraded");
//Restore the AG status bar to a graded state
var numAttempts = setNumAttempts(taskID);
outputLog = new FeedbackLog(snapWorld, taskID, "", numAttempts);
console.log(outputLog);
//outputLog = JSON.parse(prev_log);
//outputLog.snapWorld = snapWorld;
AG_bar_ungraded(outputLog);
console.log("button should change");
//document.getElementById("different-feedback").innerHTML = "This feedback does not match what is in the scripting area."
/*if (isEDX) {
setTimeout(function() {
parent.document.getElementsByClassName('overlay-button')[id_problem].style.display = "block";
}, 500);
}*/
}
//populateFeedback(outputLog);
return outputLog;
}
/* Updates the AG_status_bar with respect to the outputLog.
* - Formats CSS for 'autograding_flag' and 'autograding_button'
* If the outputLog is correct, save the Snap XML string into
* sessionStorage.
* - key = outputLog.taskID + "_c_test_state"
* Note:
* - Should only be called from outputLog.evaluateLog()
*/
function AGFinish(outputLog) {
var c_prev_log = JSON.parse(sessionStorage.getItem(outputLog.taskID + "_c_test_log"));
if (!graded) {
AG_bar_nograde();
return
}
// Verify correctness
if (outputLog.allCorrect) {
// Save the correct XML string into sessionStorage
AG_bar_graded(outputLog);
outputLog.saveSnapXML(outputLog.taskID + "_c_test_state");
} else if ((outputLog.pScore > 0) && ((c_prev_log && outputLog.pScore >= c_prev_log.pScore) || (!c_prev_log))) {
//if (outputLog.pScore >= c_prev_log.pScore && c_prev_log || !c_prev_log) {
//AG_bar_semigraded(outputLog);
//outputLog.saveSnapXML(outputLog.taskID + "_c_test_state");
// Update AG_status_bar to 'graded, but incorrect state
} else {
AG_bar_semigraded(outputLog);
}
//Save the current XML. Log is saved in gradingLog.scoreLog(...)
outputLog.saveSnapXML(outputLog.taskID + "_test_state");
//outputLog.numAttempts += 1;
if (showFeedback) {
populateFeedback(outputLog);
}
//grayOutButtons(outputLog.snapWorld, outputLog.taskID);
console.log('Autograder test Results:');
console.log(outputLog);
//populateFeedback(outputLog);
if (isEDX) {
//parent.document.getElementsByClassName('check-label')[id_problem].click();
edX_check_button.click();
}
if (!isEDX) {
populateFeedback(outputLog, false)
openResults();
}
//parent.document.getElementsByClassName('check-label')[0].click();
}
/*
* Reset state removes all saved logs and XML files, and opens a new
* Snap! file.
*/
function resetState(snapWorld, taskID) {
var numAttempts = JSON.parse(sessionStorage.getItem(taskID + "_test_log")).numAttempts;
sessionStorage.removeItem(taskID + "_test_log");
sessionStorage.removeItem(taskID + "_test_state");
sessionStorage.removeItem(taskID + "_c_test_log");
sessionStorage.removeItem(taskID + "_c_test_state");
var ide = snapWorld.children[0];
if (starter_path) {
$.get(starter_path, function(data) {
ide.openProjectString(data)},
"text");
} else {
ide.newProject();
}
var new_log = AGStart(snapWorld, taskID);
new_log.numAttempts = numAttempts;
//sessionStorage.setItem(taskID + "_test_log", new_log);
sessionStorage.setItem(taskID + "_test_state", ide.serializer.serialize(ide.stage));
new_log.saveLog();
//sessionStorage.setItem(taskID + "_test_log", new_log);
grayOutButtons(snapWorld, taskID);
}
function revertToBestState(snapWorld, taskID) {
var ide = snapWorld.children[0];
var numAttempts = JSON.parse(sessionStorage.getItem(taskID + "_test_log")).numAttempts;
var c_prev_xml = sessionStorage.getItem(taskID + "_c_test_state");
var c_prev_log = sessionStorage.getItem(taskID + "_c_test_log");
sessionStorage.setItem(taskID + "_test_state", c_prev_xml);
sessionStorage.setItem(taskID + "_test_log", c_prev_log);
var prev_log = JSON.parse(sessionStorage.getItem(taskID + "_test_log"));
prev_log.snapWorld = snapWorld;
AG_bar_graded(prev_log);
if (showFeedback) {
populateFeedback(prev_log);
}
prev_log.numAttempts = numAttempts;
ide.openProjectString(c_prev_xml);
grayOutButtons(snapWorld, taskID);
}
function revertToLastState(snapWorld, taskID) {
var ide = snapWorld.children[0];
var prev_xml = sessionStorage.getItem(taskID + "_test_state");
var prev_log = JSON.parse(sessionStorage.getItem(taskID + "_test_log"));
prev_log.snapWorld = snapWorld;
if (prev_log['allCorrect']) {
AG_bar_graded(prev_log);
} else {
AG_bar_semigraded(prev_log);
}
if (showFeedback) {
populateFeedback(prev_log);
}
ide.openProjectString(prev_xml);
grayOutButtons(snapWorld, taskID);
}
/*function revertToLastSubmit(snapWorld, taskID) {
var numAttempts = JSON.parse(sessionStorage.getItem(taskID + "_test_log")).numAttempts;
var last_xml = sessionStorage.getItem(taskID + "_last_submitted_state");
var last_log = sessionStorage.getItem(taskID + "_last_submitted_log");
var ide = snapWorld.children[0];
sessionStorage.setItem(taskID + "_test_state", last_xml);
sessionStorage.setItem(taskID + "_test_log", last_log);
var prev_log = JSON.parse(sessionStorage.getItem(taskID + "_test_log"));
if (prev_log['allCorrect']) {
AG_bar_graded(prev_log);
} else {
AG_bar_semigraded(prev_log);
}
populateFeedback(prev_log);
prev_log.numAttempts = numAttempts;
ide.openProjectString(last_xml);
grayOutButtons(snapWorld, taskID);
}*/
/* Checks if two Snap! XML strings have approximately the same state.
* The positions of scripts are ignored, as well as the order in which
* they were most recently manipulated.
* @param {String} prev_xml
* @param {String} curr_xml
* @return {Boolean} Equivalence of prev_xml and curr_xml, false if
* either are strings are undefined.
* Currently only works for one sprite with scripts
* TODO: Extend to all script groups [DONE]
* TODO: Improve XML scrubbing (Consider the following)
* - If correct solution (scripts) is subset of other [DONE]
* - Optional tags for variables, sprite position
* - Option to restore the highest scoring {gradingLog}
*
* Note: Apparently works with multiple sprites, but produces a malformed
* Snap XML string. Each sprite gets all scripts in sorted order. Needs
* further testing.
*/
function isSameSnapXML(prev_xml, curr_xml, no_subset) {
//replace script coordinates with generic 'x="0" y="0"'
// console.log('isSameSnapXML');
if ((prev_xml === null) || (curr_xml === null)) { return false; }
//Remove script coordinates
// prev_xml = prev_xml.replace(/script x="[\d]*" y="[\d]*"/g, 'script x="0" y="0"');
// curr_xml = curr_xml.replace(/script x="[\d]*" y="[\d]*"/g, 'script x="0" y="0"');
prev_xml = prev_xml.replace(/script x="(.*?)" y="(.*?)"/g, 'script x="0" y="0"');
curr_xml = curr_xml.replace(/script x="(.*?)" y="(.*?)"/g, 'script x="0" y="0"');
//Remove data hashes hashes (to allow coherence b/w reloads).
prev_xml = prev_xml.replace(/data:image(.*?)(?=<)/g, '');
curr_xml = curr_xml.replace(/data:image(.*?)(?=<)/g, '');
//If XML is identical other than images and script positions, short-circuit
if (prev_xml === curr_xml) { return true; }
//split between brackets
prev_xml_scripts = prev_xml.match(/(<script x)(.*?)(<\/script>)/g);
curr_xml_scripts = curr_xml.match(/(<script x)(.*?)(<\/script>)/g);
//split between custom blocks
prev_xml_blocks = prev_xml.match(/(<block-definition s)(.*?)(\/block-definition>)/g);
curr_xml_blocks = curr_xml.match(/(<block-definition s)(.*?)(\/block-definition>)/g);
//sort script tags and convert back to strings
//lol. weird syntax. doesn't sort if curr_xml_scripts === null.
prev_xml_scripts && prev_xml_scripts.sort().join("");
curr_xml_scripts && curr_xml_scripts.sort().join("");
//If the custom block definitions have changed
prev_xml_blocks && prev_xml_blocks.sort().join("");
curr_xml_blocks && curr_xml_blocks.sort().join("");
if(JSON.stringify(prev_xml_blocks) !== JSON.stringify(curr_xml_blocks)) {
return false;
}
//If the previous scripts are a subset of current scripts
if (!no_subset && isArrSubset(curr_xml_scripts, prev_xml_scripts)) {
//Then the solution is still present and in-tact
return true;
}
//replace unsorted scripts with sorted scripts
//TODO: Replace them properly
prev_xml = prev_xml.replace(/(<script x)(.*)(<\/script>)/g,prev_xml_scripts);
curr_xml = curr_xml.replace(/(<script x)(.*)(<\/script>)/g,curr_xml_scripts);
return prev_xml === curr_xml;
}
/*
* Helper Function for isSameSnapXML(...). Used to check if a previous
* solution is a sub-set of the current Snap! scripts.
* @param {Array:Strings} big
* @param {Array:String} small
* @return {Boolean} If 'small' is a sub-set of 'big'
*/
function isArrSubset(big, small) {
if (!big || !small) {
return false;
}
nbig = big.slice();
var index;
for (var elem of small) {
index = nbig.indexOf(elem);
if (nbig.indexOf(elem) >= 0) {
nbig.slice(index, 1);
} else {
return false;
}
}
return true;
}
function regradeOnClick(outputLog, testId) {
var test = outputLog[testId];
test.graded = false;
test.correct = false;
outputLog.numAttempts += 1;
if (test.testClass === "r") {
outputLog.startSnapTest(parseInt(testId, 10));
// for assertion tests, change feedback accordingly to whether assertion is true or false
} else if (test.testClass === "a") {
if (test.assertion()) {
test.feedback = test.pos_fb;
test.correct = true;
} else {
test.feedback = test.neg_fb;
test.correct = false;
}
test.graded = true;
}
//What about other types of tests?
outputLog.scoreLog();
console.log(outputLog);
}
function setNumAttempts(taskID) {
var prev_log = sessionStorage.getItem(taskID + "_test_log");
if (prev_log !== null && JSON.parse(prev_log).numAttempts !== undefined) {
return JSON.parse(prev_log).numAttempts;
} else {
return 0;
}
}
IDE_Morph.prototype.originalOpenProject = IDE_Morph.prototype.openProjectString;
IDE_Morph.prototype.openProjectString = function (name) {
this.originalOpenProject(name);
setTimeout(function() {
AGUpdate(world, id);
}, 1000);
}
IDE_Morph.prototype.originalCloudOpenProject = IDE_Morph.prototype.openCloudDataString;
IDE_Morph.prototype.openCloudDataString = function (name) {
this.originalCloudOpenProject(name);
setTimeout(function() {
AGUpdate(world, id);
}, 1000);
}