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/format.php b/format.php index c537c9b1..6e31aeed 100755 --- a/format.php +++ b/format.php @@ -54,19 +54,6 @@ course_set_marker($course->id, $marker); } -if ($courseformatoptions['gnumsectionsnewcourse'] == 1) { - // A new course that may have sections but does not know how many. - global $DB; - $numsections = $DB->get_field_sql('SELECT max(section) from {course_sections} - WHERE course = ?', [$course->id]); - if (!empty($numsections)) { - // Sections created, so we need set gnumsections to this. - $courseformatoptions['gnumsections'] = $numsections; - } // Else remain the same. - $format->set_gnumsections($courseformatoptions['gnumsections']); - $courseformatoptions['gnumsectionsnewcourse'] = 0; -} - // Make sure all sections are created. course_create_sections_if_missing($course, range(0, $courseformatoptions['gnumsections'])); diff --git a/lib.php b/lib.php index c6b15d68..7682d350 100755 --- a/lib.php +++ b/lib.php @@ -310,22 +310,16 @@ public function course_format_options($foreditform = false) { $courseid = $this->get_courseid(); if ($courseid == 1) { // New course. $defaultnumsections = $courseconfig->numsections; - $defaultgnumsectionsnewcourse = 1; } 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]); - $defaultgnumsectionsnewcourse = 0; } $courseformatoptions = [ 'gnumsections' => [ 'default' => $defaultnumsections, 'type' => PARAM_INT, ], - 'gnumsectionsnewcourse' => [ - 'default' => $defaultgnumsectionsnewcourse, - 'type' => PARAM_INT, - ], 'hiddensections' => [ 'default' => $courseconfig->hiddensections, 'type' => PARAM_INT, @@ -374,10 +368,6 @@ public function course_format_options($foreditform = false) { 'element_type' => 'select', 'element_attributes' => [$sectionmenu], ], - 'gnumsectionsnewcourse' => [ - 'label' => 0, - 'element_type' => 'hidden', - ], 'hiddensections' => [ 'label' => new lang_string('hiddensections'), 'help' => 'hiddensections', @@ -837,18 +827,6 @@ public function restore_gnumsections($numsections) { $this->update_course_format_options($data); } - /** - * Sets the gnumsections correctly for a new course. - * @param int $numsections The number of sections. - */ - public function set_gnumsections($numsections) { - $data = [ - 'gnumsections' => $numsections, - 'gnumsectionsnewcourse' => 0, - ]; - $this->update_course_format_options($data); - } - /** * A section has been added. Should only be called from the state actions instance. */