Skip to content

Commit

Permalink
MDL-82510 course: Add new delegated action menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaia Anabitarte committed Jul 17, 2024
1 parent fd487cd commit 34c6984
Show file tree
Hide file tree
Showing 9 changed files with 528 additions and 103 deletions.
9 changes: 9 additions & 0 deletions .upgradenotes/MDL-82510-2024071711550004.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
issueNumber: MDL-82510
notes:
core_courseformat:
- message: >-
New \core_courseformat\output\local\content\basecontrolmenu class
has been created. Existing \core_courseformat\output\local\content\cm\controlmenu
and \core_courseformat\output\local\content\section\controlmenu classes extend
the new \core_courseformat\output\local\content\basecontrolmenu class.
type: improved
9 changes: 9 additions & 0 deletions .upgradenotes/MDL-82510-2024071722441237.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
issueNumber: MDL-82510
notes:
core_course:
- message: >-
New \core_courseformat\output\local\content\cmdelegatedcontrolmenu class
has been created extending
\core_courseformat\output\local\content\basecontrolmenu class to render
delegated section action menu combining section and module action menu.
type: improved
179 changes: 179 additions & 0 deletions course/format/classes/output/local/content/basecontrolmenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?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/>.

/**
* Contains the default section controls output class.
*
* @package core_courseformat
* @copyright 2020 Ferran Recio <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_courseformat\output\local\content;

use action_menu;
use action_menu_link_secondary;
use core\output\named_templatable;
use core_courseformat\base as course_format;
use core_courseformat\output\local\courseformat_named_templatable;
use moodle_url;
use pix_icon;
use renderable;
use section_info;
use cm_info;
use stdClass;

/**
* Base class to render section controls.
*
* @package core_courseformat
* @copyright 2020 Ferran Recio <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class basecontrolmenu implements named_templatable, renderable {

use courseformat_named_templatable;

/** @var course_format the course format class */
protected $format;

/** @var section_info the course section class */
protected $section;

/** @var cm_info the course module class */
protected $mod;

/** @var string the menu ID */
protected $menuid;

/** @var action_menu the action menu */
protected $menu;

/**
* Constructor.
*
* @param course_format $format the course format
* @param section_info $section the section info
* @param cm_info|null $mod the module info
* @param string $menuid the ID value for the menu
*/
public function __construct(course_format $format, section_info $section, ?cm_info $mod = null, string $menuid = '') {
$this->format = $format;
$this->section = $section;
$this->mod = $mod;
$this->menuid = $menuid;
}

/**
* Export this data so it can be used as the context for a mustache template.
*
* @param \renderer_base $output typically, the renderer that's calling this function
* @return array data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
$menu = $this->get_action_menu($output);
if (empty($menu)) {
return new stdClass();
}

$data = (object)[
'menu' => $output->render($menu),
'hasmenu' => true,
'id' => $this->menuid,
];

return $data;
}

/**
* Generate the action menu element depending on the section.
*
* Sections controlled by a plugin will delegate the control menu to the delegated section class.
*
* @param \renderer_base $output typically, the renderer that's calling this function
* @return action_menu|null the activity action menu or null if no action menu is available
*/
public function get_action_menu(\renderer_base $output): ?action_menu {

if (!empty($this->menu)) {
return $this->menu;
}

$this->menu = $this->get_default_action_menu($output);
return $this->menu;
}

/**
* Generate the default section action menu.
*
* This method is public in case some block needs to modify the menu before output it.
*
* @param \renderer_base $output typically, the renderer that's calling this function
* @return action_menu|null the activity action menu
*/
public function get_default_action_menu(\renderer_base $output): ?action_menu {
return null;
}

/**
* Generate the default section action menu.
*
* This method is public in case some block needs to modify the menu before output it.
*
* @param \renderer_base $output typically, the renderer that's calling this function
* @return action_menu|null the activity action menu
*/
protected function format_controls(array $controls): ?action_menu {
if (empty($controls)) {
return null;
}

// Convert control array into an action_menu.
$menu = new action_menu();
$menu->set_kebab_trigger(get_string('edit'));
$menu->attributes['class'] .= ' section-actions';
$menu->attributes['data-sectionid'] = $this->section->id;
foreach ($controls as $value) {
$url = empty($value['url']) ? '' : $value['url'];
$icon = empty($value['icon']) ? '' : $value['icon'];
$name = empty($value['name']) ? '' : $value['name'];
$attr = empty($value['attr']) ? [] : $value['attr'];
$class = empty($value['pixattr']['class']) ? '' : $value['pixattr']['class'];
$al = new action_menu_link_secondary(
new moodle_url($url),
new pix_icon($icon, '', null, ['class' => "smallicon " . $class]),
$name,
$attr
);
$menu->add($al);
}
return $menu;
}

/**
* Generate the edit control items of a section.
*
* It is not clear this kind of controls are still available in 4.0 so, for now, this
* method is almost a clone of the previous section_control_items from the course/renderer.php.
*
* This method must remain public until the final deprecation of section_edit_control_items.
*
* @return array of edit control items
*/
public function section_control_items() {
return [];
}
}
33 changes: 12 additions & 21 deletions course/format/classes/output/local/content/cm/controlmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
use action_menu;
use action_menu_link;
use cm_info;
use core\output\named_templatable;
use core_courseformat\base as course_format;
use core_courseformat\output\local\content\basecontrolmenu;
use core_courseformat\output\local\courseformat_named_templatable;
use renderable;
use section_info;
use stdClass;

Expand All @@ -41,21 +40,7 @@
* @copyright 2020 Ferran Recio <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class controlmenu implements named_templatable, renderable {

use courseformat_named_templatable;

/** @var course_format the course format */
protected $format;

/** @var section_info the section object */
private $section;

/** @var action_menu the activity aciton menu */
protected $menu;

/** @var cm_info the course module instance */
protected $mod;
class controlmenu extends basecontrolmenu {

/** @var array optional display options */
protected $displayoptions;
Expand All @@ -69,9 +54,7 @@ class controlmenu implements named_templatable, renderable {
* @param array $displayoptions optional extra display options
*/
public function __construct(course_format $format, section_info $section, cm_info $mod, array $displayoptions = []) {
$this->format = $format;
$this->section = $section;
$this->mod = $mod;
parent::__construct($format, $section, $mod, $mod->id);
$this->displayoptions = $displayoptions;
}

Expand All @@ -94,7 +77,7 @@ public function export_for_template(\renderer_base $output): stdClass {
$data = (object)[
'menu' => $menu->export_for_template($output),
'hasmenu' => true,
'id' => $mod->id,
'id' => $this->menuid,
];

// After icons.
Expand All @@ -120,6 +103,14 @@ public function get_action_menu(\renderer_base $output): ?action_menu {

$mod = $this->mod;

// In case module is delegating a section, we should return delegated section action menu.
if ($delegated = $mod->get_delegated_section_info()) {
$controlmenuclass = $this->format->get_output_classname('content\\cm\\delegatedcontrolmenu');
$controlmenu = new $controlmenuclass($this->format, $delegated, $mod);

return $controlmenu->get_action_menu($output);
}

$controls = $this->cm_control_items();

if (empty($controls)) {
Expand Down
Loading

0 comments on commit 34c6984

Please sign in to comment.