Skip to content

Commit

Permalink
MDL-62534 course formats: do not purge empty sections on upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
mackensen committed Aug 20, 2018
1 parent 94409c6 commit 93c174c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 7 deletions.
33 changes: 30 additions & 3 deletions course/format/topics/db/upgradelib.php
Expand Up @@ -26,9 +26,13 @@

/**
* 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;
Expand All @@ -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');

Expand All @@ -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);
Expand All @@ -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']);
}

Expand Down Expand Up @@ -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);
}
}
25 changes: 25 additions & 0 deletions course/format/topics/tests/format_topics_upgrade_test.php
Expand Up @@ -125,4 +125,29 @@ public function test_numsections_hide_non_empty() {
// 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]));
}
}
35 changes: 31 additions & 4 deletions course/format/weeks/db/upgradelib.php
Expand Up @@ -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;
Expand All @@ -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');

Expand All @@ -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);
Expand All @@ -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']);
}

Expand Down Expand Up @@ -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);
}
}
25 changes: 25 additions & 0 deletions course/format/weeks/tests/format_weeks_upgrade_test.php
Expand Up @@ -125,4 +125,29 @@ public function test_numsections_hide_non_empty() {
// 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]));
}
}

0 comments on commit 93c174c

Please sign in to comment.