Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-29044: Move course category creation to a separate function

  • Loading branch information...
commit b1a8aa73b512466a5876c71edf84b03f709b79d8 1 parent ead4f18
@andrewnicols andrewnicols authored
View
10 course/editcategory.php
@@ -114,11 +114,11 @@
} 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);
View
26 course/lib.php
@@ -3908,6 +3908,32 @@ function create_course($data, $editoroptions = NULL) {
}
/**
+ * 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.
*
* Please note this functions does not verify any access control,
View
48 course/tests/courselib_test.php
@@ -96,4 +96,52 @@ public function test_get_course_display_name_for_list() {
$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);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.