diff --git a/mod/quiz/attemptlib.php b/mod/quiz/attemptlib.php index a774cf4d4ce..578144bd7d0 100644 --- a/mod/quiz/attemptlib.php +++ b/mod/quiz/attemptlib.php @@ -2320,6 +2320,24 @@ class quiz_attempt { $event->trigger(); } + /** + * Update the timemodifiedoffline attempt field. + * This function should be used only when web services are being used. + * + * @param int $time time stamp + * @return boolean false if the field is not updated becase web services aren't being used. + * @since Moodle 3.2 + */ + public function set_offline_modified_time($time) { + global $DB; + + // Update the timemodifiedoffline field only if web services are being used. + if (WS_SERVER) { + $attemptobj->attempt->timemodifiedoffline = $time; + } + return false; + } + } diff --git a/mod/quiz/backup/moodle2/backup_quiz_stepslib.php b/mod/quiz/backup/moodle2/backup_quiz_stepslib.php index 10ada061ba7..59b3b67013a 100644 --- a/mod/quiz/backup/moodle2/backup_quiz_stepslib.php +++ b/mod/quiz/backup/moodle2/backup_quiz_stepslib.php @@ -49,7 +49,8 @@ class backup_quiz_activity_structure_step extends backup_questions_activity_stru 'questionsperpage', 'navmethod', 'shuffleanswers', 'sumgrades', 'grade', 'timecreated', 'timemodified', 'password', 'subnet', 'browsersecurity', - 'delay1', 'delay2', 'showuserpicture', 'showblocks', 'completionattemptsexhausted', 'completionpass')); + 'delay1', 'delay2', 'showuserpicture', 'showblocks', 'completionattemptsexhausted', 'completionpass', + 'allowofflineattempts')); // Define elements for access rule subplugin settings. $this->add_subplugin_structure('quizaccess', $quiz, true); @@ -84,7 +85,7 @@ class backup_quiz_activity_structure_step extends backup_questions_activity_stru $attempt = new backup_nested_element('attempt', array('id'), array( 'userid', 'attemptnum', 'uniqueid', 'layout', 'currentpage', 'preview', - 'state', 'timestart', 'timefinish', 'timemodified', 'timecheckstate', 'sumgrades')); + 'state', 'timestart', 'timefinish', 'timemodified', 'timemodifiedoffline', 'timecheckstate', 'sumgrades')); // This module is using questions, so produce the related question states and sessions // attaching them to the $attempt element based in 'uniqueid' matching. diff --git a/mod/quiz/backup/moodle2/restore_quiz_stepslib.php b/mod/quiz/backup/moodle2/restore_quiz_stepslib.php index c8367e3f6ed..5c1cc9abd4e 100644 --- a/mod/quiz/backup/moodle2/restore_quiz_stepslib.php +++ b/mod/quiz/backup/moodle2/restore_quiz_stepslib.php @@ -375,6 +375,7 @@ class restore_quiz_activity_structure_step extends restore_questions_activity_st $data->timestart = $this->apply_date_offset($data->timestart); $data->timefinish = $this->apply_date_offset($data->timefinish); $data->timemodified = $this->apply_date_offset($data->timemodified); + if (!empty($data->timecheckstate)) { $data->timecheckstate = $this->apply_date_offset($data->timecheckstate); } else { diff --git a/mod/quiz/classes/external.php b/mod/quiz/classes/external.php index 2b7115573b7..0465a430565 100644 --- a/mod/quiz/classes/external.php +++ b/mod/quiz/classes/external.php @@ -131,7 +131,8 @@ class mod_quiz_external extends external_api { 'reviewoverallfeedback', 'questionsperpage', 'navmethod', 'sumgrades', 'grade', 'browsersecurity', 'delay1', 'delay2', 'showuserpicture', 'showblocks', 'completionattemptsexhausted', 'completionpass', 'overduehandling', - 'graceperiod', 'preferredbehaviour', 'canredoquestions'); + 'graceperiod', 'preferredbehaviour', 'canredoquestions', + 'allowofflineattempts'); $viewablefields = array_merge($viewablefields, $additionalfields); } @@ -258,6 +259,8 @@ class mod_quiz_external extends external_api { exhausted the maximum number of attempts', VALUE_OPTIONAL), 'completionpass' => new external_value(PARAM_INT, 'Whether to require passing grade', VALUE_OPTIONAL), + 'allowofflineattempts' => new external_value(PARAM_INT, 'Whether to allow the quiz to be attempted + offline in the mobile app', VALUE_OPTIONAL), 'autosaveperiod' => new external_value(PARAM_INT, 'Auto-save delay', VALUE_OPTIONAL), 'hasfeedback' => new external_value(PARAM_INT, 'Whether the quiz has any non-blank feedback text', VALUE_OPTIONAL), @@ -445,6 +448,7 @@ class mod_quiz_external extends external_api { 'timefinish' => new external_value(PARAM_INT, 'Time when the attempt was submitted. 0 if the attempt has not been submitted yet.', VALUE_OPTIONAL), 'timemodified' => new external_value(PARAM_INT, 'Last modified time.', VALUE_OPTIONAL), + 'timemodifiedoffline' => new external_value(PARAM_INT, 'Last modified time via webservices.', VALUE_OPTIONAL), 'timecheckstate' => new external_value(PARAM_INT, 'Next time quiz cron should check attempt for state changes. NULL means never check.', VALUE_OPTIONAL), 'sumgrades' => new external_value(PARAM_FLOAT, 'Total marks for this attempt.', VALUE_OPTIONAL), @@ -748,7 +752,8 @@ class mod_quiz_external extends external_api { throw new moodle_quiz_exception($quizobj, 'attemptstillinprogress'); } } - $attempt = quiz_prepare_and_start_new_attempt($quizobj, $attemptnumber, $lastattempt); + $offlineattempt = WS_SERVER ? true : false; + $attempt = quiz_prepare_and_start_new_attempt($quizobj, $attemptnumber, $lastattempt, $offlineattempt); } $result = array(); @@ -1136,6 +1141,8 @@ class mod_quiz_external extends external_api { $_POST[$element['name']] = $element['value']; } $timenow = time(); + // Update the timemodifiedoffline field. + $attemptobj->set_offline_modified_time($timenow); $attemptobj->process_auto_save($timenow); $transaction->allow_commit(); @@ -1231,7 +1238,10 @@ class mod_quiz_external extends external_api { $timeup = $params['timeup']; $result = array(); + // Update the timemodifiedoffline field. + $attemptobj->set_offline_modified_time($timenow); $result['state'] = $attemptobj->process_attempt($timenow, $finishattempt, $timeup, 0); + $result['warnings'] = $warnings; return $result; } diff --git a/mod/quiz/db/install.xml b/mod/quiz/db/install.xml index decceadc6fb..d41ecbc55cf 100644 --- a/mod/quiz/db/install.xml +++ b/mod/quiz/db/install.xml @@ -46,6 +46,7 @@ + @@ -136,6 +137,7 @@ + diff --git a/mod/quiz/db/upgrade.php b/mod/quiz/db/upgrade.php index f42c0072f83..6c122723a43 100644 --- a/mod/quiz/db/upgrade.php +++ b/mod/quiz/db/upgrade.php @@ -193,5 +193,32 @@ function xmldb_quiz_upgrade($oldversion) { // Moodle v3.1.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2016092000) { + // Define new fields to be added to quiz. + $table = new xmldb_table('quiz'); + + $field = new xmldb_field('allowofflineattempts', XMLDB_TYPE_INTEGER, '1', null, null, null, 0, 'completionpass'); + // Conditionally launch add field allowofflineattempts. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + // Quiz savepoint reached. + upgrade_mod_savepoint(true, 2016092000, 'quiz'); + } + + if ($oldversion < 2016092001) { + // New field for quiz_attemps. + $table = new xmldb_table('quiz_attempts'); + + $field = new xmldb_field('timemodifiedoffline', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, 'timemodified'); + // Conditionally launch add field timemodifiedoffline. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Quiz savepoint reached. + upgrade_mod_savepoint(true, 2016092001, 'quiz'); + } + return true; } diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index fcd70f8a560..d6f403317b2 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -123,6 +123,7 @@ function quiz_create_attempt(quiz $quizobj, $attemptnumber, $lastattempt, $timen $attempt->timestart = $timenow; $attempt->timefinish = 0; $attempt->timemodified = $timenow; + $attempt->timemodifiedoffline = 0; $attempt->state = quiz_attempt::IN_PROGRESS; $attempt->currentpage = 0; $attempt->sumgrades = null; @@ -2259,10 +2260,11 @@ function quiz_validate_new_attempt(quiz $quizobj, quiz_access_manager $accessman * @param quiz $quizobj quiz object * @param int $attemptnumber the attempt number * @param object $lastattempt last attempt object + * @param bool $offlineattempt whether is an offline attempt or not * @return object the new attempt * @since Moodle 3.1 */ -function quiz_prepare_and_start_new_attempt(quiz $quizobj, $attemptnumber, $lastattempt) { +function quiz_prepare_and_start_new_attempt(quiz $quizobj, $attemptnumber, $lastattempt, $offlineattempt = false) { global $DB, $USER; // Delete any previous preview attempts belonging to this user. @@ -2283,6 +2285,10 @@ function quiz_prepare_and_start_new_attempt(quiz $quizobj, $attemptnumber, $last $transaction = $DB->start_delegated_transaction(); + // Init the timemodifiedoffline for offline attempts. + if ($offlineattempt) { + $attempt->timemodifiedoffline = $attempt->timemodified; + } $attempt = quiz_attempt_save_started($quizobj, $quba, $attempt); $transaction->allow_commit(); diff --git a/mod/quiz/tests/external_test.php b/mod/quiz/tests/external_test.php index 9aa209efa6d..9b3472ae930 100644 --- a/mod/quiz/tests/external_test.php +++ b/mod/quiz/tests/external_test.php @@ -199,7 +199,8 @@ class mod_quiz_external_testcase extends externallib_advanced_testcase { 'reviewoverallfeedback', 'questionsperpage', 'navmethod', 'sumgrades', 'grade', 'browsersecurity', 'delay1', 'delay2', 'showuserpicture', 'showblocks', 'completionattemptsexhausted', 'completionpass', 'autosaveperiod', 'hasquestions', - 'hasfeedback', 'overduehandling', 'graceperiod', 'preferredbehaviour', 'canredoquestions'); + 'hasfeedback', 'overduehandling', 'graceperiod', 'preferredbehaviour', 'canredoquestions', + 'allowofflineattempts'); $managerfields = array('shuffleanswers', 'timecreated', 'timemodified', 'password', 'subnet'); // Add expected coursemodule and other data. diff --git a/mod/quiz/version.php b/mod/quiz/version.php index a2ce9496e4c..89938c9f071 100644 --- a/mod/quiz/version.php +++ b/mod/quiz/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016052300; +$plugin->version = 2016092001; $plugin->requires = 2016051900; $plugin->component = 'mod_quiz'; $plugin->cron = 60;