* @param bool parent if true, get list of users assigned in higher context too
* @return array()
*/
-function get_role_users($roleid, $context, $parent=false) {
+function get_role_users($roleid, $context, $parent=false, $fields='u.*') {
global $CFG;
if ($parent) {
$parentcontexts = '';
}
- $SQL = "select u.*
+ $SQL = "select $fields
from {$CFG->prefix}role_assignments r,
{$CFG->prefix}user u
where (r.contextid = $context->id $parentcontexts)
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 is not the most efficient way of doing this