465
/** User clicks "view previous attempts" button. Do an Ajax call and populate.
466
* exerciseid: "id" of the exercise's div element.
467
* filename: Filename of the exercise's XML file (used to identify the
468
* exercise when interacting with the server).
470
function open_previous(exerciseid, filename)
472
var exercisediv = document.getElementById(exerciseid);
473
var divs = exercisediv.getElementsByTagName("div");
475
for (var i=0; i<divs.length; i++)
476
if (divs[i].getAttribute("class") == "attempthistory")
477
attempthistory = divs[i];
479
/* Get handles on the four existing elements of the history box */
480
var openbutton = attempthistory.getElementsByTagName("p")[0];
481
var openarea = attempthistory.getElementsByTagName("div")[0];
482
var dropdown = attempthistory.getElementsByTagName("select")[0];
483
var textarea = attempthistory.getElementsByTagName("textarea")[0];
485
/* Activate the "open" state, and clear the dropdown box */
486
openbutton.setAttribute("style", "display: none");
487
openarea.setAttribute("style", "display: auto");
488
textarea.setAttribute("style", "display: none");
489
dom_removechildren(dropdown);
490
var pleasewait = document.createElement("option");
491
pleasewait.appendChild(document.createTextNode("Retrieving past attempts..."));
492
dropdown.appendChild(pleasewait);
494
var args = {"exercise": filename, "action": "getattempts"};
496
/* Send the form as multipart/form-data, since we are sending a whole lump
497
* of Python code, it should be treated like a file upload. */
498
/* AJAX callback function */
499
var callback = function(xhr)
506
attempts = JSON.parse(xhr.responseText);
510
alert("There was an error fetching your attempt history. "
511
+ "Please notify the administrators of this.");
514
/* Populate the attempt history div */
515
dom_removechildren(dropdown);
516
for (var i=0; i<attempts.length; i++)
518
/* An object with a date and complete */
519
attempt = attempts[i];
520
opt = document.createElement("option");
521
opt.setAttribute("value", attempt.date);
522
opt.appendChild(document.createTextNode(attempt.date));
523
if (attempt.complete)
525
/* Add a little green ball to this option
526
* This is probably hideously illegal, but looks nice :)
528
opt.appendChild(document.createTextNode(" "));
529
var img = document.createElement("img");
530
img.setAttribute("src",
531
make_path("media/images/tutorial/tiny/complete.png"));
532
img.setAttribute("alt", "Complete");
533
opt.appendChild(img);
535
dropdown.appendChild(opt);
538
ajax_call(callback, "tutorialservice", "", args, "GET");
541
function close_previous(exerciseid)
543
var exercisediv = document.getElementById(exerciseid);
544
var divs = exercisediv.getElementsByTagName("div");
546
for (var i=0; i<divs.length; i++)
547
if (divs[i].getAttribute("class") == "attempthistory")
548
attempthistory = divs[i];
550
/* Get handles on the four existing elements of the history box */
551
var openbutton = attempthistory.getElementsByTagName("p")[0];
552
var openarea = attempthistory.getElementsByTagName("div")[0];
554
/* Deactivate the "open" state */
555
openbutton.setAttribute("style", "display: auto");
556
openarea.setAttribute("style", "display: none");
559
/** User selects an attempt in the dropdown. Do an Ajax call and populate.
560
* exerciseid: "id" of the exercise's div element.
561
* filename: Filename of the exercise's XML file (used to identify the
562
* exercise when interacting with the server).
564
function select_attempt(exerciseid, filename)
566
var exercisediv = document.getElementById(exerciseid);
567
var divs = exercisediv.getElementsByTagName("div");
569
for (var i=0; i<divs.length; i++)
570
if (divs[i].getAttribute("class") == "attempthistory")
571
attempthistory = divs[i];
573
/* Get handles on the four existing elements of the history box */
574
var dropdown = attempthistory.getElementsByTagName("select")[0];
575
var textarea = attempthistory.getElementsByTagName("textarea")[0];
577
/* Get the "value" of the selected option */
578
var date = dropdown.options[dropdown.selectedIndex].getAttribute("value");
580
var args = {"exercise": filename, "action": "getattempt", "date": date};
582
/* Send the form as multipart/form-data, since we are sending a whole lump
583
* of Python code, it should be treated like a file upload. */
584
/* AJAX callback function */
585
var callback = function(xhr)
590
attempt = JSON.parse(xhr.responseText);
594
alert("There was an error fetching your attempt history. "
595
+ "Please notify the administrators of this.");
600
/* There was no data for this date - that's odd */
601
alert("There was no attempt made before that date.");
604
/* Populate the attempt text field */
605
textarea.removeAttribute("readonly");
606
dom_removechildren(textarea);
607
textarea.appendChild(document.createTextNode(attempt));
608
textarea.setAttribute("style", "display: auto");
609
//textarea.setAttribute("readonly", "readonly");
611
ajax_call(callback, "tutorialservice", "", args, "GET");