|
| 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 tool_dynamic_cohorts\local\tool_dynamic_cohorts\condition; |
| 18 | + |
| 19 | +/** |
| 20 | + * Condition based on user created date. |
| 21 | + * |
| 22 | + * @package tool_dynamic_cohorts |
| 23 | + * @copyright 2024 Catalyst IT |
| 24 | + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 25 | + */ |
| 26 | +class user_created extends user_last_login { |
| 27 | + |
| 28 | + /** |
| 29 | + * Condition name. |
| 30 | + * |
| 31 | + * @return string |
| 32 | + */ |
| 33 | + public function get_name(): string { |
| 34 | + return get_string('condition:user_created', 'tool_dynamic_cohorts'); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Gets a list of operators. |
| 39 | + * |
| 40 | + * @return array A list of operators. |
| 41 | + */ |
| 42 | + protected function get_operators(): array { |
| 43 | + return [ |
| 44 | + self::OPERATOR_IN_LAST => get_string('inlast', 'tool_dynamic_cohorts'), |
| 45 | + self::OPERATOR_BEFORE => get_string('before', 'tool_dynamic_cohorts'), |
| 46 | + self::OPERATOR_AFTER => get_string('after', 'tool_dynamic_cohorts'), |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Gets the operator form label. |
| 52 | + * |
| 53 | + * @return string The operator form label. |
| 54 | + */ |
| 55 | + protected function get_operator_form_label(): string { |
| 56 | + return get_string('usercreated', 'tool_dynamic_cohorts'); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Human-readable description of the configured condition. |
| 61 | + * |
| 62 | + * @return string |
| 63 | + */ |
| 64 | + public function get_config_description(): string { |
| 65 | + $description = ''; |
| 66 | + |
| 67 | + switch ($this->get_operator_value()) { |
| 68 | + case self::OPERATOR_BEFORE: |
| 69 | + case self::OPERATOR_AFTER: |
| 70 | + $description = get_string('usercreatedtime', 'tool_dynamic_cohorts', (object)[ |
| 71 | + 'operator' => strtolower($this->get_operators()[$this->get_operator_value()]), |
| 72 | + 'time' => userdate($this->get_time_value()), |
| 73 | + ]); |
| 74 | + break; |
| 75 | + case self::OPERATOR_IN_LAST: |
| 76 | + $description = get_string('usercreatedin', 'tool_dynamic_cohorts', $this->get_period()); |
| 77 | + break; |
| 78 | + } |
| 79 | + |
| 80 | + return $description; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Gets DB field name to apply filtering on. |
| 85 | + * |
| 86 | + * @return string |
| 87 | + */ |
| 88 | + protected function get_db_field(): string { |
| 89 | + return 'timecreated'; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Event to trigger condition. |
| 94 | + * |
| 95 | + * @return string[] |
| 96 | + */ |
| 97 | + public function get_events(): array { |
| 98 | + return [ |
| 99 | + '\core\event\user_created', |
| 100 | + ]; |
| 101 | + } |
| 102 | +} |
0 commit comments