Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
37 / 37 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| notifier | |
100.00% |
37 / 37 |
|
100.00% |
4 / 4 |
4 | |
100.00% |
1 / 1 |
| reminder | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| expired | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| course_name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| send | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
| 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\local; |
| 18 | |
| 19 | use context_course; |
| 20 | use core_user; |
| 21 | use moodle_url; |
| 22 | |
| 23 | /** |
| 24 | * Messages to learners. |
| 25 | * |
| 26 | * @package local_recertify |
| 27 | * @copyright 2026 DevLMS <contact@devlms.com> |
| 28 | * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | */ |
| 30 | final class notifier { |
| 31 | /** |
| 32 | * Tell a learner their certification expires soon. |
| 33 | * |
| 34 | * @param \stdClass $course |
| 35 | * @param int $userid |
| 36 | * @param int $expires |
| 37 | */ |
| 38 | public static function reminder(\stdClass $course, int $userid, int $expires): void { |
| 39 | $user = core_user::get_user($userid); |
| 40 | $a = (object) [ |
| 41 | 'course' => self::course_name($course), |
| 42 | 'date' => userdate($expires, get_string('strftimedate', 'langconfig'), $user->timezone ?? 99), |
| 43 | ]; |
| 44 | self::send( |
| 45 | $course, |
| 46 | $user, |
| 47 | 'reminder', |
| 48 | get_string('message:reminder:subject', 'local_recertify', $a), |
| 49 | get_string('message:reminder', 'local_recertify', $a) |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Tell a learner their certification expired and the course was reset. |
| 55 | * |
| 56 | * @param \stdClass $course |
| 57 | * @param int $userid |
| 58 | */ |
| 59 | public static function expired(\stdClass $course, int $userid): void { |
| 60 | $name = self::course_name($course); |
| 61 | $user = core_user::get_user($userid); |
| 62 | self::send( |
| 63 | $course, |
| 64 | $user, |
| 65 | 'expired', |
| 66 | get_string('message:expired:subject', 'local_recertify', $name), |
| 67 | get_string('message:expired', 'local_recertify', $name) |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Course name for messages. |
| 73 | * |
| 74 | * @param \stdClass $course |
| 75 | * @return string |
| 76 | */ |
| 77 | private static function course_name(\stdClass $course): string { |
| 78 | return format_string($course->fullname, true, ['context' => context_course::instance($course->id)]); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Send a notification. |
| 83 | * |
| 84 | * @param \stdClass $course |
| 85 | * @param \stdClass $user |
| 86 | * @param string $provider |
| 87 | * @param string $subject |
| 88 | * @param string $text |
| 89 | */ |
| 90 | private static function send(\stdClass $course, \stdClass $user, string $provider, string $subject, string $text): void { |
| 91 | $message = new \core\message\message(); |
| 92 | $message->component = 'local_recertify'; |
| 93 | $message->name = $provider; |
| 94 | $message->userfrom = core_user::get_noreply_user(); |
| 95 | $message->userto = $user; |
| 96 | $message->subject = $subject; |
| 97 | $message->fullmessage = $text; |
| 98 | $message->fullmessageformat = FORMAT_PLAIN; |
| 99 | $message->fullmessagehtml = text_to_html(s($text), false, false, true); |
| 100 | $message->smallmessage = $subject; |
| 101 | $message->notification = 1; |
| 102 | $message->contexturl = (new moodle_url('/course/view.php', ['id' => $course->id]))->out(false); |
| 103 | $message->contexturlname = self::course_name($course); |
| 104 | $message->courseid = $course->id; |
| 105 | message_send($message); |
| 106 | } |
| 107 | } |