From: thepurpleblob Date: Wed, 29 Nov 2006 13:29:00 +0000 (+0000) Subject: Merged from STABLE. Addition function to find or create category path. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=f5c154078aad99bba3e6f9ba8197d532437d2654;p=moodle.git Merged from STABLE. Addition function to find or create category path. MDL-4163 --- diff --git a/lib/questionlib.php b/lib/questionlib.php index c5c523be23..41c4496658 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -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; } -?> \ No newline at end of file +?>