Skip to content

Commit

Permalink
MDL-41045 course_add_cm_to_section should not test for sections unles…
Browse files Browse the repository at this point in the history
…s necessary
  • Loading branch information
sammarshallou committed Aug 7, 2013
1 parent a9657ad commit 99aa447
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
15 changes: 13 additions & 2 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,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)
Expand All @@ -1347,9 +1351,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";
Expand Down
49 changes: 49 additions & 0 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,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);
Expand Down

0 comments on commit 99aa447

Please sign in to comment.