30
30
/** User clicks "Run" button. Do an Ajax call and print the test output.
32
function runproblem(problemid, filename)
32
function runexercise(exerciseid, filename)
34
34
/* Get the source code the student is submitting */
35
var problemdiv = document.getElementById(problemid);
36
var problembox = problemdiv.getElementsByTagName("textarea")[0];
37
var code = problembox.value;
35
var exercisediv = document.getElementById(problemid);
36
var exercisebox = exercisediv.getElementsByTagName("textarea")[0];
37
var code = exercisebox.value;
39
39
/* Dump the entire file to the console */
40
40
console_enter_line(code, "block");
44
44
/** Given a response object (JSON-parsed object), displays the result of the
45
* test to the user. This modifies the given problemdiv's children.
45
* test to the user. This modifies the given exercisediv's children.
47
function handle_runresponse(problemdiv, runresponse)
47
function handle_runresponse(exercisediv, runresponse)
49
var runoutput = problemdiv.getElementsByTagName("textarea")[1];
49
var runoutput = exercisediv.getElementsByTagName("textarea")[1];
50
50
dom_removechildren(runoutput);
51
51
runoutput.appendChild(document.createTextNode(runresponse.stdout));
54
54
/** User clicks "Submit" button. Do an Ajax call and run the test.
55
* problemid: "id" of the problem's div element.
56
* filename: Filename of the problem's XML file (used to identify the problem
55
* exerciseid: "id" of the exercise's div element.
56
* filename: Filename of the exercise's XML file (used to identify the exercise
57
57
* when interacting with the server).
59
function submitproblem(problemid, filename)
59
function submitexercise(exerciseid, filename)
61
61
/* Get the source code the student is submitting */
62
var problemdiv = document.getElementById(problemid);
63
var problembox = problemdiv.getElementsByTagName("textarea")[0];
64
var code = problembox.value;
62
var exercisediv = document.getElementById(exerciseid);
63
var exercisebox = exercisediv.getElementsByTagName("textarea")[0];
64
var code = exercisebox.value;
66
var args = {"code": code, "problem": filename, "action": "test"};
66
var args = {"code": code, "exercise": filename, "action": "test"};
68
68
/* Send the form as multipart/form-data, since we are sending a whole lump
69
69
* of Python code, it should be treated like a file upload. */
70
70
var xhr = ajax_call("tutorialservice", "", args, "POST",
71
71
"multipart/form-data");
72
72
var testresponse = JSON.parse(xhr.responseText);
73
handle_testresponse(problemdiv, testresponse);
73
handle_testresponse(exercisediv, testresponse);
76
/** Given a problem div, return the testoutput div which is its child.
76
/** Given a exercise div, return the testoutput div which is its child.
77
77
* (The div which is its child whose class is "testoutput".
79
function get_testoutput(problemdiv)
79
function get_testoutput(exercisediv)
81
var childs = problemdiv.childNodes;
81
var childs = exercisediv.childNodes;
84
84
for (i=0; i<childs.length; i++)
85
if (childs[i].nodeType == problemdiv.ELEMENT_NODE &&
85
if (childs[i].nodeType == exercisediv.ELEMENT_NODE &&
86
86
childs[i].getAttribute("class") == "testoutput")
91
91
/** Given a response object (JSON-parsed object), displays the result of the
92
* test to the user. This modifies the given problemdiv's children.
92
* test to the user. This modifies the given exercisediv's children.
94
function handle_testresponse(problemdiv, testresponse)
94
function handle_testresponse(exercisediv, testresponse)
96
var testoutput = get_testoutput(problemdiv);
96
var testoutput = get_testoutput(exercisediv);