]> git.mjollnir.org Git - moodle.git/commitdiff
Merged from STABLE. Addition function to find or create category path.
authorthepurpleblob <thepurpleblob>
Wed, 29 Nov 2006 13:29:00 +0000 (13:29 +0000)
committerthepurpleblob <thepurpleblob>
Wed, 29 Nov 2006 13:29:00 +0000 (13:29 +0000)
MDL-4163

lib/questionlib.php

index c5c523be23418115c055eb96de7deeb20c4c6f01..41c449665830e5bf83f9ad74717c4c7233b2959a 100644 (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;
 }
 
-?>
\ No newline at end of file
+?>