MDL-72991 Course: Fix move_section_to is not updating correct section

When we change the position of two sections, we just need to update
the position of the affected sections, not all the sections in the course.
This will improve the performance since the system only executes the queries to affected sections.
Also, the system only clears the cache for affected sections, not all the sections in the course.
This commit is contained in:
Huong Nguyen
2022-03-15 10:37:55 +07:00
parent 6d7dbaa685
commit 1f85802f86
2 changed files with 30 additions and 2 deletions
+2 -2
View File
@@ -1378,12 +1378,12 @@ function move_section_to($course, $section, $destination, $ignorenumsections = f
// uniqueness constraint
$transaction = $DB->start_delegated_transaction();
foreach ($movedsections as $id => $position) {
if ($sections[$id] !== $position) {
if ((int) $sections[$id] !== $position) {
$DB->set_field('course_sections', 'section', -$position, array('id' => $id));
}
}
foreach ($movedsections as $id => $position) {
if ($sections[$id] !== $position) {
if ((int) $sections[$id] !== $position) {
$DB->set_field('course_sections', 'section', $position, array('id' => $id));
}
}
+28
View File
@@ -996,6 +996,34 @@ class core_course_courselib_testcase extends advanced_testcase {
$this->assertEquals(3, $course->marker);
}
/**
* Test move_section_to method.
* Make sure that we only update the moving sections, not all the sections in the current course.
*
* @return void
*/
public function test_move_section_to() {
global $DB, $CFG;
$this->resetAfterTest();
$this->setAdminUser();
// Generate the course and pre-requisite module.
$course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 3], ['createsections' => true]);
ob_start();
$DB->set_debug(true);
// Move section.
move_section_to($course, 2, 3);
$DB->set_debug(false);
$debuginfo = ob_get_contents();
ob_end_clean();
$sectionmovequerycount = substr_count($debuginfo, 'UPDATE ' . $CFG->phpunit_prefix . 'course_sections SET');
// We are updating the course_section table in steps to avoid breaking database uniqueness constraint.
// So the queries will be doubled. See: course/lib.php:1423
// Make sure that we only need 4 queries to update the position of section 2 and section 3.
$this->assertEquals(4, $sectionmovequerycount);
}
public function test_course_can_delete_section() {
global $DB;
$this->resetAfterTest(true);