Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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