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 311 and 401] [Versions 311 and 402] [Versions 311 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          $encodedpath = base64_encode(json_encode($params));
 100  
 101          $node = [
 102              'shorttitle' => $content->get_name(),
 103              'title' => $file->get_filename(),
 104              'datemodified' => $file->get_timemodified(),
 105              'datecreated' => $file->get_timecreated(),
 106              'author' => $file->get_author(),
 107              'license' => $file->get_license(),
 108              'isref' => $file->is_external_file(),
 109              'size' => $file->get_filesize(),
 110              'source' => $encodedpath,
 111              'icon' => $OUTPUT->image_url(file_file_icon($file, 24))->out(false),
 112              'thumbnail' => $OUTPUT->image_url(file_file_icon($file, 90))->out(false)
 113          ];
 114  
 115          if ($file->get_status() == 666) {
 116              $node['originalmissing'] = true;
 117          }
 118  
 119          return $node;
 120      }
 121  
 122      /**
 123       * Generate a navigation node.
 124       *
 125       * @param \context $context The context
 126       * @return array The navigation node
 127       */
 128      public static function create_navigation_node(\context $context): array {
 129          return [
 130              'path' => base64_encode(json_encode(['contextid' => $context->id])),
 131              'name' => $context->get_context_name(false)
 132          ];
 133      }
 134  }