32
32
*/
33
33
class user_role extends condition_base {
34
34
35
+ /**
36
+ * Operator for user who have role.
37
+ */
38
+ public const OPERATOR_HAVE_ROLE = 0 ;
39
+
40
+ /**
41
+ * Operator for user who do not have role.
42
+ */
43
+ public const OPERATOR_DO_NOT_HAVE_ROLE = 1 ;
44
+
35
45
/**
36
46
* Condition name.
37
47
*
@@ -58,12 +68,32 @@ protected function get_all_roles(): array {
58
68
return $ roles ;
59
69
}
60
70
71
+ /**
72
+ * Gets a list of operators.
73
+ *
74
+ * @return array A list of operators.
75
+ */
76
+ protected function get_operators (): array {
77
+ return [
78
+ self ::OPERATOR_HAVE_ROLE => get_string ('haverole ' , 'tool_dynamic_cohorts ' ),
79
+ self ::OPERATOR_DO_NOT_HAVE_ROLE => get_string ('donothaverole ' , 'tool_dynamic_cohorts ' ),
80
+ ];
81
+ }
82
+
61
83
/**
62
84
* Add config form elements.
63
85
*
64
86
* @param \MoodleQuickForm $mform
65
87
*/
66
88
public function config_form_add (\MoodleQuickForm $ mform ): void {
89
+ // Operator.
90
+ $ mform ->addElement (
91
+ 'select ' ,
92
+ 'operator ' ,
93
+ get_string ('operator ' , 'tool_dynamic_cohorts ' ),
94
+ $ this ->get_operators ()
95
+ );
96
+
67
97
// Role field.
68
98
$ mform ->addElement ('select ' , 'roleid ' , get_string ('role ' ), $ this ->get_all_roles ());
69
99
@@ -114,6 +144,15 @@ public function config_form_validate(array $data): array {
114
144
return $ errors ;
115
145
}
116
146
147
+ /**
148
+ * Gets operator.
149
+ *
150
+ * @return int
151
+ */
152
+ protected function get_operator_value (): int {
153
+ return $ this ->get_config_data ()['operator ' ] ?? self ::OPERATOR_HAVE_ROLE ;
154
+ }
155
+
117
156
/**
118
157
* Gets configured role ID.
119
158
*
@@ -171,16 +210,21 @@ public function get_config_description(): string {
171
210
172
211
switch ($ this ->get_contextlevel_value ()) {
173
212
case CONTEXT_SYSTEM :
174
- return get_string ('condition:user_role_description_system ' , 'tool_dynamic_cohorts ' , $ rolename );
213
+ return get_string ('condition:user_role_description_system ' , 'tool_dynamic_cohorts ' , (object )[
214
+ 'role ' => $ rolename ,
215
+ 'operator ' => $ this ->get_operators ()[$ this ->get_operator_value ()],
216
+ ]);
175
217
176
218
case CONTEXT_COURSECAT :
177
219
$ children = !empty ($ this ->get_includechildren_value ()) ? get_string ('includechildren ' , 'tool_dynamic_cohorts ' ) : '' ;
178
220
$ categoryname = core_course_category::get ($ this ->get_categoryid_value ())->get_formatted_name ();
179
221
180
222
return get_string ('condition:user_role_description_category ' , 'tool_dynamic_cohorts ' , (object ) [
181
223
'role ' => $ rolename ,
224
+ 'operator ' => $ this ->get_operators ()[$ this ->get_operator_value ()],
182
225
'categoryname ' => $ categoryname ,
183
226
'categoryid ' => $ this ->get_categoryid_value (),
227
+
184
228
]) . ' ' . $ children ;
185
229
186
230
case CONTEXT_COURSE :
@@ -189,6 +233,7 @@ public function get_config_description(): string {
189
233
190
234
return get_string ('condition:user_role_description_course ' , 'tool_dynamic_cohorts ' , (object ) [
191
235
'role ' => $ rolename ,
236
+ 'operator ' => $ this ->get_operators ()[$ this ->get_operator_value ()],
192
237
'coursename ' => $ coursename ,
193
238
'courseid ' => $ this ->get_courseid_value (),
194
239
]);
@@ -242,16 +287,15 @@ public function get_sql(): condition_sql {
242
287
$ params [$ roleidparam ] = $ roleid ;
243
288
244
289
$ ratable = condition_sql::generate_table_alias ();
245
- $ join = "JOIN {role_assignments} $ ratable ON ( $ ratable.userid = u.id) " ;
246
- $ where = "$ ratable.roleid = : $ roleidparam " ;
290
+ $ innerwhere = "$ ratable.roleid = : $ roleidparam " ;
247
291
248
292
switch ($ this ->get_contextlevel_value ()) {
249
293
case CONTEXT_SYSTEM :
250
294
$ context = context_system::instance ();
251
295
$ contextid = $ context ->id ;
252
296
$ contextidparam = condition_sql::generate_param_alias ();
253
297
$ params [$ contextidparam ] = $ contextid ;
254
- $ where .= " AND $ ratable.contextid = : $ contextidparam " ;
298
+ $ innerwhere .= " AND $ ratable.contextid = : $ contextidparam " ;
255
299
256
300
break ;
257
301
case CONTEXT_COURSECAT :
@@ -273,9 +317,9 @@ public function get_sql(): condition_sql {
273
317
condition_sql::generate_param_alias ()
274
318
);
275
319
$ params = array_merge ($ params , $ childcparams );
276
- $ where .= " AND ( $ ratable.contextid $ parentcontexsql OR $ ratable.contextid $ childcontextsql ) " ;
320
+ $ innerwhere .= " AND ( $ ratable.contextid $ parentcontexsql OR $ ratable.contextid $ childcontextsql ) " ;
277
321
} else {
278
- $ where .= " AND $ ratable.contextid $ parentcontexsql " ;
322
+ $ innerwhere .= " AND $ ratable.contextid $ parentcontexsql " ;
279
323
}
280
324
281
325
break ;
@@ -290,11 +334,21 @@ public function get_sql(): condition_sql {
290
334
);
291
335
292
336
$ params = array_merge ($ params , $ cparams );
293
- $ where .= " AND $ ratable.contextid $ contextsql " ;
337
+ $ innerwhere .= " AND $ ratable.contextid $ contextsql " ;
294
338
295
339
break ;
296
340
}
297
341
342
+ $ outertable = condition_sql::generate_table_alias ();
343
+
344
+ $ join = "LEFT JOIN (SELECT {$ ratable }.userid
345
+ FROM {role_assignments} $ ratable
346
+ WHERE $ innerwhere) {$ outertable }
347
+ ON u.id = {$ outertable }.userid " ;
348
+
349
+ $ haverole = $ this ->get_operator_value () == self ::OPERATOR_HAVE_ROLE ;
350
+ $ where = $ haverole ? " $ outertable.userid is NOT NULL " : " $ outertable.userid is NULL " ;
351
+
298
352
$ sql = new condition_sql ($ join , $ where , $ params );
299
353
}
300
354
0 commit comments