From 17f1782c1217ab5b83de6bd55a4e7a7adb0abe5a Mon Sep 17 00:00:00 2001 From: jamiesensei Date: Thu, 24 Jul 2008 17:36:33 +0000 Subject: [PATCH] MDL-15818 "allow quiz reports to register cron routines" - Quiz reports can now include files cron.php which include functions quiz_report_{reportname}_cron that will be executed from the moodle cron. They must also modify their quiz_report record to set cron to the number of seconds between cron execution. --- admin/cron.php | 30 +++++++++++++++++++++++++++++- mod/quiz/db/install.xml | 6 ++++-- mod/quiz/db/upgrade.php | 22 ++++++++++++++++++++++ mod/quiz/version.php | 2 +- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/admin/cron.php b/admin/cron.php index 9cb16df0fa..28fa5ff0aa 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -157,6 +157,34 @@ } mtrace('Finished blocks'); + mtrace("Starting quiz reports"); + if ($reports = $DB->get_records_select('quiz_report', "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()) { + if (!$DB->set_field('quiz_report', "lastcron", $timenow, array("id"=>$report->id))) { + mtrace("Error: could not update timestamp for $report->name"); + } + } + if (isset($pre_dbqueries)) { + mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries"); + mtrace("... used " . (microtime(1) - $pre_time) . " seconds"); + } + mtrace("done."); + } + } + } + } + mtrace("Finished quiz reports"); + mtrace('Starting admin reports'); // Admin reports do not have a database table that lists them. Instead a // report includes cron.php with function report_reportname_cron() if it wishes @@ -167,7 +195,7 @@ $cronfile = $CFG->dirroot.'/'.$CFG->admin.'/report/'.$report.'/cron.php'; if (file_exists($cronfile)) { require_once($cronfile); - $cronfunction = 'report_'.$report.'_cron'; + $cronfunction = 'quiz_report_'.$report.'_cron'; mtrace('Processing cron function for '.$report.'...', ''); $pre_dbqueries = null; $pre_dbqueries = $DB->perf_get_queries(); diff --git a/mod/quiz/db/install.xml b/mod/quiz/db/install.xml index 33f9d1b8f8..e81b4825de 100755 --- a/mod/quiz/db/install.xml +++ b/mod/quiz/db/install.xml @@ -1,5 +1,5 @@ - @@ -297,7 +297,9 @@ - + + + diff --git a/mod/quiz/db/upgrade.php b/mod/quiz/db/upgrade.php index 05ac39f7eb..a0ca4967a5 100644 --- a/mod/quiz/db/upgrade.php +++ b/mod/quiz/db/upgrade.php @@ -86,6 +86,28 @@ function xmldb_quiz_upgrade($oldversion=0) { upgrade_mod_savepoint($result, 2008072401, 'quiz'); } + if ($result && $oldversion < 2008072402) { + + /// Define field lastcron to be added to quiz_report + $table = new xmldb_table('quiz_report'); + $field = new xmldb_field('lastcron', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'displayorder'); + + /// Conditionally launch add field lastcron + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + /// Define field cron to be added to quiz_report + $field = new xmldb_field('cron', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'lastcron'); + + /// Conditionally launch add field cron + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + /// quiz savepoint reached + upgrade_mod_savepoint($result, 2008072402, 'quiz'); + } return $result; diff --git a/mod/quiz/version.php b/mod/quiz/version.php index 942cfedbe8..676568c21e 100644 --- a/mod/quiz/version.php +++ b/mod/quiz/version.php @@ -5,7 +5,7 @@ // This fragment is called by moodle_needs_upgrading() and /admin/index.php //////////////////////////////////////////////////////////////////////////////// -$module->version = 2008072401; // The (date) version of this module +$module->version = 2008072402; // The (date) version of this module $module->requires = 2008072401; // Requires this Moodle version $module->cron = 0; // How often should cron check this module (seconds)? -- 2.39.5