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 400 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   * Content bank files repository helpers.
  19   *
  20   * @package    repository_contentbank
  21   * @copyright  2020 Mihail Geshoski <mihail@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace repository_contentbank;
  26  
  27  use repository_contentbank\browser\contentbank_browser;
  28  
  29  /**
  30   * Helper class for content bank files repository.
  31   *
  32   * @package    repository_contentbank
  33   * @copyright  2020 Mihail Geshoski <mihail@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class helper {
  37  
  38      /**
  39       * Get the content bank repository browser for a certain context.
  40       *
  41       * @param \context $context The context
  42       * @return \repository_contentbank\browser\contentbank_browser|null The content bank repository browser
  43       */
  44      public static function get_contentbank_browser(\context $context): ?contentbank_browser {
  45          switch ($context->contextlevel) {
  46              case CONTEXT_SYSTEM:
  47                  return new \repository_contentbank\browser\contentbank_browser_context_system($context);
  48              case CONTEXT_COURSECAT:
  49                  return new \repository_contentbank\browser\contentbank_browser_context_coursecat($context);
  50              case CONTEXT_COURSE:
  51                  return new \repository_contentbank\browser\contentbank_browser_context_course($context);
  52          }
  53          return null;
  54      }
  55  
  56      /**
  57       * Create the context folder node.
  58       *
  59       * @param string $name The name of the context folder node
  60       * @param string $path The path to the context folder node
  61       * @return array The context folder node
  62       */
  63      public static function create_context_folder_node(string $name, string $path): array {
  64          global $OUTPUT;
  65  
  66          return [
  67              'title' => $name,
  68              'datemodified' => '',
  69              'datecreated' => '',
  70              'path' => $path,
  71              'thumbnail' => $OUTPUT->image_url(file_folder_icon(90))->out(false),
  72              'children' => []
  73          ];
  74      }
  75  
  76      /**
  77       * Create the content bank content node.
  78       *
  79       * @param \core_contentbank\content $content The content bank content
  80       * @return array|null The content bank content node
  81       */
  82      public static function create_contentbank_content_node(\core_contentbank\content $content): ?array {
  83          global $OUTPUT;
  84          // Only content files are currently supported, but should be able to create content folder nodes in the future.
  85          // Early return if the content is not a stored file.
  86          if (!$file = $content->get_file()) {
  87              return null;
  88          }
  89  
  90          $params = [
  91              'contextid' => $file->get_contextid(),
  92              'component' => $file->get_component(),
  93              'filearea'  => $file->get_filearea(),
  94              'itemid'    => $file->get_itemid(),
  95              'filepath'  => $file->get_filepath(),
  96              'filename'  => $file->get_filename()
  97          ];
  98  
  99          $contenttype = $content->get_content_type_instance();
 100          $encodedpath = base64_encode(json_encode($params));
 101  
 102          $node = [
 103              'shorttitle' => $content->get_name(),
 104              'title' => $file->get_filename(),
 105              'datemodified' => $file->get_timemodified(),
 106              'datecreated' => $file->get_timecreated(),
 107              'author' => $file->get_author(),
 108              'license' => $file->get_license(),
 109              'isref' => $file->is_external_file(),
 110              'size' => $file->get_filesize(),
 111              'source' => $encodedpath,
 112              'icon' => $contenttype->get_icon($content),
 113              'thumbnail' => $contenttype->get_icon($content)
 114          ];
 115  
 116          if ($file->get_status() == 666) {
 117              $node['originalmissing'] = true;
 118          }
 119  
 120          return $node;
 121      }
 122  
 123      /**
 124       * Generate a navigation node.
 125       *
 126       * @param \context $context The context
 127       * @return array The navigation node
 128       */
 129      public static function create_navigation_node(\context $context): array {
 130          return [
 131              'path' => base64_encode(json_encode(['contextid' => $context->id])),
 132              'name' => $context->get_context_name(false)
 133          ];
 134      }
 135  }