diff --git a/course/format/topics/db/upgradelib.php b/course/format/topics/db/upgradelib.php index 7182ceadf31..e231cbcd2a0 100644 --- a/course/format/topics/db/upgradelib.php +++ b/course/format/topics/db/upgradelib.php @@ -26,9 +26,13 @@ defined('MOODLE_INTERNAL') || die(); /** * This method finds all courses in 'topics' format that have actual number of sections - * bigger than their 'numsections' course format option. - * For each such course we call {@link format_topics_upgrade_hide_extra_sections()} and - * either delete or hide "orphaned" sections. + * different than their 'numsections' course format option. + * + * For courses where there are more sections than numsections, we call + * {@link format_topics_upgrade_hide_extra_sections()} and + * either delete or hide "orphaned" sections. For courses where there are fewer sections + * than numsections, we call {@link format_topics_upgrade_add_empty_sections()} to add + * these sections. */ function format_topics_upgrade_remove_numsections() { global $DB; @@ -49,6 +53,7 @@ function format_topics_upgrade_remove_numsections() { $actual = $DB->get_records_sql_menu($sql1, $params); $numsections = $DB->get_records_sql_menu($sql2, $params); $needfixing = []; + $needsections = []; $defaultnumsections = get_config('moodlecourse', 'numsections'); @@ -60,6 +65,8 @@ function format_topics_upgrade_remove_numsections() { } if ($sectionsactual > $n) { $needfixing[$courseid] = $n; + } else if ($sectionsactual < $n) { + $needsections[$courseid] = $n; } } unset($actual); @@ -69,6 +76,10 @@ function format_topics_upgrade_remove_numsections() { format_topics_upgrade_hide_extra_sections($courseid, $numsections); } + foreach ($needsections as $courseid => $numsections) { + format_topics_upgrade_add_empty_sections($courseid, $numsections); + } + $DB->delete_records('course_format_options', ['format' => 'topics', 'sectionid' => 0, 'name' => 'numsections']); } @@ -115,3 +126,19 @@ function format_topics_upgrade_hide_extra_sections($courseid, $numsections) { $DB->execute("UPDATE {course_sections} SET visible = 0 WHERE id " . $sql, $params); } } + +/** + * This method adds empty sections to courses which have fewer sections than their + * 'numsections' course format option and adds these empty sections. + * + * @param int $courseid + * @param int $numsections + */ +function format_topics_upgrade_add_empty_sections($courseid, $numsections) { + global $DB; + $existingsections = $DB->get_fieldset_sql('SELECT section from {course_sections} WHERE course = ?', [$courseid]); + $newsections = array_diff(range(0, $numsections), $existingsections); + foreach ($newsections as $sectionnum) { + course_create_section($courseid, $sectionnum, true); + } +} diff --git a/course/format/topics/tests/format_topics_upgrade_test.php b/course/format/topics/tests/format_topics_upgrade_test.php index e32c67dc39d..690c71589ea 100644 --- a/course/format/topics/tests/format_topics_upgrade_test.php +++ b/course/format/topics/tests/format_topics_upgrade_test.php @@ -125,4 +125,29 @@ class format_topics_upgrade_testcase extends advanced_testcase { // The module is still visible. $this->assertEquals(1, $DB->get_field('course_modules', 'visible', ['id' => $cm->cmid])); } + + /** + * Test upgrade step to add empty sections. + */ + public function test_numsections_add_empty_sections() { + global $DB; + + $this->resetAfterTest(true); + + $params = array('format' => 'topics', 'numsections' => 16, 'startdate' => 1445644800); + $course = $this->getDataGenerator()->create_course($params); + + // This test is executed after 'numsections' option was already removed. + // Set the 'numsections' course format value to 18, simulating the scenario in which there are fewer real sections. + $DB->insert_record('course_format_options', ['courseid' => $course->id, 'format' => 'topics', + 'sectionid' => 0, 'name' => 'numsections', 'value' => '18']); + + // There are 16 sections. + $this->assertEquals(17, $DB->count_records('course_sections', ['course' => $course->id])); + + format_topics_upgrade_remove_numsections(); + + // Confirm that the upgrade method added the missing empty sections. + $this->assertEquals(19, $DB->count_records('course_sections', ['course' => $course->id])); + } } diff --git a/course/format/weeks/db/upgradelib.php b/course/format/weeks/db/upgradelib.php index 26571f1a56f..2c5a5b1225f 100644 --- a/course/format/weeks/db/upgradelib.php +++ b/course/format/weeks/db/upgradelib.php @@ -25,10 +25,14 @@ defined('MOODLE_INTERNAL') || die(); /** - * This method finds all courses in 'weeks' format that have actual number of sections - * bigger than their 'numsections' course format option. - * For each such course we call {@link format_weeks_upgrade_hide_extra_sections()} and - * either delete or hide "orphaned" sections. + * This method finds all courses in 'topics' format that have actual number of sections + * different than their 'numsections' course format option. + * + * For courses where there are more sections than numsections, we call + * {@link format_weeks_upgrade_hide_extra_sections()} and + * either delete or hide "orphaned" sections. For courses where there are fewer sections + * than numsections, we call {@link format_weeks_upgrade_add_empty_sections()} to add + * these sections. */ function format_weeks_upgrade_remove_numsections() { global $DB; @@ -49,6 +53,7 @@ function format_weeks_upgrade_remove_numsections() { $actual = $DB->get_records_sql_menu($sql1, $params); $numsections = $DB->get_records_sql_menu($sql2, $params); $needfixing = []; + $needsections = []; $defaultnumsections = get_config('moodlecourse', 'numsections'); @@ -60,6 +65,8 @@ function format_weeks_upgrade_remove_numsections() { } if ($sectionsactual > $n) { $needfixing[$courseid] = $n; + } else if ($sectionsactual < $n) { + $needsections[$courseid] = $n; } } unset($actual); @@ -69,6 +76,10 @@ function format_weeks_upgrade_remove_numsections() { format_weeks_upgrade_hide_extra_sections($courseid, $numsections); } + foreach ($needsections as $courseid => $numsections) { + format_weeks_upgrade_add_empty_sections($courseid, $numsections); + } + $DB->delete_records('course_format_options', ['format' => 'weeks', 'sectionid' => 0, 'name' => 'numsections']); } @@ -115,3 +126,19 @@ function format_weeks_upgrade_hide_extra_sections($courseid, $numsections) { $DB->execute("UPDATE {course_sections} SET visible = 0 WHERE id " . $sql, $params); } } + +/** + * This method adds empty sections to courses which have fewer sections than their + * 'numsections' course format option and adds these empty sections. + * + * @param int $courseid + * @param int $numsections + */ +function format_weeks_upgrade_add_empty_sections($courseid, $numsections) { + global $DB; + $existingsections = $DB->get_fieldset_sql('SELECT section from {course_sections} WHERE course = ?', [$courseid]); + $newsections = array_diff(range(0, $numsections), $existingsections); + foreach ($newsections as $sectionnum) { + course_create_section($courseid, $sectionnum, true); + } +} diff --git a/course/format/weeks/tests/format_weeks_upgrade_test.php b/course/format/weeks/tests/format_weeks_upgrade_test.php index e44cc5a204f..3cc8f153009 100644 --- a/course/format/weeks/tests/format_weeks_upgrade_test.php +++ b/course/format/weeks/tests/format_weeks_upgrade_test.php @@ -125,4 +125,29 @@ class format_weeks_upgrade_testcase extends advanced_testcase { // The module is still visible. $this->assertEquals(1, $DB->get_field('course_modules', 'visible', ['id' => $cm->cmid])); } + + /** + * Test upgrade step to add empty sections. + */ + public function test_numsections_add_empty_sections() { + global $DB; + + $this->resetAfterTest(true); + + $params = array('format' => 'weeks', 'numsections' => 16, 'startdate' => 1445644800); + $course = $this->getDataGenerator()->create_course($params); + + // This test is executed after 'numsections' option was already removed. + // Set the 'numsections' course format value to 18, simulating the scenario in which there are fewer real sections. + $DB->insert_record('course_format_options', ['courseid' => $course->id, 'format' => 'weeks', + 'sectionid' => 0, 'name' => 'numsections', 'value' => '18']); + + // There are 16 sections. + $this->assertEquals(17, $DB->count_records('course_sections', ['course' => $course->id])); + + format_weeks_upgrade_remove_numsections(); + + // Confirm that the upgrade method added the missing empty sections. + $this->assertEquals(19, $DB->count_records('course_sections', ['course' => $course->id])); + } }