Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Contains the default activity control menu.
  19   *
  20   * @package   core_courseformat
  21   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_courseformat\output\local\content\cm;
  26  
  27  use action_menu;
  28  use action_menu_link;
  29  use cm_info;
  30  use core\output\named_templatable;
  31  use core_courseformat\base as course_format;
  32  use core_courseformat\output\local\courseformat_named_templatable;
  33  use renderable;
  34  use section_info;
  35  use stdClass;
  36  
  37  /**
  38   * Base class to render a course module menu inside a course format.
  39   *
  40   * @package   core_courseformat
  41   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  42   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class controlmenu implements named_templatable, renderable {
  45  
  46      use courseformat_named_templatable;
  47  
  48      /** @var course_format the course format */
  49      protected $format;
  50  
  51      /** @var section_info the section object */
  52      private $section;
  53  
  54      /** @var action_menu the activity aciton menu */
  55      protected $menu;
  56  
  57      /** @var cm_info the course module instance */
  58      protected $mod;
  59  
  60      /** @var array optional display options */
  61      protected $displayoptions;
  62  
  63      /**
  64       * Constructor.
  65       *
  66       * @param course_format $format the course format
  67       * @param section_info $section the section info
  68       * @param cm_info $mod the course module ionfo
  69       * @param array $displayoptions optional extra display options
  70       */
  71      public function __construct(course_format $format, section_info $section, cm_info $mod, array $displayoptions = []) {
  72          $this->format = $format;
  73          $this->section = $section;
  74          $this->mod = $mod;
  75          $this->displayoptions = $displayoptions;
  76      }
  77  
  78      /**
  79       * Export this data so it can be used as the context for a mustache template.
  80       *
  81       * @param \renderer_base $output typically, the renderer that's calling this function
  82       * @return stdClass data context for a mustache template
  83       */
  84      public function export_for_template(\renderer_base $output): stdClass {
  85  
  86          $mod = $this->mod;
  87  
  88          $menu = $this->get_action_menu($output);
  89  
  90          if (empty($menu)) {
  91              return new stdClass();
  92          }
  93  
  94          $data = (object)[
  95              'menu' => $menu->export_for_template($output),
  96              'hasmenu' => true,
  97              'id' => $mod->id,
  98          ];
  99  
 100          // After icons.
 101          if (!empty($mod->afterediticons)) {
 102              $data->afterediticons = $mod->afterediticons;
 103          }
 104  
 105          return $data;
 106      }
 107  
 108      /**
 109       * Generate the aciton menu element.
 110       *
 111       * This method is public in case some block needs to modify the menu before output it.
 112       * @param \renderer_base $output typically, the renderer that's calling this function
 113       * @return aciton_menu the activity action menu
 114       */
 115      public function get_action_menu(\renderer_base $output): ?action_menu {
 116  
 117          if (!empty($this->menu)) {
 118              return $this->menu;
 119          }
 120  
 121          $mod = $this->mod;
 122  
 123          $controls = $this->cm_control_items();
 124  
 125          if (empty($controls)) {
 126              return null;
 127          }
 128  
 129          // Convert control array into an action_menu.
 130          $menu = new action_menu();
 131          $icon = $output->pix_icon('i/menu', get_string('edit'));
 132          $menu->set_menu_trigger($icon, 'btn btn-icon d-flex align-items-center justify-content-center');
 133  
 134          $menu->attributes['class'] .= ' section-cm-edit-actions commands';
 135  
 136          // Prioritise the menu ahead of all other actions.
 137          $menu->prioritise = true;
 138  
 139          $ownerselector = $displayoptions['ownerselector'] ?? '#module-' . $mod->id;
 140          $menu->set_owner_selector($ownerselector);
 141  
 142          $constraint = $displayoptions['constraintselector'] ?? '.course-content';
 143          $menu->set_constraint($constraint);
 144  
 145          foreach ($controls as $control) {
 146              if ($control instanceof action_menu_link) {
 147                  $control->add_class('cm-edit-action');
 148              }
 149              $menu->add($control);
 150          }
 151  
 152          $this->menu = $menu;
 153  
 154          return $menu;
 155      }
 156  
 157      /**
 158       * Generate the edit control items of a course module.
 159       *
 160       * This method uses course_get_cm_edit_actions function to get the cm actions.
 161       * However, format plugins can override the method to add or remove elements
 162       * from the menu.
 163       *
 164       * @return array of edit control items
 165       */
 166      protected function cm_control_items() {
 167          $format = $this->format;
 168          $mod = $this->mod;
 169          $sectionreturn = $format->get_section_number();
 170          if (!empty($this->displayoptions['disableindentation']) || !$format->uses_indentation()) {
 171              $indent = -1;
 172          } else {
 173              $indent = $mod->indent;
 174          }
 175          return course_get_cm_edit_actions($mod, $indent, $sectionreturn);
 176      }
 177  }