Search moodle.org's
Developer Documentation

See Release Notes

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