From 53663f2ea126b18709711d2126ea501f3b9c6956 Mon Sep 17 00:00:00 2001 From: Mikhail Golenkov Date: Tue, 24 Nov 2020 11:33:27 +1100 Subject: [PATCH] MDL-69773 block_section_links: Add an option to display section name --- blocks/section_links/block_section_links.php | 9 +++- blocks/section_links/edit_form.php | 3 ++ .../lang/en/block_section_links.php | 2 + blocks/section_links/renderer.php | 9 +++- blocks/section_links/settings.php | 5 +++ .../tests/behat/show_section_name.feature | 43 +++++++++++++++++++ blocks/section_links/upgrade.txt | 6 +++ blocks/section_links/version.php | 2 +- 8 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 blocks/section_links/tests/behat/show_section_name.feature create mode 100644 blocks/section_links/upgrade.txt diff --git a/blocks/section_links/block_section_links.php b/blocks/section_links/block_section_links.php index 6c638ea47da..f7e555e5360 100644 --- a/blocks/section_links/block_section_links.php +++ b/blocks/section_links/block_section_links.php @@ -105,6 +105,9 @@ class block_section_links extends block_base { } } + // Whether or not section name should be displayed. + $showsectionname = !empty($config->showsectionname) ? true : false; + // Prepare an array of sections to create links for. $sections = array(); $canviewhidden = has_capability('moodle/course:update', $context); @@ -126,13 +129,17 @@ class block_section_links extends block_base { $sections[$i]->highlight = true; $sectiontojumpto = $section->section; } + if ($showsectionname) { + $sections[$i]->name = $courseformat->get_section_name($i); + } } } if (!empty($sections)) { // Render the sections. $renderer = $this->page->get_renderer('block_section_links'); - $this->content->text = $renderer->render_section_links($this->page->course, $sections, $sectiontojumpto); + $this->content->text = $renderer->render_section_links($this->page->course, $sections, + $sectiontojumpto, $showsectionname); } return $this->content; diff --git a/blocks/section_links/edit_form.php b/blocks/section_links/edit_form.php index 63a3593aa61..3cedaf078cb 100644 --- a/blocks/section_links/edit_form.php +++ b/blocks/section_links/edit_form.php @@ -82,5 +82,8 @@ class block_section_links_edit_form extends block_edit_form { $mform->addHelpButton('config_incby'.$i, 'incby'.$i, 'block_section_links'); } + $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'); } } \ 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 b2678987e34..950a0bbe08d 100644 --- a/blocks/section_links/lang/en/block_section_links.php +++ b/blocks/section_links/lang/en/block_section_links.php @@ -34,6 +34,8 @@ $string['numsections2'] = 'Alternative number of sections'; $string['numsections2_help'] = 'Once the number of sections in the course reaches this number then the Alternative increment by value is used.'; $string['pluginname'] = 'Section links'; $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['topics'] = 'Topics'; $string['weeks'] = 'Weeks'; $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 338855b9238..a1ebe8756bb 100644 --- a/blocks/section_links/renderer.php +++ b/blocks/section_links/renderer.php @@ -38,10 +38,12 @@ class block_section_links_renderer extends plugin_renderer_base { * @param stdClass $course The course we are rendering for. * @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. * @return string The HTML to display. */ - public function render_section_links(stdClass $course, array $sections, $jumptosection = false) { - $html = html_writer::start_tag('ol', array('class' => 'inline-list')); + public function render_section_links(stdClass $course, array $sections, $jumptosection = false, $showsectionname = false) { + $olparams = $showsectionname ? ['class' => 'unlist'] : ['class' => 'inline-list']; + $html = html_writer::start_tag('ol', $olparams); foreach ($sections as $section) { $attributes = array(); if (!$section->visible) { @@ -49,6 +51,9 @@ class block_section_links_renderer extends plugin_renderer_base { } $html .= html_writer::start_tag('li'); $sectiontext = $section->section; + if ($showsectionname) { + $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 2fcb4a4a729..ef18237c5d0 100644 --- a/blocks/section_links/settings.php +++ b/blocks/section_links/settings.php @@ -48,4 +48,9 @@ if ($ADMIN->fulltree) { get_string('incby'.$i.'_help', 'block_section_links'), $selected[$i][1], $increments)); } + + $settings->add(new admin_setting_configcheckbox('block_section_links/showsectionname', + get_string('showsectionname', 'block_section_links'), + get_string('showsectionname_help', 'block_section_links'), + 0)); } \ No newline at end of file diff --git a/blocks/section_links/tests/behat/show_section_name.feature b/blocks/section_links/tests/behat/show_section_name.feature new file mode 100644 index 00000000000..8c0b713baf8 --- /dev/null +++ b/blocks/section_links/tests/behat/show_section_name.feature @@ -0,0 +1,43 @@ +@block @block_section_links +Feature: The Section links block can be configured to display section name in addition to section number + + Background: + Given the following "courses" exist: + | fullname | shortname | category | numsections | coursedisplay | + | Course 1 | C1 | 0 | 10 | 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 I log in as "admin" + And I set the following administration settings values: + | showsectionname | 1 | + And I am on "Course 1" course homepage with editing mode on + And I add the "Section links" block + And I log out + + Scenario: Student can see section name under the Section links block + Given I log in as "student1" + When I am on "Course 1" course homepage + Then I should see "7: Topic 7" in the "Section links" "block" + And I follow "7: Topic 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 | No | + And I click on "Save changes" "button" + Then I should not see "7: Topic 7" in the "Section links" "block" + And I should see "7" in the "Section links" "block" + And I follow "7" + And I should see "First assignment" diff --git a/blocks/section_links/upgrade.txt b/blocks/section_links/upgrade.txt new file mode 100644 index 00000000000..bf4f9a4776d --- /dev/null +++ b/blocks/section_links/upgrade.txt @@ -0,0 +1,6 @@ +This file describes API changes in the section_links block code. + +=== 3.11 === + +* New optional parameter $showsectionname has been added to render_section_links(). Setting this to true will display + section name in addition to section number. diff --git a/blocks/section_links/version.php b/blocks/section_links/version.php index 96451a276ef..dd454897ed0 100644 --- a/blocks/section_links/version.php +++ b/blocks/section_links/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2021052500; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2021052501; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2021052500; // Requires this Moodle version $plugin->component = 'block_section_links'; // Full name of the plugin (used for diagnostics)