Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.00% |
23 / 25 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| settings | |
92.00% |
23 / 25 |
|
75.00% |
3 / 4 |
9.04 | |
0.00% |
0 / 1 |
| get | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| save | |
86.67% |
13 / 15 |
|
0.00% |
0 / 1 |
5.06 | |||
| reminder_days | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| enabled_courses | |
100.00% |
1 / 1 |
|
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 stdClass; |
| 20 | |
| 21 | /** |
| 22 | * Recertification settings per course. |
| 23 | * |
| 24 | * @package local_recertify |
| 25 | * @copyright 2026 DevLMS <contact@devlms.com> |
| 26 | * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 27 | */ |
| 28 | final class settings { |
| 29 | /** @var string Table. */ |
| 30 | private const TABLE = 'local_recertify_course'; |
| 31 | |
| 32 | /** |
| 33 | * Settings for a course, with defaults when none are saved. |
| 34 | * |
| 35 | * @param int $courseid |
| 36 | * @return stdClass courseid, enabled, perioddays, reminderdays |
| 37 | */ |
| 38 | public static function get(int $courseid): stdClass { |
| 39 | global $DB; |
| 40 | return $DB->get_record(self::TABLE, ['courseid' => $courseid]) ?: (object) [ |
| 41 | 'courseid' => $courseid, |
| 42 | 'enabled' => 0, |
| 43 | 'perioddays' => 365, |
| 44 | 'reminderdays' => '30,7', |
| 45 | ]; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Save settings for a course. |
| 50 | * |
| 51 | * @param int $courseid |
| 52 | * @param bool $enabled |
| 53 | * @param int $perioddays How long a completion stays valid, in days. |
| 54 | * @param int[] $reminderdays Days before expiry to remind. Values outside 1 to the period are dropped. |
| 55 | */ |
| 56 | public static function save(int $courseid, bool $enabled, int $perioddays, array $reminderdays): void { |
| 57 | global $DB; |
| 58 | if ($perioddays < 1) { |
| 59 | throw new \invalid_parameter_exception('The period must be at least one day.'); |
| 60 | } |
| 61 | $days = array_unique(array_filter(array_map('intval', $reminderdays), fn($d) => $d >= 1 && $d < $perioddays)); |
| 62 | rsort($days); |
| 63 | $record = (object) [ |
| 64 | 'courseid' => $courseid, |
| 65 | 'enabled' => $enabled ? 1 : 0, |
| 66 | 'perioddays' => $perioddays, |
| 67 | 'reminderdays' => implode(',', $days), |
| 68 | 'timemodified' => time(), |
| 69 | ]; |
| 70 | if ($id = $DB->get_field(self::TABLE, 'id', ['courseid' => $courseid])) { |
| 71 | $record->id = $id; |
| 72 | $DB->update_record(self::TABLE, $record); |
| 73 | } else { |
| 74 | $DB->insert_record(self::TABLE, $record); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Reminder days, largest first. |
| 80 | * |
| 81 | * @param stdClass $cfg |
| 82 | * @return int[] |
| 83 | */ |
| 84 | public static function reminder_days(stdClass $cfg): array { |
| 85 | $days = array_filter(array_map('intval', explode(',', (string) $cfg->reminderdays)), fn($d) => $d > 0); |
| 86 | rsort($days); |
| 87 | return array_values(array_unique($days)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Courses with recertification switched on. |
| 92 | * |
| 93 | * @return int[] |
| 94 | */ |
| 95 | public static function enabled_courses(): array { |
| 96 | global $DB; |
| 97 | return array_map('intval', $DB->get_fieldset_select(self::TABLE, 'courseid', 'enabled = 1')); |
| 98 | } |
| 99 | } |