2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This plugin is used to access the content bank files.
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
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once($CFG->dirroot
. '/repository/lib.php');
30 * repository_contentbank class is used to browse the content bank files
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
36 class repository_contentbank
extends repository
{
41 * @param string $encodedpath
45 public function get_listing($encodedpath = '', $page = '') {
49 $ret['dynload'] = true;
50 $ret['nosearch'] = false;
51 $ret['nologin'] = true;
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
));
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;
72 // If not, return the system context as a default context.
73 if (empty($context)) {
74 $context = context_system
::instance();
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();
93 * Is this repository used to browse moodle files?
97 public function has_moodle_files() {
102 * Tells how the file can be picked from this repository.
106 public function supported_returntypes() {
107 return FILE_INTERNAL | FILE_REFERENCE
;
111 * Which return type should be selected by default.
115 public function default_returntype() {
116 return FILE_REFERENCE
;
120 * Is this repository accessing private data?
124 public function contains_private_data() {
129 * Repository method to make sure that user can access particular file.
131 * This is checked when user tries to pick the file from repository to deal with
132 * potential parameter substitutions in request
134 * @param string $source
135 * @return bool whether the file is accessible by current user
137 public function file_is_accessible($source) {
140 $fileparams = json_decode(base64_decode($source));
141 $itemid = clean_param($fileparams->itemid
, PARAM_INT
);
142 $contextid = clean_param($fileparams->contextid
, PARAM_INT
);
144 $contentbankfile = $DB->get_record('contentbank_content', ['id' => $itemid]);
145 $plugin = \core_plugin_manager
::instance()->get_plugin_info($contentbankfile->contenttype
);
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();
158 * Return search results.
160 * @param string $search
164 public function search($search, $page = 0) {
166 $ret['nologin'] = true;
167 $ret['list'] = \repository_contentbank\contentbank_search
::get_search_contents($search);