mirror of
https://github.com/moodle/moodle.git
synced 2025-08-07 01:46:45 +02:00
Merge branch 'MDL-62534-master' of https://github.com/mackensen/moodle
This commit is contained in:
commit
e9f1946ca1
4 changed files with 111 additions and 7 deletions
|
@ -26,9 +26,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method finds all courses in 'topics' format that have actual number of sections
|
* This method finds all courses in 'topics' format that have actual number of sections
|
||||||
* bigger than their 'numsections' course format option.
|
* different 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.
|
* 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() {
|
function format_topics_upgrade_remove_numsections() {
|
||||||
global $DB;
|
global $DB;
|
||||||
|
@ -49,6 +53,7 @@ function format_topics_upgrade_remove_numsections() {
|
||||||
$actual = $DB->get_records_sql_menu($sql1, $params);
|
$actual = $DB->get_records_sql_menu($sql1, $params);
|
||||||
$numsections = $DB->get_records_sql_menu($sql2, $params);
|
$numsections = $DB->get_records_sql_menu($sql2, $params);
|
||||||
$needfixing = [];
|
$needfixing = [];
|
||||||
|
$needsections = [];
|
||||||
|
|
||||||
$defaultnumsections = get_config('moodlecourse', 'numsections');
|
$defaultnumsections = get_config('moodlecourse', 'numsections');
|
||||||
|
|
||||||
|
@ -60,6 +65,8 @@ function format_topics_upgrade_remove_numsections() {
|
||||||
}
|
}
|
||||||
if ($sectionsactual > $n) {
|
if ($sectionsactual > $n) {
|
||||||
$needfixing[$courseid] = $n;
|
$needfixing[$courseid] = $n;
|
||||||
|
} else if ($sectionsactual < $n) {
|
||||||
|
$needsections[$courseid] = $n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unset($actual);
|
unset($actual);
|
||||||
|
@ -69,6 +76,10 @@ function format_topics_upgrade_remove_numsections() {
|
||||||
format_topics_upgrade_hide_extra_sections($courseid, $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']);
|
$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);
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -125,4 +125,29 @@ class format_topics_upgrade_testcase extends advanced_testcase {
|
||||||
// The module is still visible.
|
// The module is still visible.
|
||||||
$this->assertEquals(1, $DB->get_field('course_modules', 'visible', ['id' => $cm->cmid]));
|
$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]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,10 +25,14 @@
|
||||||
defined('MOODLE_INTERNAL') || die();
|
defined('MOODLE_INTERNAL') || die();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method finds all courses in 'weeks' format that have actual number of sections
|
* This method finds all courses in 'topics' format that have actual number of sections
|
||||||
* bigger than their 'numsections' course format option.
|
* different 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.
|
* 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() {
|
function format_weeks_upgrade_remove_numsections() {
|
||||||
global $DB;
|
global $DB;
|
||||||
|
@ -49,6 +53,7 @@ function format_weeks_upgrade_remove_numsections() {
|
||||||
$actual = $DB->get_records_sql_menu($sql1, $params);
|
$actual = $DB->get_records_sql_menu($sql1, $params);
|
||||||
$numsections = $DB->get_records_sql_menu($sql2, $params);
|
$numsections = $DB->get_records_sql_menu($sql2, $params);
|
||||||
$needfixing = [];
|
$needfixing = [];
|
||||||
|
$needsections = [];
|
||||||
|
|
||||||
$defaultnumsections = get_config('moodlecourse', 'numsections');
|
$defaultnumsections = get_config('moodlecourse', 'numsections');
|
||||||
|
|
||||||
|
@ -60,6 +65,8 @@ function format_weeks_upgrade_remove_numsections() {
|
||||||
}
|
}
|
||||||
if ($sectionsactual > $n) {
|
if ($sectionsactual > $n) {
|
||||||
$needfixing[$courseid] = $n;
|
$needfixing[$courseid] = $n;
|
||||||
|
} else if ($sectionsactual < $n) {
|
||||||
|
$needsections[$courseid] = $n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unset($actual);
|
unset($actual);
|
||||||
|
@ -69,6 +76,10 @@ function format_weeks_upgrade_remove_numsections() {
|
||||||
format_weeks_upgrade_hide_extra_sections($courseid, $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']);
|
$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);
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -125,4 +125,29 @@ class format_weeks_upgrade_testcase extends advanced_testcase {
|
||||||
// The module is still visible.
|
// The module is still visible.
|
||||||
$this->assertEquals(1, $DB->get_field('course_modules', 'visible', ['id' => $cm->cmid]));
|
$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]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue