From 4ae4d8d1b6bf43975d8cd150876f4d444663f203 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 16 Dec 2022 16:41:43 +0000 Subject: [PATCH] MDL-76614 quiz: merge mod_quiz_overdue_attempt_updater into task The only place this code was used was in mod_quiz\task\update_overdue_attempts so neater to combine them into one class. --- .../task/update_overdue_attempts.php} | 51 +++++-- mod/quiz/cronlib.php | 157 +++++---------------- mod/quiz/deprecatedlib.php | 28 ++++ mod/quiz/tests/attempts_test.php | 14 +- mod/quiz/upgrade.txt | 4 + 5 files changed, 108 insertions(+), 146 deletions(-) copy mod/quiz/{cronlib.php => classes/task/update_overdue_attempts.php} (78%) rewrite mod/quiz/cronlib.php (80%) diff --git a/mod/quiz/cronlib.php b/mod/quiz/classes/task/update_overdue_attempts.php similarity index 78% copy from mod/quiz/cronlib.php copy to mod/quiz/classes/task/update_overdue_attempts.php index 51509e7a95c..98b389f0757 100644 --- a/mod/quiz/cronlib.php +++ b/mod/quiz/classes/task/update_overdue_attempts.php @@ -15,27 +15,53 @@ // along with Moodle. If not, see . /** - * Library code used by quiz cron. + * Update Overdue Attempts Task * - * @package mod_quiz - * @copyright 2012 the Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package mod_quiz + * @copyright 2017 Michael Hughes + * @author Michael Hughes + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace mod_quiz\task; +use moodle_exception; +use moodle_recordset; +use quiz_attempt; defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot . '/mod/quiz/locallib.php'); - /** - * This class holds all the code for automatically updating all attempts that have - * gone over their time limit. + * Update Overdue Attempts Task + * + * @package mod_quiz + * @copyright 2017 Michael Hughes + * @author Michael Hughes + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * - * @copyright 2012 the Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_overdue_attempt_updater { +class update_overdue_attempts extends \core\task\scheduled_task { + + public function get_name() { + return get_string('updateoverdueattemptstask', 'mod_quiz'); + } + + /** + * Close off any overdue attempts. + */ + public function execute() { + global $CFG; + + $timenow = time(); + $processto = $timenow - get_config('quiz', 'graceperiodmin'); + + mtrace(' Looking for quiz overdue quiz attempts...'); + + list($count, $quizcount) = $this->update_all_overdue_attempts($timenow, $processto); + + mtrace(' Considered ' . $count . ' attempts in ' . $quizcount . ' quizzes.'); + } /** * Do the processing required. @@ -43,7 +69,7 @@ class mod_quiz_overdue_attempt_updater { * @param int $processto only process attempt with timecheckstate longer ago than this. * @return array with two elements, the number of attempt considered, and how many different quizzes that was. */ - public function update_overdue_attempts($timenow, $processto) { + public function update_all_overdue_attempts($timenow, $processto) { global $DB; $attemptstoprocess = $this->get_list_of_overdue_attempts($processto); @@ -101,8 +127,7 @@ class mod_quiz_overdue_attempt_updater { public function get_list_of_overdue_attempts($processto) { global $DB; - - // SQL to compute timeclose and timelimit for each attempt: + // SQL to compute timeclose and timelimit for each attempt. $quizausersql = quiz_get_attempt_usertime_sql( "iquiza.state IN ('inprogress', 'overdue') AND iquiza.timecheckstate <= :iprocessto"); diff --git a/mod/quiz/cronlib.php b/mod/quiz/cronlib.php dissimilarity index 80% index 51509e7a95c..bf6091bab87 100644 --- a/mod/quiz/cronlib.php +++ b/mod/quiz/cronlib.php @@ -1,125 +1,32 @@ -. - -/** - * Library code used by quiz cron. - * - * @package mod_quiz - * @copyright 2012 the Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - - -defined('MOODLE_INTERNAL') || die(); - -require_once($CFG->dirroot . '/mod/quiz/locallib.php'); - - -/** - * This class holds all the code for automatically updating all attempts that have - * gone over their time limit. - * - * @copyright 2012 the Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class mod_quiz_overdue_attempt_updater { - - /** - * Do the processing required. - * @param int $timenow the time to consider as 'now' during the processing. - * @param int $processto only process attempt with timecheckstate longer ago than this. - * @return array with two elements, the number of attempt considered, and how many different quizzes that was. - */ - public function update_overdue_attempts($timenow, $processto) { - global $DB; - - $attemptstoprocess = $this->get_list_of_overdue_attempts($processto); - - $course = null; - $quiz = null; - $cm = null; - - $count = 0; - $quizcount = 0; - foreach ($attemptstoprocess as $attempt) { - try { - - // If we have moved on to a different quiz, fetch the new data. - if (!$quiz || $attempt->quiz != $quiz->id) { - $quiz = $DB->get_record('quiz', array('id' => $attempt->quiz), '*', MUST_EXIST); - $cm = get_coursemodule_from_instance('quiz', $attempt->quiz); - $quizcount += 1; - } - - // If we have moved on to a different course, fetch the new data. - if (!$course || $course->id != $quiz->course) { - $course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST); - } - - // Make a specialised version of the quiz settings, with the relevant overrides. - $quizforuser = clone($quiz); - $quizforuser->timeclose = $attempt->usertimeclose; - $quizforuser->timelimit = $attempt->usertimelimit; - - // Trigger any transitions that are required. - $attemptobj = new quiz_attempt($attempt, $quizforuser, $cm, $course); - $attemptobj->handle_if_time_expired($timenow, false); - $count += 1; - - } catch (moodle_exception $e) { - // If an error occurs while processing one attempt, don't let that kill cron. - mtrace("Error while processing attempt {$attempt->id} at {$attempt->quiz} quiz:"); - mtrace($e->getMessage()); - mtrace($e->getTraceAsString()); - // Close down any currently open transactions, otherwise one error - // will stop following DB changes from being committed. - $DB->force_transaction_rollback(); - } - } - - $attemptstoprocess->close(); - return array($count, $quizcount); - } - - /** - * @return moodle_recordset of quiz_attempts that need to be processed because time has - * passed. The array is sorted by courseid then quizid. - */ - public function get_list_of_overdue_attempts($processto) { - global $DB; - - - // SQL to compute timeclose and timelimit for each attempt: - $quizausersql = quiz_get_attempt_usertime_sql( - "iquiza.state IN ('inprogress', 'overdue') AND iquiza.timecheckstate <= :iprocessto"); - - // This query should have all the quiz_attempts columns. - return $DB->get_recordset_sql(" - SELECT quiza.*, - quizauser.usertimeclose, - quizauser.usertimelimit - - FROM {quiz_attempts} quiza - JOIN {quiz} quiz ON quiz.id = quiza.quiz - JOIN ( $quizausersql ) quizauser ON quizauser.id = quiza.id - - WHERE quiza.state IN ('inprogress', 'overdue') - AND quiza.timecheckstate <= :processto - ORDER BY quiz.course, quiza.quiz", - - array('processto' => $processto, 'iprocessto' => $processto)); - } -} +. + +/** + * Library code used by quiz cron. + * + * @package mod_quiz + * @copyright 2012 the Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @todo MDL-76612 delete this file as part of Moodle 4.6 development. + * @deprecated This file is no longer required in Moodle 4.2+. + */ + +defined('MOODLE_INTERNAL') || die(); + +debugging('This file is no longer required in Moodle 4.2+. Please do not include/require it.', DEBUG_DEVELOPER); + +require_once($CFG->dirroot . '/mod/quiz/locallib.php'); +require_once($CFG->dirroot . '/mod/quiz/deprecatedlib.php'); diff --git a/mod/quiz/deprecatedlib.php b/mod/quiz/deprecatedlib.php index f430d76d502..42a87b04056 100644 --- a/mod/quiz/deprecatedlib.php +++ b/mod/quiz/deprecatedlib.php @@ -23,6 +23,7 @@ */ use mod_quiz\access_manager; +use mod_quiz\task\update_overdue_attempts; /** * Internal function used in quiz_get_completion_state. Check passing grade (or no attempts left) requirement for completion. @@ -134,3 +135,30 @@ function quiz_get_completion_state($course, $cm, $userid, $type) { return true; } + +/** + * @copyright 2012 the Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @deprecated since Moodle 4.2. Code moved to mod_quiz\task\update_overdue_attempts. + * @todo MDL-71196 Final deprecation in Moodle 4.3 + */ +class mod_quiz_overdue_attempt_updater { + + /** + * @deprecated since Moodle 4.2. Code moved to mod_quiz\task\update_overdue_attempts. that was. + */ + public function update_overdue_attempts($timenow, $processto) { + debugging('mod_quiz_overdue_attempt_updater has been deprecated. The code wsa moved to ' . + 'mod_quiz\task\update_overdue_attempts.'); + return (new update_overdue_attempts())->update_all_overdue_attempts((int) $timenow, (int) $processto); + } + + /** + * @deprecated since Moodle 4.2. Code moved to mod_quiz\task\update_overdue_attempts. + */ + public function get_list_of_overdue_attempts($processto) { + debugging('mod_quiz_overdue_attempt_updater has been deprecated. The code wsa moved to ' . + 'mod_quiz\task\update_overdue_attempts.'); + return (new update_overdue_attempts())->get_list_of_overdue_attempts((int) $processto); + } +} diff --git a/mod/quiz/tests/attempts_test.php b/mod/quiz/tests/attempts_test.php index 52abb662895..da714842221 100644 --- a/mod/quiz/tests/attempts_test.php +++ b/mod/quiz/tests/attempts_test.php @@ -16,7 +16,9 @@ namespace mod_quiz; -use mod_quiz_overdue_attempt_updater; +use core_question_generator; +use mod_quiz\task\update_overdue_attempts; +use mod_quiz_generator; use question_engine; use quiz; @@ -40,12 +42,8 @@ class attempts_test extends \advanced_testcase { * update_overdue_attempts(). */ public function test_bulk_update_functions() { - global $DB,$CFG; - - require_once($CFG->dirroot.'/mod/quiz/cronlib.php'); - + global $DB; $this->resetAfterTest(); - $this->setAdminUser(); // Setup course, user and groups @@ -390,7 +388,7 @@ class attempts_test extends \advanced_testcase { // Test get_list_of_overdue_attempts(). // - $overduehander = new mod_quiz_overdue_attempt_updater(); + $overduehander = new update_overdue_attempts(); $attempts = $overduehander->get_list_of_overdue_attempts(100000); // way in the future $count = 0; @@ -417,7 +415,7 @@ class attempts_test extends \advanced_testcase { // Test update_overdue_attempts(). // - [$count, $quizcount] = $overduehander->update_overdue_attempts(1000, 940); + [$count, $quizcount] = $overduehander->update_all_overdue_attempts(1000, 940); $attempts = $DB->get_records('quiz_attempts', null, 'quiz, userid, attempt', 'id, quiz, userid, attempt, state, timestart, timefinish, timecheckstate'); diff --git a/mod/quiz/upgrade.txt b/mod/quiz/upgrade.txt index 2eaa0eebfc6..0973fdf4b4d 100644 --- a/mod/quiz/upgrade.txt +++ b/mod/quiz/upgrade.txt @@ -38,6 +38,9 @@ This files describes API changes in the quiz code. - quiz_access_manager => mod_quiz\access_manager - mod_quiz_preflight_check_form => mod_quiz\form\preflight_check_form +* The following classes have been deprecated: + - mod_quiz_overdue_attempt_updater - merged into mod_quiz\task\update_overdue_attempts + * As part of the clean-up, the following files are no longer required, and if you try to include them, you will get a debugging notices telling you not to: - mod/quiz/report/attemptsreport.php @@ -47,6 +50,7 @@ This files describes API changes in the quiz code. - mod/quiz/report/default.php - mod/quiz/accessmanager.php - mod/quiz/accessmanager_form.php + - mod/quiz/cronlib.php === 4.1 === -- 2.11.4.GIT