mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
accesslib:get_role_users() revamp
Now get_role_users() joins with contexts and roles too, so we can push more work into it, and simplify the callers. One important change is that the $view flag gets reworked into $gethidden, pushing the cap check to the caller. This commit is followed by a cleanup of the callers...
This commit is contained in:
parent
8fe9c4de51
commit
867f957fc5
1 changed files with 33 additions and 24 deletions
|
@ -4058,58 +4058,67 @@ function get_users_by_capability($context, $capability, $fields='', $sort='',
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets all the users assigned this role in this context or higher
|
* gets all the users assigned this role in this context or higher
|
||||||
* @param int roleid
|
* @param int roleid (can also be an array of ints!)
|
||||||
* @param int contextid
|
* @param int contextid
|
||||||
* @param bool parent if true, get list of users assigned in higher context too
|
* @param bool parent if true, get list of users assigned in higher context too
|
||||||
|
* @param string fields - fields from user (u.) , role assignment (ra) or role (r.)
|
||||||
|
* @param string sort - sort from user (u.) , role assignment (ra) or role (r.)
|
||||||
|
* @param bool gethidden - whether to fetch hidden enrolments too
|
||||||
* @return array()
|
* @return array()
|
||||||
*/
|
*/
|
||||||
function get_role_users($roleid, $context, $parent=false, $fields='', $sort='u.lastname ASC, u.firstname ASC', $view=false, $limitfrom='', $limitnum='', $group='') {
|
function get_role_users($roleid, $context, $parent=false, $fields='', $sort='u.lastname ASC', $gethidden=true) {
|
||||||
global $CFG;
|
global $CFG;
|
||||||
|
|
||||||
if (empty($fields)) {
|
if (empty($fields)) {
|
||||||
$fields = 'u.id, u.confirmed, u.username, u.firstname, u.lastname, '.
|
$fields = 'u.id, u.confirmed, u.username, u.firstname, u.lastname, '.
|
||||||
'u.maildisplay, u.mailformat, u.maildigest, u.email, u.city, '.
|
'u.maildisplay, u.mailformat, u.maildigest, u.email, u.city, '.
|
||||||
'u.country, u.picture, u.idnumber, u.department, u.institution, '.
|
'u.country, u.picture, u.idnumber, u.department, u.institution, '.
|
||||||
'u.emailstop, u.lang, u.timezone';
|
'u.emailstop, u.lang, u.timezone, r.name as rolename';
|
||||||
}
|
}
|
||||||
|
|
||||||
// whether this assignment is hidden
|
// whether this assignment is hidden
|
||||||
$hiddensql = ($view && !has_capability('moodle/role:viewhiddenassigns', $context))? ' AND r.hidden = 0 ':'';
|
$hiddensql = $gethidden ? '': ' AND ra.hidden = 0 ';
|
||||||
|
|
||||||
|
$parentcontexts = '';
|
||||||
if ($parent) {
|
if ($parent) {
|
||||||
if ($contexts = get_parent_contexts($context)) {
|
$parentcontexts = substr($context->path, 1); // kill leading slash
|
||||||
$parentcontexts = ' OR r.contextid IN ('.implode(',', $contexts).')';
|
$parentcontexts = str_replace('/', ',', $parentcontexts);
|
||||||
} else {
|
if ($parentcontexts !== '') {
|
||||||
$parentcontexts = '';
|
$parentcontexts = ' OR ra.contextid IN ('.$parentcontexts.' )';
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
$parentcontexts = '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($roleid) {
|
if (is_array($roleid)) {
|
||||||
$roleselect = "AND r.roleid = $roleid";
|
$roleselect = ' AND ra.roleid IN (' . join(',',$roleid) .')';
|
||||||
|
} elseif (is_int($roleid)) {
|
||||||
|
$roleselect = "AND ra.roleid = $roleid";
|
||||||
} else {
|
} else {
|
||||||
$roleselect = '';
|
$roleselect = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($group) {
|
if ($group) {
|
||||||
$groupsql = "{$CFG->prefix}groups_members gm, ";
|
$groupjoin = "JOIN {$CFG->prefix}groups_members gm
|
||||||
$groupwheresql = " AND gm.userid = u.id AND gm.groupid = $group ";
|
ON gm.userid = u.id";
|
||||||
|
$groupselect = " AND gm.groupid = $group ";
|
||||||
} else {
|
} else {
|
||||||
$groupsql = '';
|
$groupjoin = '';
|
||||||
$groupwheresql = '';
|
$groupselect = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$SQL = "SELECT $fields
|
$SQL = "SELECT $fields, ra.roleid
|
||||||
FROM {$CFG->prefix}role_assignments r,
|
FROM {$CFG->prefix}role_assignments ra
|
||||||
$groupsql
|
JOIN {$CFG->prefix}user u
|
||||||
{$CFG->prefix}user u
|
ON u.id = ra.userid
|
||||||
WHERE (r.contextid = $context->id $parentcontexts)
|
JOIN {$CFG->prefix}role r
|
||||||
$groupwheresql
|
ON ra.roleid = r.id
|
||||||
AND u.id = r.userid $roleselect
|
$groupjoin
|
||||||
|
WHERE (ra.contextid = $context->id $parentcontexts)
|
||||||
|
$roleselect
|
||||||
|
$groupselect
|
||||||
$hiddensql
|
$hiddensql
|
||||||
ORDER BY $sort
|
ORDER BY $sort
|
||||||
"; // join now so that we can just use fullname() later
|
"; // join now so that we can just use fullname() later
|
||||||
return get_records_sql($SQL, $limitfrom, $limitnum);
|
return get_records_sql($SQL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue