MDL-29044: Move course category creation to a separate function
authorAndrew Robert Nicols <andrew.nicols@luns.net.uk>
Mon, 16 Apr 2012 14:27:10 +0000 (15:27 +0100)
committerAndrew Robert Nicols <andrew.nicols@luns.net.uk>
Tue, 24 Apr 2012 09:57:12 +0000 (10:57 +0100)
course/editcategory.php
course/lib.php
course/tests/courselib_test.php

index ff4bad5..97f791a 100644 (file)
@@ -114,11 +114,11 @@ if ($mform->is_cancelled()) {
     } else {
         // Create a new category.
         $newcategory->description = $data->description_editor['text'];
-        $newcategory->sortorder = 999;
-        $newcategory->id = $DB->insert_record('course_categories', $newcategory);
-        $newcategory->context = get_context_instance(CONTEXT_COURSECAT, $newcategory->id);
-        $categorycontext = $newcategory->context;
-        mark_context_dirty($newcategory->context->path);
+
+        // Don't overwrite the $newcategory object as it'll be processed by file_postupdate_standard_editor in a moment
+        $category = create_course_category($newcategory);
+        $newcategory->id = $category->id;
+        $categorycontext = $category->context;
     }
 
     $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext, 'coursecat', 'description', 0);
index 42edb6a..c838034 100644 (file)
@@ -3907,6 +3907,32 @@ function create_course($data, $editoroptions = NULL) {
     return $course;
 }
 
+/**
+ * Create a new course category and marks the context as dirty
+ *
+ * This function does not set the sortorder for the new category and
+ * @see{fix_course_sortorder} should be called after creating a new course
+ * category
+ *
+ * Please note that this function does not verify access control.
+ *
+ * @param object $category All of the data required for an entry in the course_categories table
+ * @return object new course category
+ */
+function create_course_category($category) {
+    global $DB;
+
+    $category->timemodified = time();
+    $category->id = $DB->insert_record('course_categories', $category);
+    $category = $DB->get_record('course_categories', array('id' => $category->id));
+
+    // We should mark the context as dirty
+    $category->context = context_coursecat::instance($category->id);
+    $category->context->mark_dirty();
+
+    return $category;
+}
+
 /**
  * Update a course.
  *
index cf0822b..f7f4f80 100644 (file)
@@ -96,4 +96,52 @@ class courselib_testcase extends advanced_testcase {
         $CFG->courselistshortnames = 1;
         $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
     }
+
+    public function test_create_course_category() {
+        global $CFG, $DB;
+        $this->resetAfterTest(true);
+
+        // Create the category
+        $data = new stdClass();
+        $data->name = 'aaa';
+        $data->description = 'aaa';
+        $data->idnumber = '';
+
+        $category1 = create_course_category($data);
+
+        // Initially confirm that base data was inserted correctly
+        $this->assertEquals($data->name, $category1->name);
+        $this->assertEquals($data->description, $category1->description);
+        $this->assertEquals($data->idnumber, $category1->idnumber);
+
+        // sortorder should be blank initially
+        $this->assertEmpty($category1->sortorder);
+
+        // Calling fix_course_sortorder() should provide a new sortorder
+        fix_course_sortorder();
+        $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
+
+        $this->assertGreaterThanOrEqual(1, $category1->sortorder);
+
+        // Create two more categories and test the sortorder worked correctly
+        $data->name = 'ccc';
+        $category2 = create_course_category($data);
+        $this->assertEmpty($category2->sortorder);
+
+        $data->name = 'bbb';
+        $category3 = create_course_category($data);
+        $this->assertEmpty($category3->sortorder);
+
+        // Calling fix_course_sortorder() should provide a new sortorder to give category1,
+        // category2, category3. New course categories are ordered by id not name
+        fix_course_sortorder();
+
+        $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
+        $category2 = $DB->get_record('course_categories', array('id' => $category2->id));
+        $category3 = $DB->get_record('course_categories', array('id' => $category3->id));
+
+        $this->assertGreaterThanOrEqual($category1->sortorder, $category2->sortorder);
+        $this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder);
+        $this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder);
+    }
 }