Merged from STABLE. Addition function to find or create category path.

MDL-4163
This commit is contained in:
thepurpleblob 2006-11-29 13:29:00 +00:00
parent 98de921075
commit f5c154078a

View File

@ -1533,6 +1533,42 @@ function question_categorylist($categoryid) {
return $categorylist;
}
/**
* find and/or create the category described by a delimited list
* e.g. tom/dick/harry
* @param string catpath delimited category path
* @param string delimiter path delimiting character
* @param int courseid course to search for categories
* @return mixed category object or null if fails
*/
function create_category_path( $catpath, $delimiter='/', $courseid=0 ) {
$catnames = explode( $delimiter, $catpath );
$parent = 0;
$category = null;
foreach ($catnames as $catname) {
if ($category = get_record( 'question_categories', 'name', $catname, 'course', $courseid, 'parent', $parent )) {
$parent = $category->id;
}
else {
// create the new category
$category = new object;
$category->course = $courseid;
$category->name = $catname;
$category->info = '';
$category->publish = false;
$category->parent = $parent;
$category->sortorder = 999;
$category->stamp = make_unique_id_code();
if (!($id = insert_record( 'question_categories', $category ))) {
error( "cannot create new category - $catname" );
}
$category->id = $id;
$parent = $id;
}
}
return $category;
}
//===========================
// Import/Export Functions
@ -1627,4 +1663,4 @@ function default_export_filename($course,$category) {
return $export_name;
}
?>
?>