MDL-62748 course: maxsections is a limit
authorDamyon Wiese <damyon@moodle.com>
Mon, 16 Jul 2018 07:58:49 +0000 (15:58 +0800)
committerJenkins <jenkins@worker02.test.in.moodle.com>
Tue, 4 Sep 2018 20:44:45 +0000 (22:44 +0200)
For course formats shipped with core, maxsections should be a limit to the number of sections
allowed in the course.

course/amd/build/actions.min.js
course/amd/src/actions.js
course/changenumsections.php
course/format/lib.php
course/format/renderer.php
lang/en/moodle.php

index 904a587..2875e39 100644 (file)
Binary files a/course/amd/build/actions.min.js and b/course/amd/build/actions.min.js differ
index 9a2a3c7..76d4232 100644 (file)
@@ -588,9 +588,10 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
                 // Add a handler for "Add sections" link to ask for a number of sections to add.
                 str.get_string('numberweeks').done(function(strNumberSections) {
                     var trigger = $(SELECTOR.ADDSECTIONS),
                 // Add a handler for "Add sections" link to ask for a number of sections to add.
                 str.get_string('numberweeks').done(function(strNumberSections) {
                     var trigger = $(SELECTOR.ADDSECTIONS),
-                        modalTitle = trigger.attr('data-add-sections');
+                        modalTitle = trigger.attr('data-add-sections'),
+                        newSections = trigger.attr('new-sections');
                     var modalBody = $('<div><label for="add_section_numsections"></label> ' +
                     var modalBody = $('<div><label for="add_section_numsections"></label> ' +
-                        '<input id="add_section_numsections" type="number" min="1" value="1"></div>');
+                        '<input id="add_section_numsections" type="number" min="1" max="' + newSections + '" value="1"></div>');
                     modalBody.find('label').html(strNumberSections);
                     ModalFactory.create({
                         title: modalTitle,
                     modalBody.find('label').html(strNumberSections);
                     ModalFactory.create({
                         title: modalTitle,
index c3d2a55..8f6ea28 100644 (file)
@@ -45,6 +45,30 @@ require_login($course);
 require_capability('moodle/course:update', context_course::instance($course->id));
 require_sesskey();
 
 require_capability('moodle/course:update', context_course::instance($course->id));
 require_sesskey();
 
+$desirednumsections = 0;
+$courseformat = course_get_format($course);
+$lastsectionnumber = $courseformat->get_last_section_number();
+$maxsections = $courseformat->get_max_sections();
+
+if (isset($courseformatoptions['numsections']) && $increase !== null) {
+    $desirednumsections = $courseformatoptions['numsections'] + 1;
+} else if (course_get_format($course)->uses_sections() && $insertsection !== null) {
+    // Count the sections in the course.
+    $desirednumsections = $lastsectionnumber + $numsections;
+}
+
+if ($desirednumsections > $maxsections) {
+    // Increase in number of sections is not allowed.
+    \core\notification::warning(get_string('maxsectionslimit', 'moodle', $maxsections));
+    $increase = null;
+    $insertsection = null;
+    $numsections = 0;
+
+    if (!$returnurl) {
+        $returnurl = course_get_url($course);
+    }
+}
+
 if (isset($courseformatoptions['numsections']) && $increase !== null) {
     if ($increase) {
         // Add an additional section.
 if (isset($courseformatoptions['numsections']) && $increase !== null) {
     if ($increase) {
         // Add an additional section.
index f70371c..0ba58c6 100644 (file)
@@ -287,6 +287,18 @@ abstract class format_base {
         return (int)max(array_keys($sections));
     }
 
         return (int)max(array_keys($sections));
     }
 
+    /**
+     * Method used to get the maximum number of sections for this course format.
+     * @return int
+     */
+    public function get_max_sections() {
+        $maxsections = get_config('moodlecourse', 'maxsections');
+        if (!isset($maxsections) || !is_numeric($maxsections)) {
+            $maxsections = 52;
+        }
+        return $maxsections;
+    }
+
     /**
      * Returns true if the course has a front page.
      *
     /**
      * Returns true if the course has a front page.
      *
index 357c0a3..45ff162 100644 (file)
@@ -952,7 +952,10 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
             return '';
         }
 
             return '';
         }
 
-        $options = course_get_format($course)->get_format_options();
+        $format = course_get_format($course);
+        $options = $format->get_format_options();
+        $maxsections = $format->get_max_sections();
+        $lastsection = $format->get_last_section_number();
         $supportsnumsections = array_key_exists('numsections', $options);
 
         if ($supportsnumsections) {
         $supportsnumsections = array_key_exists('numsections', $options);
 
         if ($supportsnumsections) {
@@ -963,13 +966,15 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
             echo html_writer::start_tag('div', array('id' => 'changenumsections', 'class' => 'mdl-right'));
 
             // Increase number of sections.
             echo html_writer::start_tag('div', array('id' => 'changenumsections', 'class' => 'mdl-right'));
 
             // Increase number of sections.
-            $straddsection = get_string('increasesections', 'moodle');
-            $url = new moodle_url('/course/changenumsections.php',
-                array('courseid' => $course->id,
-                      'increase' => true,
-                      'sesskey' => sesskey()));
-            $icon = $this->output->pix_icon('t/switch_plus', $straddsection);
-            echo html_writer::link($url, $icon.get_accesshide($straddsection), array('class' => 'increase-sections'));
+            if ($lastsection < $maxsections) {
+                $straddsection = get_string('increasesections', 'moodle');
+                $url = new moodle_url('/course/changenumsections.php',
+                    array('courseid' => $course->id,
+                          'increase' => true,
+                          'sesskey' => sesskey()));
+                $icon = $this->output->pix_icon('t/switch_plus', $straddsection);
+                echo html_writer::link($url, $icon.get_accesshide($straddsection), array('class' => 'increase-sections'));
+            }
 
             if ($course->numsections > 0) {
                 // Reduce number of sections sections.
 
             if ($course->numsections > 0) {
                 // Reduce number of sections sections.
@@ -985,11 +990,14 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
             echo html_writer::end_tag('div');
 
         } else if (course_get_format($course)->uses_sections()) {
             echo html_writer::end_tag('div');
 
         } else if (course_get_format($course)->uses_sections()) {
+            if ($lastsection >= $maxsections) {
+                // Don't allow more sections if we already hit the limit.
+                return;
+            }
             // Current course format does not have 'numsections' option but it has multiple sections suppport.
             // Display the "Add section" link that will insert a section in the end.
             // Note to course format developers: inserting sections in the other positions should check both
             // capabilities 'moodle/course:update' and 'moodle/course:movesections'.
             // Current course format does not have 'numsections' option but it has multiple sections suppport.
             // Display the "Add section" link that will insert a section in the end.
             // Note to course format developers: inserting sections in the other positions should check both
             // capabilities 'moodle/course:update' and 'moodle/course:movesections'.
-
             echo html_writer::start_tag('div', array('id' => 'changenumsections', 'class' => 'mdl-right'));
             if (get_string_manager()->string_exists('addsections', 'format_'.$course->format)) {
                 $straddsections = get_string('addsections', 'format_'.$course->format);
             echo html_writer::start_tag('div', array('id' => 'changenumsections', 'class' => 'mdl-right'));
             if (get_string_manager()->string_exists('addsections', 'format_'.$course->format)) {
                 $straddsections = get_string('addsections', 'format_'.$course->format);
@@ -1002,8 +1010,9 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
                 $url->param('sectionreturn', $sectionreturn);
             }
             $icon = $this->output->pix_icon('t/add', $straddsections);
                 $url->param('sectionreturn', $sectionreturn);
             }
             $icon = $this->output->pix_icon('t/add', $straddsections);
+            $newsections = $maxsections - $lastsection;
             echo html_writer::link($url, $icon . $straddsections,
             echo html_writer::link($url, $icon . $straddsections,
-                array('class' => 'add-sections', 'data-add-sections' => $straddsections));
+                array('class' => 'add-sections', 'data-add-sections' => $straddsections, 'new-sections' => $newsections));
             echo html_writer::end_tag('div');
         }
     }
             echo html_writer::end_tag('div');
         }
     }
index 30a3b95..a854d19 100644 (file)
@@ -1162,6 +1162,7 @@ $string['markedthistopic'] = 'This topic is highlighted as the current topic';
 $string['markthistopic'] = 'Highlight this topic as the current topic';
 $string['matchingsearchandrole'] = 'Matching \'{$a->search}\' and {$a->role}';
 $string['maxareabytesreached'] = 'The file (or the total size of several files) is larger than the space remaining in this area.';
 $string['markthistopic'] = 'Highlight this topic as the current topic';
 $string['matchingsearchandrole'] = 'Matching \'{$a->search}\' and {$a->role}';
 $string['maxareabytesreached'] = 'The file (or the total size of several files) is larger than the space remaining in this area.';
+$string['maxsectionslimit'] = 'Cannot create new section as it would exceed the maximum number of sections allowed for this course ({$a}).';
 $string['maxfilesize'] = 'Maximum size for new files: {$a}';
 $string['maxfilesreached'] = 'You are allowed to attach a maximum of {$a} file(s) to this item';
 $string['maximumchars'] = 'Maximum of {$a} characters';
 $string['maxfilesize'] = 'Maximum size for new files: {$a}';
 $string['maxfilesreached'] = 'You are allowed to attach a maximum of {$a} file(s) to this item';
 $string['maximumchars'] = 'Maximum of {$a} characters';