Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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 section controls output class.
  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\section;
  26  
  27  use action_menu;
  28  use action_menu_link_secondary;
  29  use context_course;
  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 moodle_url;
  34  use pix_icon;
  35  use renderable;
  36  use section_info;
  37  use stdClass;
  38  
  39  /**
  40   * Base class to render section controls.
  41   *
  42   * @package   core_courseformat
  43   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  44   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class controlmenu implements named_templatable, renderable {
  47  
  48      use courseformat_named_templatable;
  49  
  50      /** @var course_format the course format class */
  51      protected $format;
  52  
  53      /** @var section_info the course section class */
  54      protected $section;
  55  
  56      /**
  57       * Constructor.
  58       *
  59       * @param course_format $format the course format
  60       * @param section_info $section the section info
  61       */
  62      public function __construct(course_format $format, section_info $section) {
  63          $this->format = $format;
  64          $this->section = $section;
  65      }
  66  
  67      /**
  68       * Export this data so it can be used as the context for a mustache template.
  69       *
  70       * @param renderer_base $output typically, the renderer that's calling this function
  71       * @return array data context for a mustache template
  72       */
  73      public function export_for_template(\renderer_base $output): stdClass {
  74  
  75          $section = $this->section;
  76  
  77          $controls = $this->section_control_items();
  78  
  79          if (empty($controls)) {
  80              return new stdClass();
  81          }
  82  
  83          // Convert control array into an action_menu.
  84          $menu = new action_menu();
  85          $icon = $output->pix_icon('i/menu', get_string('edit'));
  86          $menu->set_menu_trigger($icon, 'btn btn-icon d-flex align-items-center justify-content-center');
  87          $menu->attributes['class'] .= ' section-actions';
  88          foreach ($controls as $value) {
  89              $url = empty($value['url']) ? '' : $value['url'];
  90              $icon = empty($value['icon']) ? '' : $value['icon'];
  91              $name = empty($value['name']) ? '' : $value['name'];
  92              $attr = empty($value['attr']) ? [] : $value['attr'];
  93              $class = empty($value['pixattr']['class']) ? '' : $value['pixattr']['class'];
  94              $al = new action_menu_link_secondary(
  95                  new moodle_url($url),
  96                  new pix_icon($icon, '', null, ['class' => "smallicon " . $class]),
  97                  $name,
  98                  $attr
  99              );
 100              $menu->add($al);
 101          }
 102  
 103          $data = (object)[
 104              'menu' => $output->render($menu),
 105              'hasmenu' => true,
 106              'id' => $section->id,
 107          ];
 108  
 109          return $data;
 110      }
 111  
 112      /**
 113       * Generate the edit control items of a section.
 114       *
 115       * It is not clear this kind of controls are still available in 4.0 so, for now, this
 116       * method is almost a clone of the previous section_control_items from the course/renderer.php.
 117       *
 118       * This method must remain public until the final deprecation of section_edit_control_items.
 119       *
 120       * @return array of edit control items
 121       */
 122      public function section_control_items() {
 123          global $USER;
 124  
 125          $format = $this->format;
 126          $section = $this->section;
 127          $course = $format->get_course();
 128          $sectionreturn = $format->get_section_number();
 129          $user = $USER;
 130  
 131          $usecomponents = $format->supports_components();
 132          $coursecontext = context_course::instance($course->id);
 133          $numsections = $format->get_last_section_number();
 134          $isstealth = $section->section > $numsections;
 135  
 136          $baseurl = course_get_url($course, $sectionreturn);
 137          $baseurl->param('sesskey', sesskey());
 138  
 139          $controls = [];
 140  
 141          if (!$isstealth && has_capability('moodle/course:update', $coursecontext, $user)) {
 142              if ($section->section > 0
 143                  && get_string_manager()->string_exists('editsection', 'format_'.$format->get_format())) {
 144                  $streditsection = get_string('editsection', 'format_'.$format->get_format());
 145              } else {
 146                  $streditsection = get_string('editsection');
 147              }
 148  
 149              $controls['edit'] = [
 150                  'url'   => new moodle_url('/course/editsection.php', ['id' => $section->id, 'sr' => $sectionreturn]),
 151                  'icon' => 'i/settings',
 152                  'name' => $streditsection,
 153                  'pixattr' => ['class' => ''],
 154                  'attr' => ['class' => 'icon edit'],
 155              ];
 156          }
 157  
 158          if ($section->section) {
 159              $url = clone($baseurl);
 160              if (!$isstealth) {
 161                  if (has_capability('moodle/course:sectionvisibility', $coursecontext, $user)) {
 162                      if ($section->visible) { // Show the hide/show eye.
 163                          $strhidefromothers = get_string('hidefromothers', 'format_'.$course->format);
 164                          $url->param('hide', $section->section);
 165                          $controls['visiblity'] = [
 166                              'url' => $url,
 167                              'icon' => 'i/hide',
 168                              'name' => $strhidefromothers,
 169                              'pixattr' => ['class' => ''],
 170                              'attr' => [
 171                                  'class' => 'icon editing_showhide',
 172                                  'data-sectionreturn' => $sectionreturn,
 173                                  'data-action' => 'hide',
 174                              ],
 175                          ];
 176                      } else {
 177                          $strshowfromothers = get_string('showfromothers', 'format_'.$course->format);
 178                          $url->param('show',  $section->section);
 179                          $controls['visiblity'] = [
 180                              'url' => $url,
 181                              'icon' => 'i/show',
 182                              'name' => $strshowfromothers,
 183                              'pixattr' => ['class' => ''],
 184                              'attr' => [
 185                                  'class' => 'icon editing_showhide',
 186                                  'data-sectionreturn' => $sectionreturn,
 187                                  'data-action' => 'show',
 188                              ],
 189                          ];
 190                      }
 191                  }
 192  
 193                  if (!$sectionreturn && has_capability('moodle/course:movesections', $coursecontext, $user)) {
 194                      if ($usecomponents) {
 195                          // This tool will appear only when the state is ready.
 196                          $url = clone ($baseurl);
 197                          $url->param('movesection', $section->section);
 198                          $url->param('section', $section->section);
 199                          $controls['movesection'] = [
 200                              'url' => $url,
 201                              'icon' => 'i/dragdrop',
 202                              'name' => get_string('move', 'moodle'),
 203                              'pixattr' => ['class' => ''],
 204                              'attr' => [
 205                                  'class' => 'icon move waitstate',
 206                                  'data-action' => 'moveSection',
 207                                  'data-id' => $section->id,
 208                              ],
 209                          ];
 210                      }
 211                      // Legacy move up and down links for non component-based formats.
 212                      $url = clone($baseurl);
 213                      if ($section->section > 1) { // Add a arrow to move section up.
 214                          $url->param('section', $section->section);
 215                          $url->param('move', -1);
 216                          $strmoveup = get_string('moveup');
 217                          $controls['moveup'] = [
 218                              'url' => $url,
 219                              'icon' => 'i/up',
 220                              'name' => $strmoveup,
 221                              'pixattr' => ['class' => ''],
 222                              'attr' => ['class' => 'icon moveup whilenostate'],
 223                          ];
 224                      }
 225  
 226                      $url = clone($baseurl);
 227                      if ($section->section < $numsections) { // Add a arrow to move section down.
 228                          $url->param('section', $section->section);
 229                          $url->param('move', 1);
 230                          $strmovedown = get_string('movedown');
 231                          $controls['movedown'] = [
 232                              'url' => $url,
 233                              'icon' => 'i/down',
 234                              'name' => $strmovedown,
 235                              'pixattr' => ['class' => ''],
 236                              'attr' => ['class' => 'icon movedown whilenostate'],
 237                          ];
 238                      }
 239                  }
 240              }
 241  
 242              if (course_can_delete_section($course, $section)) {
 243                  if (get_string_manager()->string_exists('deletesection', 'format_'.$course->format)) {
 244                      $strdelete = get_string('deletesection', 'format_'.$course->format);
 245                  } else {
 246                      $strdelete = get_string('deletesection');
 247                  }
 248                  $url = new moodle_url(
 249                      '/course/editsection.php',
 250                      [
 251                          'id' => $section->id,
 252                          'sr' => $sectionreturn,
 253                          'delete' => 1,
 254                          'sesskey' => sesskey(),
 255                      ]
 256                  );
 257                  $controls['delete'] = [
 258                      'url' => $url,
 259                      'icon' => 'i/delete',
 260                      'name' => $strdelete,
 261                      'pixattr' => ['class' => ''],
 262                      'attr' => [
 263                          'class' => 'icon editing_delete',
 264                          'data-action' => 'deleteSection',
 265                          'data-id' => $section->id,
 266                      ],
 267                  ];
 268              }
 269          }
 270  
 271          return $controls;
 272      }
 273  }