Permalink
Browse files

MDL-30635 let quiz reports do cron in the standard way.

The old legacy way is still supported in 2.2, but this support will be
dropped in 2.3.
  • Loading branch information...
1 parent 7ca0ece commit 25200f80f74cb03cf8947b5d3adf9a27002b7a0b @timhunt timhunt committed Dec 7, 2011
View
@@ -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');
View
@@ -438,11 +438,40 @@ 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
*/
function quiz_cron() {
- return true;
+ global $DB;
+
+ // First handle standard plugins.
+ cron_execute_plugin_type('quiz', 'quiz reports');
+
+ // The deal with any plugins that do it the legacy way.
+ mtrace("Starting legacy 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 legacy quiz reports");
}
/**
@@ -1,62 +0,0 @@
-<?php
-// This file is part of Moodle - http://moodle.org/
-//
-// Moodle is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Moodle is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
-
-/**
- * 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;
-}
@@ -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;
+}
@@ -25,6 +25,7 @@
defined('MOODLE_INTERNAL') || die();
-$plugin->version = 2011062600;
-$plugin->requires = 2011060313;
+$plugin->version = 2011122300;
+$plugin->requires = 2011060313;
+$plugin->cron = 18000;
$plugin->component = 'quiz_statistics';
@@ -0,0 +1,17 @@
+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 quizaccess_mypluginname_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.

0 comments on commit 25200f8

Please sign in to comment.