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  namespace core_contentbank\output;
  18  
  19  use context_course;
  20  use context_coursecat;
  21  use core_contentbank\content;
  22  use core_contentbank\contentbank;
  23  use renderable;
  24  use templatable;
  25  use renderer_base;
  26  use stdClass;
  27  
  28  /**
  29   * Class containing data for bank content
  30   *
  31   * @package    core_contentbank
  32   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class bankcontent implements renderable, templatable {
  36  
  37      /**
  38       * @var \core_contentbank\content[]    Array of content bank contents.
  39       */
  40      private $contents;
  41  
  42      /**
  43       * @var array   $toolbar object.
  44       */
  45      private $toolbar;
  46  
  47      /**
  48       * @var \context    Given context. Null by default.
  49       */
  50      private $context;
  51  
  52      /**
  53       * @var array   Course categories that the user has access to.
  54       */
  55      private $allowedcategories;
  56  
  57      /**
  58       * @var array   Courses that the user has access to.
  59       */
  60      private $allowedcourses;
  61  
  62      /**
  63       * Construct this renderable.
  64       *
  65       * @param \core_contentbank\content[] $contents   Array of content bank contents.
  66       * @param array $toolbar List of content bank toolbar options.
  67       * @param \context|null $context Optional context to check (default null)
  68       * @param contentbank $cb Contenbank object.
  69       */
  70      public function __construct(array $contents, array $toolbar, ?\context $context, contentbank $cb) {
  71          $this->contents = $contents;
  72          $this->toolbar = $toolbar;
  73          $this->context = $context;
  74          list($this->allowedcategories, $this->allowedcourses) = $cb->get_contexts_with_capabilities_by_user();
  75      }
  76  
  77      /**
  78       * Export the data.
  79       *
  80       * @param renderer_base $output
  81       * @return stdClass
  82       */
  83      public function export_for_template(renderer_base $output): stdClass {
  84          global $PAGE, $SITE;
  85  
  86          $PAGE->requires->js_call_amd('core_contentbank/search', 'init');
  87          $PAGE->requires->js_call_amd('core_contentbank/sort', 'init');
  88  
  89          $data = new stdClass();
  90          $contentdata = array();
  91          foreach ($this->contents as $content) {
  92              $file = $content->get_file();
  93              $filesize = $file ? $file->get_filesize() : 0;
  94              $mimetype = $file ? get_mimetype_description($file) : '';
  95              $contenttypeclass = $content->get_content_type().'\\contenttype';
  96              $contenttype = new $contenttypeclass($this->context);
  97              if ($content->get_visibility() == content::VISIBILITY_UNLISTED) {
  98                  $name = get_string('visibilitytitleunlisted', 'contentbank', $content->get_name());
  99              } else {
 100                  $name = $content->get_name();
 101              }
 102              $author = \core_user::get_user($content->get_content()->usercreated);
 103              $contentdata[] = array(
 104                  'name' => $name,
 105                  'title' => strtolower($name),
 106                  'link' => $contenttype->get_view_url($content),
 107                  'icon' => $contenttype->get_icon($content),
 108                  'uses' => count($content->get_uses()),
 109                  'timemodified' => $content->get_timemodified(),
 110                  'bytes' => $filesize,
 111                  'size' => display_size($filesize),
 112                  'type' => $mimetype,
 113                  'author' => fullname($author),
 114                  'visibilityunlisted' => $content->get_visibility() == content::VISIBILITY_UNLISTED
 115              );
 116          }
 117          $data->viewlist = get_user_preferences('core_contentbank_view_list');
 118          $data->contents = $contentdata;
 119          // The tools are displayed in the action bar on the index page.
 120          foreach ($this->toolbar as $tool) {
 121              // Customize the output of a tool, like dropdowns.
 122              $method = 'export_tool_'.$tool['action'];
 123              if (method_exists($this, $method)) {
 124                  $this->$method($tool);
 125              }
 126              $data->tools[] = $tool;
 127          }
 128  
 129          $allowedcontexts = [];
 130          $systemcontext = \context_system::instance();
 131          if (has_capability('moodle/contentbank:access', $systemcontext)) {
 132              $allowedcontexts[$systemcontext->id] = get_string('coresystem');
 133          }
 134          $options = [];
 135          foreach ($this->allowedcategories as $allowedcategory) {
 136              $options[$allowedcategory->ctxid] = format_string($allowedcategory->name, true, [
 137                  'context' => context_coursecat::instance($allowedcategory->ctxinstance),
 138              ]);
 139          }
 140          if (!empty($options)) {
 141              $allowedcontexts['categories'] = [get_string('coursecategories') => $options];
 142          }
 143          $options = [];
 144          foreach ($this->allowedcourses as $allowedcourse) {
 145              // Don't add the frontpage course to the list.
 146              if ($allowedcourse->id != $SITE->id) {
 147                  $options[$allowedcourse->ctxid] = format_string($allowedcourse->fullname, true, [
 148                      'context' => context_course::instance($allowedcourse->ctxinstance),
 149                  ]);
 150              }
 151          }
 152          if (!empty($options)) {
 153              $allowedcontexts['courses'] = [get_string('courses') => $options];
 154          }
 155          if (!empty($allowedcontexts)) {
 156              $strchoosecontext = get_string('choosecontext', 'core_contentbank');
 157              $singleselect = new \single_select(
 158                  new \moodle_url('/contentbank/index.php'),
 159                  'contextid',
 160                  $allowedcontexts,
 161                  $this->context->id,
 162                  $strchoosecontext
 163              );
 164              $singleselect->set_label($strchoosecontext, ['class' => 'sr-only']);
 165              $data->allowedcontexts = $singleselect->export_for_template($output);
 166          }
 167  
 168          return $data;
 169      }
 170  
 171      /**
 172       * Adds the content type items to display to the Add dropdown.
 173       *
 174       * Each content type is represented as an object with the properties:
 175       *     - name: the name of the content type.
 176       *     - baseurl: the base content type editor URL.
 177       *     - types: different types of the content type to display as dropdown items.
 178       *
 179       * @param array $tool Data for rendering the Add dropdown, including the editable content types.
 180       */
 181      private function export_tool_add(array &$tool) {
 182          $editabletypes = $tool['contenttypes'];
 183  
 184          $addoptions = [];
 185          foreach ($editabletypes as $class => $type) {
 186              $contentype = new $class($this->context);
 187              // Get the creation options of each content type.
 188              $types = $contentype->get_contenttype_types();
 189              if ($types) {
 190                  // Add a text describing the content type as first option. This will be displayed in the drop down to
 191                  // separate the options for the different content types.
 192                  $contentdesc = new stdClass();
 193                  $contentdesc->typename = get_string('description', $contentype->get_contenttype_name());
 194                  array_unshift($types, $contentdesc);
 195                  // Context data for the template.
 196                  $addcontenttype = new stdClass();
 197                  // Content type name.
 198                  $addcontenttype->name = $type;
 199                  // Content type editor base URL.
 200                  $tool['link']->param('plugin', $type);
 201                  $addcontenttype->baseurl = $tool['link']->out();
 202                  // Different types of the content type.
 203                  $addcontenttype->types = $types;
 204                  $addoptions[] = $addcontenttype;
 205              }
 206          }
 207  
 208          $tool['contenttypes'] = $addoptions;
 209      }
 210  }