Skip to content

Commit

Permalink
MDL-30635 enable standard cron for all question and quiz plugin types.
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
timhunt committed Jan 6, 2012
1 parent 0e84b16 commit c2f5e2a
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 105 deletions.
31 changes: 2 additions & 29 deletions lib/cronlib.php
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
15 changes: 15 additions & 0 deletions 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() {};
6 changes: 2 additions & 4 deletions mod/quiz/db/install.xml
Expand Up @@ -140,10 +140,8 @@
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="name"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="name of the report, same as the directory name" PREVIOUS="id" NEXT="displayorder"/>
<FIELD NAME="displayorder" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" COMMENT="display order for report tabs" PREVIOUS="name" NEXT="lastcron"/>
<FIELD NAME="lastcron" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" COMMENT="timestamp when cron was last run for this report." PREVIOUS="displayorder" NEXT="cron"/>
<FIELD NAME="cron" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" COMMENT="0 if there is no cron for this report (default) or the time between crons otherwise." PREVIOUS="lastcron" NEXT="capability"/>
<FIELD NAME="capability" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Capability required to see this report. May be blank which means use the default of mod/quiz:viewreport. This is used when deciding which tabs to render." PREVIOUS="cron"/>
<FIELD NAME="displayorder" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" COMMENT="display order for report tabs" PREVIOUS="name" NEXT="capability"/>
<FIELD NAME="capability" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Capability required to see this report. May be blank which means use the default of mod/quiz:viewreport. This is used when deciding which tabs to render." PREVIOUS="displayorder"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
30 changes: 30 additions & 0 deletions mod/quiz/db/upgrade.php
Expand Up @@ -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;
}

11 changes: 5 additions & 6 deletions mod/quiz/lib.php
Expand Up @@ -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');
}

/**
Expand Down
62 changes: 0 additions & 62 deletions mod/quiz/report/statistics/cron.php

This file was deleted.

1 change: 0 additions & 1 deletion mod/quiz/report/statistics/db/install.php
Expand Up @@ -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')) {
Expand Down
37 changes: 37 additions & 0 deletions mod/quiz/report/statistics/lib.php
Expand Up @@ -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
Expand All @@ -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;
}
5 changes: 3 additions & 2 deletions mod/quiz/report/statistics/version.php
Expand Up @@ -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';
22 changes: 22 additions & 0 deletions 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.
2 changes: 1 addition & 1 deletion mod/quiz/version.php
Expand Up @@ -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;
7 changes: 7 additions & 0 deletions question/behaviour/upgrade.txt
Expand Up @@ -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() {};
7 changes: 7 additions & 0 deletions question/format/upgrade.txt
Expand Up @@ -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() {};
15 changes: 15 additions & 0 deletions 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
Expand Down

0 comments on commit c2f5e2a

Please sign in to comment.