Merge branch 'MDL-81648-main' of https://github.com/roland04/moodle

This commit is contained in:
Sara Arjona 2024-06-10 15:48:32 +02:00
commit 6764cc71ac
No known key found for this signature in database
5 changed files with 130 additions and 3 deletions

View File

@ -124,4 +124,13 @@ abstract class sectiondelegate {
): ?action_menu { ): ?action_menu {
return $controlmenu->get_default_action_menu($output); return $controlmenu->get_default_action_menu($output);
} }
/**
* Get the parent section of the current delegated section if any.
*
* @return section_info|null
*/
public function get_parent_section(): ?section_info {
return null;
}
} }

View File

@ -0,0 +1,90 @@
<?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;
use cm_info;
use section_info;
use stdClass;
/**
* Class sectiondelegatemodule
*
* @package core_courseformat
* @copyright 2024 Mikel Martín <mikel@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class sectiondelegatemodule extends sectiondelegate {
/** @var section_info $sectioninfo The section_info object of the delegated section module */
/** @var cm_info|null $cm The cm_info object of the delegated section module */
private $cm = null;
/** @var stdClass|null $course The course object of the delegated section module */
private $course = null;
/**
* Constructor.
* @param section_info $sectioninfo
*/
public function __construct(
protected section_info $sectioninfo
) {
parent::__construct($sectioninfo);
[$this->course, $this->cm] = get_course_and_cm_from_instance(
$this->sectioninfo->itemid,
$this->get_module_name(),
$this->sectioninfo->course,
);
}
/**
* Get the parent section of the current delegated section.
*
* @return section_info|null
*/
public function get_parent_section(): ?section_info {
return $this->cm->get_section_info();
}
/**
* Get the course object.
*
* @return cm_info
*/
public function get_cm(): cm_info {
return $this->cm;
}
/**
* Get the course object.
*
* @return stdClass
*/
public function get_course(): stdClass {
return $this->course;
}
/**
* Get the module name from the section component frankenstyle name.
*
* @return string
*/
private function get_module_name(): string {
return \core_component::normalize_component($this->sectioninfo->component)[1];
}
}

View File

@ -100,13 +100,13 @@ class sectiondelegate_test extends \advanced_testcase {
$sectioninfo = formatactions::section($course)->create_delegated('test_component', 1); $sectioninfo = formatactions::section($course)->create_delegated('test_component', 1);
/** @var testsectiondelegate */ /** @var testsectiondelegate $delegated */
$delegated = $sectioninfo->get_component_instance(); $delegated = $sectioninfo->get_component_instance();
$format = course_get_format($course); $format = course_get_format($course);
$outputclass = $format->get_output_classname('content\\section\\controlmenu'); $outputclass = $format->get_output_classname('content\\section\\controlmenu');
/** @var \core_courseformat\output\local\content\section\controlmenu */ /** @var \core_courseformat\output\local\content\section\controlmenu $controlmenu */
$controlmenu = new $outputclass($format, $sectioninfo); $controlmenu = new $outputclass($format, $sectioninfo);
$renderer = $PAGE->get_renderer('format_' . $course->format); $renderer = $PAGE->get_renderer('format_' . $course->format);
$sectionmenu = $controlmenu->get_action_menu($renderer); $sectionmenu = $controlmenu->get_action_menu($renderer);
@ -141,4 +141,21 @@ class sectiondelegate_test extends \advanced_testcase {
$result = $delegated->get_section_action_menu($format, $controlmenu, $renderer); $result = $delegated->get_section_action_menu($format, $controlmenu, $renderer);
$this->assertNull($result); $this->assertNull($result);
} }
/**
* Test get_parent_section().
*
* @covers ::get_parent_section
*/
public function test_get_parent_section(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 1]);
$sectioninfo = formatactions::section($course)->create_delegated('test_component', 1);
/** @var testsectiondelegate $delegated */
$delegated = $sectioninfo->get_component_instance();
$this->assertNull($delegated->get_parent_section());
}
} }

View File

@ -2295,10 +2295,20 @@ class global_navigation extends navigation_node {
continue; continue;
} }
$parentnode = $coursenode;
// Set the parent node to the parent section if this is a delegated section.
if ($section->is_delegated()) {
$parentsection = $section->get_component_instance()->get_parent_section();
if ($parentsection) {
$parentnode = $coursenode->find($parentsection->id, self::TYPE_SECTION) ?: $coursenode;
}
}
$sectionname = get_section_name($course, $section); $sectionname = get_section_name($course, $section);
$url = course_get_url($course, $section->section, array('navigation' => true)); $url = course_get_url($course, $section->section, array('navigation' => true));
$sectionnode = $coursenode->add($sectionname, $url, navigation_node::TYPE_SECTION, $sectionnode = $parentnode->add($sectionname, $url, navigation_node::TYPE_SECTION,
null, $section->id, new pix_icon('i/section', '')); null, $section->id, new pix_icon('i/section', ''));
$sectionnode->nodetype = navigation_node::NODETYPE_BRANCH; $sectionnode->nodetype = navigation_node::NODETYPE_BRANCH;
$sectionnode->hidden = (!$section->visible || !$section->available); $sectionnode->hidden = (!$section->visible || !$section->available);

View File

@ -68,6 +68,7 @@ class boostnavbar implements \renderable {
} }
} }
if ($this->page->context->contextlevel == CONTEXT_COURSE) { if ($this->page->context->contextlevel == CONTEXT_COURSE) {
$removesections = course_get_format($this->page->course)->can_sections_be_removed_from_navigation();
// Remove any duplicate navbar nodes. // Remove any duplicate navbar nodes.
$this->remove_duplicate_items(); $this->remove_duplicate_items();
// Remove 'My courses' and 'Courses' if we are in the course context. // Remove 'My courses' and 'Courses' if we are in the course context.