From b09c036bb81d40186c592cf7892ac495489698b4 Mon Sep 17 00:00:00 2001 From: Michael Hughes Date: Fri, 22 Feb 2019 16:39:37 +0000 Subject: [PATCH] MDL-60683 quiz: Replace quiz legacy cron with tasks --- .../task/legacy_quiz_accessrules_cron.php | 52 +++++++++++++++ .../classes/task/legacy_quiz_reports_cron.php | 53 +++++++++++++++ .../classes/task/update_overdue_attempts.php | 65 +++++++++++++++++++ mod/quiz/db/tasks.php | 56 ++++++++++++++++ mod/quiz/lang/en/quiz.php | 3 + mod/quiz/lib.php | 26 -------- .../classes/task/quiz_statistics_cleanup.php | 55 ++++++++++++++++ mod/quiz/report/statistics/db/tasks.php | 39 +++++++++++ .../statistics/lang/en/quiz_statistics.php | 1 + mod/quiz/report/statistics/lib.php | 14 ---- mod/quiz/report/statistics/version.php | 1 - mod/quiz/upgrade.txt | 5 ++ mod/quiz/version.php | 1 - 13 files changed, 329 insertions(+), 42 deletions(-) create mode 100644 mod/quiz/classes/task/legacy_quiz_accessrules_cron.php create mode 100644 mod/quiz/classes/task/legacy_quiz_reports_cron.php create mode 100644 mod/quiz/classes/task/update_overdue_attempts.php create mode 100644 mod/quiz/db/tasks.php create mode 100644 mod/quiz/report/statistics/classes/task/quiz_statistics_cleanup.php create mode 100644 mod/quiz/report/statistics/db/tasks.php diff --git a/mod/quiz/classes/task/legacy_quiz_accessrules_cron.php b/mod/quiz/classes/task/legacy_quiz_accessrules_cron.php new file mode 100644 index 00000000000..899eb863e49 --- /dev/null +++ b/mod/quiz/classes/task/legacy_quiz_accessrules_cron.php @@ -0,0 +1,52 @@ +. + +/** + * Legacy Cron Quiz Access Rules Task + * + * @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; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/mod/quiz/locallib.php'); + +/** + * Legacy Cron Quiz Access Rules Task + * + * @package mod_quiz + * @copyright 2017 Michael Hughes + * @author Michael Hughes + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + */ +class legacy_quiz_accessrules_cron extends \core\task\scheduled_task { + + public function get_name() { + return get_string('legacyquizaccessrulescron', 'mod_quiz'); + } + + /** + * Execute all quizaccess subplugins legacy cron tasks. + */ + public function execute() { + cron_execute_plugin_type('quizaccess', 'quiz access rules'); + } +} diff --git a/mod/quiz/classes/task/legacy_quiz_reports_cron.php b/mod/quiz/classes/task/legacy_quiz_reports_cron.php new file mode 100644 index 00000000000..7153338aa13 --- /dev/null +++ b/mod/quiz/classes/task/legacy_quiz_reports_cron.php @@ -0,0 +1,53 @@ +. + +/** + * Legacy Cron Quiz Reports Task + * + * @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; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/mod/quiz/locallib.php'); + +/** + * Legacy Cron Quiz Reports Task + * + * @package mod_quiz + * @copyright 2017 Michael Hughes + * @author Michael Hughes + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + */ +class legacy_quiz_reports_cron extends \core\task\scheduled_task { + + public function get_name() { + return get_string('legacyquizreportscron', 'mod_quiz'); + } + + /** + * Execute all quizreport sub-plugins cron tasks. + */ + public function execute() { + cron_execute_plugin_type('quiz', 'quiz reports'); + } +} diff --git a/mod/quiz/classes/task/update_overdue_attempts.php b/mod/quiz/classes/task/update_overdue_attempts.php new file mode 100644 index 00000000000..54fa14e96bb --- /dev/null +++ b/mod/quiz/classes/task/update_overdue_attempts.php @@ -0,0 +1,65 @@ +. + +/** + * 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 + */ +namespace mod_quiz\task; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/mod/quiz/locallib.php'); + +/** + * 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 + * + */ +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; + + require_once($CFG->dirroot . '/mod/quiz/cronlib.php'); + $timenow = time(); + $overduehander = new \mod_quiz_overdue_attempt_updater(); + + $processto = $timenow - get_config('quiz', 'graceperiodmin'); + + mtrace(' Looking for quiz overdue quiz attempts...'); + + list($count, $quizcount) = $overduehander->update_overdue_attempts($timenow, $processto); + + mtrace(' Considered ' . $count . ' attempts in ' . $quizcount . ' quizzes.'); + } +} diff --git a/mod/quiz/db/tasks.php b/mod/quiz/db/tasks.php new file mode 100644 index 00000000000..757d9f40bdc --- /dev/null +++ b/mod/quiz/db/tasks.php @@ -0,0 +1,56 @@ +. + +/** + * Definition of Quiz scheduled tasks. + * + * @package mod_quiz + * @category task + * @copyright 2017 Michael Hughes + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$tasks = [ + [ + 'classname' => 'mod_quiz\task\update_overdue_attempts', + 'blocking' => 0, + 'minute' => '*', + 'hour' => '*', + 'day' => '*', + 'dayofweek' => '*', + 'month' => '*' + ], + [ + 'classname' => 'mod_quiz\task\legacy_quiz_reports_cron', + 'blocking' => 0, + 'minute' => '*', + 'hour' => '*', + 'day' => '*', + 'dayofweek' => '*', + 'month' => '*' + ], + [ + 'classname' => 'mod_quiz\task\legacy_quiz_accessrules_cron', + 'blocking' => 0, + 'minute' => '*', + 'hour' => '*', + 'day' => '*', + 'dayofweek' => '*', + 'month' => '*' + ] +]; diff --git a/mod/quiz/lang/en/quiz.php b/mod/quiz/lang/en/quiz.php index 2d9a3cd1f04..f4480b7b86d 100644 --- a/mod/quiz/lang/en/quiz.php +++ b/mod/quiz/lang/en/quiz.php @@ -453,6 +453,8 @@ $string['layoutasshown'] = 'Page layout as shown.'; $string['layoutasshownwithpages'] = 'Page layout as shown. (Automatic new page every {$a} questions.)'; $string['layoutshuffledandpaged'] = 'Questions randomly shuffled with {$a} questions per page.'; $string['layoutshuffledsinglepage'] = 'Questions randomly shuffled, all on one page.'; +$string['legacyquizaccessrulescron'] = 'Legacy Cron Quiz Access Rules'; +$string['legacyquizreportscron'] = 'Legacy Cron Quiz Reports'; $string['link'] = 'Link'; $string['listitems'] = 'Listing of items in quiz'; $string['literal'] = 'Literal'; @@ -958,6 +960,7 @@ $string['unfinished'] = 'open'; $string['ungraded'] = 'Ungraded'; $string['unit'] = 'Unit'; $string['unknowntype'] = 'Question type not supported at line {$a}. The question will be ignored'; +$string['updateoverdueattemptstask'] = 'Updating overdue quiz attempts'; $string['updatesettings'] = 'Update quiz settings'; $string['updatequizslotswithrandomxofy'] = 'Updating quiz slots with "random" question data ({$a->done}/{$a->total})'; $string['updatingatttemptgrades'] = 'Updating attempt grades.'; diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 3ffd67e8e8c..6dd7d885db4 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -578,32 +578,6 @@ function quiz_user_complete($course, $user, $mod, $quiz) { return true; } -/** - * Quiz periodic clean-up tasks. - */ -function quiz_cron() { - global $CFG; - - require_once($CFG->dirroot . '/mod/quiz/cronlib.php'); - mtrace(''); - - $timenow = time(); - $overduehander = new mod_quiz_overdue_attempt_updater(); - - $processto = $timenow - get_config('quiz', 'graceperiodmin'); - - mtrace(' Looking for quiz overdue quiz attempts...'); - - list($count, $quizcount) = $overduehander->update_overdue_attempts($timenow, $processto); - - mtrace(' Considered ' . $count . ' attempts in ' . $quizcount . ' quizzes.'); - - // Run cron for our sub-plugin types. - cron_execute_plugin_type('quiz', 'quiz reports'); - cron_execute_plugin_type('quizaccess', 'quiz access rules'); - - return true; -} /** * @param int|array $quizids A quiz ID, or an array of quiz IDs. diff --git a/mod/quiz/report/statistics/classes/task/quiz_statistics_cleanup.php b/mod/quiz/report/statistics/classes/task/quiz_statistics_cleanup.php new file mode 100644 index 00000000000..5d266f3c8eb --- /dev/null +++ b/mod/quiz/report/statistics/classes/task/quiz_statistics_cleanup.php @@ -0,0 +1,55 @@ +. + +/** + * Legacy Cron Quiz Reports Task + * + * @package quiz_statistics + * @copyright 2017 Michael Hughes, University of Strathclyde + * @author Michael Hughes + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + */ +namespace quiz_statistics\task; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Legacy Cron Quiz Reports Task + * + * @package quiz_statistics + * @copyright 2017 Michael Hughes + * @author Michael Hughes + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + */ +class quiz_statistics_cleanup extends \core\task\scheduled_task { + public function get_name() { + return get_string('quizstatisticscleanuptask', 'quiz_statistics'); + } + + /** + * Run the clean up task. + */ + public function execute() { + global $DB; + + $expiretime = time() - 4 * HOURSECS; + $DB->delete_records_select('quiz_statistics', 'timemodified < ?', array($expiretime)); + + return true; + } +} diff --git a/mod/quiz/report/statistics/db/tasks.php b/mod/quiz/report/statistics/db/tasks.php new file mode 100644 index 00000000000..c682ddc35e8 --- /dev/null +++ b/mod/quiz/report/statistics/db/tasks.php @@ -0,0 +1,39 @@ +. + +/** + * Legacy Cron Quiz Reports Task + * + * @package quiz_statistics + * @copyright 2017 Michael Hughes, University of Strathclyde + * @author Michael Hughes + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + */ + +defined('MOODLE_INTERNAL') || die(); + +$tasks = [ + [ + 'classname' => 'quiz_statistics\task\quiz_statistics_cleanup', + 'blocking' => 0, + 'minute' => 'R', + 'hour' => '*/5', + 'day' => '*', + 'dayofweek' => '*', + 'month' => '*' + ] +]; diff --git a/mod/quiz/report/statistics/lang/en/quiz_statistics.php b/mod/quiz/report/statistics/lang/en/quiz_statistics.php index 40b58498254..310364b602d 100644 --- a/mod/quiz/report/statistics/lang/en/quiz_statistics.php +++ b/mod/quiz/report/statistics/lang/en/quiz_statistics.php @@ -93,6 +93,7 @@ $string['questioninformation'] = 'Question information'; $string['questionname'] = 'Question name'; $string['questionnumber'] = 'Q#'; $string['questionstatistics'] = 'Question statistics'; +$string['quizstatisticscleanuptask'] = 'Clean up old quiz statistics cache records'; $string['questionstatsfilename'] = 'questionstats'; $string['questiontype'] = 'Question type'; $string['quizinformation'] = 'Quiz information'; diff --git a/mod/quiz/report/statistics/lib.php b/mod/quiz/report/statistics/lib.php index 416981be39f..8fe0c19da93 100644 --- a/mod/quiz/report/statistics/lib.php +++ b/mod/quiz/report/statistics/lib.php @@ -59,17 +59,3 @@ function quiz_statistics_question_preview_pluginfile($previewcontext, $questioni send_stored_file($file, 0, 0, $forcedownload, $options); } - -/** - * Quiz statistics report cron code. Deletes cached data more than a certain age. - */ -function quiz_statistics_cron() { - global $DB; - - mtrace("\n Cleaning up old quiz statistics cache records...", ''); - - $expiretime = time() - 5*HOURSECS; - $DB->delete_records_select('quiz_statistics', 'timemodified < ?', array($expiretime)); - - return true; -} diff --git a/mod/quiz/report/statistics/version.php b/mod/quiz/report/statistics/version.php index 36ad4eea313..2caeb2f4853 100644 --- a/mod/quiz/report/statistics/version.php +++ b/mod/quiz/report/statistics/version.php @@ -26,5 +26,4 @@ defined('MOODLE_INTERNAL') || die(); $plugin->version = 2018120300; $plugin->requires = 2018112800; -$plugin->cron = 18000; $plugin->component = 'quiz_statistics'; diff --git a/mod/quiz/upgrade.txt b/mod/quiz/upgrade.txt index f0265b024c5..929452baad0 100644 --- a/mod/quiz/upgrade.txt +++ b/mod/quiz/upgrade.txt @@ -1,5 +1,10 @@ This files describes API changes in the quiz code. +=== 3.7 === + +* Quiz_cron() has been removed. Sub-plugins should implemented scheduled tasks, however legacy cron in subplugins are + supported. + === 3.6 === * The following renamed classes have been completely removed: diff --git a/mod/quiz/version.php b/mod/quiz/version.php index 496ac21158b..0e7e1fc40a7 100644 --- a/mod/quiz/version.php +++ b/mod/quiz/version.php @@ -27,4 +27,3 @@ defined('MOODLE_INTERNAL') || die(); $plugin->version = 2018120300; $plugin->requires = 2018112800; $plugin->component = 'mod_quiz'; -$plugin->cron = 60;