Skip to content

Commit

Permalink
Added the sort_categories_by_tree() function that
Browse files Browse the repository at this point in the history
returns an ordered array of categories following
the parent-child relationships. It doesn't forget
any category and returns all the categories passed
although their parent were incorrect. Try to build
as much tree structure as possible.

Merged from MOODLE_15_STABLE
  • Loading branch information
stronk7 committed May 30, 2005
1 parent 2019116 commit ca653ec
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions mod/quiz/locallib.php
Expand Up @@ -2095,6 +2095,34 @@ function quiz_get_category_menu($courseid, $published=false) {
return $catmenu;
}

function sort_categories_by_tree(&$categories, $id = 0, $level = 1) {
// returns the categories with their names ordered following parent-child relationships
// finally it tries to return pending categories (those being orphaned, whose parent is
// incorrect) to avoid missing any category from original array.
$children = array();
$keys = array_keys($categories);

foreach ($keys as $key) {
if (!isset($categories[$key]->processed) && $categories[$key]->parent == $id) {
$children[$key] = $categories[$key];
$categories[$key]->processed = true;
$children = $children + sort_categories_by_tree($categories, $children[$key]->id, $level+1);
}
}
//If level = 1, we have finished, try to look for non processed categories (bad parent) and sort them too
if ($level == 1) {
foreach ($keys as $key) {
//If not processed and it's a good candidate to start (because its parent doesn't exist in the course)
if (!isset($categories[$key]->processed) && !record_exists('quiz_categories', 'course', $categories[$key]->course, 'id', $categories[$key]->parent)) {
$children[$key] = $categories[$key];
$categories[$key]->processed = true;
$children = $children + sort_categories_by_tree($categories, $children[$key]->id, $level+1);
}
}
}
return $children;
}

function add_indented_names(&$categories, $id = 0, $indent = 0) {
// returns the categories with their names indented to show parent-child relationships
$fillstr = '   ';
Expand Down

0 comments on commit ca653ec

Please sign in to comment.