Skip to content

Commit

Permalink
Work in progress #189.
Browse files Browse the repository at this point in the history
  • Loading branch information
gjb2048 committed Sep 27, 2023
1 parent f35f715 commit 19f2c6f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
15 changes: 15 additions & 0 deletions format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));

Expand Down
35 changes: 29 additions & 6 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 19f2c6f

Please sign in to comment.