You are here

function add_indented_names in Quiz 6.6

Same name and namespace in other branches
  1. 6.5 includes/moodle/lib/questionlib.php \add_indented_names()

Format categories into an indented list reflecting the tree structure.

Parameters

array $categories An array of category objects, for example from the.:

Return value

array The formatted list of categories.

1 call to add_indented_names()
question_category_options in includes/moodle/lib/questionlib.php
Output an array of question categories.

File

includes/moodle/lib/questionlib.php, line 1831

Code

function add_indented_names($categories, $nochildrenof = -1) {

  // Add an array to each category to hold the child category ids. This array will be removed
  // again by flatten_category_tree(). It should not be used outside these two functions.
  foreach (array_keys($categories) as $id) {
    $categories[$id]->childids = array();
  }

  // Build the tree structure, and record which categories are top-level.
  // We have to be careful, because the categories array may include published
  // categories from other courses, but not their parents.
  $toplevelcategoryids = array();
  foreach (array_keys($categories) as $id) {
    if (!empty($categories[$id]->parent) && array_key_exists($categories[$id]->parent, $categories)) {
      $categories[$categories[$id]->parent]->childids[] = $id;
    }
    else {
      $toplevelcategoryids[] = $id;
    }
  }

  // Flatten the tree to and add the indents.
  $newcategories = array();
  foreach ($toplevelcategoryids as $id) {
    $newcategories = $newcategories + flatten_category_tree($categories, $id, 0, $nochildrenof);
  }
  return $newcategories;
}