]> git.mjollnir.org Git - moodle.git/commitdiff
user's role / capabiltiy report: sort the context tree.
authortjhunt <tjhunt>
Tue, 25 Nov 2008 07:29:14 +0000 (07:29 +0000)
committertjhunt <tjhunt>
Tue, 25 Nov 2008 07:29:14 +0000 (07:29 +0000)
admin/report/capability/index.php
admin/roles/usersroles.php
lib/accesslib.php

index 293d0580ce77e850ef911e242de9878bf9a875a0..1beb9d4056a7543a416fd025c994c1ea2b437c0e 100644 (file)
@@ -114,7 +114,7 @@ if ($capability) {
 
     // Now load those contexts.
     list($sqlcontexttest, $contextparams) = $DB->get_in_or_equal($requiredcontexts);
-    $contexts = $DB->get_records_select('context', 'id ' . $sqlcontexttest, $contextparams);
+    $contexts = get_sorted_contexts('ctx.id ' . $sqlcontexttest, $contextparams);
 
     // Prepare some empty arrays to hold the data we are about to compute.
     foreach ($contexts as $conid => $con) {
index 7c3a6712124cdf58a37e74578484f31dacd4e3ca..06f82922cc40ab7ebca45410b73647b56af94cdd 100644 (file)
@@ -88,7 +88,7 @@ $requiredcontexts = array_unique($requiredcontexts);
 /// Now load those contexts.
 if ($requiredcontexts) {
     list($sqlcontexttest, $contextparams) = $DB->get_in_or_equal($requiredcontexts);
-    $contexts = $DB->get_records_select('context', 'id ' . $sqlcontexttest, $contextparams);
+    $contexts = get_sorted_contexts('ctx.id ' . $sqlcontexttest, $contextparams);
 } else {
     $contexts = array();
 }
index 4ff260edd96d1fe4ddda97100bcee35793f7557e..d668a63913242d8c52745fcf4a9e1992b2cce68c 100755 (executable)
@@ -3698,6 +3698,34 @@ function is_inside_frontpage($context) {
     return strpos($context->path . '/', $frontpagecontext->path . '/') === 0;
 }
 
+/**
+ * Does get_records_select on the context table, and returns the results ordered
+ * by contextlevel, and then the natural sort order within each level.
+ * for the purpose of $select, you need to know that the context table has been
+ * aliased to ctx, so for example, you can call get_sorted_contexts('ctx.depth = 3');
+ *
+ * @param string $select the contents of the WHERE clause. Remember to do ctx.fieldname.
+ * @param array $params any parameters required by $select.
+ * @return array the requested context records.
+ */
+function get_sorted_contexts($select, $params = array()) {
+    global $DB;
+    if ($select) {
+        $select = 'WHERE ' . $select;
+    }
+    return $DB->get_records_sql("
+            SELECT ctx.*
+            FROM {context} ctx
+            LEFT JOIN {user} u ON ctx.contextlevel = 30 AND u.id = ctx.instanceid
+            LEFT JOIN {course_categories} cat ON ctx.contextlevel = 40 AND cat.id = ctx.instanceid
+            LEFT JOIN {course} c ON ctx.contextlevel = 50 AND c.id = ctx.instanceid
+            LEFT JOIN {course_modules} cm ON ctx.contextlevel = 70 AND cm.id = ctx.instanceid
+            LEFT JOIN {block_instance} bi ON ctx.contextlevel = 80 AND bi.id = ctx.instanceid
+            $select
+            ORDER BY ctx.contextlevel, bi.position, COALESCE(cat.sortorder, c.sortorder, cm.section, bi.weight), u.lastname, u.firstname, cm.id
+            ", $params);
+}
+
 /**
  * Recursive function which, given a context, find all its children context ids.
  *