]> git.mjollnir.org Git - moodle.git/commitdiff
Added count_role_users() as a more efficient thing to do than get_role_users()
authormoodler <moodler>
Thu, 28 Sep 2006 04:42:49 +0000 (04:42 +0000)
committermoodler <moodler>
Thu, 28 Sep 2006 04:42:49 +0000 (04:42 +0000)
lib/accesslib.php

index 4f6fb518f96e36d90df4007c30bf00b43f1d8d95..31c2e3957167212fb6d2d520de10abe55b8db050 100755 (executable)
@@ -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