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.
   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   * This plugin is used to access the content bank files.
  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  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->dirroot . '/repository/lib.php');
  28  
  29  /**
  30   * repository_contentbank class is used to browse the content bank files
  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 repository_contentbank extends repository {
  37  
  38      /**
  39       * Get file listing.
  40       *
  41       * @param string $encodedpath
  42       * @param string $page
  43       * @return array
  44       */
  45      public function get_listing($encodedpath = '', $page = '') {
  46          global $SITE;
  47  
  48          $ret = [];
  49          $ret['dynload'] = true;
  50          $ret['nosearch'] = false;
  51          $ret['nologin'] = true;
  52  
  53          // Return the parameters from the encoded path if the encoded path is not empty.
  54          if (!empty($encodedpath)) {
  55              $params = json_decode(base64_decode($encodedpath), true);
  56              if (is_array($params) && isset($params['contextid'])) {
  57                  $context = context::instance_by_id(clean_param($params['contextid'], PARAM_INT));
  58              }
  59          }
  60          // Return the current context if the context was not specified in the encoded path.
  61          // The current context should be an instance of context_system, context_coursecat or course related contexts.
  62          if (empty($context) && !empty($this->context)) {
  63              if ($this->context instanceof \context_system || $this->context instanceof \context_coursecat) {
  64                  $context = $this->context;
  65              } else if ($coursecontext = $this->context->get_course_context(false)) {
  66                  // Skip if front page context.
  67                  if ($coursecontext->instanceid !== $SITE->id) {
  68                      $context = $coursecontext;
  69                  }
  70              }
  71          }
  72          // If not, return the system context as a default context.
  73          if (empty($context)) {
  74              $context = context_system::instance();
  75          }
  76  
  77          $ret['list'] = [];
  78          $ret['path'] = [];
  79  
  80          // Get the content bank browser for the specified context.
  81          if ($browser = \repository_contentbank\helper::get_contentbank_browser($context)) {
  82              $manageurl = new moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
  83              $canaccesscontent = has_capability('moodle/contentbank:access', $context);
  84              $ret['manage'] = $canaccesscontent ? $manageurl->out() : '';
  85              $ret['list'] = $browser->get_content();
  86              $ret['path'] = $browser->get_navigation();
  87          }
  88  
  89          return $ret;
  90      }
  91  
  92      /**
  93       * Is this repository used to browse moodle files?
  94       *
  95       * @return boolean
  96       */
  97      public function has_moodle_files() {
  98          return true;
  99      }
 100  
 101      /**
 102       * Tells how the file can be picked from this repository.
 103       *
 104       * @return int
 105       */
 106      public function supported_returntypes() {
 107          return FILE_INTERNAL | FILE_REFERENCE;
 108      }
 109  
 110      /**
 111       * Which return type should be selected by default.
 112       *
 113       * @return int
 114       */
 115      public function default_returntype() {
 116          return FILE_REFERENCE;
 117      }
 118  
 119      /**
 120       * Is this repository accessing private data?
 121       *
 122       * @return bool
 123       */
 124      public function contains_private_data() {
 125          return false;
 126      }
 127  
 128      /**
 129       * Repository method to make sure that user can access particular file.
 130       *
 131       * This is checked when user tries to pick the file from repository to deal with
 132       * potential parameter substitutions in request
 133       *
 134       * @param string $source
 135       * @return bool whether the file is accessible by current user
 136       */
 137      public function file_is_accessible($source) {
 138          global $DB;
 139  
 140          $fileparams = json_decode(base64_decode($source));
 141          $itemid = clean_param($fileparams->itemid, PARAM_INT);
 142          $contextid = clean_param($fileparams->contextid, PARAM_INT);
 143  
 144          $contentbankfile = $DB->get_record('contentbank_content', ['id' => $itemid]);
 145          $plugin = \core_plugin_manager::instance()->get_plugin_info($contentbankfile->contenttype);
 146  
 147          $managerclass = "\\$contentbankfile->contenttype\\content";
 148          if ($plugin && $plugin->is_enabled() && class_exists($managerclass)) {
 149              $context = \context::instance_by_id($contextid);
 150              $browser = \repository_contentbank\helper::get_contentbank_browser($context);
 151              return $browser->can_access_content();
 152          }
 153  
 154          return false;
 155      }
 156  
 157      /**
 158       * Return search results.
 159       *
 160       * @param string $search
 161       * @param int $page
 162       * @return array
 163       */
 164      public function search($search, $page = 0) {
 165          $ret = [];
 166          $ret['nologin'] = true;
 167          $ret['list'] = \repository_contentbank\contentbank_search::get_search_contents($search);
 168  
 169          return $ret;
 170      }
 171  }