mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
Added count_role_users() as a more efficient thing to do than get_role_users()
This commit is contained in:
parent
15fdf6199e
commit
bac2f88ae8
1 changed files with 30 additions and 2 deletions
|
@ -2756,7 +2756,7 @@ function get_users_by_capability($context, $capability, $fields='', $sort='',
|
||||||
* @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
|
||||||
* @return array()
|
* @return array()
|
||||||
*/
|
*/
|
||||||
function get_role_users($roleid, $context, $parent=false) {
|
function get_role_users($roleid, $context, $parent=false, $fields='u.*') {
|
||||||
global $CFG;
|
global $CFG;
|
||||||
|
|
||||||
if ($parent) {
|
if ($parent) {
|
||||||
|
@ -2769,7 +2769,7 @@ function get_role_users($roleid, $context, $parent=false) {
|
||||||
$parentcontexts = '';
|
$parentcontexts = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$SQL = "select u.*
|
$SQL = "select $fields
|
||||||
from {$CFG->prefix}role_assignments r,
|
from {$CFG->prefix}role_assignments r,
|
||||||
{$CFG->prefix}user u
|
{$CFG->prefix}user u
|
||||||
where (r.contextid = $context->id $parentcontexts)
|
where (r.contextid = $context->id $parentcontexts)
|
||||||
|
@ -2779,6 +2779,34 @@ function get_role_users($roleid, $context, $parent=false) {
|
||||||
return get_records_sql($SQL);
|
return get_records_sql($SQL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Counts all the users assigned this role in this context or higher
|
||||||
|
* @param int roleid
|
||||||
|
* @param int contextid
|
||||||
|
* @param bool parent if true, get list of users assigned in higher context too
|
||||||
|
* @return array()
|
||||||
|
*/
|
||||||
|
function count_role_users($roleid, $context, $parent=false) {
|
||||||
|
global $CFG;
|
||||||
|
|
||||||
|
if ($parent) {
|
||||||
|
if ($contexts = get_parent_contexts($context)) {
|
||||||
|
$parentcontexts = 'r.contextid IN ('.implode(',', $contexts).')';
|
||||||
|
} else {
|
||||||
|
$parentcontexts = '';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$parentcontexts = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$SQL = "SELECT count(*)
|
||||||
|
FROM {$CFG->prefix}role_assignments r
|
||||||
|
WHERE (r.contextid = $context->id $parentcontexts)
|
||||||
|
AND r.roleid = $roleid";
|
||||||
|
|
||||||
|
return count_records_sql($SQL);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function gets the list of courses that this user has a particular capability in
|
* This function gets the list of courses that this user has a particular capability in
|
||||||
* This is not the most efficient way of doing this
|
* This is not the most efficient way of doing this
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue