From: moodler Date: Thu, 28 Sep 2006 04:42:49 +0000 (+0000) Subject: Added count_role_users() as a more efficient thing to do than get_role_users() X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=bac2f88ae882061ff6602d4f923ecd76c35d4063;p=moodle.git Added count_role_users() as a more efficient thing to do than get_role_users() --- diff --git a/lib/accesslib.php b/lib/accesslib.php index 4f6fb518f9..31c2e39571 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -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 * @return array() */ -function get_role_users($roleid, $context, $parent=false) { +function get_role_users($roleid, $context, $parent=false, $fields='u.*') { global $CFG; if ($parent) { @@ -2769,7 +2769,7 @@ function get_role_users($roleid, $context, $parent=false) { $parentcontexts = ''; } - $SQL = "select u.* + $SQL = "select $fields from {$CFG->prefix}role_assignments r, {$CFG->prefix}user u where (r.contextid = $context->id $parentcontexts) @@ -2779,6 +2779,34 @@ function get_role_users($roleid, $context, $parent=false) { 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