forked from maxdougherty/snap_autograder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AG_EDX.js
179 lines (149 loc) · 6.81 KB
/
AG_EDX.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
// var courseID = ""; // e.g. "BJCx"
// // taskID uniquely identifies the task for saving in browser localStorage.
// var taskID = "AG_D1_T1";
// var id = courseID + taskID;
var AG_state = {
'checkState': false,
'comment': "Please run the Snap! Autograder to view feedback.",
'feedback': {}
};
var AG_EDX = (function() {
var channel;
if (window.parent !== window) {
channel = Channel.build({
window: window.parent,
origin: "*",
scope: "JSInput"
});
channel.bind("getGrade", getGrade);
channel.bind("getState", getState);
channel.bind("setState", setState);
}
// The following return value may or may not be used to grade
// server-side.
// If getState and setState are used, then the Python grader also gets
// access to the return value of getState and can choose it instead to
// grade.
function getGrade() {
console.log("getGrade");
//Grab Snap ide and testLog, null if AGTest() has not been called.
//var ide = world.children[0];
// console.log("THE ID IS: " + id);
if (sessionStorage.getItem(id + "_test_log") !== null){
var glog = JSON.parse(sessionStorage.getItem(id + "_test_log"));
var snapXML = sessionStorage.getItem(id + "_test_state");
// console.log(snapXML);
//Save Snap XML in Local Storage
// localStorage.setItem(id, xmlString);
//If AGTest() has been called, save the gradeLog in
if (glog !== undefined) {
//Convert to an AG_state
glog["showFeedback"] = showFeedback;
//var edx_log = AG_log(glog, snapXML);
var edx_log = glog;
edx_log["snapXML"] = snapXML;
//console.log(JSON.stringify(edx_log));
//saves correct student answer, as well as state, in case student returns to question
//sessionStorage.setItem(id + "_last_submitted_log", sessionStorage.getItem(id + "_test_log"));
//sessionStorage.setItem(id + "_last_submitted_state", snapXML);
//localStorage.setItem(id + "_ag_output", JSON.stringify(edx_log));
}
console.log("GET GRADE SUCCEEDING");
//console.log(encodeURIComponent(JSON.stringify(edx_log)));
return encodeURIComponent(JSON.stringify(edx_log));
} else {
return JSON.stringify(AG_state);
}
}
function getState() {
console.log("getState");
// return encodeURIComponent(JSON.stringify(AG_state));
// if _test_state and _test_log exist
// encode world and state
// else
// return 'never graded'
var graded_xml = sessionStorage.getItem(id + "_test_state");
var graded_log = sessionStorage.getItem(id + "_test_log");
var correct_xml = sessionStorage.getItem(id + "_c_test_state");
var correct_log = sessionStorage.getItem(id + "_c_test_log");
/*var submit_xml = sessionStorage.getItem(id + "_last_submitted_state");
var submit_log = sessionStorage.getItem(id + "_last_submitted_log");*/
if (!graded_xml || !graded_log) {
return 'never graded';
}
var output = {out_log:graded_log,state:encodeURIComponent(graded_xml)};
if (correct_xml && correct_log) {
output['c_log'] = correct_log;
output['c_state'] = encodeURIComponent(correct_xml);
}
/*if (submit_xml && submit_log) {
output['submit_log'] = submit_log;
output['submit_state'] = encodeURIComponent(submit_xml);
}*/
//console.log(encodeURIComponent(correct_xml));
output = encodeURI(JSON.stringify(output));
//console.log(output);
return output;
// var last_xml = localStorage.getItem(id + "_test_state");
// if (last_xml !== null) {
// return encodeURI(encodeURIComponent(last_xml));
// } else {
// var ide = world.children[0];
// var world_string = ide.serializer.serialize(ide.stage);
// return encodeURI(encodeURIComponent(world_string);
// }
// var ide = world.children[0];
// var world_string = ide.serializer.serialize(ide.stage);
// //return encodeURI(encodeURIComponent(world_string));
// return encodeURI(world_string);
}
//EDX: Used to save the world state into edX. FOR RELOAD
function setState() {
console.log('SET STATE IS CALLED');
var last_state_string = arguments.length === 1 ? arguments[0] : arguments[1];
//var ide = world.children[0];
if (last_state_string === 'starter file') {
var starter_xml = $.get(starter_path, function(data) {
sessionStorage.setItem(id + "starter_file", data)}, "text"); //TODO: Loading here still unsafe
return;
} else if (last_state_string === 'never graded') {
return;
} else if (last_state_string === 'no starter file') {
return;
} else {
var last_state = JSON.parse(last_state_string);
//console.log(last_state);
//console.log(last_state.out_log)
last_state.state = decodeURIComponent(last_state.state);
//console.log(last_state.out_log);
sessionStorage.setItem(id + '_test_state', last_state.state);
sessionStorage.setItem(id + '_test_log', last_state.out_log);
if (last_state.c_state && last_state.c_log) {
sessionStorage.setItem(id + '_c_test_state', decodeURIComponent(last_state.c_state));
sessionStorage.setItem(id + '_c_test_log', last_state.c_log);
}
console.log("submitted...");
/*if (last_state.submit_state && last_state.submit_log) {
sessionStorage.setItem(id + '_last_submitted_state', decodeURIComponent(last_state.submit_state));
sessionStorage.setItem(id + '_last_submitted_log', last_state.submit_log);
}*/
}
// var last_xml = arguments.length === 1 ? arguments[0] : arguments[1];
// var ide = world.children[0];
// if (last_xml === "starter file") {
// var starter_xml = $.get(starter_path, function(data) {
// console.log(data);
// ide.openProjectString(data)}, "text");
// return
// }
// console.log(last_xml);
// //var last_xml = arguments.length === 1 ? arguments[0] : arguments[1];
// //state = JSON.parse(stateStr);
// //ide.openProjectString(decodeURIComponent(last_xml));
// ide.openProjectString(last_xml);
}
return {
getState: getState,
setState: setState,
getGrade: getGrade};
}());