Moved some category functions back to questionlib. Original move

caused all sorts of problems.
This commit is contained in:
thepurpleblob
2006-05-08 10:40:42 +00:00
parent 3b438e0db9
commit f1068df6c6
-90
View File
@@ -115,96 +115,6 @@ function question_category_menu($courseid, $published=false) {
}
return $catmenu;
}
/**
* 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.
*/
function sort_categories_by_tree(&$categories, $id = 0, $level = 1) {
$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('question_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;
}
/**
* flattens tree structure created by add_indented_named
* (adding the names)
* @param array cats tree structure of categories
* @param int depth tree depth tracker (for indenting)
* @return array flattened, formatted list
*/
function flatten_category_tree( $cats, $depth=0 ) {
$newcats = array();
$fillstr = '   ';
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 $newcats;
}
/**
* format categories into indented list
* @param array categories categories array (from db)
* @return array formatted list of categories
*/
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;
}
/**
* prints a form to choose categories