Skip to content

Commit

Permalink
MDL-3054 record the current page of a quiz attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
mackensen authored and kordan committed Jan 19, 2012
1 parent abd7382 commit 4e2f2fa
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
5 changes: 5 additions & 0 deletions mod/quiz/attempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noquestionsfound');
}

// Update attempt page
if ($attemptobj->get_currentpage() != $page) {
$DB->set_field('quiz_attempts', 'currentpage', $page);
}

// Initialise the JavaScript.
$headtags = $attemptobj->get_html_head_contributions($page);
$PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());
Expand Down
5 changes: 5 additions & 0 deletions mod/quiz/attemptlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,11 @@ public function get_attempt_number() {
public function get_userid() {
return $this->attempt->userid;
}

/** @return int the current page of the attempt. */
public function get_currentpage() {
return $this->attempt->currentpage;
}

/**
* @return bool whether this attempt has been finished (true) or is still
Expand Down
5 changes: 3 additions & 2 deletions mod/quiz/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@
<FIELD NAME="timestart" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="sumgrades" NEXT="timefinish"/>
<FIELD NAME="timefinish" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timestart" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timefinish" NEXT="layout"/>
<FIELD NAME="layout" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" PREVIOUS="timemodified" NEXT="preview"/>
<FIELD NAME="preview" TYPE="int" LENGTH="3" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="layout" NEXT="needsupgradetonewqe"/>
<FIELD NAME="layout" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" PREVIOUS="timemodified" NEXT="currentpage"/>
<FIELD NAME="currentpage" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" PREVIOUS="layout" NEXT="preview"/>
<FIELD NAME="preview" TYPE="int" LENGTH="3" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="currentpage" NEXT="needsupgradetonewqe"/>
<FIELD NAME="needsupgradetonewqe" TYPE="int" LENGTH="3" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="preview"/>
</FIELDS>
<KEYS>
Expand Down
12 changes: 12 additions & 0 deletions mod/quiz/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ function xmldb_quiz_upgrade($oldversion) {
// quiz savepoint reached
upgrade_mod_savepoint(true, 2011120701, 'quiz');
}

if ($oldversion < 2011120703) {
// Track page of quiz attempts
$table = new xmldb_table('quiz_attempts');

$field = new xmldb_field('currentpage', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);

if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
upgrade_mod_savepoint(true, 2011120703, 'quiz');
}

return true;
}
Expand Down
10 changes: 9 additions & 1 deletion mod/quiz/startattempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// Get submitted parameters.
$id = required_param('cmid', PARAM_INT); // Course module id
$forcenew = optional_param('forcenew', false, PARAM_BOOL); // Used to force a new preview
$page = optional_param('page', 0, PARAM_INT); // Page to jump to in the attempt.
$page = optional_param('page', -1, PARAM_INT); // Page to jump to in the attempt.

if (!$cm = get_coursemodule_from_id('quiz', $id)) {
print_error('invalidcoursemodule');
Expand Down Expand Up @@ -86,6 +86,10 @@
if ($lastattempt && !$lastattempt->timefinish) {
$currentattemptid = $lastattempt->id;
$messages = $accessmanager->prevent_access();

if ($page == -1) {
$page = $lastattempt->currentpage;
}

} else {
// Get number for the next or unfinished attempt
Expand All @@ -99,6 +103,10 @@

$messages = $accessmanager->prevent_access() +
$accessmanager->prevent_new_attempt(count($attempts), $lastattempt);

if ($page == -1) {
$page = 0;
}
}

// Check access.
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

defined('MOODLE_INTERNAL') || die();

$module->version = 2011120702; // The current module version (Date: YYYYMMDDXX)
$module->version = 2011120703; // The current module version (Date: YYYYMMDDXX)
$module->requires = 2011112900; // Requires this Moodle version
$module->component = 'mod_quiz'; // Full name of the plugin (used for diagnostics)
$module->cron = 60;

0 comments on commit 4e2f2fa

Please sign in to comment.