]> git.mjollnir.org Git - moodle.git/commitdiff
datalib: get_admins() - Rewrite to avoid get_users_by_capability()
authormartinlanghoff <martinlanghoff>
Sun, 6 Jan 2008 23:24:50 +0000 (23:24 +0000)
committermartinlanghoff <martinlanghoff>
Sun, 6 Jan 2008 23:24:50 +0000 (23:24 +0000)
get_admins() and get_admin() were counting on
get_users_by_capability() returning a role-assignment id to pick the
"primary" admin account. With the get_users_by_capability() rewrite,
we no longer have an RA id to clearly blame for the capability.

So, rewrite get_admins() based on the known-good SQL used in
is_siteadmin().

MDL-12452

lib/datalib.php

index f9c22dac140cc9d8a8f97eac0aa231d4486e0893..541b40b83962d7a2cda25544a31e7e29651b1a57 100644 (file)
@@ -58,7 +58,8 @@ function get_admin () {
 }
 
 /**
- * Returns list of all admins
+ * Returns list of all admins, using 1 DB query. It depends on DB schema v1.7
+ * but does not depend on the v1.9 datastructures (context.path, etc).
  *
  * @uses $CFG
  * @return object
@@ -67,10 +68,26 @@ function get_admins() {
 
     global $CFG;
 
-    $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
-
-    return get_users_by_capability($context, 'moodle/site:doanything', 'u.*, ra.id as adminid', 'ra.id ASC'); // only need first one
+    $sql = "SELECT ra.userid, SUM(rc.permission) AS permission, MIN(ra.id) AS adminid
+            FROM " . $CFG->prefix . "role_capabilities rc
+            JOIN " . $CFG->prefix . "context ctx
+              ON ctx.id=rc.contextid
+            JOIN " . $CFG->prefix . "role_assignments ra
+              ON ra.roleid=rc.roleid AND ra.contextid=ctx.id
+            WHERE ctx.contextlevel=10
+              AND rc.capability IN ('moodle/site:config',
+                                    'moodle/legacy:admin',
+                                    'moodle/site:doanything')       
+            GROUP BY ra.userid
+            HAVING SUM(rc.permission) > 0";
+
+    $sql = "SELECT u.*, ra.adminid
+            FROM  " . $CFG->prefix . "user u
+            JOIN ($sql) ra
+              ON u.id=ra.userid
+            ORDER BY ra.adminid ASC";
 
+    return get_records_sql($sql);
 }