MDL-65044 core: convert legacy cron to scheduled task

This commit is contained in:
Simey Lameze
2019-03-12 10:42:49 +08:00
parent 3271c39c74
commit be8f453b74

View File

@ -42,10 +42,60 @@ class grade_cron_task extends scheduled_task {
* Throw exceptions on errors (the job will be retried). * Throw exceptions on errors (the job will be retried).
*/ */
public function execute() { public function execute() {
global $CFG; global $CFG, $DB;
require_once($CFG->libdir.'/gradelib.php'); $now = time();
grade_cron(); $sql = "SELECT i.*
FROM {grade_items} i
WHERE i.locked = 0
AND i.locktime > 0
AND i.locktime < ?
AND EXISTS (SELECT 'x'
FROM {grade_items} c
WHERE c.itemtype='course'
AND c.needsupdate=0
AND c.courseid=i.courseid)";
// Go through all courses that have proper final grades and lock them if needed.
$rs = $DB->get_recordset_sql($sql, array($now));
foreach ($rs as $item) {
$gradeitem = new \grade_item($item, false);
$gradeitem->locked = $now;
$gradeitem->update('locktime');
}
$rs->close();
$gradeinst = new \grade_grade();
$fields = 'g.' . implode(',g.', $gradeinst->required_fields);
$sql = "SELECT $fields
FROM {grade_grades} g, {grade_items} i
WHERE g.locked = 0
AND g.locktime > 0
AND g.locktime < ?
AND g.itemid = i.id
AND EXISTS (SELECT 'x'
FROM {grade_items} c
WHERE c.itemtype = 'course'
AND c.needsupdate = 0
AND c.courseid = i.courseid)";
// Go through all courses that have proper final grades and lock them if needed.
$rs = $DB->get_recordset_sql($sql, array($now));
foreach ($rs as $grade) {
$gradegrade = new \grade_grade($grade, false);
$gradegrade->locked = $now;
$gradegrade->update('locktime');
}
$rs->close();
// Cleanup history tables.
if (!empty($CFG->gradehistorylifetime)) {
$histlifetime = $now - ($CFG->gradehistorylifetime * DAYSECS);
$tables = array('grade_outcomes_history', 'grade_categories_history', 'grade_items_history', 'grade_grades_history', 'scale_history');
foreach ($tables as $table) {
if ($DB->delete_records_select($table, "timemodified < ?", array($histlifetime))) {
mtrace(" Deleted old grade history records from '$table'");
}
}
}
} }
} }