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 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [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  
  18  namespace core_contentbank\output;
  19  
  20  use context;
  21  use core_contentbank\content;
  22  use core_contentbank\contenttype;
  23  use moodle_url;
  24  use renderable;
  25  use renderer_base;
  26  use stdClass;
  27  use templatable;
  28  
  29  /**
  30   * Class containing data for the content view.
  31   *
  32   * @copyright  2020 Victor Deniz <victor@moodle.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class viewcontent implements renderable, templatable {
  36      /**
  37       * @var contenttype Content bank content type.
  38       */
  39      private $contenttype;
  40  
  41      /**
  42       * @var stdClass Record of the contentbank_content table.
  43       */
  44      private $content;
  45  
  46      /**
  47       * Construct this renderable.
  48       *
  49       * @param contenttype $contenttype Content bank content type.
  50       * @param content $content Record of the contentbank_content table.
  51       */
  52      public function __construct(contenttype $contenttype, content $content) {
  53          $this->contenttype = $contenttype;
  54          $this->content = $content;
  55      }
  56  
  57      /**
  58       * Get the content of the "More" dropdown in the tertiary navigation
  59       *
  60       * @return array|null The options to be displayed in a dropdown in the tertiary navigation
  61       * @throws \moodle_exception
  62       */
  63      protected function get_edit_actions_dropdown(): ?array {
  64          global $PAGE;
  65          $options = [];
  66          if ($this->contenttype->can_manage($this->content)) {
  67              // Add the visibility item to the menu.
  68              switch($this->content->get_visibility()) {
  69                  case content::VISIBILITY_UNLISTED:
  70                      $visibilitylabel = get_string('visibilitysetpublic', 'core_contentbank');
  71                      $newvisibility = content::VISIBILITY_PUBLIC;
  72                      break;
  73                  case content::VISIBILITY_PUBLIC:
  74                      $visibilitylabel = get_string('visibilitysetunlisted', 'core_contentbank');
  75                      $newvisibility = content::VISIBILITY_UNLISTED;
  76                      break;
  77                  default:
  78                      $url = new \moodle_url('/contentbank/index.php', ['contextid' => $this->content->get_contextid()]);
  79                      throw new moodle_exception('contentvisibilitynotfound', 'error', $url, $this->content->get_visibility());
  80              }
  81  
  82              if ($visibilitylabel) {
  83                  $options[$visibilitylabel] = [
  84                      'data-action' => 'setcontentvisibility',
  85                      'data-visibility' => $newvisibility,
  86                      'data-contentid' => $this->content->get_id(),
  87                  ];
  88              }
  89  
  90              // Add the rename content item to the menu.
  91              $options[get_string('rename')] = [
  92                  'data-action' => 'renamecontent',
  93                  'data-contentname' => $this->content->get_name(),
  94                  'data-contentid' => $this->content->get_id(),
  95              ];
  96  
  97              if ($this->contenttype->can_upload()) {
  98                  $options[get_string('replacecontent', 'contentbank')] = ['data-action' => 'upload'];
  99  
 100                  $PAGE->requires->js_call_amd(
 101                      'core_contentbank/upload',
 102                      'initModal',
 103                      [
 104                          '[data-action=upload]',
 105                          \core_contentbank\form\upload_files::class,
 106                          $this->content->get_contextid(),
 107                          $this->content->get_id()
 108                      ]
 109                  );
 110              }
 111          }
 112  
 113          if ($this->contenttype->can_download($this->content)) {
 114              $url = new moodle_url($this->contenttype->get_download_url($this->content));
 115              $options[get_string('download')] = [
 116                  'url' => $url->out()
 117              ];
 118          }
 119  
 120          if ($this->contenttype->can_copy($this->content)) {
 121              // Add the copy content item to the menu.
 122              $options[get_string('copycontent', 'contentbank')] = [
 123                  'data-action' => 'copycontent',
 124                  'data-contentname' => get_string('copyof', 'contentbank', $this->content->get_name()),
 125                  'data-contentid' => $this->content->get_id(),
 126              ];
 127          }
 128  
 129          // Add the delete content item to the menu.
 130          if ($this->contenttype->can_delete($this->content)) {
 131              $options[get_string('delete')] = [
 132                  'data-action' => 'deletecontent',
 133                  'data-contentname' => $this->content->get_name(),
 134                  'data-uses' => count($this->content->get_uses()),
 135                  'data-contentid' => $this->content->get_id(),
 136                  'data-contextid' => $this->content->get_contextid(),
 137              ];
 138          }
 139  
 140          $dropdown = [];
 141          if ($options) {
 142              foreach ($options as $key => $attribs) {
 143                  $url = $attribs['url'] ?? '#';
 144                  $dropdown['options'][] = [
 145                      'label' => $key,
 146                      'url' => $url,
 147                      'attributes' => array_map(function ($key, $value) {
 148                          return [
 149                              'name' => $key,
 150                              'value' => $value
 151                          ];
 152                      }, array_keys($attribs), $attribs)
 153                  ];
 154              }
 155          }
 156  
 157          return $dropdown;
 158      }
 159  
 160      /**
 161       * Export this data so it can be used as the context for a mustache template.
 162       *
 163       * @param renderer_base $output
 164       *
 165       * @return stdClass
 166       */
 167      public function export_for_template(renderer_base $output): stdClass {
 168          $data = new stdClass();
 169  
 170          // Get the content type html.
 171          $contenthtml = $this->contenttype->get_view_content($this->content);
 172          $data->contenthtml = $contenthtml;
 173  
 174          // Check if the user can edit this content type.
 175          if ($this->contenttype->can_edit($this->content)) {
 176              $data->usercanedit = true;
 177              $urlparams = [
 178                  'contextid' => $this->content->get_contextid(),
 179                  'plugin' => $this->contenttype->get_plugin_name(),
 180                  'id' => $this->content->get_id()
 181              ];
 182              $editcontenturl = new moodle_url('/contentbank/edit.php', $urlparams);
 183              $data->editcontenturl = $editcontenturl->out(false);
 184          }
 185  
 186          // Close/exit link for those users who can access that context.
 187          $context = context::instance_by_id($this->content->get_contextid());
 188          if (has_capability('moodle/contentbank:access', $context)) {
 189              $closeurl = new moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
 190              $data->closeurl = $closeurl->out(false);
 191          }
 192  
 193          $data->actionmenu = $this->get_edit_actions_dropdown();
 194          $data->heading = $this->content->get_name();
 195          if ($this->content->get_visibility() == content::VISIBILITY_UNLISTED) {
 196              $data->heading = get_string('visibilitytitleunlisted', 'contentbank', $data->heading);
 197          }
 198  
 199          return $data;
 200      }
 201  }