|
| 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 | +use tool_dynamic_cohorts\condition_base; |
| 20 | +use tool_dynamic_cohorts\condition_sql; |
| 21 | +use context_course; |
| 22 | + |
| 23 | +/** |
| 24 | + * Condition based on user's enrolment. |
| 25 | + * |
| 26 | + * @package tool_dynamic_cohorts |
| 27 | + * @copyright 2024 Catalyst IT |
| 28 | + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + */ |
| 30 | +class user_enrolment extends condition_base { |
| 31 | + |
| 32 | + /** |
| 33 | + * Operator for not enrolled users. |
| 34 | + */ |
| 35 | + public const OPERATOR_NOT_ENROLLED = 0; |
| 36 | + |
| 37 | + /** |
| 38 | + * Operator for enrolled users. |
| 39 | + */ |
| 40 | + public const OPERATOR_ENROLLED = 1; |
| 41 | + |
| 42 | + /** |
| 43 | + * Condition name. |
| 44 | + * |
| 45 | + * @return string |
| 46 | + */ |
| 47 | + public function get_name(): string { |
| 48 | + return get_string('condition:user_enrolment', 'tool_dynamic_cohorts'); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Gets a list of all roles. |
| 53 | + * |
| 54 | + * @return array |
| 55 | + */ |
| 56 | + protected function get_roles(): array { |
| 57 | + $roles = [ |
| 58 | + 0 => get_string('any'), |
| 59 | + ]; |
| 60 | + foreach (role_get_names() as $role) { |
| 61 | + if ($role->archetype === 'guest') { |
| 62 | + continue; |
| 63 | + } |
| 64 | + $roles[$role->id] = $role->localname; |
| 65 | + } |
| 66 | + |
| 67 | + return $roles; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns a list of enrolment methods. |
| 72 | + * |
| 73 | + * @return array |
| 74 | + */ |
| 75 | + protected function get_enrolment_methods(): array { |
| 76 | + $enrolmentmethods = ['' => get_string('any')]; |
| 77 | + foreach (enrol_get_plugins(false) as $method) { |
| 78 | + $name = $method->get_name(); |
| 79 | + $enrolmentmethods[$name] = get_string('pluginname', 'enrol_' . $name); |
| 80 | + } |
| 81 | + |
| 82 | + return $enrolmentmethods; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Gets a list of operators. |
| 87 | + * |
| 88 | + * @return array A list of operators. |
| 89 | + */ |
| 90 | + protected function get_operators(): array { |
| 91 | + return [ |
| 92 | + self::OPERATOR_ENROLLED => get_string('enrolled', 'tool_dynamic_cohorts'), |
| 93 | + self::OPERATOR_NOT_ENROLLED => get_string('notenrolled', 'tool_dynamic_cohorts'), |
| 94 | + ]; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Add config form elements. |
| 99 | + * |
| 100 | + * @param \MoodleQuickForm $mform |
| 101 | + */ |
| 102 | + public function config_form_add(\MoodleQuickForm $mform): void { |
| 103 | + // Operator. |
| 104 | + $mform->addElement( |
| 105 | + 'select', |
| 106 | + 'operator', |
| 107 | + get_string('operator', 'tool_dynamic_cohorts'), |
| 108 | + $this->get_operators() |
| 109 | + ); |
| 110 | + |
| 111 | + // Course. |
| 112 | + $mform->addElement('course', 'courseid', get_string('course')); |
| 113 | + $mform->setType('courseid', PARAM_INT); |
| 114 | + $mform->hideIf('courseid', 'contextlevel', 'in', [CONTEXT_SYSTEM, CONTEXT_COURSECAT]); |
| 115 | + |
| 116 | + // Enrolment method. |
| 117 | + $mform->addElement( |
| 118 | + 'select', |
| 119 | + 'enrolmethod', |
| 120 | + get_string('enrolmethod', 'tool_dynamic_cohorts'), |
| 121 | + $this->get_enrolment_methods() |
| 122 | + ); |
| 123 | + $mform->setType('enrolmethod', PARAM_COMPONENT); |
| 124 | + |
| 125 | + // Role. |
| 126 | + $mform->addElement('select', 'roleid', get_string('role'), $this->get_roles()); |
| 127 | + $mform->setType('roleid', PARAM_INT); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Validate config form elements. |
| 132 | + * |
| 133 | + * @param array $data Data to validate. |
| 134 | + * @return array |
| 135 | + */ |
| 136 | + public function config_form_validate(array $data): array { |
| 137 | + $errors = []; |
| 138 | + |
| 139 | + if (empty($data['courseid'])) { |
| 140 | + $errors['courseid'] = get_string('required'); |
| 141 | + } |
| 142 | + |
| 143 | + return $errors; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Gets operator. |
| 148 | + * |
| 149 | + * @return int |
| 150 | + */ |
| 151 | + protected function get_operator_value(): int { |
| 152 | + return $this->get_config_data()['operator'] ?? self::OPERATOR_ENROLLED; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Gets configured role ID. |
| 157 | + * |
| 158 | + * @return int |
| 159 | + */ |
| 160 | + protected function get_roleid_value(): int { |
| 161 | + return $this->get_config_data()['roleid'] ?? 0; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Gets configured enrolment method. |
| 166 | + * |
| 167 | + * @return string |
| 168 | + */ |
| 169 | + protected function get_enrolment_method_value(): string { |
| 170 | + return $this->get_config_data()['enrolmethod'] ?? ''; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Gets configured course ID. |
| 175 | + * |
| 176 | + * @return int |
| 177 | + */ |
| 178 | + protected function get_courseid_value(): int { |
| 179 | + return $this->get_config_data()['courseid'] ?? 0; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Human-readable description of the configured condition. |
| 184 | + * |
| 185 | + * @return string |
| 186 | + */ |
| 187 | + public function get_config_description(): string { |
| 188 | + global $DB; |
| 189 | + |
| 190 | + $coursename = $DB->get_field('course', 'fullname', ['id' => $this->get_courseid_value()]); |
| 191 | + $coursename = format_string($coursename, true, ['context' => \context_system::instance(), 'escape' => false]); |
| 192 | + |
| 193 | + return get_string('condition:user_enrolment_description', 'tool_dynamic_cohorts', (object) [ |
| 194 | + 'operator' => strtolower($this->get_operators()[$this->get_operator_value()]), |
| 195 | + 'role' => $this->get_roles()[$this->get_roleid_value()], |
| 196 | + 'coursename' => $coursename, |
| 197 | + 'courseid' => $this->get_courseid_value(), |
| 198 | + 'enrolmethod' => $this->get_enrolment_methods()[$this->get_enrolment_method_value()], |
| 199 | + ]); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Human-readable description of the broken condition. |
| 204 | + * |
| 205 | + * @return string |
| 206 | + */ |
| 207 | + public function get_broken_description(): string { |
| 208 | + global $DB; |
| 209 | + |
| 210 | + // Missing role. |
| 211 | + if (!$DB->get_record('role', ['id' => $this->get_roleid_value()])) { |
| 212 | + return get_string('missingrole', 'tool_dynamic_cohorts'); |
| 213 | + } |
| 214 | + |
| 215 | + // Missing course. |
| 216 | + if (!$DB->get_record('course', ['id' => $this->get_courseid_value()])) { |
| 217 | + return get_string('missingcourse', 'tool_dynamic_cohorts'); |
| 218 | + } |
| 219 | + |
| 220 | + // Missing enrolment method. |
| 221 | + if (!empty($this->get_enrolment_method_value()) |
| 222 | + && !array_key_exists($this->get_enrolment_method_value(), $this->get_enrolment_methods())) { |
| 223 | + return get_string('missingenrolmentmethod', 'tool_dynamic_cohorts', $this->get_enrolment_method_value()); |
| 224 | + } |
| 225 | + |
| 226 | + return parent::get_broken_description(); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Gets SQL data for building SQL. |
| 231 | + * |
| 232 | + * @return condition_sql |
| 233 | + */ |
| 234 | + public function get_sql(): condition_sql { |
| 235 | + $sql = new condition_sql('', '1=0', []); |
| 236 | + |
| 237 | + if (!$this->is_broken()) { |
| 238 | + $join = ''; |
| 239 | + $params = []; |
| 240 | + |
| 241 | + // Enrolment tables. |
| 242 | + $uetable = condition_sql::generate_table_alias(); |
| 243 | + $enroltable = condition_sql::generate_table_alias(); |
| 244 | + |
| 245 | + // Course parameter. |
| 246 | + $courseidparam = condition_sql::generate_param_alias(); |
| 247 | + $params[$courseidparam] = $this->get_courseid_value(); |
| 248 | + |
| 249 | + // In case we are filtering by enrolment method. |
| 250 | + $enrolmethodwhere = ''; |
| 251 | + $enrolmethod = $this->get_enrolment_method_value(); |
| 252 | + if (!empty($enrolmethod)) { |
| 253 | + $enrolmethodparam = condition_sql::generate_param_alias(); |
| 254 | + $params[$enrolmethodparam] = $enrolmethod; |
| 255 | + $enrolmethodwhere = "AND $enroltable.enrol = :{$enrolmethodparam}"; |
| 256 | + } |
| 257 | + |
| 258 | + // In case we are filtering by role. |
| 259 | + $rolesql = ''; |
| 260 | + $rolewhere = ''; |
| 261 | + $roleid = $this->get_roleid_value(); |
| 262 | + if (!empty($roleid)) { |
| 263 | + $outertable = condition_sql::generate_table_alias(); |
| 264 | + $ratable = condition_sql::generate_table_alias(); |
| 265 | + $roleidparam = condition_sql::generate_param_alias(); |
| 266 | + $params[$roleidparam] = $roleid; |
| 267 | + |
| 268 | + $context = context_course::instance($this->get_courseid_value()); |
| 269 | + $contexidtparam = condition_sql::generate_param_alias(); |
| 270 | + $params[$contexidtparam] = $context->id; |
| 271 | + |
| 272 | + $rolesql = "LEFT JOIN (SELECT $ratable.userid |
| 273 | + FROM {role_assignments} $ratable |
| 274 | + WHERE $ratable.roleid = :{$roleidparam} |
| 275 | + AND $ratable.contextid = :{$contexidtparam} |
| 276 | + ) {$outertable} ON {$uetable}.userid = {$outertable}.userid "; |
| 277 | + |
| 278 | + $rolewhere = "AND {$outertable}.userid is NOT NULL"; |
| 279 | + } |
| 280 | + |
| 281 | + $operator = $this->get_operator_value() == self::OPERATOR_ENROLLED ? 'EXISTS' : 'NOT EXISTS'; |
| 282 | + |
| 283 | + $where = "{$operator} (SELECT 1 FROM {user_enrolments} {$uetable} |
| 284 | + JOIN {enrol} {$enroltable} ON ({$enroltable}.id = {$uetable}.enrolid AND {$enroltable}.status = 0) |
| 285 | + $rolesql |
| 286 | + WHERE {$uetable}.userid = u.id |
| 287 | + AND $enroltable.courseid = :{$courseidparam} |
| 288 | + AND {$uetable}.status = 0 |
| 289 | + $enrolmethodwhere |
| 290 | + $rolewhere)"; |
| 291 | + |
| 292 | + $sql = new condition_sql($join, $where, $params); |
| 293 | + } |
| 294 | + |
| 295 | + return $sql; |
| 296 | + } |
| 297 | + |
| 298 | + /** |
| 299 | + * Is condition broken. |
| 300 | + * |
| 301 | + * @return bool |
| 302 | + */ |
| 303 | + public function is_broken(): bool { |
| 304 | + global $DB; |
| 305 | + |
| 306 | + if ($this->get_config_data()) { |
| 307 | + // Check role exists. |
| 308 | + if (!empty($this->get_roleid_value()) && !$DB->get_record('role', ['id' => $this->get_roleid_value()])) { |
| 309 | + return true; |
| 310 | + } |
| 311 | + |
| 312 | + // Check course exists. |
| 313 | + if (!$DB->get_record('course', ['id' => $this->get_courseid_value()])) { |
| 314 | + return true; |
| 315 | + } |
| 316 | + |
| 317 | + // Enrolment method is not valid. |
| 318 | + if (!empty($this->get_enrolment_method_value()) |
| 319 | + && !array_key_exists($this->get_enrolment_method_value(), $this->get_enrolment_methods())) { |
| 320 | + return true; |
| 321 | + } |
| 322 | + } |
| 323 | + |
| 324 | + return false; |
| 325 | + } |
| 326 | + |
| 327 | + /** |
| 328 | + * Gets a list of event classes the condition will be triggered on. |
| 329 | + * |
| 330 | + * @return string[] |
| 331 | + */ |
| 332 | + public function get_events(): array { |
| 333 | + return [ |
| 334 | + 'core\event\role_assigned', |
| 335 | + 'core\event\role_unassigned', |
| 336 | + 'core\event\user_enrolment_created', |
| 337 | + 'core\event\user_enrolment_updated', |
| 338 | + 'core\event\user_enrolment_deleted', |
| 339 | + ]; |
| 340 | + } |
| 341 | +} |
0 commit comments