Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 402 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  namespace core_courseformat\output\local\content;
  18  
  19  use core\output\named_templatable;
  20  use core_courseformat\base as course_format;
  21  use core_courseformat\output\local\courseformat_named_templatable;
  22  use renderable;
  23  use stdClass;
  24  
  25  /**
  26   * Contains the bulk editor tools bar.
  27   *
  28   * @package   core_courseformat
  29   * @copyright 2023 Ferran Recio <ferran@moodle.com>
  30   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class bulkedittools implements named_templatable, renderable {
  33      use courseformat_named_templatable;
  34  
  35      /** @var core_courseformat\base the course format class */
  36      protected $format;
  37  
  38      /**
  39       * Constructor.
  40       *
  41       * @param course_format $format the course format
  42       */
  43      public function __construct(course_format $format) {
  44          $this->format = $format;
  45      }
  46  
  47      /**
  48       * Export this data so it can be used as the context for a mustache template (core/inplace_editable).
  49       *
  50       * @param renderer_base $output typically, the renderer that's calling this function
  51       * @return stdClass data context for a mustache template
  52       */
  53      public function export_for_template(\renderer_base $output): stdClass {
  54          $format = $this->format;
  55          $course = $format->get_course();
  56  
  57          $data = (object)[
  58              'id' => $course->id,
  59              'actions' => $this->get_toolbar_actions(),
  60          ];
  61          $data->hasactions = !empty($data->actions);
  62          return $data;
  63      }
  64  
  65      /**
  66       * Get the toolbar actions.
  67       * @return array the array of buttons
  68       */
  69      protected function get_toolbar_actions(): array {
  70          return array_merge(
  71              array_values($this->section_control_items()),
  72              array_values($this->cm_control_items()),
  73          );
  74      }
  75  
  76      /**
  77       * Generate the bulk edit control items of a course module.
  78       *
  79       * Format plugins can override the method to add or remove elements
  80       * from the toolbar.
  81       *
  82       * @return array of edit control items
  83       */
  84      protected function cm_control_items(): array {
  85          global $USER;
  86          $format = $this->format;
  87          $context = $format->get_context();
  88          $user = $USER;
  89  
  90          $controls = [];
  91  
  92          if (has_capability('moodle/course:activityvisibility', $context, $user)) {
  93              $controls['availability'] = [
  94                  'icon' => 't/show',
  95                  'action' => 'cmAvailability',
  96                  'name' => get_string('availability'),
  97                  'title' => get_string('cmavailability', 'core_courseformat'),
  98                  'bulk' => 'cm',
  99              ];
 100          }
 101  
 102  
 103          $duplicatecapabilities = ['moodle/backup:backuptargetimport', 'moodle/restore:restoretargetimport'];
 104          if (has_all_capabilities($duplicatecapabilities, $context, $user)) {
 105              $controls['duplicate'] = [
 106                  'icon' => 't/copy',
 107                  'action' => 'cmDuplicate',
 108                  'name' => get_string('duplicate'),
 109                  'title' => get_string('cmsduplicate', 'core_courseformat'),
 110                  'bulk' => 'cm',
 111              ];
 112          }
 113  
 114  
 115          $hasmanageactivities = has_capability('moodle/course:manageactivities', $context, $user);
 116          if ($hasmanageactivities) {
 117              $controls['move'] = [
 118                  'icon' => 'i/dragdrop',
 119                  'action' => 'moveCm',
 120                  'name' => get_string('move'),
 121                  'title' => get_string('cmsmove', 'core_courseformat'),
 122                  'bulk' => 'cm',
 123              ];
 124  
 125              $controls['delete'] = [
 126                  'icon' => 'i/delete',
 127                  'action' => 'cmDelete',
 128                  'name' => get_string('delete'),
 129                  'title' => get_string('cmsdelete', 'core_courseformat'),
 130                  'bulk' => 'cm',
 131              ];
 132          }
 133  
 134          return $controls;
 135      }
 136  
 137      /**
 138       * Generate the bulk edit control items of a section.
 139       *
 140       * Format plugins can override the method to add or remove elements
 141       * from the toolbar.
 142       *
 143       * @return array of edit control items
 144       */
 145      protected function section_control_items(): array {
 146          global $USER;
 147          $format = $this->format;
 148          $context = $format->get_context();
 149          $sectionreturn = $format->get_section_number();
 150          $user = $USER;
 151  
 152          $controls = [];
 153  
 154          if (has_capability('moodle/course:sectionvisibility', $context, $user)) {
 155              $controls['availability'] = [
 156                  'icon' => 't/show',
 157                  'action' => 'sectionAvailability',
 158                  'name' => get_string('availability'),
 159                  'title' => $this->format->get_format_string('sectionsavailability'),
 160                  'bulk' => 'section',
 161              ];
 162          }
 163  
 164          if (!$sectionreturn && has_capability('moodle/course:movesections', $context, $user)) {
 165              $controls['move'] = [
 166                  'icon' => 'i/dragdrop',
 167                  'action' => 'moveSection',
 168                  'name' => get_string('move', 'moodle'),
 169                  'title' => $this->format->get_format_string('sectionsmove'),
 170                  'bulk' => 'section',
 171              ];
 172          }
 173  
 174          $deletecapabilities = ['moodle/course:movesections', 'moodle/course:update'];
 175          if (!$sectionreturn && has_all_capabilities($deletecapabilities, $context, $user)) {
 176              $controls['delete'] = [
 177                  'icon' => 'i/delete',
 178                  'action' => 'deleteSection',
 179                  'name' => get_string('delete'),
 180                  'title' => $this->format->get_format_string('sectionsdelete'),
 181                  'bulk' => 'section',
 182              ];
 183          }
 184  
 185          return $controls;
 186      }
 187  }