Skip to content

Commit

Permalink
MDL-82555 block_section_links: Add 'Display section number' setting.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaboesch committed Jan 31, 2025
1 parent 07881a5 commit 149ffb6
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .upgradenotes/MDL-82555-2024073113325722.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion blocks/section_links/block_section_links.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion blocks/section_links/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
}
2 changes: 2 additions & 0 deletions blocks/section_links/lang/en/block_section_links.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
11 changes: 9 additions & 2 deletions blocks/section_links/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion blocks/section_links/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@
get_string('showsectionname', 'block_section_links'),
get_string('showsectionname_help', 'block_section_links'),
0));
}

$settings->add(new admin_setting_configcheckbox('block_section_links/showsectionnumber',
get_string('showsectionnumber', 'block_section_links'),
get_string('showsectionnumber_help', 'block_section_links'),
1));
}
5 changes: 3 additions & 2 deletions blocks/section_links/tests/behat/show_section_name.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
48 changes: 48 additions & 0 deletions blocks/section_links/tests/behat/show_section_number.feature
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion blocks/section_links/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 149ffb6

Please sign in to comment.