From 7c05d8a3d2aca9466a725d476ab4d90c924c3ae9 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Mon, 16 Jul 2018 15:58:49 +0800 Subject: [PATCH] MDL-62748 course: maxsections is a limit 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 | Bin 7453 -> 7490 bytes course/amd/src/actions.js | 5 +++-- course/changenumsections.php | 24 ++++++++++++++++++++++++ course/format/lib.php | 12 ++++++++++++ course/format/renderer.php | 29 +++++++++++++++++++---------- lang/en/moodle.php | 1 + 6 files changed, 59 insertions(+), 12 deletions(-) diff --git a/course/amd/build/actions.min.js b/course/amd/build/actions.min.js index 904a5873b87de2f3128df129185fab8dca289a94..2875e390ae2c2c7dc0faa3c49f40e0f9be253659 100644 GIT binary patch delta 64 zcmbPhb;xRivy?=#USdf}k%m%UYPoK4YH~?teqOPXrcT;qC#gcN+{6l7C3WpoZS~0; Ur9>IiCLfn_XH477FU`jU0Lb4IOaK4? delta 43 zcmX?PHP>o`v()4msREI*#GKMpTO~s!I~)C!%rZN5P3zRjZ=~EAQ#YGP^DzMcN3spC diff --git a/course/amd/src/actions.js b/course/amd/src/actions.js index 9a2a3c7820a..76d42320d6d 100644 --- a/course/amd/src/actions.js +++ b/course/amd/src/actions.js @@ -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), - modalTitle = trigger.attr('data-add-sections'); + modalTitle = trigger.attr('data-add-sections'), + newSections = trigger.attr('new-sections'); var modalBody = $('
' + - '
'); + ''); modalBody.find('label').html(strNumberSections); ModalFactory.create({ title: modalTitle, diff --git a/course/changenumsections.php b/course/changenumsections.php index c3d2a55f394..8f6ea284ae2 100644 --- a/course/changenumsections.php +++ b/course/changenumsections.php @@ -45,6 +45,30 @@ require_login($course); 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. diff --git a/course/format/lib.php b/course/format/lib.php index f70371c1137..0ba58c6287f 100644 --- a/course/format/lib.php +++ b/course/format/lib.php @@ -287,6 +287,18 @@ abstract class format_base { 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. * diff --git a/course/format/renderer.php b/course/format/renderer.php index 357c0a33894..45ff162e6b4 100644 --- a/course/format/renderer.php +++ b/course/format/renderer.php @@ -952,7 +952,10 @@ abstract class format_section_renderer_base extends plugin_renderer_base { 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) { @@ -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. - $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. @@ -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()) { + 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'. - 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); + $newsections = $maxsections - $lastsection; 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'); } } diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 30a3b9535fe..a854d1978e5 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -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['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'; -- 2.43.0