Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-41045 course_add_cm_to_section should not test for sections unles…

…s necessary
  • Loading branch information...
commit 1a60cd4e5c0619ef3505f9d439f8effd109b446b 1 parent f5dd4b4
@sammarshallou sammarshallou authored
Showing with 62 additions and 2 deletions.
  1. +13 −2 course/lib.php
  2. +49 −0 course/tests/courselib_test.php
View
15 course/lib.php
@@ -2798,6 +2798,10 @@ function course_create_sections_if_missing($courseorid, $sections) {
*
* Updates both tables {course_sections} and {course_modules}
*
+ * Note: This function does not use modinfo PROVIDED that the section you are
+ * adding the module to already exists. If the section does not exist, it will
+ * build modinfo if necessary and create the section.
+ *
* @param int|stdClass $courseorid course id or course object
* @param int $cmid id of the module already existing in course_modules table
* @param int $sectionnum relative number of the section (field course_sections.section)
@@ -2817,9 +2821,16 @@ function course_add_cm_to_section($courseorid, $cmid, $sectionnum, $beforemod =
} else {
$courseid = $courseorid;
}
- course_create_sections_if_missing($courseorid, $sectionnum);
// Do not try to use modinfo here, there is no guarantee it is valid!
- $section = $DB->get_record('course_sections', array('course'=>$courseid, 'section'=>$sectionnum), '*', MUST_EXIST);
+ $section = $DB->get_record('course_sections',
+ array('course' => $courseid, 'section' => $sectionnum), '*', IGNORE_MISSING);
+ if (!$section) {
+ // This function call requires modinfo.
+ course_create_sections_if_missing($courseorid, $sectionnum);
+ $section = $DB->get_record('course_sections',
+ array('course' => $courseid, 'section' => $sectionnum), '*', MUST_EXIST);
+ }
+
$modarray = explode(",", trim($section->sequence));
if (empty($section->sequence)) {
$newsequence = "$cmid";
View
49 course/tests/courselib_test.php
@@ -97,6 +97,55 @@ public function test_create_course_sections() {
$this->assertEquals(range(0, $course->numsections + 1), $sectionscreated);
}
+ public function test_course_add_cm_to_section() {
+ global $DB;
+ $this->resetAfterTest(true);
+
+ // Create course with 1 section.
+ $course = $this->getDataGenerator()->create_course(
+ array('shortname' => 'GrowingCourse',
+ 'fullname' => 'Growing Course',
+ 'numsections' => 1),
+ array('createsections' => true));
+
+ // Trash modinfo.
+ rebuild_course_cache($course->id, true);
+
+ // Create some cms for testing.
+ $cmids = array();
+ for ($i=0; $i<4; $i++) {
+ $cmids[$i] = $DB->insert_record('course_modules', array('course' => $course->id));
+ }
+
+ // Add it to section that exists.
+ course_add_cm_to_section($course, $cmids[0], 1);
+
+ // Check it got added to sequence.
+ $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
+ $this->assertEquals($cmids[0], $sequence);
+
+ // Add a second, this time using courseid variant of parameters.
+ course_add_cm_to_section($course->id, $cmids[1], 1);
+ $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
+ $this->assertEquals($cmids[0] . ',' . $cmids[1], $sequence);
+
+ // Check modinfo was not rebuilt (important for performance if calling
+ // repeatedly).
+ $this->assertNull($DB->get_field('course', 'modinfo', array('id' => $course->id)));
+
+ // Add one to section that doesn't exist (this might rebuild modinfo).
+ course_add_cm_to_section($course, $cmids[2], 2);
+ $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
+ $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
+ $this->assertEquals($cmids[2], $sequence);
+
+ // Add using the 'before' option.
+ course_add_cm_to_section($course, $cmids[3], 2, $cmids[2]);
+ $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
+ $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
+ $this->assertEquals($cmids[3] . ',' . $cmids[2], $sequence);
+ }
+
public function test_reorder_sections() {
global $DB;
$this->resetAfterTest(true);
Please sign in to comment.
Something went wrong with that request. Please try again.