From 90ce66a9cee02c8271328ed35c0817bf03332c7c Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 30 Jul 2019 07:55:50 +0100 Subject: [PATCH] MDL-66178 user: filter course participants for users with no roles. --- user/lib.php | 12 +++++++++--- user/renderer.php | 2 +- user/tests/behat/filter_participants.feature | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/user/lib.php b/user/lib.php index 477e614f59e..d38ad9bc0df 100644 --- a/user/lib.php +++ b/user/lib.php @@ -1293,7 +1293,7 @@ function user_get_tagged_users($tag, $exclusivemode = false, $fromctx = 0, $ctx * @param int $courseid The course id * @param int $groupid The groupid, 0 means all groups and USERSWITHOUTGROUP no group * @param int $accesssince The time since last access, 0 means any time - * @param int $roleid The role id, 0 means all roles + * @param int $roleid The role id, 0 means all roles and -1 no roles * @param int $enrolid The enrolment id, 0 means all enrolment methods will be returned. * @param int $statusid The user enrolment status, -1 means all enrolments regardless of the status will be returned, if allowed. * @param string|array $search The search that was performed, empty means perform no search @@ -1367,8 +1367,14 @@ function user_get_participants_sql($courseid, $groupid = 0, $accesssince = 0, $r list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx'); - $wheres[] = "u.id IN (SELECT userid FROM {role_assignments} WHERE roleid = :roleid AND contextid $relatedctxsql)"; - $params = array_merge($params, array('roleid' => $roleid), $relatedctxparams); + // Get users without any role. + if ($roleid == -1) { + $wheres[] = "u.id NOT IN (SELECT userid FROM {role_assignments} WHERE contextid $relatedctxsql)"; + $params = array_merge($params, $relatedctxparams); + } else { + $wheres[] = "u.id IN (SELECT userid FROM {role_assignments} WHERE roleid = :roleid AND contextid $relatedctxsql)"; + $params = array_merge($params, array('roleid' => $roleid), $relatedctxparams); + } } if (!empty($search)) { diff --git a/user/renderer.php b/user/renderer.php index 1e8461f695d..a1f22c1aa37 100644 --- a/user/renderer.php +++ b/user/renderer.php @@ -135,7 +135,7 @@ class core_user_renderer extends plugin_renderer_base { } $criteria = get_string('role'); - $roleoptions = []; + $roleoptions = $this->format_filter_option(USER_FILTER_ROLE, $criteria, -1, get_string('noroles', 'role')); foreach ($roles as $id => $role) { $roleoptions += $this->format_filter_option(USER_FILTER_ROLE, $criteria, $id, $role); } diff --git a/user/tests/behat/filter_participants.feature b/user/tests/behat/filter_participants.feature index f0097f25a6c..13d815c8207 100644 --- a/user/tests/behat/filter_participants.feature +++ b/user/tests/behat/filter_participants.feature @@ -98,6 +98,23 @@ Feature: Course participants can be filtered | Group: Group A | Student 1 | Student 2 | | Student 3 | XX-IGNORE-XX | | Group: Group B | Student 2 | | | Student 1 | Student 3 | + @javascript + Scenario: Filter users who have no role in a course + Given I log in as "teacher1" + And I am on "Course 1" course homepage + And I navigate to course participants + And I click on "Student 1's role assignments" "link" + And I click on ".form-autocomplete-selection [aria-selected=true]" "css_element" + And I press key "27" in the field "Student 1's role assignments" + And I click on "Save changes" "link" + When I open the autocomplete suggestions list + And I click on "Role: No roles" item in the autocomplete list + Then I should see "Student 1" in the "participants" "table" + And I should not see "Student 2" in the "participants" "table" + And I should not see "Student 3" in the "participants" "table" + And I should not see "Student 4" in the "participants" "table" + And I should not see "Teacher 1" in the "participants" "table" + @javascript Scenario: Multiple filters applied Given I log in as "teacher1" -- 2.43.0