diff --git a/group/lib.php b/group/lib.php index 8fd4c127e4a80..6c9e52c28641c 100644 --- a/group/lib.php +++ b/group/lib.php @@ -646,11 +646,10 @@ function groups_get_members_by_role($groupid, $courseid, $fields='u.*', u.id AS userid, $fields FROM {groups_members} gm JOIN {user} u ON u.id = gm.userid - JOIN {role_assignments} ra ON ra.userid = u.id - JOIN {role} r ON r.id = ra.roleid + LEFT JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.contextid ".get_related_contexts_string($context).") + LEFT JOIN {role} r ON r.id = ra.roleid WHERE gm.groupid=:mgroupid - AND ra.contextid ".get_related_contexts_string($context). - $extrawheretest." + ".$extrawheretest." ORDER BY r.sortorder, $sort"; $whereparams['mgroupid'] = $groupid; $rs = $DB->get_recordset_sql($sql, $whereparams); @@ -687,7 +686,7 @@ function groups_calculate_role_people($rs, $context) { if (!array_key_exists($rec->userid, $users)) { // User data includes all the optional fields, but not any of the // stuff we added to get the role details - $userdata=clone($rec); + $userdata = clone($rec); unset($userdata->roleid); unset($userdata->roleshortname); unset($userdata->rolename); @@ -720,7 +719,7 @@ function groups_calculate_role_people($rs, $context) { $rs->close(); // Return false if there weren't any users - if (count($users)==0) { + if (count($users) == 0) { return false; } @@ -730,12 +729,18 @@ function groups_calculate_role_people($rs, $context) { $roledata->users = array(); $roles['*'] = $roledata; + $roledata = new stdClass(); + $roledata->name = get_string('noroles','role'); + $roledata->users = array(); + $roles[0] = $roledata; + // Now we rearrange the data to store users by role foreach ($users as $userid=>$userdata) { $rolecount = count($userdata->roles); - if ($rolecount==0) { - debugging("Unexpected: user $userid is missing roles"); - } else if($rolecount>1) { + if ($rolecount == 0) { + // does not have any roles + $roleid = 0; + } else if($rolecount > 1) { $roleid = '*'; } else { $roleid = $userdata->roles[0]->id; diff --git a/lang/en/role.php b/lang/en/role.php index 6d2bb152d77a7..f992ba1dd31c6 100644 --- a/lang/en/role.php +++ b/lang/en/role.php @@ -224,6 +224,7 @@ $string['nocapabilitiesincontext'] = 'No capabilities available in this context'; $string['noneinthisx'] = 'None in this {$a}'; $string['noneinthisxmatching'] = 'No users matching \'{$a->search}\' in this {$a->contexttype}'; +$string['noroles'] = 'No roles'; $string['noroleassignments'] = 'This user does not have any role assignments anywhere in this site.'; $string['notabletoassignroleshere'] = 'You are not able to assign any roles here'; $string['notabletooverrideroleshere'] = 'You are not able to override the permissions on any roles here'; diff --git a/user/selector/lib.php b/user/selector/lib.php index 02f879d657a0e..c6215dbc7988e 100644 --- a/user/selector/lib.php +++ b/user/selector/lib.php @@ -751,7 +751,7 @@ public function print_user_summaries($courseid) { global $DB, $PAGE; $usersummaries = array(); - + // Get other groups user already belongs to $usergroups = array(); $potentialmembersids = $this->potentialmembersids; @@ -792,10 +792,12 @@ public function find_users($search) { // Get list of allowed roles. $context = get_context_instance(CONTEXT_COURSE, $this->courseid); - if (!$validroleids = groups_get_possible_roles($context)) { - return array(); + if ($validroleids = groups_get_possible_roles($context)) { + list($roleids, $roleparams) = $DB->get_in_or_equal($validroleids, SQL_PARAMS_NAMED, 'r00'); + } else { + $roleids = " = -1"; + $roleparams = array(); } - list($roleids, $roleparams) = $DB->get_in_or_equal($validroleids, SQL_PARAMS_NAMED, 'r00'); // Get the search condition. list($searchcondition, $searchparams) = $this->search_sql($search, 'u'); @@ -810,11 +812,9 @@ public function find_users($search) { WHERE igm.userid = u.id AND ig.courseid = :courseid) AS numgroups"; $sql = " FROM {user} u JOIN ($enrolsql) e ON e.id = u.id - JOIN {role_assignments} ra ON ra.userid = u.id - JOIN {role} r ON r.id = ra.roleid - WHERE ra.contextid " . get_related_contexts_string($context) . " - AND u.deleted = 0 - AND ra.roleid $roleids + LEFT JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.contextid " . get_related_contexts_string($context) . " AND ra.roleid $roleids) + LEFT JOIN {role} r ON r.id = ra.roleid + WHERE u.deleted = 0 AND u.id NOT IN (SELECT userid FROM {groups_members} WHERE groupid = :groupid)