diff --git a/Changes.md b/Changes.md index ab73e655..482d1156 100644 --- a/Changes.md +++ b/Changes.md @@ -5,6 +5,7 @@ Version 401.2.1 - TBR 1. Fix 'Webp image causes an error and makes course unaccessible' - #190. 2. Fix "Hidden sections are shown as not available" doesn't seem to work. - #192. 3. Add 'Flex containers' justify-content property options' - #191. +4. Fix 'Problems with creating multiple courses from template via CSV in Grid Format' - #189. Version 401.2.0 - 16/08/2023 ---------------------------- diff --git a/backup/moodle2/restore_format_grid_plugin.class.php b/backup/moodle2/restore_format_grid_plugin.class.php index e91013df..c09b1b2b 100644 --- a/backup/moodle2/restore_format_grid_plugin.class.php +++ b/backup/moodle2/restore_format_grid_plugin.class.php @@ -197,10 +197,25 @@ public function process_gridsection($data) { $data = (object) $data; + $target = $this->step->get_task()->get_target(); + if (($target == backup::TARGET_NEW_COURSE) || + ($target == backup::TARGET_CURRENT_ADDING) || + ($target == backup::TARGET_CURRENT_DELETING) || + ($target == backup::TARGET_EXISTING_DELETING)) { + /* This ensures that when a course is created from an uploaded CSV file that the number of sections is correct. + Thus when an existing course or course file is used but the course restore code is not called. + Because the backup file / course being restored from has the correct 'sections', i.e. that will be in the + 'course_sections' table. */ + $courseid = $this->task->get_courseid(); + static $gnumsections = 0; + $gnumsections++; + // 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); + $courseformat->restore_gnumsections($gnumsections); + } /* Allow this to process even if not in the grid format so that our event observer on 'course_restored' can perform a clean up of restored grid image files after all the data is in place in the database for this to happen properly. */ - $target = $this->step->get_task()->get_target(); if (($target == backup::TARGET_NEW_COURSE) || ($target == backup::TARGET_CURRENT_DELETING) || ($target == backup::TARGET_EXISTING_DELETING) || diff --git a/lib.php b/lib.php index 5276aef3..7682d350 100755 --- a/lib.php +++ b/lib.php @@ -46,17 +46,18 @@ class format_grid extends core_courseformat\base { * @return format_grid */ protected function __construct($format, $courseid) { - parent::__construct($format, $courseid); if ($courseid === 0) { global $COURSE; $courseid = $COURSE->id; // Save lots of global $COURSE as we will never be the site course. } parent::__construct($format, $courseid); - $currentsettings = $this->get_settings(); - if (!empty($currentsettings['popup'])) { - if ($currentsettings['popup'] == 2) { - $this->coursedisplay = COURSE_DISPLAY_SINGLEPAGE; + if ($courseid != 1) { + $currentsettings = $this->get_settings(); + if (!empty($currentsettings['popup'])) { + if ($currentsettings['popup'] == 2) { + $this->coursedisplay = COURSE_DISPLAY_SINGLEPAGE; + } } } } @@ -309,7 +310,7 @@ public function course_format_options($foreditform = false) { $courseid = $this->get_courseid(); if ($courseid == 1) { // New course. $defaultnumsections = $courseconfig->numsections; - } else { // Existing course that may not have 'numsections' - see get_last_section(). + } else { // Existing course that may not have '(g)numsections' - see get_last_section(). global $DB; $defaultnumsections = $DB->get_field_sql('SELECT max(section) from {course_sections} WHERE course = ?', [$courseid]);