diff --git a/format.php b/format.php index 6e31aeed..1ade4f81 100755 --- a/format.php +++ b/format.php @@ -54,6 +54,21 @@ 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 not created, so we need to use the default course setting, + which could be zero but this will still work. */ + $numsections = get_config('moodlecourse', 'numsections'); + } + $format->set_gnumsections($numsections); + $courseformatoptions['gnumsections'] = $numsections; + $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 5276aef3..c6b15d68 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,16 +310,22 @@ 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(). + $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, @@ -367,6 +374,10 @@ 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', @@ -826,6 +837,18 @@ 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. */