Commit 759adfff authored by tjhunt's avatar tjhunt
Browse files

user's role / capabiltiy report: sort the context tree.

parent 28c58294
......@@ -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) {
......
......@@ -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();
}
......
......@@ -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.
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment