From c960a9c5b8886041c7a865085d3d205ded52da33 Mon Sep 17 00:00:00 2001 From: ferran Date: Tue, 29 Apr 2025 11:29:45 +0200 Subject: [PATCH] MDL-84291 format_weeks: max initial sections setting --- .upgradenotes/MDL-84291-2025042902162982.yml | 8 ++ course/format/weeks/db/upgrade.php | 14 ++++ course/format/weeks/lang/en/format_weeks.php | 2 + course/format/weeks/lib.php | 3 +- course/format/weeks/settings.php | 8 ++ .../tests/behat/default_sections.feature | 74 +++++++++++++++++++ course/format/weeks/version.php | 2 +- 7 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 .upgradenotes/MDL-84291-2025042902162982.yml create mode 100644 course/format/weeks/tests/behat/default_sections.feature diff --git a/.upgradenotes/MDL-84291-2025042902162982.yml b/.upgradenotes/MDL-84291-2025042902162982.yml new file mode 100644 index 00000000000..d82580f7175 --- /dev/null +++ b/.upgradenotes/MDL-84291-2025042902162982.yml @@ -0,0 +1,8 @@ +issueNumber: MDL-84291 +notes: + format_weeks: + - message: >- + The weekly sections format now has a system setting called Maximum + initial number of weeks that replaced the old "Max sections" when + creating a new course + type: improved diff --git a/course/format/weeks/db/upgrade.php b/course/format/weeks/db/upgrade.php index 9ed64d17b8f..65965ffaa18 100644 --- a/course/format/weeks/db/upgrade.php +++ b/course/format/weeks/db/upgrade.php @@ -46,5 +46,19 @@ function xmldb_format_weeks_upgrade($oldversion) { // Automatically generated Moodle v5.0.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2025052600) { + $config = get_config('format_weeks'); + // Crerate the default maxinitialsections setting if is not set. + if (!isset($config->maxinitialsections)) { + // The system may have some maxsections defined. We will keep the same value. + $courseconfig = get_config('moodlecourse'); + $max = (int) $courseconfig->maxsections; + $config->maxinitialsections = $max ?: 52; + set_config('maxinitialsections', $config->maxinitialsections, 'format_weeks'); + } + + upgrade_plugin_savepoint(true, 2025052600, 'format', 'weeks'); + } + return true; } diff --git a/course/format/weeks/lang/en/format_weeks.php b/course/format/weeks/lang/en/format_weeks.php index 58f009fafe3..5730599a48b 100644 --- a/course/format/weeks/lang/en/format_weeks.php +++ b/course/format/weeks/lang/en/format_weeks.php @@ -27,6 +27,8 @@ $string['automaticenddate'] = 'Calculate the end date from the number of section $string['automaticenddate_help'] = 'If enabled, the end date for the course will be automatically calculated from the number of sections and the course start date.'; $string['currentsection'] = 'Current week'; $string['hidefromothers'] = 'Hide'; +$string['maxinitialsections'] = 'Maximum number of weeks at course creation'; +$string['maxinitialsections_help'] = 'Sets the maximum number of weeks that can be assigned to a course at the time of creation. This limit helps prevent accidental creation of overly long courses. It does not restrict teachers from adding more weeks later during course editing.'; $string['page-course-view-weeks'] = 'Any course main page in weekly sections format'; $string['page-course-view-weeks-x'] = 'Any course page in weekly sections format'; $string['pluginname'] = 'Weekly sections'; diff --git a/course/format/weeks/lib.php b/course/format/weeks/lib.php index 5b65792f3e4..9c3c76942af 100644 --- a/course/format/weeks/lib.php +++ b/course/format/weeks/lib.php @@ -319,7 +319,8 @@ class format_weeks extends core_courseformat\base { // The "Number of sections" option is no longer available when editing course, instead teachers should // delete and add sections when needed. $courseconfig = get_config('moodlecourse'); - $max = (int)$courseconfig->maxsections; + $weeksconfig = get_config('format_weeks'); + $max = (int)$weeksconfig->maxinitialsections; $element = $mform->addElement('select', 'numsections', get_string('numberweeks'), range(0, $max ?: 52)); $mform->setType('numsections', PARAM_INT); if (is_null($mform->getElementValue('numsections'))) { diff --git a/course/format/weeks/settings.php b/course/format/weeks/settings.php index 036e5aad869..bbbbd22d7df 100644 --- a/course/format/weeks/settings.php +++ b/course/format/weeks/settings.php @@ -33,4 +33,12 @@ if ($ADMIN->fulltree) { new lang_string('indentation_help', 'format_weeks').'
'.$link, 1 )); + + $settings->add(new admin_setting_configtext( + name: 'format_weeks/maxinitialsections', + visiblename: new lang_string('maxinitialsections', 'format_weeks'), + description: new lang_string('maxinitialsections_help', 'format_weeks'), + defaultsetting: 52, + paramtype: PARAM_INT, + )); } diff --git a/course/format/weeks/tests/behat/default_sections.feature b/course/format/weeks/tests/behat/default_sections.feature new file mode 100644 index 00000000000..c7bf393fdcc --- /dev/null +++ b/course/format/weeks/tests/behat/default_sections.feature @@ -0,0 +1,74 @@ +@format @format_weeks +Feature: Weeks format courses are created with the system default number of sections + In order to create courses + As a course creator + I need my week courses to be created as the system default number of sections + + @javascript + Scenario: Weeks formats cannot be created with more sections than the format max + Given the following config values are set as admin: + | maxinitialsections | 5 | format_weeks | + | numsections | 40 | moodlecourse | + When I log in as "admin" + And I navigate to "Courses > Manage courses and categories" in site administration + And I click on "Create new course" "link" + And I expand all fieldsets + And I set the field "Course full name" to "Course 1" + And I set the field "Course short name" to "C1" + And I set the field "Format" to "Weekly sections" + Then the "Number of sections" select box should contain "5" + And the "Number of sections" select box should not contain "6" + And I expand all fieldsets + And I set the field "Number of sections" to "5" + And I click on "Save and display" "button" + And "[data-for='section'][data-number='1']" "css_element" should exist + And "[data-for='section'][data-number='2']" "css_element" should exist + And "[data-for='section'][data-number='3']" "css_element" should exist + And "[data-for='section'][data-number='4']" "css_element" should exist + And "[data-for='section'][data-number='5']" "css_element" should exist + And "[data-for='section'][data-number='6']" "css_element" should not exist + + @javascript + Scenario: Weeks formats will be created with the system default + Given the following config values are set as admin: + | numsections | 5 | moodlecourse | + When I log in as "admin" + And I navigate to "Courses > Manage courses and categories" in site administration + And I click on "Create new course" "link" + And I expand all fieldsets + And I set the field "Course full name" to "Course 1" + And I set the field "Course short name" to "C1" + And I set the field "Format" to "Weekly sections" + Then the "Number of sections" select box should contain "52" + And the "Number of sections" select box should not contain "53" + And I click on "Save and display" "button" + And "[data-for='section'][data-number='1']" "css_element" should exist + And "[data-for='section'][data-number='2']" "css_element" should exist + And "[data-for='section'][data-number='3']" "css_element" should exist + And "[data-for='section'][data-number='4']" "css_element" should exist + And "[data-for='section'][data-number='5']" "css_element" should exist + And "[data-for='section'][data-number='6']" "css_element" should not exist + + @javascript + Scenario: Weeks formats can be created with a specific number of sections + Given the following config values are set as admin: + | numsections | 4 | moodlecourse | + | maxinitialsections | 10 | format_weeks | + When I log in as "admin" + And I navigate to "Courses > Manage courses and categories" in site administration + And I click on "Create new course" "link" + And I expand all fieldsets + And I set the field "Course full name" to "Course 1" + And I set the field "Course short name" to "C1" + And I set the field "Format" to "Weekly sections" + Then the "Number of sections" select box should contain "10" + And the "Number of sections" select box should not contain "11" + And I expand all fieldsets + And I set the field "Number of sections" to "5" + And I click on "Save and display" "button" + And "[data-for='section'][data-number='1']" "css_element" should exist + And "[data-for='section'][data-number='2']" "css_element" should exist + And "[data-for='section'][data-number='3']" "css_element" should exist + And "[data-for='section'][data-number='4']" "css_element" should exist + And "[data-for='section'][data-number='5']" "css_element" should exist + And "[data-for='section'][data-number='6']" "css_element" should not exist diff --git a/course/format/weeks/version.php b/course/format/weeks/version.php index 4899784a3b5..c19c7a69437 100644 --- a/course/format/weeks/version.php +++ b/course/format/weeks/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2025041400; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2025052600; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2025040800; // Requires this Moodle version. $plugin->component = 'format_weeks'; // Full name of the plugin (used for diagnostics).