Skip to content

Commit

Permalink
Rewrite of add_indented_names() function.
Browse files Browse the repository at this point in the history
Bit longer, but *much* faster for large numbers of categories.
  • Loading branch information
thepurpleblob committed Mar 2, 2006
1 parent 5433ee8 commit 2dd6d66
Showing 1 changed file with 46 additions and 12 deletions.
58 changes: 46 additions & 12 deletions lib/questionlib.php
Expand Up @@ -1054,24 +1054,58 @@ function sort_categories_by_tree(&$categories, $id = 0, $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
function flatten_category_tree( $cats, $depth=0 ) {
// flattens tree structure created by add_indented_named
// (adding the names)
$newcats = array();
$fillstr = '   ';
$fill = str_repeat($fillstr, $indent);
$children = array();
$keys = array_keys($categories);

foreach ($keys as $key) {
if (!isset($categories[$key]->processed) && $categories[$key]->parent == $id) {
$children[$key] = $categories[$key];
$children[$key]->indentedname = $fill . $children[$key]->name;
$categories[$key]->processed = true;
$children = $children + add_indented_names($categories, $children[$key]->id, $indent + 1);
foreach ($cats as $key => $cat) {
$newcats[$key] = $cat;
$newcats[$key]->indentedname = str_repeat($fillstr,$depth) . $cat->name;
// recurse if the category has children
if (!empty($cat->children)) {
$newcats += flatten_category_tree( $cat->children, $depth+1 );
}
}
return $children;

return $newcats;
}

function add_indented_names( $categories ) {

// iterate through categories adding new fields
// and creating references
foreach ($categories as $key => $category) {
$categories[$key]->children = array();
$categories[$key]->link = &$categories[$key];
}

// create tree structure of children
// link field is used to track 'new' place of category in tree
foreach ($categories as $key => $category) {
if (!empty($category->parent)) {
$categories[$category->parent]->link->children[$key] = $categories[$key];
$categories[$key]->link = &$categories[$category->parent]->link->children[$key];
}
}

// remove top level categories with parents
$newcats = array();
foreach ($categories as $key => $category) {
unset( $category->link );
if (empty($category->parent)) {
$newcats[$key] = $category;
}
}

// walk the tree to flatten revised structure
$categories = flatten_category_tree( $newcats );

return $categories;
}


/**
* Displays a select menu of categories with appended course names
*
Expand Down

0 comments on commit 2dd6d66

Please sign in to comment.