} 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);
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.
*
$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);
+ }
}