From 149ffb62ae7318fca71e700203053598fab5f369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luca=20B=C3=B6sch?= Date: Wed, 31 Jul 2024 15:34:22 +0200 Subject: [PATCH] MDL-82555 block_section_links: Add 'Display section number' setting. --- .upgradenotes/MDL-82555-2024073113325722.yml | 8 ++++ blocks/section_links/block_section_links.php | 5 +- blocks/section_links/edit_form.php | 6 ++- .../lang/en/block_section_links.php | 2 + blocks/section_links/renderer.php | 11 ++++- blocks/section_links/settings.php | 7 ++- .../tests/behat/show_section_name.feature | 5 +- .../tests/behat/show_section_number.feature | 48 +++++++++++++++++++ blocks/section_links/version.php | 2 +- 9 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 .upgradenotes/MDL-82555-2024073113325722.yml create mode 100644 blocks/section_links/tests/behat/show_section_number.feature diff --git a/.upgradenotes/MDL-82555-2024073113325722.yml b/.upgradenotes/MDL-82555-2024073113325722.yml new file mode 100644 index 0000000000000..6bdaa91f70f39 --- /dev/null +++ b/.upgradenotes/MDL-82555-2024073113325722.yml @@ -0,0 +1,8 @@ +issueNumber: MDL-82555 +notes: + block_section_links: + - message: >- + New optional parameter $showsectionnumber has been added to + render_section_links(). Setting this to false will disable the display + of the section number when the section name is displayed. + type: improved diff --git a/blocks/section_links/block_section_links.php b/blocks/section_links/block_section_links.php index f2c4532f9d49f..c55724cb1deb1 100644 --- a/blocks/section_links/block_section_links.php +++ b/blocks/section_links/block_section_links.php @@ -110,6 +110,9 @@ public function get_content() { // Whether or not section name should be displayed. $showsectionname = !empty($config->showsectionname) ? true : false; + // Whether or not section number should be displayed. + $showsectionnumber = !empty($config->showsectionnumber) ? true : false; + // Prepare an array of sections to create links for. $sections = array(); $canviewhidden = has_capability('moodle/course:update', $context); @@ -141,7 +144,7 @@ public function get_content() { // Render the sections. $renderer = $this->page->get_renderer('block_section_links'); $this->content->text = $renderer->render_section_links($this->page->course, $sections, - $sectiontojumpto, $showsectionname); + $sectiontojumpto, $showsectionname, $showsectionnumber); } return $this->content; diff --git a/blocks/section_links/edit_form.php b/blocks/section_links/edit_form.php index 3cedaf078cbe9..49fc109ad1041 100644 --- a/blocks/section_links/edit_form.php +++ b/blocks/section_links/edit_form.php @@ -85,5 +85,9 @@ protected function specific_definition($mform) { $mform->addElement('selectyesno', 'config_showsectionname', get_string('showsectionname', 'block_section_links')); $mform->setDefault('config_showsectionname', !empty($config->showsectionname) ? 1 : 0); $mform->addHelpButton('config_showsectionname', 'showsectionname', 'block_section_links'); + + $mform->addElement('selectyesno', 'config_showsectionnumber', get_string('showsectionnumber', 'block_section_links')); + $mform->setDefault('config_showsectionnumber', !empty($config->showsectionnumber) ? 1 : 0); + $mform->addHelpButton('config_showsectionnumber', 'showsectionnumber', 'block_section_links'); } -} \ No newline at end of file +} diff --git a/blocks/section_links/lang/en/block_section_links.php b/blocks/section_links/lang/en/block_section_links.php index adb679bac4b65..b6d0d7a9fe389 100644 --- a/blocks/section_links/lang/en/block_section_links.php +++ b/blocks/section_links/lang/en/block_section_links.php @@ -36,4 +36,6 @@ $string['section_links:addinstance'] = 'Add a new section links block'; $string['showsectionname'] = 'Display section name'; $string['showsectionname_help'] = 'Display section name in addition to section number'; +$string['showsectionnumber'] = 'Display section number'; +$string['showsectionnumber_help'] = 'Display section number in addition to section name'; $string['privacy:metadata'] = 'The Section links block only shows data stored in other locations.'; diff --git a/blocks/section_links/renderer.php b/blocks/section_links/renderer.php index 632a37a2ccb77..e4a7dd6fbb239 100644 --- a/blocks/section_links/renderer.php +++ b/blocks/section_links/renderer.php @@ -39,9 +39,11 @@ class block_section_links_renderer extends plugin_renderer_base { * @param array $sections An array of section objects to render. * @param bool|int The section to provide a jump to link for. * @param bool $showsectionname Whether or not section name should be displayed. + * @param bool $showsectionnumber Whether or not section number should be displayed. * @return string The HTML to display. */ - public function render_section_links(stdClass $course, array $sections, $jumptosection = false, $showsectionname = false) { + public function render_section_links(stdClass $course, array $sections, $jumptosection = false, $showsectionname = false, + $showsectionnumber = true) { $olparams = $showsectionname ? ['class' => 'unlist'] : ['class' => 'inline-list']; $html = html_writer::start_tag('ol', $olparams); foreach ($sections as $section) { @@ -50,9 +52,14 @@ public function render_section_links(stdClass $course, array $sections, $jumptos $attributes['class'] = 'dimmed'; } $html .= html_writer::start_tag('li'); + $sectiontext = ''; $sectiontext = $section->section; if ($showsectionname) { - $sectiontext .= ': ' . $section->name; + if ($showsectionnumber) { + $sectiontext .= ': ' . $section->name; + } else { + $sectiontext = $section->name; + } } if ($section->highlight) { $sectiontext = html_writer::tag('strong', $sectiontext); diff --git a/blocks/section_links/settings.php b/blocks/section_links/settings.php index ef18237c5d0f8..12c570ce89963 100644 --- a/blocks/section_links/settings.php +++ b/blocks/section_links/settings.php @@ -53,4 +53,9 @@ get_string('showsectionname', 'block_section_links'), get_string('showsectionname_help', 'block_section_links'), 0)); -} \ No newline at end of file + + $settings->add(new admin_setting_configcheckbox('block_section_links/showsectionnumber', + get_string('showsectionnumber', 'block_section_links'), + get_string('showsectionnumber_help', 'block_section_links'), + 1)); +} diff --git a/blocks/section_links/tests/behat/show_section_name.feature b/blocks/section_links/tests/behat/show_section_name.feature index 79e36f5bc9547..c7472391f8822 100644 --- a/blocks/section_links/tests/behat/show_section_name.feature +++ b/blocks/section_links/tests/behat/show_section_name.feature @@ -21,8 +21,9 @@ Feature: The Section links block can be configured to display section name in ad | teacher1 | C1 | editingteacher | | student1 | C1 | student | And the following config values are set as admin: - | showsectionname | 1 | block_section_links | - | unaddableblocks | | theme_boost| + | showsectionname | 1 | block_section_links | + | showsectionnumber | 1 | block_section_links | + | unaddableblocks | | theme_boost | And the following "blocks" exist: | blockname | contextlevel | reference | pagetypepattern | defaultregion | | section_links | Course | C1 | course-view-* | side-pre | diff --git a/blocks/section_links/tests/behat/show_section_number.feature b/blocks/section_links/tests/behat/show_section_number.feature new file mode 100644 index 0000000000000..901c032fc9e27 --- /dev/null +++ b/blocks/section_links/tests/behat/show_section_number.feature @@ -0,0 +1,48 @@ +@block @block_section_links +Feature: The Section links block can be configured to display or omit the section number in addition to section name + + Background: + Given the following "course" exists: + | fullname | Course 1 | + | shortname | C1 | + | category | 0 | + | numsections | 10 | + | coursedisplay | 1 | + | initsections | 1 | + And the following "activities" exist: + | activity | name | course | idnumber | section | + | assign | First assignment | C1 | assign1 | 7 | + And the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | 1 | student1@example.com | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And the following config values are set as admin: + | showsectionname | 1 | block_section_links | + | showsectionnumber | 1 | block_section_links | + | unaddableblocks | | theme_boost | + And the following "blocks" exist: + | blockname | contextlevel | reference | pagetypepattern | defaultregion | + | section_links | Course | C1 | course-view-* | side-pre | + + Scenario: Student can see section name as well as section number under the Section links block + When I am on the "Course 1" course page logged in as student1 + Then I should see "7: Section 7" in the "Section links" "block" + And I follow "7: Section 7" + And I should see "First assignment" + + Scenario: Teacher can configure existing Section links block to display section number or section name + Given I log in as "teacher1" + And I am on "Course 1" course homepage with editing mode on + When I configure the "Section links" block + And I set the following fields to these values: + | Display section name | Yes | + | Display section number | No | + And I click on "Save changes" "button" + Then I should not see "7: Section 7" in the "Section links" "block" + And I should see "Section 7" in the "Section links" "block" + And I follow "Section 7" + And I should see "First assignment" diff --git a/blocks/section_links/version.php b/blocks/section_links/version.php index e20e43c8ef6f7..74142f86534e0 100644 --- a/blocks/section_links/version.php +++ b/blocks/section_links/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024121800; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2024121801; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2024100100; // Requires this Moodle version. $plugin->component = 'block_section_links'; // Full name of the plugin (used for diagnostics)