Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
79.17% |
19 / 24 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| process | |
79.17% |
19 / 24 |
|
33.33% |
1 / 3 |
8.58 | |
0.00% |
0 / 1 |
| get_name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| run | |
81.82% |
18 / 22 |
|
0.00% |
0 / 1 |
6.22 | |||
| 1 | <?php |
| 2 | // This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>. |
| 16 | |
| 17 | namespace local_recertify\task; |
| 18 | |
| 19 | use local_recertify\event\certification_expired; |
| 20 | use local_recertify\local\{notifier, resetter, schedule, settings}; |
| 21 | |
| 22 | /** |
| 23 | * Daily: send reminders, then reset expired certifications, in every course that has it on. |
| 24 | * |
| 25 | * @package local_recertify |
| 26 | * @copyright 2026 DevLMS <contact@devlms.com> |
| 27 | * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 28 | */ |
| 29 | class process extends \core\task\scheduled_task { |
| 30 | /** |
| 31 | * Task name. |
| 32 | * |
| 33 | * @return string |
| 34 | */ |
| 35 | public function get_name() { |
| 36 | return get_string('task:process', 'local_recertify'); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Run for now. |
| 41 | */ |
| 42 | public function execute() { |
| 43 | $this->run(time()); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Run for a given time. |
| 48 | * |
| 49 | * @param int $now |
| 50 | */ |
| 51 | public function run(int $now): void { |
| 52 | global $DB; |
| 53 | foreach (settings::enabled_courses() as $courseid) { |
| 54 | $course = $DB->get_record('course', ['id' => $courseid]); |
| 55 | if (!$course) { |
| 56 | continue; |
| 57 | } |
| 58 | $cfg = settings::get($courseid); |
| 59 | foreach (schedule::due_reminders($cfg, $now) as $reminder) { |
| 60 | notifier::reminder($course, $reminder->userid, $reminder->expires); |
| 61 | schedule::mark_reminded($cfg, $reminder, $now); |
| 62 | } |
| 63 | $resets = 0; |
| 64 | foreach (schedule::due_resets($cfg, $now) as $userid) { |
| 65 | try { |
| 66 | $historyid = resetter::reset($courseid, $userid); |
| 67 | } catch (\Throwable $e) { |
| 68 | mtrace("Recertification: could not reset user $userid in course $courseid: " . $e->getMessage()); |
| 69 | continue; |
| 70 | } |
| 71 | certification_expired::create([ |
| 72 | 'objectid' => $historyid, |
| 73 | 'context' => \context_course::instance($courseid), |
| 74 | 'relateduserid' => $userid, |
| 75 | ])->trigger(); |
| 76 | notifier::expired($course, $userid); |
| 77 | $resets++; |
| 78 | } |
| 79 | mtrace("Recertification: course $courseid, $resets reset."); |
| 80 | } |
| 81 | } |
| 82 | } |