Merge branch 'MDL-32570-master' of git://github.com/danpoltawski/moodle
[moodle.git] / enrol / imsenterprise / lib.php
index f3dcdc0..e2cf641 100644 (file)
@@ -414,10 +414,10 @@ function process_group_tag($tagcontents) {
                         } else {
                             // If not found and not allowed to create, stick with default
                             $this->log_line('Category '.$group->category.' not found in Moodle database, so using default category instead.');
-                            $course->category = 1;
+                            $course->category = $this->get_default_category_id();
                         }
                     } else {
-                        $course->category = 1;
+                        $course->category = $this->get_default_category_id();
                     }
                     $course->timecreated = time();
                     $course->startdate = time();
@@ -833,6 +833,27 @@ function load_role_mappings() {
     }
 
 
+    /**
+     * Get the default category id (often known as 'Miscellaneous'),
+     * statically cached to avoid multiple DB lookups on big imports.
+     *
+     * @return int id of default category.
+     */
+    private function get_default_category_id() {
+        global $CFG;
+        require_once($CFG->libdir.'/coursecatlib.php');
+
+        static $defaultcategoryid = null;
+
+        if ($defaultcategoryid === null) {
+            $category = coursecat::get_default();
+            $defaultcategoryid = $category->id;
+        }
+
+        return $defaultcategoryid;
+    }
+
+
 } // end of class