@@ -123,20 +123,35 @@ public static function meets_cohorts_requirements($notificationid, $userid) {
123
123
124
124
public static function meets_roles_requirements ($ notificationid , $ userid , $ blockid ) {
125
125
global $ DB ;
126
- if (!$ roles = $ DB ->get_records ('block_advnotifications_roles ' , ['notificationid ' => $ notificationid ])) {
126
+ if (!$ roles = $ DB ->get_records ('block_advnotifications_roles ' , ['notificationid ' => $ notificationid ], '' , ' roleid ' )) {
127
127
return true ; // There is no role restriction.
128
128
}
129
- if ($ blockid ) {
129
+ $ roles = array_keys ($ roles );
130
+
131
+ if ($ blockid > 0 ) {
130
132
$ context = \context_block::instance ($ blockid );
133
+ if ($ coursecontext = $ context ->get_course_context (false )) {
134
+ $ context = $ coursecontext ;
135
+ } else {
136
+ $ context = $ context ->get_parent_context (); // It may be category or user context.
137
+ if ($ context instanceof \context_user) {
138
+ $ context = \context_system::instance ();
139
+ }
140
+ }
131
141
} else {
132
142
$ context = \context_system::instance ();
133
143
}
134
- foreach ($ roles as $ r ) {
135
- if (!user_has_role_assignment ($ userid , $ r ->roleid , $ context ->id )) {
136
- return false ;
137
- }
138
- }
139
- return true ;
144
+ list ($ rolessql , $ params ) = $ DB ->get_in_or_equal ($ roles , SQL_PARAMS_NAMED , 'roleid ' );
145
+ $ sql =
146
+ 'SELECT 1
147
+ FROM {role_assignments} ra
148
+ JOIN {context} ctx
149
+ ON ctx.id = ra.contextid
150
+ WHERE ' . $ DB ->sql_like ('ctx.path ' , ':ctxpath ' , false ) . '
151
+ AND roleid ' . $ rolessql ;
152
+ $ params ['ctxpath ' ] = $ context ->path . '% ' ;
153
+
154
+ return $ DB ->record_exists_sql ($ sql , $ params );
140
155
}
141
156
142
157
public static function get_users_for_notification ($ notification ) {
@@ -146,11 +161,15 @@ public static function get_users_for_notification($notification) {
146
161
$ wheres = [];
147
162
$ params = [];
148
163
149
- if (!empty ($ notification ->blockid ) && $ notification ->blockid > 0 ) {
150
- $ bcontext = \context_block::instance ($ notification ->blockid );
151
- $ coursecontext = $ bcontext ->get_course_context (false );
164
+ if ($ notification ->blockid > 0 ) {
165
+ $ context = \context_block::instance ($ notification ->blockid );
166
+ if ($ coursecontext = $ context ->get_course_context (false )) {
167
+ $ context = $ coursecontext ;
168
+ } else {
169
+ $ context = $ context ->get_parent_context (); // It may be category context.
170
+ }
152
171
} else {
153
- $ coursecontext = \context_system::instance ();
172
+ $ context = \context_system::instance ();
154
173
}
155
174
156
175
if ($ DB ->record_exists ('block_advnotifications_coh ' , ['notificationid ' => $ notification ->id ])) {
@@ -165,16 +184,17 @@ public static function get_users_for_notification($notification) {
165
184
166
185
if ($ roles = $ DB ->get_records_menu ('block_advnotifications_roles ' , ['notificationid ' => $ notification ->id ], '' , 'roleid ' )) {
167
186
168
- // TODO: role assignments in sub-contexts.
169
187
$ roles = array_keys ($ roles );
170
- list ($ rolesql , $ roleparams ) = $ DB ->get_in_or_equal ($ roles , SQL_PARAMS_NAMED , 'roleid ' );
188
+ list ($ rolessql , $ rolesparams ) = $ DB ->get_in_or_equal ($ roles , SQL_PARAMS_NAMED , 'roleid ' );
171
189
$ joins [] =
172
190
' JOIN {role_assignments} ra
173
- ON ra.userid = u.id ' ;
174
- $ wheres [] = ' ra.contextid = :contextid ' ;
175
- $ wheres [] = ' ra.roleid ' . $ rolesql ;
176
- $ params ['contextid ' ] = $ coursecontext ->id ;
177
- $ params = array_merge ($ params , $ roleparams );
191
+ ON ra.userid = u.id
192
+ JOIN {context} ctx
193
+ ON ctx.id = ra.contextid ' ;
194
+ $ wheres [] = $ DB ->sql_like ('ctx.path ' , ':ctxpath ' , false );
195
+ $ wheres [] = ' ra.roleid ' . $ rolessql ;
196
+ $ params ['ctxpath ' ] = $ context ->path . '% ' ;
197
+ $ params = array_merge ($ params , $ rolesparams );
178
198
}
179
199
180
200
if ($ fields = $ DB ->get_records ('block_advnotifications_field ' , ['notificationid ' => $ notification ->id ])) {
@@ -222,14 +242,6 @@ public static function get_users_for_notification($notification) {
222
242
}
223
243
}
224
244
225
- if (isset ($ coursecontext )) {
226
- // TODO: enrolment in sub contexts.
227
- $ enrolledjoin = get_enrolled_join ($ coursecontext , 'u.id ' , true );
228
- $ joins [] = $ enrolledjoin ->joins ;
229
- $ wheres [] = $ enrolledjoin ->wheres ;
230
- $ params = array_merge ($ params , $ enrolledjoin ->params );
231
- }
232
-
233
245
$ sql = "SELECT u.id
234
246
FROM {user} u
235
247
" . implode ("\n" , $ joins ) . "
0 commit comments