Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   * core_contentbank specific renderers
  19   *
  20   * @package   core_contentbank
  21   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_contentbank\output;
  26  
  27  use renderable;
  28  use templatable;
  29  use renderer_base;
  30  use stdClass;
  31  use core_contentbank\content;
  32  
  33  /**
  34   * Class containing data for bank content
  35   *
  36   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class bankcontent implements renderable, templatable {
  40  
  41      /**
  42       * @var \core_contentbank\content[]    Array of content bank contents.
  43       */
  44      private $contents;
  45  
  46      /**
  47       * @var array   $toolbar object.
  48       */
  49      private $toolbar;
  50  
  51      /**
  52       * @var \context    Given context. Null by default.
  53       */
  54      private $context;
  55  
  56      /**
  57       * Construct this renderable.
  58       *
  59       * @param \core_contentbank\content[] $contents   Array of content bank contents.
  60       * @param array $toolbar     List of content bank toolbar options.
  61       * @param \context|null $context Optional context to check (default null)
  62       */
  63      public function __construct(array $contents, array $toolbar, ?\context $context) {
  64          $this->contents = $contents;
  65          $this->toolbar = $toolbar;
  66          $this->context = $context;
  67      }
  68  
  69      /**
  70       * Export the data.
  71       *
  72       * @param renderer_base $output
  73       * @return stdClass
  74       */
  75      public function export_for_template(renderer_base $output): stdClass {
  76          global $PAGE;
  77  
  78          $PAGE->requires->js_call_amd('core_contentbank/search', 'init');
  79          $PAGE->requires->js_call_amd('core_contentbank/sort', 'init');
  80  
  81          $data = new stdClass();
  82          $contentdata = array();
  83          foreach ($this->contents as $content) {
  84              $file = $content->get_file();
  85              $filesize = $file ? $file->get_filesize() : 0;
  86              $mimetype = $file ? get_mimetype_description($file) : '';
  87              $contenttypeclass = $content->get_content_type().'\\contenttype';
  88              $contenttype = new $contenttypeclass($this->context);
  89              if ($content->get_visibility() == content::VISIBILITY_UNLISTED) {
  90                  $name = get_string('visibilitytitleunlisted', 'contentbank', $content->get_name());
  91              } else {
  92                  $name = $content->get_name();
  93              }
  94              $author = \core_user::get_user($content->get_content()->usercreated);
  95              $contentdata[] = array(
  96                  'name' => $name,
  97                  'title' => strtolower($name),
  98                  'link' => $contenttype->get_view_url($content),
  99                  'icon' => $contenttype->get_icon($content),
 100                  'uses' => count($content->get_uses()),
 101                  'timemodified' => $content->get_timemodified(),
 102                  'bytes' => $filesize,
 103                  'size' => display_size($filesize),
 104                  'type' => $mimetype,
 105                  'author' => fullname($author),
 106                  'visibilityunlisted' => $content->get_visibility() == content::VISIBILITY_UNLISTED
 107              );
 108          }
 109          $data->viewlist = get_user_preferences('core_contentbank_view_list');
 110          $data->contents = $contentdata;
 111          // The tools are displayed in the action bar on the index page.
 112          foreach ($this->toolbar as $tool) {
 113              // Customize the output of a tool, like dropdowns.
 114              $method = 'export_tool_'.$tool['action'];
 115              if (method_exists($this, $method)) {
 116                  $this->$method($tool);
 117              }
 118              $data->tools[] = $tool;
 119          }
 120  
 121          return $data;
 122      }
 123  
 124      /**
 125       * Adds the content type items to display to the Add dropdown.
 126       *
 127       * Each content type is represented as an object with the properties:
 128       *     - name: the name of the content type.
 129       *     - baseurl: the base content type editor URL.
 130       *     - types: different types of the content type to display as dropdown items.
 131       *
 132       * @param array $tool Data for rendering the Add dropdown, including the editable content types.
 133       */
 134      private function export_tool_add(array &$tool) {
 135          $editabletypes = $tool['contenttypes'];
 136  
 137          $addoptions = [];
 138          foreach ($editabletypes as $class => $type) {
 139              $contentype = new $class($this->context);
 140              // Get the creation options of each content type.
 141              $types = $contentype->get_contenttype_types();
 142              if ($types) {
 143                  // Add a text describing the content type as first option. This will be displayed in the drop down to
 144                  // separate the options for the different content types.
 145                  $contentdesc = new stdClass();
 146                  $contentdesc->typename = get_string('description', $contentype->get_contenttype_name());
 147                  array_unshift($types, $contentdesc);
 148                  // Context data for the template.
 149                  $addcontenttype = new stdClass();
 150                  // Content type name.
 151                  $addcontenttype->name = $type;
 152                  // Content type editor base URL.
 153                  $tool['link']->param('plugin', $type);
 154                  $addcontenttype->baseurl = $tool['link']->out();
 155                  // Different types of the content type.
 156                  $addcontenttype->types = $types;
 157                  $addoptions[] = $addcontenttype;
 158              }
 159          }
 160  
 161          $tool['contenttypes'] = $addoptions;
 162      }
 163  }