|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +namespace core_course\task; |
| 18 | + |
| 19 | +use core\task\adhoc_task; |
| 20 | + |
| 21 | +/** |
| 22 | + * Asynchronously reset a course |
| 23 | + * |
| 24 | + * @package core_course |
| 25 | + * @copyright 2025 onwards Catalyst IT EU {@link https://catalyst-eu.net} |
| 26 | + * @author Mark Johnson <mark.johnson@catalyst-eu.net> |
| 27 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ |
| 28 | +class reset_course extends adhoc_task { |
| 29 | + use \core\task\logging_trait; |
| 30 | + use \core\task\stored_progress_task_trait; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var int The course weight above which an ad-hoc reset will be used. |
| 34 | + */ |
| 35 | + const ADHOC_THRESHOLD = 1000; |
| 36 | + |
| 37 | + /** |
| 38 | + * Create and return an instance of this task for a given course ID. |
| 39 | + * |
| 40 | + * @param \stdClass $data |
| 41 | + * @return self |
| 42 | + */ |
| 43 | + public static function create(\stdClass $data): self { |
| 44 | + $task = new reset_course(); |
| 45 | + $task->set_custom_data($data); |
| 46 | + $task->set_component('core_course'); |
| 47 | + return $task; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get the customdata for an instance of this task for the given course ID. |
| 52 | + * |
| 53 | + * We save all the options selected on the reset form in the task's custom data, which makes it difficult to reconstruct |
| 54 | + * for the purposes of displaying a task indicator. As we shouldn't be resetting the same course multiple times at once, |
| 55 | + * this lets us search for an instance of the task containing the course ID in the customdata, then returns the whole field |
| 56 | + * to be passed to {@see self::create()}. |
| 57 | + * |
| 58 | + * @param int $courseid |
| 59 | + * @return \stdClass|null |
| 60 | + * @throws \dml_exception |
| 61 | + */ |
| 62 | + public static function get_data_by_courseid(int $courseid): ?\stdClass { |
| 63 | + global $DB; |
| 64 | + $where = 'classname = ? AND ' . $DB->sql_like('customdata', '?'); |
| 65 | + $params = [ |
| 66 | + '\\' . self::class, |
| 67 | + '%"id":' . $courseid . ',%', |
| 68 | + ]; |
| 69 | + $customdata = $DB->get_field_select('task_adhoc', 'customdata', $where, $params); |
| 70 | + return $customdata ? json_decode($customdata) : null; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Run reset_course_userdata for the provided course id. |
| 75 | + * |
| 76 | + * @return void |
| 77 | + * @throws \dml_exception |
| 78 | + */ |
| 79 | + public function execute(): void { |
| 80 | + $data = $this->get_custom_data(); |
| 81 | + $this->start_stored_progress(); |
| 82 | + $this->log_start("Resetting course ID {$data->id}"); |
| 83 | + // Ensure the course exists. |
| 84 | + try { |
| 85 | + $course = get_course($data->id); |
| 86 | + } catch (\dml_missing_record_exception $e) { |
| 87 | + $this->log("Course with id {$data->id} not found. It may have been deleted. Skipping reset."); |
| 88 | + return; |
| 89 | + } |
| 90 | + $this->log("Found course {$course->shortname}. Starting reset."); |
| 91 | + $results = reset_course_userdata($data, $this->get_progress(), maxseconds: null); |
| 92 | + |
| 93 | + // Work out the max length of each column for nicer formatting. |
| 94 | + $done = get_string('statusdone'); |
| 95 | + $lengths = array_reduce( |
| 96 | + $results, |
| 97 | + function ($carry, $result) { |
| 98 | + foreach ($carry as $key => $length) { |
| 99 | + $carry[$key] = max(strlen($result[$key]), $length); |
| 100 | + } |
| 101 | + return $carry; |
| 102 | + }, |
| 103 | + ['component' => 0, 'item' => 0, 'error' => 0] |
| 104 | + ); |
| 105 | + $lengths['error'] = max(strlen($done), $lengths['error']); |
| 106 | + |
| 107 | + $this->log( |
| 108 | + str_pad(get_string('resetcomponent'), $lengths['component']) . ' | ' . |
| 109 | + str_pad(get_string('resettask'), $lengths['item']) . ' | ' . |
| 110 | + str_pad(get_string('resetstatus'), $lengths['error']) |
| 111 | + ); |
| 112 | + foreach ($results as $result) { |
| 113 | + $this->log( |
| 114 | + str_pad($result['component'], $lengths['component']) . ' | ' . |
| 115 | + str_pad($result['item'], $lengths['item']) . ' | ' . |
| 116 | + str_pad($result['error'] ?: $done, $lengths['error']) |
| 117 | + ); |
| 118 | + } |
| 119 | + $this->log_finish('Reset complete.'); |
| 120 | + } |
| 121 | +} |
0 commit comments