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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [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  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          // Add the delete content item to the menu.
 121          if ($this->contenttype->can_delete($this->content)) {
 122              $options[get_string('delete')] = [
 123                  'data-action' => 'deletecontent',
 124                  'data-contentname' => $this->content->get_name(),
 125                  'data-uses' => count($this->content->get_uses()),
 126                  'data-contentid' => $this->content->get_id(),
 127                  'data-contextid' => $this->content->get_contextid(),
 128              ];
 129          }
 130  
 131          $dropdown = [];
 132          if ($options) {
 133              foreach ($options as $key => $attribs) {
 134                  $url = $attribs['url'] ?? '#';
 135                  $dropdown['options'][] = [
 136                      'label' => $key,
 137                      'url' => $url,
 138                      'attributes' => array_map(function ($key, $value) {
 139                          return [
 140                              'name' => $key,
 141                              'value' => $value
 142                          ];
 143                      }, array_keys($attribs), $attribs)
 144                  ];
 145              }
 146          }
 147  
 148          return $dropdown;
 149      }
 150  
 151      /**
 152       * Export this data so it can be used as the context for a mustache template.
 153       *
 154       * @param renderer_base $output
 155       *
 156       * @return stdClass
 157       */
 158      public function export_for_template(renderer_base $output): stdClass {
 159          $data = new stdClass();
 160  
 161          // Get the content type html.
 162          $contenthtml = $this->contenttype->get_view_content($this->content);
 163          $data->contenthtml = $contenthtml;
 164  
 165          // Check if the user can edit this content type.
 166          if ($this->contenttype->can_edit($this->content)) {
 167              $data->usercanedit = true;
 168              $urlparams = [
 169                  'contextid' => $this->content->get_contextid(),
 170                  'plugin' => $this->contenttype->get_plugin_name(),
 171                  'id' => $this->content->get_id()
 172              ];
 173              $editcontenturl = new moodle_url('/contentbank/edit.php', $urlparams);
 174              $data->editcontenturl = $editcontenturl->out(false);
 175          }
 176  
 177          // Close/exit link for those users who can access that context.
 178          $context = context::instance_by_id($this->content->get_contextid());
 179          if (has_capability('moodle/contentbank:access', $context)) {
 180              $closeurl = new moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
 181              $data->closeurl = $closeurl->out(false);
 182          }
 183  
 184          $data->actionmenu = $this->get_edit_actions_dropdown();
 185          $data->heading = $this->content->get_name();
 186          if ($this->content->get_visibility() == content::VISIBILITY_UNLISTED) {
 187              $data->heading = get_string('visibilitytitleunlisted', 'contentbank', $data->heading);
 188          }
 189  
 190          return $data;
 191      }
 192  }