Skip to content

Commit

Permalink
MDL-58926 quiz: Addition of AJAX based quiz timer.
Browse files Browse the repository at this point in the history
  • Loading branch information
rboyatt committed Jun 6, 2020
1 parent 84e8bc4 commit 747f5ba
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
13 changes: 12 additions & 1 deletion mod/quiz/autosave.ajax.php
Expand Up @@ -61,4 +61,15 @@

$attemptobj->process_auto_save($timenow);
$transaction->allow_commit();
echo 'OK';

// Calculate time remaining.
$timeleft = $attemptobj->get_time_left_display($timenow);

// Build response, only returning timeleft if quiz in-progress
// has a time limit.
$r = new stdClass();
$r->status = "OK";
if ($timeleft !== false) {
$r->timeleft = $timeleft;
}
echo json_encode($r);
15 changes: 15 additions & 0 deletions mod/quiz/module.js
Expand Up @@ -58,6 +58,9 @@ M.mod_quiz.timer = {
// so we can cancel.
timeoutid: null,

// Threshold for updating time remaining, in milliseconds.
threshold: 3000,

/**
* @param Y the YUI object
* @param start, the timer starting time, in seconds.
Expand Down Expand Up @@ -130,6 +133,18 @@ M.mod_quiz.timer = {

// Arrange for this method to be called again soon.
M.mod_quiz.timer.timeoutid = setTimeout(M.mod_quiz.timer.update, 100);
},

// Allow the end time of the quiz to be updated.
updateEndTime: function(timeleft) {
var newtimeleft = new Date().getTime() + timeleft * 1000;

// Only update if change is greater than the threshold, so the
// time doesn't bounce around unnecessarily.
if (Math.abs(newtimeleft - M.mod_quiz.timer.endtime) > M.mod_quiz.timer.threshold) {
M.mod_quiz.timer.endtime = newtimeleft;
M.mod_quiz.timer.update();
}
}
};

Expand Down
Expand Up @@ -358,13 +358,19 @@ M.mod_quiz.autosave = {
},

save_done: function(transactionid, response) {
if (response.responseText !== 'OK') {
var autosavedata = JSON.parse(response.responseText);
if (autosavedata.status !== 'OK') {
// Because IIS is useless, Moodle can't send proper HTTP response
// codes, so we have to detect failures manually.
this.save_failed(transactionid, response);
return;
}

if (typeof autosavedata.timeleft !== 'undefined') {
Y.log('Updating timer: ' + autosavedata.timeleft + ' seconds remain.', 'debug', 'moodle-mod_quiz-timer');
M.mod_quiz.timer.updateEndTime(autosavedata.timeleft);
}

Y.log('Save completed.', 'debug', 'moodle-mod_quiz-autosave');
this.save_transaction = null;

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -350,13 +350,18 @@ M.mod_quiz.autosave = {
},

save_done: function(transactionid, response) {
if (response.responseText !== 'OK') {
var autosavedata = JSON.parse(response.responseText);
if (autosavedata.status !== 'OK') {
// Because IIS is useless, Moodle can't send proper HTTP response
// codes, so we have to detect failures manually.
this.save_failed(transactionid, response);
return;
}

if (typeof autosavedata.timeleft !== 'undefined') {
M.mod_quiz.timer.updateEndTime(autosavedata.timeleft);
}

this.save_transaction = null;

if (this.dirty) {
Expand Down
8 changes: 7 additions & 1 deletion mod/quiz/yui/src/autosave/js/autosave.js
Expand Up @@ -356,13 +356,19 @@ M.mod_quiz.autosave = {
},

save_done: function(transactionid, response) {
if (response.responseText !== 'OK') {
var autosavedata = JSON.parse(response.responseText);
if (autosavedata.status !== 'OK') {
// Because IIS is useless, Moodle can't send proper HTTP response
// codes, so we have to detect failures manually.
this.save_failed(transactionid, response);
return;
}

if (typeof autosavedata.timeleft !== 'undefined') {
Y.log('Updating timer: ' + autosavedata.timeleft + ' seconds remain.', 'debug', 'moodle-mod_quiz-timer');
M.mod_quiz.timer.updateEndTime(autosavedata.timeleft);
}

Y.log('Save completed.', 'debug', 'moodle-mod_quiz-autosave');
this.save_transaction = null;

Expand Down

0 comments on commit 747f5ba

Please sign in to comment.