From c2f5e2ab816e4bc067085b0f9c59b01739d945a9 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 7 Dec 2011 16:29:49 +0000 Subject: [PATCH] MDL-30635 enable standard cron for all question and quiz plugin types. * Support for old non-standard cron for quiz reports dropped. (Standard cron support was added in 2.2 * Cron support added for qbehaviour, qformat and quizacces plugins. * qtypes were already supported in the standard way. --- lib/cronlib.php | 31 +----------- mod/quiz/accessrule/upgrade.txt | 15 ++++++ mod/quiz/db/install.xml | 6 +-- mod/quiz/db/upgrade.php | 30 +++++++++++ mod/quiz/lib.php | 11 ++-- mod/quiz/report/statistics/cron.php | 62 ----------------------- mod/quiz/report/statistics/db/install.php | 1 - mod/quiz/report/statistics/lib.php | 37 ++++++++++++++ mod/quiz/report/statistics/version.php | 5 +- mod/quiz/report/upgrade.txt | 22 ++++++++ mod/quiz/version.php | 2 +- question/behaviour/upgrade.txt | 7 +++ question/format/upgrade.txt | 7 +++ question/type/upgrade.txt | 15 ++++++ 14 files changed, 146 insertions(+), 105 deletions(-) create mode 100644 mod/quiz/accessrule/upgrade.txt delete mode 100644 mod/quiz/report/statistics/cron.php create mode 100644 mod/quiz/report/upgrade.txt diff --git a/lib/cronlib.php b/lib/cronlib.php index 83978d645aba4..11c3def5f868c 100644 --- a/lib/cronlib.php +++ b/lib/cronlib.php @@ -300,35 +300,6 @@ function cron_run() { mtrace('Finished blocks'); - //TODO: get rid of this bloody hardcoded quiz module stuff, this must be done from quiz_cron()! - mtrace("Starting quiz reports"); - if ($reports = $DB->get_records_select('quiz_reports', "cron > 0 AND ((? - lastcron) > cron)", array($timenow))) { - foreach ($reports as $report) { - $cronfile = "$CFG->dirroot/mod/quiz/report/$report->name/cron.php"; - if (file_exists($cronfile)) { - include_once($cronfile); - $cron_function = 'quiz_report_'.$report->name."_cron"; - if (function_exists($cron_function)) { - mtrace("Processing quiz report cron function $cron_function ...", ''); - $pre_dbqueries = null; - $pre_dbqueries = $DB->perf_get_queries(); - $pre_time = microtime(1); - if ($cron_function()) { - $DB->set_field('quiz_reports', "lastcron", $timenow, array("id"=>$report->id)); - } - if (isset($pre_dbqueries)) { - mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries"); - mtrace("... used " . (microtime(1) - $pre_time) . " seconds"); - } - @set_time_limit(0); - mtrace("done."); - } - } - } - } - mtrace("Finished quiz reports"); - - mtrace('Starting admin reports'); cron_execute_plugin_type('report'); mtrace('Finished admin reports'); @@ -428,6 +399,8 @@ function cron_run() { // TODO: Repository lib.php files are messed up (include many other files, etc), so it is // currently not possible to implement repository plugin cron using this infrastructure // cron_execute_plugin_type('repository', 'repository plugins'); + cron_execute_plugin_type('qbehaviour', 'question behaviours'); + cron_execute_plugin_type('qformat', 'question import/export formats'); cron_execute_plugin_type('qtype', 'question types'); cron_execute_plugin_type('plagiarism', 'plagiarism plugins'); cron_execute_plugin_type('theme', 'themes'); diff --git a/mod/quiz/accessrule/upgrade.txt b/mod/quiz/accessrule/upgrade.txt new file mode 100644 index 0000000000000..12f117ba82f40 --- /dev/null +++ b/mod/quiz/accessrule/upgrade.txt @@ -0,0 +1,15 @@ +This files describes API changes for quiz access rule plugins. + +Overview of this plugin type at http://docs.moodle.org/dev/Quiz_access_rules + + +=== 2.2 === + +* This plugin type was new in Moodle 2.2! + + +=== 2.3 === + +* This plugin type now supports cron in the standard way. If required, Create a + lib.php file containing +function quizaccess_mypluginname_cron() {}; diff --git a/mod/quiz/db/install.xml b/mod/quiz/db/install.xml index 8ed07e5125565..d57135c2bd6f2 100644 --- a/mod/quiz/db/install.xml +++ b/mod/quiz/db/install.xml @@ -140,10 +140,8 @@ - - - - + + diff --git a/mod/quiz/db/upgrade.php b/mod/quiz/db/upgrade.php index 367448ea0a5f3..92d7d984deee5 100644 --- a/mod/quiz/db/upgrade.php +++ b/mod/quiz/db/upgrade.php @@ -40,6 +40,36 @@ function xmldb_quiz_upgrade($oldversion) { // Moodle v2.2.0 release upgrade line // Put any upgrade step following this + if ($oldversion < 2011120700) { + + // Define field lastcron to be dropped from quiz_reports + $table = new xmldb_table('quiz_reports'); + $field = new xmldb_field('lastcron'); + + // Conditionally launch drop field lastcron + if ($dbman->field_exists($table, $field)) { + $dbman->drop_field($table, $field); + } + + // quiz savepoint reached + upgrade_mod_savepoint(true, 2011120700, 'quiz'); + } + + if ($oldversion < 2011120701) { + + // Define field cron to be dropped from quiz_reports + $table = new xmldb_table('quiz_reports'); + $field = new xmldb_field('cron'); + + // Conditionally launch drop field cron + if ($dbman->field_exists($table, $field)) { + $dbman->drop_field($table, $field); + } + + // quiz savepoint reached + upgrade_mod_savepoint(true, 2011120701, 'quiz'); + } + return true; } diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 9e2bf23f09a1c..dc24934a46ade 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -435,14 +435,13 @@ function quiz_user_complete($course, $user, $mod, $quiz) { } /** - * Function to be run periodically according to the moodle cron - * This function searches for things that need to be done, such - * as sending out mail, toggling flags etc ... - * - * @return bool true + * Quiz periodic clean-up tasks. */ function quiz_cron() { - return true; + + // Run cron for our sub-plugin types. + cron_execute_plugin_type('quiz', 'quiz reports'); + cron_execute_plugin_type('quizaccess', 'quiz access rules'); } /** diff --git a/mod/quiz/report/statistics/cron.php b/mod/quiz/report/statistics/cron.php deleted file mode 100644 index 35ebabc612a25..0000000000000 --- a/mod/quiz/report/statistics/cron.php +++ /dev/null @@ -1,62 +0,0 @@ -. - -/** - * Quiz statistics report cron code. - * - * @package quiz - * @subpackage statistics - * @copyright 2008 Jamie Pratt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - - -defined('MOODLE_INTERNAL') || die(); - - -/** - * Quiz statistics report cron code. Deletes cached data more than a certain age. - */ -function quiz_report_statistics_cron() { - global $DB; - - $expiretime = time() - 5*HOURSECS; - $todelete = $DB->get_records_select_menu('quiz_statistics', 'timemodified < ?', - array($expiretime), '', 'id, 1'); - - if (!$todelete) { - return true; - } - - list($todeletesql, $todeleteparams) = $DB->get_in_or_equal(array_keys($todelete)); - - if (!$DB->delete_records_select('quiz_question_statistics', - 'quizstatisticsid ' . $todeletesql, $todeleteparams)) { - mtrace('Error deleting out of date quiz_question_statistics records.'); - } - - if (!$DB->delete_records_select('quiz_question_response_stats', - 'quizstatisticsid ' . $todeletesql, $todeleteparams)) { - mtrace('Error deleting out of date quiz_question_response_stats records.'); - } - - if (!$DB->delete_records_select('quiz_statistics', - 'id ' . $todeletesql, $todeleteparams)) { - mtrace('Error deleting out of date quiz_statistics records.'); - } - - return true; -} diff --git a/mod/quiz/report/statistics/db/install.php b/mod/quiz/report/statistics/db/install.php index d127a593ba701..24ab53f04e22d 100644 --- a/mod/quiz/report/statistics/db/install.php +++ b/mod/quiz/report/statistics/db/install.php @@ -37,7 +37,6 @@ function xmldb_quiz_statistics_install() { $record = new stdClass(); $record->name = 'statistics'; $record->displayorder = 8000; - $record->cron = 18000; $record->capability = 'quiz/statistics:view'; if ($dbman->table_exists('quiz_reports')) { diff --git a/mod/quiz/report/statistics/lib.php b/mod/quiz/report/statistics/lib.php index 06d7fd97b3e03..efa3ed791eb78 100644 --- a/mod/quiz/report/statistics/lib.php +++ b/mod/quiz/report/statistics/lib.php @@ -24,6 +24,9 @@ */ +defined('MOODLE_INTERNAL') || die(); + + /** * Serve questiontext files in the question text when they are displayed in this report. * @param context $context the context @@ -44,3 +47,37 @@ function quiz_statistics_questiontext_preview_pluginfile($context, $questionid, question_send_questiontext_file($questionid, $args, $forcedownload); } + +/** +* Quiz statistics report cron code. Deletes cached data more than a certain age. +*/ +function quiz_report_statistics_cron() { + global $DB; + + $expiretime = time() - 5*HOURSECS; + $todelete = $DB->get_records_select_menu('quiz_statistics', 'timemodified < ?', + array($expiretime), '', 'id, 1'); + + if (!$todelete) { + return true; + } + + list($todeletesql, $todeleteparams) = $DB->get_in_or_equal(array_keys($todelete)); + + if (!$DB->delete_records_select('quiz_question_statistics', + 'quizstatisticsid ' . $todeletesql, $todeleteparams)) { + mtrace('Error deleting out of date quiz_question_statistics records.'); + } + + if (!$DB->delete_records_select('quiz_question_response_stats', + 'quizstatisticsid ' . $todeletesql, $todeleteparams)) { + mtrace('Error deleting out of date quiz_question_response_stats records.'); + } + + if (!$DB->delete_records_select('quiz_statistics', + 'id ' . $todeletesql, $todeleteparams)) { + mtrace('Error deleting out of date quiz_statistics records.'); + } + + return true; +} diff --git a/mod/quiz/report/statistics/version.php b/mod/quiz/report/statistics/version.php index 235a722e95ef1..cfd14ceb981ff 100644 --- a/mod/quiz/report/statistics/version.php +++ b/mod/quiz/report/statistics/version.php @@ -25,6 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2011062600; -$plugin->requires = 2011060313; +$plugin->version = 2011062600; +$plugin->requires = 2011060313; +$plugin->cron = 18000; $plugin->component = 'quiz_statistics'; diff --git a/mod/quiz/report/upgrade.txt b/mod/quiz/report/upgrade.txt new file mode 100644 index 0000000000000..c12e7ff470b3c --- /dev/null +++ b/mod/quiz/report/upgrade.txt @@ -0,0 +1,22 @@ +This files describes API changes for quiz report plugins. + +Overview of this plugin type at http://docs.moodle.org/dev/Quiz_reports + + +=== earlier versions === + +* ... API changes were not documented properly. Sorry. (There weren't many!) + + +=== 2.2 === + +* Plugins should be converted to implement cron in the standard way. In lib.php, +define a +function quiz_myreportname_cron() {}; +This replaces the old way of having a separate cron.php file. Also, the cron +frequency should be defined in version.php, not in the quiz_reports table. + + +=== 2.3 === + +* Support for the old way of doing cron in a separate cron.php file has been removed. diff --git a/mod/quiz/version.php b/mod/quiz/version.php index 12deea0ed54ba..39f8bfd3606e8 100644 --- a/mod/quiz/version.php +++ b/mod/quiz/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$module->version = 2011112900; // The current module version (Date: YYYYMMDDXX) +$module->version = 2011120701; // 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 = 0; diff --git a/question/behaviour/upgrade.txt b/question/behaviour/upgrade.txt index 63b941ee3de37..5f51d0aff00b4 100644 --- a/question/behaviour/upgrade.txt +++ b/question/behaviour/upgrade.txt @@ -14,3 +14,10 @@ $plugin->dependencies = array( is_compatible_question method. You should change your behaviour to override the new method, not the old one. This change has been implemented in a backwards-compatible way, so behaviours will not break. + + +=== 2.3 === + +* This plugin type now supports cron in the standard way. If required, Create a + lib.php file containing +function qbehaviour_mypluginname_cron() {}; diff --git a/question/format/upgrade.txt b/question/format/upgrade.txt index 1516b9cd521a4..595dbff7f50c7 100644 --- a/question/format/upgrade.txt +++ b/question/format/upgrade.txt @@ -15,3 +15,10 @@ other plugin types. $string['pluginname'] = 'Aiken format'; $string['pluginname_help'] = 'This is a simple format ...'; $string['pluginname_link'] = 'qformat/aiken'; + + +=== 2.3 === + +* This plugin type now supports cron in the standard way. If required, Create a + lib.php file containing +function qformat_mypluginname_cron() {}; diff --git a/question/type/upgrade.txt b/question/type/upgrade.txt index acaacd322a1be..949d8d27a25e5 100644 --- a/question/type/upgrade.txt +++ b/question/type/upgrade.txt @@ -1,5 +1,20 @@ This files describes API changes for question type plugins. +=== 2.0 === + +* Lots of changes due to all the API changes in Moodle 2.0. + +* This plugin type now supports cron in the standard way. If required, Create a + lib.php file containing +function qtype_mypluginname_cron() {}; + + +=== 2.1 === + +* Lots of API changes due to the new question engine. See +http://docs.moodle.org/dev/Developing_a_Question_Type#Converting_a_Moodle_2.0_question_type + + === 2.2 === * The XML import/export base class has had some minor API changes. The