From b0997b27fcd57cbbbb9c7957c32c8d4281b858c8 Mon Sep 17 00:00:00 2001 From: Gareth J Barnard Date: Mon, 3 Jun 2013 23:57:52 +0100 Subject: [PATCH] MDL-39764 course_format: Allow courses to have numsections > maxsections. --- course/format/topics/lib.php | 32 ++++++++++++++++++++++++++++++++ course/format/weeks/lib.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/course/format/topics/lib.php b/course/format/topics/lib.php index debe045691d..480d2c83dce 100644 --- a/course/format/topics/lib.php +++ b/course/format/topics/lib.php @@ -257,6 +257,38 @@ class format_topics extends format_base { return $courseformatoptions; } + /** + * Adds format options elements to the course/section edit form. + * + * This function is called from {@link course_edit_form::definition_after_data()}. + * + * @param MoodleQuickForm $mform form the elements are added to. + * @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form. + * @return array array of references to the added form elements. + */ + public function create_edit_form_elements(&$mform, $forsection = false) { + $elements = parent::create_edit_form_elements($mform, $forsection); + /* + * Increase the number of sections combo box values if the user has increased the number of sections + * using the icon on the course page beyond course 'maxsections' or course 'maxsections' has been + * reduced below the number of sections already set for the course on the site administration course + * defaults page. This is so that the number of sections is not reduced leaving unintended orphaned + * activities / resources. + */ + if (!$forsection) { + $maxsections = get_config('moodlecourse', 'maxsections'); + $numsections = $mform->getElementValue('numsections'); + $numsections = $numsections[0]; + if ($numsections > $maxsections) { + $element = $mform->getElement('numsections'); + for ($i = $maxsections+1; $i <= $numsections; $i++) { + $element->addOption("$i", $i); + } + } + } + return $elements; + } + /** * Updates format options for a course * diff --git a/course/format/weeks/lib.php b/course/format/weeks/lib.php index 40686d4d419..0c5edfa74c8 100644 --- a/course/format/weeks/lib.php +++ b/course/format/weeks/lib.php @@ -262,6 +262,38 @@ class format_weeks extends format_base { return $courseformatoptions; } + /** + * Adds format options elements to the course/section edit form. + * + * This function is called from {@link course_edit_form::definition_after_data()}. + * + * @param MoodleQuickForm $mform form the elements are added to. + * @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form. + * @return array array of references to the added form elements. + */ + public function create_edit_form_elements(&$mform, $forsection = false) { + $elements = parent::create_edit_form_elements($mform, $forsection); + /* + * Increase the number of sections combo box values if the user has increased the number of sections + * using the icon on the course page beyond course 'maxsections' or course 'maxsections' has been + * reduced below the number of sections already set for the course on the site administration course + * defaults page. This is so that the number of sections is not reduced leaving unintended orphaned + * activities / resources. + */ + if (!$forsection) { + $maxsections = get_config('moodlecourse', 'maxsections'); + $numsections = $mform->getElementValue('numsections'); + $numsections = $numsections[0]; + if ($numsections > $maxsections) { + $element = $mform->getElement('numsections'); + for ($i = $maxsections+1; $i <= $numsections; $i++) { + $element->addOption("$i", $i); + } + } + } + return $elements; + } + /** * Updates format options for a course *