MDL-85433 navigation: Generalise secondary navigation for single format

Introduce flexibility by allowing course formats to define custom logic
for retrieving activity modules.
This commit is contained in:
Sara Arjona
2025-06-13 15:26:08 +02:00
parent badade3137
commit 2513bca047
3 changed files with 53 additions and 4 deletions
@@ -0,0 +1,8 @@
issueNumber: MDL-85433
notes:
core_courseformat:
- message: >-
A new interface, `main_activity_interface`, is now available. Course
format plugins should implement it if they intend to display only a
single activity in the course page.
type: improved
@@ -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/>.
namespace core_courseformat;
/**
* Contains the interface that any course format plugin should implement
* when it wants to only display a single activity in the course page.
*
* @package core_courseformat
* @copyright 2025 Sara Arjona <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface main_activity_interface {
/**
* Get the main activity of the course.
*
* @return \cm_info|null The main activity object or null if main activity is not set.
*/
public function get_main_activity(): ?\cm_info;
}
+10 -4
View File
@@ -1108,10 +1108,16 @@ class secondary extends view {
// the course in order to obtain the required module settings navigation.
if ($page->context instanceof \context_course) {
$this->page->set_secondary_active_tab($coursesecondarynode->key);
// Get the currently used module in the single activity course.
$module = current(array_filter(get_course_mods($course->id), function ($module) {
return $module->visible == 1;
}));
// Get the currently used module in course.
$format = course_get_format($course);
if ($format instanceof \core_courseformat\main_activity_interface) {
$module = $format->get_main_activity();
} else {
$module = current(array_filter(get_course_mods($course->id), function ($module) {
return $module->visible == 1;
}));
}
// If the default module for the single course format has not been set yet, skip displaying the module
// related navigation in the secondary navigation.
if (!$module) {