Skip to content

Commit 3893d9b

Browse files
committed
add audiences support when sending notifications
1 parent 6514697 commit 3893d9b

File tree

3 files changed

+102
-9
lines changed

3 files changed

+102
-9
lines changed

classes/audience.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,104 @@ public static function meets_roles_requirements($notificationid, $userid, $block
138138
}
139139
return true;
140140
}
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+
}
141241
}

classes/task/sendnotifications.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,7 @@ public function execute() {
5959
return;
6060
}
6161

62-
if (!empty($notification->blockid)) {
63-
$bcontext = \context_block::instance($notification->blockid);
64-
}
65-
if (isset($bcontext) && $ccontext = $bcontext->get_course_context(false)) {
66-
$users = get_enrolled_users($ccontext, '', 0 , 'u.id');
67-
} else {
68-
$users = $DB->get_records('user', ['deleted' => 0, 'suspended' => 0], '', 'id');
69-
}
62+
$users = \block_advnotifications\audience::get_users_for_notification($notification);
7063

7164
foreach ($users as $u) {
7265
$eventdata = new \core\message\message();

pages/audience.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
$notification = $DB->get_record('block_advnotifications', ['id' => $id]);
3030

31-
if ($notification->blockid) {
31+
if ($notification->blockid > 0) {
3232
$bcontext = context_block::instance($notification->blockid);
3333
$ctx = $bcontext->get_course_context(false);
3434
}

0 commit comments

Comments
 (0)