From be8f453b74fbd367a1ebf1518b1f27453a6d68f1 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Tue, 12 Mar 2019 10:42:49 +0800 Subject: [PATCH] MDL-65044 core: convert legacy cron to scheduled task --- lib/classes/task/grade_cron_task.php | 56 ++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/lib/classes/task/grade_cron_task.php b/lib/classes/task/grade_cron_task.php index 063a8ef1cdf..c5600e47a89 100644 --- a/lib/classes/task/grade_cron_task.php +++ b/lib/classes/task/grade_cron_task.php @@ -42,10 +42,60 @@ class grade_cron_task extends scheduled_task { * Throw exceptions on errors (the job will be retried). */ public function execute() { - global $CFG; + global $CFG, $DB; - require_once($CFG->libdir.'/gradelib.php'); - grade_cron(); + $now = time(); + $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'"); + } + } + } } } -- 2.43.0