From 7504fed97afe2323272cc304e2bdfa093b432ded Mon Sep 17 00:00:00 2001 From: Gareth Barnard <1058419+gjb2048@users.noreply.github.com> Date: Sun, 24 Mar 2024 18:07:24 +0000 Subject: [PATCH] Improved solution to 'Problems with creating multiple courses from template via CSV in Grid Format' - #189, related to #203. --- Changes.md | 1 + backup/moodle2/restore_format_grid_plugin.class.php | 12 +++++++++--- format.php | 7 +++++++ lang/en/format_grid.php | 2 +- lib.php | 12 +++++++++++- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Changes.md b/Changes.md index c63736fc..09cdf9f3 100644 --- a/Changes.md +++ b/Changes.md @@ -6,6 +6,7 @@ Version 403.1.2 - TBR 1. Fix 'Orphaned Section still appear in Index Drawer'. 2. Fix 'Restrict access can cause no content in other sections to be shown.' - #202. 3. Fix 'Import quiz error + Number of topics reduced to 1' - #203. +4. Improved solution to 'Problems with creating multiple courses from template via CSV in Grid Format' - #189, related to #203. Version 403.1.1 - 22/01/2024 ---------------------------- diff --git a/backup/moodle2/restore_format_grid_plugin.class.php b/backup/moodle2/restore_format_grid_plugin.class.php index 0faf7691..9a88a93c 100644 --- a/backup/moodle2/restore_format_grid_plugin.class.php +++ b/backup/moodle2/restore_format_grid_plugin.class.php @@ -187,6 +187,8 @@ public function process_gridsection($data) { $target = $task->get_target(); if ( ($target == backup::TARGET_NEW_COURSE) || + // TARGET_CURRENT_ADDING used by both import and CSV course creation with template course, go figure! + ($target == backup::TARGET_CURRENT_ADDING) || ($target == backup::TARGET_CURRENT_DELETING) || ($target == backup::TARGET_EXISTING_DELETING) ) { @@ -199,9 +201,13 @@ public function process_gridsection($data) { // We don't know how many more sections there is and also don't know if this is the last. $courseformat = course_get_format($courseid); if ($courseformat->get_format() == 'grid') { - static $gnumsections = 0; - $gnumsections++; - $courseformat->restore_gnumsections($gnumsections); + // Not calling the format's 'restore_gnumsections' as '-1' fails validation. + $DB->set_field('course_format_options', 'value', -1, + [ + 'courseid' => $courseid, + 'name' => 'gnumsections', + ] + ); } } /* Allow this to process even if not in the grid format so that our event observer on 'course_restored' diff --git a/format.php b/format.php index 23c700e4..899bfcc6 100755 --- a/format.php +++ b/format.php @@ -54,6 +54,13 @@ course_set_marker($course->id, $marker); } +if ($courseformatoptions['gnumsections'] < 0) { + // Repair if number of sections is unknown on a restore. + $numberofsections = $format->get_last_section_number(); + $format->restore_gnumsections($numberofsections); + $courseformatoptions['gnumsections'] = $numberofsections; +} + // Make sure all sections are created. course_create_sections_if_missing($course, range(0, $courseformatoptions['gnumsections'])); diff --git a/lang/en/format_grid.php b/lang/en/format_grid.php index 8f197628..5af93c5f 100644 --- a/lang/en/format_grid.php +++ b/lang/en/format_grid.php @@ -140,7 +140,7 @@ $string['settings'] = 'Settings'; $string['settingssettings'] = 'Settings settings'; $string['settingssettingsdesc'] = 'Grid format settings'; -$string['stealthwarning'] = 'Warning: Course has {$a} orphaned section(s) with content.'; +$string['stealthwarning'] = 'Warning: Course has {$a} orphaned section(s) with content. Resolve as soon as possible. Note: Importing into this course from another will turn orphaned sections into real ones - there is no current solution to this!'; $string['love'] = 'love'; $string['versioninfo'] = 'Release {$a->release}, version {$a->version} on Moodle {$a->moodle}. Made with {$a->love} in Great Britain.'; $string['versionalpha'] = 'Alpha version - Almost certainly contains bugs. This is a development version for developers \'only\'! Don\'t even think of installing on a production server!'; diff --git a/lib.php b/lib.php index 055c0179..308ca0fc 100755 --- a/lib.php +++ b/lib.php @@ -114,7 +114,9 @@ public function get_settings($invalidate = false) { public function get_last_section_number() { $course = $this->get_course(); if (isset($course->gnumsections)) { - return $course->gnumsections; + if ($course->gnumsections >= 0) { + return $course->gnumsections; + } } return parent::get_last_section_number(); @@ -593,6 +595,14 @@ public function create_edit_form_elements(&$mform, $forsection = false) { $maxsections = get_config('moodlecourse', 'maxsections'); $numsections = $mform->getElementValue('gnumsections'); $numsections = $numsections[0]; + if ($numsections < 0) { + $numsections = $this->get_last_section_number(); + /* Instead of setValue on the element as the default gets reused when the form is re-arranged by + 'definition_after_data' in '/course/edit_form.php', specifically the calls to 'insertElementBefore' + after this method was called. */ + $mform->setDefault('gnumsections', $numsections); + $this->restore_gnumsections($numsections); + } if ($numsections > $maxsections) { $element = $mform->getElement('gnumsections'); for ($i = $maxsections + 1; $i <= $numsections; $i++) {