From: tjhunt Date: Tue, 25 Nov 2008 07:29:14 +0000 (+0000) Subject: user's role / capabiltiy report: sort the context tree. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=759adfffa9dd499f47a3e471b8924deaed179165;p=moodle.git user's role / capabiltiy report: sort the context tree. --- diff --git a/admin/report/capability/index.php b/admin/report/capability/index.php index 293d0580ce..1beb9d4056 100644 --- a/admin/report/capability/index.php +++ b/admin/report/capability/index.php @@ -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) { diff --git a/admin/roles/usersroles.php b/admin/roles/usersroles.php index 7c3a671212..06f82922cc 100644 --- a/admin/roles/usersroles.php +++ b/admin/roles/usersroles.php @@ -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(); } diff --git a/lib/accesslib.php b/lib/accesslib.php index 4ff260edd9..d668a63913 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -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. *