mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
Merged from STABLE. Addition function to find or create category path.
MDL-4163
This commit is contained in:
parent
98de921075
commit
f5c154078a
1 changed files with 37 additions and 1 deletions
|
@ -1533,6 +1533,42 @@ function question_categorylist($categoryid) {
|
||||||
return $categorylist;
|
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
|
// Import/Export Functions
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue