@@ -138,4 +138,104 @@ public static function meets_roles_requirements($notificationid, $userid, $block
138
138
}
139
139
return true ;
140
140
}
141
+
142
+ public static function get_users_for_notification ($ notification ) {
143
+ global $ DB ;
144
+
145
+ $ joins = [];
146
+ $ wheres = [];
147
+ $ params = [];
148
+
149
+ if (!empty ($ notification ->blockid ) && $ notification ->blockid > 0 ) {
150
+ $ bcontext = \context_block::instance ($ notification ->blockid );
151
+ $ coursecontext = $ bcontext ->get_course_context (false );
152
+ } else {
153
+ $ coursecontext = \context_system::instance ();
154
+ }
155
+
156
+ if ($ DB ->record_exists ('block_advnotifications_coh ' , ['notificationid ' => $ notification ->id ])) {
157
+ $ joins [] =
158
+ ' JOIN {cohort_members} cm
159
+ ON cm.userid = u.id
160
+ JOIN {block_advnotifications_coh} anc
161
+ ON anc.cohortid = cm.cohortid ' ;
162
+ $ wheres [] = 'anc.notificationid = :notificationid ' ;
163
+ $ params ['notificationid ' ] = $ notification ->id ;
164
+ }
165
+
166
+ if ($ roles = $ DB ->get_records_menu ('block_advnotifications_roles ' , ['notificationid ' => $ notification ->id ], '' , 'roleid ' )) {
167
+
168
+ // TODO: role assignments in sub-contexts.
169
+ $ roles = array_keys ($ roles );
170
+ list ($ rolesql , $ roleparams ) = $ DB ->get_in_or_equal ($ roles , SQL_PARAMS_NAMED , 'roleid ' );
171
+ $ joins [] =
172
+ ' 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 );
178
+ }
179
+
180
+ if ($ fields = $ DB ->get_records ('block_advnotifications_field ' , ['notificationid ' => $ notification ->id ])) {
181
+ $ customfields = [];
182
+ $ fieldsapi = \core_user \fields::for_name ();
183
+ foreach ($ fields as $ key => $ f ) {
184
+ if (strpos ($ f ->userfield , 'profile_field_ ' ) === false ) {
185
+ switch ($ f ->operator ) {
186
+ case 'equals ' :
187
+ $ wheres [] = " u. {$ f ->userfield } = :field {$ key }" ;
188
+ $ params ['field ' .$ key ] = $ f ->fieldvalue ;
189
+ break ;
190
+ case 'beginwith ' :
191
+ $ wheres [] = $ DB ->sql_like ("u. {$ f ->userfield }" , ":field {$ key }" , false );
192
+ $ params ['field ' .$ key ] = $ f ->fieldvalue . '% ' ;
193
+ break ;
194
+ case 'contains ' :
195
+ $ wheres [] = $ DB ->sql_like ("u. {$ f ->userfield }" , ":field {$ key }" , false );
196
+ $ params ['field ' .$ key ] = '% ' . $ f ->fieldvalue . '% ' ;
197
+ break ;
198
+ }
199
+ } else {
200
+ $ fieldsapi ->including ($ f ->userfield );
201
+ $ customfields [] = $ f ;
202
+ }
203
+ }
204
+ $ fieldsall = $ fieldsapi ->get_sql ('u ' , true );
205
+ $ joins [] = $ fieldsall ->joins ;
206
+ $ params = array_merge ($ params , $ fieldsall ->params );
207
+ foreach ($ customfields as $ key => $ f ) {
208
+ switch ($ f ->operator ) {
209
+ case 'equals ' :
210
+ $ wheres [] = " {$ fieldsall ->mappings [$ f ->userfield ]} = :field {$ key }" ;
211
+ $ params ['field ' .$ key ] = $ f ->fieldvalue ;
212
+ break ;
213
+ case 'beginwith ' :
214
+ $ wheres [] = $ DB ->sql_like ($ fieldsall ->mappings [$ f ->userfield ], ":field {$ key }" , false );
215
+ $ params ['field ' .$ key ] = $ f ->fieldvalue . '% ' ;
216
+ break ;
217
+ case 'contains ' :
218
+ $ wheres [] = $ DB ->sql_like ($ fieldsall ->mappings [$ f ->userfield ], ":field {$ key }" , false );
219
+ $ params ['field ' .$ key ] = '% ' . $ f ->fieldvalue . '% ' ;
220
+ break ;
221
+ }
222
+ }
223
+ }
224
+
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
+ $ sql = "SELECT u.id
234
+ FROM {user} u
235
+ " . implode ("\n" , $ joins ) . "
236
+ WHERE u.deleted = 0
237
+ AND " . implode ("\n AND " , $ wheres );
238
+
239
+ return $ DB ->get_records_sql ($ sql , $ params );
240
+ }
141
241
}
0 commit comments