Merge branch 'MDL-67813-master-1' of git://github.com/mihailges/moodle
[moodle.git] / repository / contentbank / lib.php
CommitLineData
fa12d5dc
MG
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
25defined('MOODLE_INTERNAL') || die();
26
27require_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 */
36class 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;
966442c4 50 $ret['nosearch'] = false;
fa12d5dc
MG
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 * Is this repository accessing private data?
112 *
113 * @return bool
114 */
115 public function contains_private_data() {
116 return false;
117 }
118
119 /**
120 * Repository method to make sure that user can access particular file.
121 *
122 * This is checked when user tries to pick the file from repository to deal with
123 * potential parameter substitutions in request
124 *
125 * @param string $source
126 * @return bool whether the file is accessible by current user
127 */
128 public function file_is_accessible($source) {
129 global $DB;
130
131 $fileparams = json_decode(base64_decode($source));
132 $itemid = clean_param($fileparams->itemid, PARAM_INT);
133 $contextid = clean_param($fileparams->contextid, PARAM_INT);
134
135 $contentbankfile = $DB->get_record('contentbank_content', ['id' => $itemid]);
136 $plugin = \core_plugin_manager::instance()->get_plugin_info($contentbankfile->contenttype);
137
138 $managerclass = "\\$contentbankfile->contenttype\\content";
139 if ($plugin && $plugin->is_enabled() && class_exists($managerclass)) {
140 $context = \context::instance_by_id($contextid);
141 $browser = \repository_contentbank\helper::get_contentbank_browser($context);
142 return $browser->can_access_content();
143 }
144
145 return false;
146 }
966442c4
MG
147
148 /**
149 * Return search results.
150 *
151 * @param string $search
152 * @param int $page
153 * @return array
154 */
155 public function search($search, $page = 0) {
156 $ret = [];
157 $ret['nologin'] = true;
158 $ret['list'] = \repository_contentbank\contentbank_search::get_search_contents($search);
159
160 return $ret;
161 }
fa12d5dc 162}