Skip to content

Commit

Permalink
MDL-60683 quiz: Replace quiz legacy cron with tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
mhughes2k committed Feb 22, 2019
1 parent 1249995 commit b09c036
Show file tree
Hide file tree
Showing 13 changed files with 329 additions and 42 deletions.
52 changes: 52 additions & 0 deletions mod/quiz/classes/task/legacy_quiz_accessrules_cron.php
@@ -0,0 +1,52 @@
<?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/>.

/**
* 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');
}
}
53 changes: 53 additions & 0 deletions mod/quiz/classes/task/legacy_quiz_reports_cron.php
@@ -0,0 +1,53 @@
<?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/>.

/**
* 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');
}
}
65 changes: 65 additions & 0 deletions mod/quiz/classes/task/update_overdue_attempts.php
@@ -0,0 +1,65 @@
<?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/>.

/**
* 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.');
}
}
56 changes: 56 additions & 0 deletions mod/quiz/db/tasks.php
@@ -0,0 +1,56 @@
<?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/>.

/**
* Definition of Quiz scheduled tasks.
*
* @package mod_quiz
* @category task
* @copyright 2017 Michael Hughes <michaelhughes@strath.ac.uk>
* @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' => '*'
]
];
3 changes: 3 additions & 0 deletions mod/quiz/lang/en/quiz.php
Expand Up @@ -453,6 +453,8 @@
$string['layoutasshownwithpages'] = 'Page layout as shown. <small>(Automatic new page every {$a} questions.)</small>';
$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';
Expand Down Expand Up @@ -958,6 +960,7 @@
$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.';
Expand Down
26 changes: 0 additions & 26 deletions mod/quiz/lib.php
Expand Up @@ -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.
Expand Down
@@ -0,0 +1,55 @@
<?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/>.

/**
* Legacy Cron Quiz Reports Task
*
* @package quiz_statistics
* @copyright 2017 Michael Hughes, University of Strathclyde
* @author Michael Hughes <michaelhughes@strath.ac.uk>
* @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;
}
}
39 changes: 39 additions & 0 deletions mod/quiz/report/statistics/db/tasks.php
@@ -0,0 +1,39 @@
<?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/>.

/**
* Legacy Cron Quiz Reports Task
*
* @package quiz_statistics
* @copyright 2017 Michael Hughes, University of Strathclyde
* @author Michael Hughes <michaelhughes@strath.ac.uk>
* @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' => '*'
]
];
1 change: 1 addition & 0 deletions mod/quiz/report/statistics/lang/en/quiz_statistics.php
Expand Up @@ -93,6 +93,7 @@
$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';
Expand Down

0 comments on commit b09c036

Please sign in to comment.