From ca653ec70686d9e3fd128c40e56463292fc4c0bb Mon Sep 17 00:00:00 2001 From: stronk7 Date: Mon, 30 May 2005 23:59:42 +0000 Subject: [PATCH] Added the sort_categories_by_tree() function that 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 --- mod/quiz/locallib.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index e0c8c2d93035b..cd0010aaf840d 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -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 = '   ';