MDL-85392 block_site_main_menu: Add course support

To make the Main site menu block more versatile for use in various courses,
the following improvements have been made:

- Customizable title: Users can give a title to the block, that's relevant to
its context within a specific course.
- Expanded course compatibility: The block now functions correctly even in
course formats that don't have a dedicated course view page, such as Single
activity courses.
- Renamed for clarity: The block has been renamed to better reflect its broader
applicability.
This commit is contained in:
Sara Arjona
2025-05-22 18:33:37 +02:00
parent b4a8c83ea3
commit 4fe2e858b5
7 changed files with 187 additions and 37 deletions
@@ -0,0 +1,10 @@
issueNumber: MDL-85392
notes:
block_site_main_menu:
- message: >-
The "Main menu" block has been renamed to "Additional activities." Its
title is now customizable, and it can be used in course formats without
a dedicated view page (for instance, Single activity). On the Home page,
this block has also been renamed; administrators will need to manually
revert the name if they wish to retain "Main menu" after upgrading.
type: improved
+56 -4
View File
@@ -26,10 +26,28 @@ class block_site_main_menu extends block_base {
$this->title = get_string('pluginname', 'block_site_main_menu');
}
function applicable_formats() {
return array('site' => true);
#[\Override]
public function specialization() {
if (isset($this->config->title)) {
$this->title = format_string($this->config->title, true, ['context' => $this->context]);
} else {
$this->title = get_string('pluginname', 'block_site_main_menu');
}
}
#[\Override]
function applicable_formats() {
$format = course_get_format($this->get_block_course());
$applicableformat = $format && !$format->has_view_page();
return [
'course-view' => $applicableformat,
'mod' => $applicableformat,
'site' => true,
];
}
#[\Override]
function get_content() {
if ($this->content !== NULL) {
return $this->content;
@@ -43,8 +61,7 @@ class block_site_main_menu extends block_base {
return $this->content;
}
$course = get_site();
$course = $this->get_block_course();
course_create_sections_if_missing($course, 0);
$format = course_get_format($course);
$modinfo = $format->get_modinfo();
@@ -64,4 +81,39 @@ class block_site_main_menu extends block_base {
);
return $this->content;
}
/**
* Get the course for the block.
*
* @return stdClass The course object.
*/
protected function get_block_course(): stdClass {
global $COURSE;
if (!empty($this->page)) {
$course = $this->page->course;
}
if (empty($course)) {
$course = $COURSE;
}
if ($this->context) {
$context = $this->context->get_parent_context();
if ($context->contextlevel == CONTEXT_COURSE) {
$courseid = $context->instanceid;
} else if ($context->contextlevel == CONTEXT_SYSTEM) {
$courseid = SITEID;
} else {
$coursecontext = $context->get_course_context(false);
if ($coursecontext) {
$courseid = $coursecontext->instanceid;
}
}
}
if (isset($courseid) && $courseid != $course->id) {
$course = get_course($courseid);
}
return $course;
}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Form for editing The Additional activities block instances.
*
* @package block_site_main_menu
* @copyright 2025 Sara Arjona <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_site_main_menu_edit_form extends block_edit_form {
#[\Override]
protected function specific_definition($mform) {
// Fields for editing HTML block title and contents.
$mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
$mform->addElement('text', 'config_title', get_string('configtitle', 'block_site_main_menu'));
$mform->setDefault('config_title', get_string('pluginname', 'block_site_main_menu'));
$mform->setType('config_title', PARAM_TEXT);
}
}
@@ -23,6 +23,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['pluginname'] = 'Main menu';
$string['site_main_menu:addinstance'] = 'Add a new main menu block';
$string['privacy:metadata'] = 'The Main menu block only shows data stored in other locations.';
$string['configtitle'] = 'Title';
$string['pluginname'] = 'Additional activities';
$string['privacy:metadata'] = 'The Additional activities block only shows data stored in other locations.';
$string['site_main_menu:addinstance'] = 'Add a new Additional activities block';
@@ -1,8 +1,8 @@
@block @block_site_main_menu
Feature: Add URL to main menu block
Feature: Add URL to Additional activities block
In order to add helpful resources for students
As a admin
I need to add URLs to the main menu block and check it works.
I need to add URLs to the Additional activities block and check it works.
Background:
Given the following "courses" exist:
@@ -14,7 +14,7 @@ Feature: Add URL to main menu block
| site_main_menu | System | 1 | site-index | side-pre |
@javascript
Scenario: Add a URL in menu block and ensure it appears
Scenario: Add a URL in Additional activities block and ensure it appears
Given I log in as "admin"
And I am on site homepage
And the following "activity" exists:
@@ -30,15 +30,15 @@ Feature: Add URL to main menu block
| id_display | In pop-up |
And I press "Save and return to course"
And I turn editing mode on
Then "reference link" "link" should exist in the "Main menu" "block"
And "Add an activity or resource" "button" should exist in the "Main menu" "block"
Then "reference link" "link" should exist in the "Additional activities" "block"
And "Add an activity or resource" "button" should exist in the "Additional activities" "block"
@javascript
Scenario: Add a URL in menu block can appear in the entire site
Scenario: Add a URL in Additional activities block can appear in the entire site
When I log in as "admin"
And I am on site homepage
And I turn editing mode on
And I configure the "Main menu" block
And I configure the "Additional activities" block
And I set the following fields to these values:
| Page contexts | Display throughout the entire site |
And I press "Save changes"
@@ -54,19 +54,19 @@ Feature: Add URL to main menu block
And I set the following fields to these values:
| id_display | Embed |
And I press "Save and return to course"
Then I click on "reference link" "link" in the "Main menu" "block"
And "reference link" "link" should exist in the "Main menu" "block"
Then I click on "reference link" "link" in the "Additional activities" "block"
And "reference link" "link" should exist in the "Additional activities" "block"
And I am on the "C1" "Course" page
And "reference link" "link" should exist in the "Main menu" "block"
And "reference link" "link" should exist in the "Additional activities" "block"
And I navigate to "Badges > Add a new badge" in site administration
And "reference link" "link" should exist in the "Main menu" "block"
And "reference link" "link" should exist in the "Additional activities" "block"
@javascript
Scenario: Add a URL in menu block can appear in any front page
When I log in as "admin"
And I am on site homepage
And I turn editing mode on
And I configure the "Main menu" block
And I configure the "Additional activities" block
And I set the following fields to these values:
| Page contexts | Display on the site home and any pages added to the site home. |
And I press "Save changes"
@@ -82,20 +82,20 @@ Feature: Add URL to main menu block
And I set the following fields to these values:
| id_display | Embed |
And I press "Save and return to course"
Then I click on "reference link" "link" in the "Main menu" "block"
And "reference link" "link" should exist in the "Main menu" "block"
Then I click on "reference link" "link" in the "Additional activities" "block"
And "reference link" "link" should exist in the "Additional activities" "block"
And I am on the "C1" "Course" page
And "Main menu" "block" should not exist
And "Additional activities" "block" should not exist
And I navigate to "Badges > Add a new badge" in site administration
And "Main menu" "block" should not exist
And "Additional activities" "block" should not exist
@javascript
Scenario: When the "Main Menu" block is displayed throrought the entire site, adding an URL in a course
Scenario: When the Additional activities block is displayed throrought the entire site, adding an URL in a course
results in adding it in the course and not in the frontpage
Given I log in as "admin"
And I am on site homepage
And I turn editing mode on
And I configure the "Main menu" block
And I configure the "Additional activities" block
And I set the following fields to these values:
| Page contexts | Display throughout the entire site |
And I press "Save changes"
@@ -112,5 +112,5 @@ Feature: Add URL to main menu block
And I set the following fields to these values:
| id_display | In pop-up |
And I press "Save and return to course"
Then "reference link" "link" should not exist in the "Main menu" "block"
Then "reference link" "link" should not exist in the "Additional activities" "block"
And I should see "mooooooooodle" in the "region-main" "region"
@@ -0,0 +1,33 @@
@block @block_site_main_menu @addablocklink
Feature: Additional activities block also supported in courses
In order to use Additional activities block in a course
As a teacher
I need to add it to a course and check it works.
Background:
Given the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | singleactivity |
| Course 2 | C2 | topics |
And the following "activities" exist:
| activity | course | name |
| forum | C1 | My forum 1 |
| forum | C2 | My forum 2 |
And the following "users" exist:
| username | firstname | lastname |
| teacher1 | Teacher | One |
| student1 | Student | One |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| teacher1 | C2 | editingteacher |
@javascript
Scenario: Additional activities block only can be added to courses without view page
Given I am on the "Course 1" "course" page logged in as "teacher1"
And I turn editing mode on
When I click on "Add a block" "link"
Then I should see "Additional activities"
But I am on the "Course 2" "course" page
And I click on "Add a block" "link"
And I should not see "Additional activities"
@@ -1,11 +1,11 @@
@block @block_site_main_menu
Feature: Edit activities in main menu block
In order to use main menu block
Feature: Edit activities in Additional activities block
In order to use Additional activities block
As an admin
I need to add and edit activities there
@javascript
Scenario: Edit name of acitivity in-place in site main menu block
Scenario: Edit name of activity in-place in Additional activities block
Given the following "activity" exists:
| activity | forum |
| course | Acceptance test site |
@@ -25,7 +25,7 @@ Feature: Edit activities in main menu block
And I should see "New forum name"
@javascript
Scenario: Activities in main menu block can be made available but not visible on a course page
Scenario: Activities in Additional activities block can be made available but not visible on a course page
Given the following config values are set as admin:
| allowstealth | 1 |
And the following "blocks" exist:
@@ -53,11 +53,11 @@ Feature: Edit activities in main menu block
And I turn editing mode off
And I should see "Available but not shown on course page" in the "My forum name" "core_courseformat > Activity visibility"
And I log out
And I should not see "My forum name" in the "Main menu" "block"
And I should see "Visible forum" in the "Main menu" "block"
And I should not see "My forum name" in the "Additional activities" "block"
And I should see "Visible forum" in the "Additional activities" "block"
@javascript
Scenario: The move activity modal allow to move from the main menu block to the main content
Scenario: The move activity modal allow to move from the Additional activities block to the main content
Given the following "activity" exists:
| activity | forum |
| course | Acceptance test site |
@@ -81,7 +81,7 @@ Feature: Edit activities in main menu block
And I should not see "My forum name" in the "block_site_main_menu_section" "region"
@javascript
Scenario: The move activity modal allow to move from the main content to the main menu block
Scenario: The move activity modal allow to move from the main content to the Additional activities block
Given the following "activity" exists:
| activity | forum |
| course | Acceptance test site |
@@ -106,7 +106,7 @@ Feature: Edit activities in main menu block
And I should see "My forum name" in the "block_site_main_menu_section" "region"
@javascript
Scenario: Admin can delete an activity in the main menu block
Scenario: Admin can delete an activity in the Additional activities block
Given the following "activity" exists:
| activity | forum |
| course | Acceptance test site |
@@ -125,7 +125,7 @@ Feature: Edit activities in main menu block
Then I should not see "My forum name" in the "block_site_main_menu_section" "region"
@javascript
Scenario: Admin can duplicate an activity in the main menu block
Scenario: Admin can duplicate an activity in the Additional activities block
Given the following "activity" exists:
| activity | forum |
| course | Acceptance test site |
@@ -143,7 +143,7 @@ Feature: Edit activities in main menu block
Then I should see "My forum name (copy)" in the "block_site_main_menu_section" "region"
@javascript
Scenario: Admin can move right and left an activity in the main menu block
Scenario: Admin can move right and left an activity in the Additional activities block
Given the following "activity" exists:
| activity | forum |
| course | Acceptance test site |
@@ -167,3 +167,22 @@ Feature: Edit activities in main menu block
And I open "My forum name" actions menu
And "Move right" "link" should be visible
And "Move left" "link" should not be visible
@javascript
Scenario: Additional activities block title can be changed
Given the following "activity" exists:
| activity | forum |
| course | Acceptance test site |
| name | My forum name |
| idnumber | forum |
And the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| site_main_menu | System | 1 | site-index | side-pre |
And I log in as "admin"
And I am on site homepage
And I turn editing mode on
When I configure the "Additional activities" block
And I set the field "Title" to "My new title"
And I press "Save changes"
Then "My new title" "block" should exist
And "Additional activities" "block" should not exist