Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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              $author = \core_user::get_user($content->get_content()->usercreated);
  90              $contentdata[] = array(
  91                  'name' => $name,
  92                  'title' => strtolower($name),
  93                  'link' => $contenttype->get_view_url($content),
  94                  'icon' => $contenttype->get_icon($content),
  95                  'timemodified' => $content->get_timemodified(),
  96                  'bytes' => $filesize,
  97                  'size' => display_size($filesize),
  98                  'type' => $mimetype,
  99                  'author' => fullname($author),
 100              );
 101          }
 102          $data->viewlist = get_user_preferences('core_contentbank_view_list');
 103          $data->contents = $contentdata;
 104          // The tools are displayed in the action bar on the index page.
 105          foreach ($this->toolbar as $tool) {
 106              // Customize the output of a tool, like dropdowns.
 107              $method = 'export_tool_'.$tool['action'];
 108              if (method_exists($this, $method)) {
 109                  $this->$method($tool);
 110              }
 111              $data->tools[] = $tool;
 112          }
 113  
 114          return $data;
 115      }
 116  
 117      /**
 118       * Adds the content type items to display to the Add dropdown.
 119       *
 120       * Each content type is represented as an object with the properties:
 121       *     - name: the name of the content type.
 122       *     - baseurl: the base content type editor URL.
 123       *     - types: different types of the content type to display as dropdown items.
 124       *
 125       * @param array $tool Data for rendering the Add dropdown, including the editable content types.
 126       */
 127      private function export_tool_add(array &$tool) {
 128          $editabletypes = $tool['contenttypes'];
 129  
 130          $addoptions = [];
 131          foreach ($editabletypes as $class => $type) {
 132              $contentype = new $class($this->context);
 133              // Get the creation options of each content type.
 134              $types = $contentype->get_contenttype_types();
 135              if ($types) {
 136                  // Add a text describing the content type as first option. This will be displayed in the drop down to
 137                  // separate the options for the different content types.
 138                  $contentdesc = new stdClass();
 139                  $contentdesc->typename = get_string('description', $contentype->get_contenttype_name());
 140                  array_unshift($types, $contentdesc);
 141                  // Context data for the template.
 142                  $addcontenttype = new stdClass();
 143                  // Content type name.
 144                  $addcontenttype->name = $type;
 145                  // Content type editor base URL.
 146                  $tool['link']->param('plugin', $type);
 147                  $addcontenttype->baseurl = $tool['link']->out();
 148                  // Different types of the content type.
 149                  $addcontenttype->types = $types;
 150                  $addoptions[] = $addcontenttype;
 151              }
 152          }
 153  
 154          $tool['contenttypes'] = $addoptions;
 155      }
 156  }