Skip to content

Commit 3254ba0

Browse files
committed
roles filter refactor
if the block is in a course, it will consider users with any of the selected roles assigned in the course or in any sub contexts. if the block is in a category, it will consider users with any of the selected roles assigned in the category or in any sub contexts. if the block is in the dashboard, it will consider users with any of the selected roles assigned in any context. if the block is in the system context, it will consider users with any of the selected roles assigned in any context.
1 parent 3893d9b commit 3254ba0

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

classes/audience.php

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,35 @@ public static function meets_cohorts_requirements($notificationid, $userid) {
123123

124124
public static function meets_roles_requirements($notificationid, $userid, $blockid) {
125125
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')) {
127127
return true; // There is no role restriction.
128128
}
129-
if ($blockid) {
129+
$roles = array_keys($roles);
130+
131+
if ($blockid > 0) {
130132
$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+
}
131141
} else {
132142
$context = \context_system::instance();
133143
}
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);
140155
}
141156

142157
public static function get_users_for_notification($notification) {
@@ -146,11 +161,15 @@ public static function get_users_for_notification($notification) {
146161
$wheres = [];
147162
$params = [];
148163

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+
}
152171
} else {
153-
$coursecontext = \context_system::instance();
172+
$context = \context_system::instance();
154173
}
155174

156175
if ($DB->record_exists('block_advnotifications_coh', ['notificationid' => $notification->id])) {
@@ -165,16 +184,17 @@ public static function get_users_for_notification($notification) {
165184

166185
if ($roles = $DB->get_records_menu('block_advnotifications_roles', ['notificationid' => $notification->id], '', 'roleid')) {
167186

168-
// TODO: role assignments in sub-contexts.
169187
$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');
171189
$joins[] =
172190
' 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);
178198
}
179199

180200
if ($fields = $DB->get_records('block_advnotifications_field', ['notificationid' => $notification->id])) {
@@ -222,14 +242,6 @@ public static function get_users_for_notification($notification) {
222242
}
223243
}
224244

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-
233245
$sql = "SELECT u.id
234246
FROM {user} u
235247
" . implode("\n", $joins) . "

0 commit comments

Comments
 (0)