weekly release 4.0dev
[moodle.git] / contentbank / classes / contentbank.php
CommitLineData
33b8ca26
AA
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/**
1bb3663c 18 * Content bank class
33b8ca26
AA
19 *
20 * @package core_contentbank
21 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_contentbank;
26
75f58cbf 27use core_plugin_manager;
48bcb1f2 28use stored_file;
f2a9bb6e 29use context;
48bcb1f2 30
33b8ca26 31/**
1bb3663c 32 * Content bank class
33b8ca26
AA
33 *
34 * @package core_contentbank
35 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class contentbank {
75f58cbf
VDF
39 /** @var array Enabled content types. */
40 private $enabledcontenttypes = null;
33b8ca26
AA
41
42 /**
43 * Obtains the list of core_contentbank_content objects currently active.
44 *
45 * The list does not include players which are disabled.
46 *
47 * @return string[] Array of contentbank contenttypes.
48 */
54ce66bd 49 public function get_enabled_content_types(): array {
75f58cbf
VDF
50 if (!is_null($this->enabledcontenttypes)) {
51 return $this->enabledcontenttypes;
52 }
53
33b8ca26
AA
54 $enabledtypes = \core\plugininfo\contenttype::get_enabled_plugins();
55 $types = [];
56 foreach ($enabledtypes as $name) {
1bb3663c
SA
57 $contenttypeclassname = "\\contenttype_$name\\contenttype";
58 $contentclassname = "\\contenttype_$name\\content";
59 if (class_exists($contenttypeclassname) && class_exists($contentclassname)) {
75f58cbf 60 $types[$contenttypeclassname] = $name;
33b8ca26
AA
61 }
62 }
75f58cbf 63 return $this->enabledcontenttypes = $types;
33b8ca26
AA
64 }
65
66 /**
67 * Obtains an array of supported extensions by active plugins.
68 *
69 * @return array The array with all the extensions supported and the supporting plugin names.
70 */
71 public function load_all_supported_extensions(): array {
72 $extensionscache = \cache::make('core', 'contentbank_enabled_extensions');
73 $supportedextensions = $extensionscache->get('enabled_extensions');
74 if ($supportedextensions === false) {
75 // Load all enabled extensions.
76 $supportedextensions = [];
77 foreach ($this->get_enabled_content_types() as $type) {
78 $classname = "\\contenttype_$type\\contenttype";
1bb3663c
SA
79 $contenttype = new $classname;
80 if ($contenttype->is_feature_supported($contenttype::CAN_UPLOAD)) {
81 $extensions = $contenttype->get_manageable_extensions();
82 foreach ($extensions as $extension) {
83 if (array_key_exists($extension, $supportedextensions)) {
84 $supportedextensions[$extension][] = $type;
85 } else {
86 $supportedextensions[$extension] = [$type];
33b8ca26
AA
87 }
88 }
89 }
90 }
91 $extensionscache->set('enabled_extensions', $supportedextensions);
92 }
93 return $supportedextensions;
94 }
95
96 /**
97 * Obtains an array of supported extensions in the given context.
98 *
f2a9bb6e 99 * @param context $context Optional context to check (default null)
33b8ca26
AA
100 * @return array The array with all the extensions supported and the supporting plugin names.
101 */
f2a9bb6e 102 public function load_context_supported_extensions(context $context = null): array {
33b8ca26
AA
103 $extensionscache = \cache::make('core', 'contentbank_context_extensions');
104
105 $contextextensions = $extensionscache->get($context->id);
106 if ($contextextensions === false) {
107 $contextextensions = [];
108 $supportedextensions = $this->load_all_supported_extensions();
109 foreach ($supportedextensions as $extension => $types) {
110 foreach ($types as $type) {
111 $classname = "\\contenttype_$type\\contenttype";
1bb3663c
SA
112 $contenttype = new $classname($context);
113 if ($contenttype->can_upload()) {
114 $contextextensions[$extension] = $type;
115 break;
33b8ca26
AA
116 }
117 }
118 }
119 $extensionscache->set($context->id, $contextextensions);
120 }
121 return $contextextensions;
122 }
123
124 /**
125 * Obtains a string with all supported extensions by active plugins.
126 * Mainly to use as filepicker options parameter.
127 *
f2a9bb6e 128 * @param context $context Optional context to check (default null)
33b8ca26
AA
129 * @return string A string with all the extensions supported.
130 */
f2a9bb6e 131 public function get_supported_extensions_as_string(context $context = null) {
33b8ca26
AA
132 $supported = $this->load_context_supported_extensions($context);
133 $extensions = array_keys($supported);
134 return implode(',', $extensions);
135 }
136
137 /**
138 * Returns the file extension for a file.
139 *
140 * @param string $filename The name of the file
141 * @return string The extension of the file
142 */
143 public function get_extension(string $filename) {
144 $dot = strrpos($filename, '.');
145 if ($dot === false) {
146 return '';
147 }
148 return strtolower(substr($filename, $dot));
149 }
150
151 /**
152 * Get the first content bank plugin supports a file extension.
153 *
154 * @param string $extension Content file extension
f2a9bb6e 155 * @param context $context $context Optional context to check (default null)
33b8ca26
AA
156 * @return string contenttype name supports the file extension or null if the extension is not supported by any allowed plugin.
157 */
f2a9bb6e 158 public function get_extension_supporter(string $extension, context $context = null): ?string {
33b8ca26
AA
159 $supporters = $this->load_context_supported_extensions($context);
160 if (array_key_exists($extension, $supporters)) {
161 return $supporters[$extension];
162 }
163 return null;
164 }
fb445c76
SA
165
166 /**
167 * Find the contents with %$search% in the contextid defined.
168 * If contextid and search are empty, all contents are returned.
169 * In all the cases, only the contents for the enabled contentbank-type plugins are returned.
54ce66bd
SA
170 * No content-type permissions are validated here. It is the caller responsability to check that the user can access to them.
171 * The only validation done here is, for each content, a call to the method $content->is_view_allowed().
fb445c76
SA
172 *
173 * @param string|null $search Optional string to search (for now it will search only into the name).
174 * @param int $contextid Optional contextid to search.
54ce66bd 175 * @param array $contenttypenames Optional array with the list of content-type names to search.
fb445c76
SA
176 * @return array The contents for the enabled contentbank-type plugins having $search as name and placed in $contextid.
177 */
54ce66bd 178 public function search_contents(?string $search = null, ?int $contextid = 0, ?array $contenttypenames = null): array {
fb445c76
SA
179 global $DB;
180
181 $contents = [];
182
183 // Get only contents for enabled content-type plugins.
54ce66bd
SA
184 $contenttypes = [];
185 $enabledcontenttypes = $this->get_enabled_content_types();
186 foreach ($enabledcontenttypes as $contenttypename) {
187 if (empty($contenttypenames) || in_array($contenttypename, $contenttypenames)) {
188 $contenttypes[] = "contenttype_$contenttypename";
189 }
190 }
191
fb445c76
SA
192 if (empty($contenttypes)) {
193 // Early return if there are no content-type plugins enabled.
194 return $contents;
195 }
196
197 list($sqlcontenttypes, $params) = $DB->get_in_or_equal($contenttypes, SQL_PARAMS_NAMED);
198 $sql = " contenttype $sqlcontenttypes ";
199
200 // Filter contents on this context (if defined).
201 if (!empty($contextid)) {
202 $params['contextid'] = $contextid;
203 $sql .= ' AND contextid = :contextid ';
204 }
205
206 // Search for contents having this string (if defined).
207 if (!empty($search)) {
208 $sql .= ' AND ' . $DB->sql_like('name', ':name', false, false);
209 $params['name'] = '%' . $DB->sql_like_escape($search) . '%';
210 }
211
7616fbfd 212 $records = $DB->get_records_select('contentbank_content', $sql, $params, 'name ASC');
fb445c76
SA
213 foreach ($records as $record) {
214 $contentclass = "\\$record->contenttype\\content";
215 $content = new $contentclass($record);
216 if ($content->is_view_allowed()) {
217 $contents[] = $content;
218 }
219 }
220
221 return $contents;
222 }
48bcb1f2
AA
223
224 /**
225 * Create content from a file information.
226 *
3dfbd5a1
FR
227 * @throws file_exception If file operations fail
228 * @throws dml_exception if the content creation fails
48bcb1f2
AA
229 * @param \context $context Context where to upload the file and content.
230 * @param int $userid Id of the user uploading the file.
231 * @param stored_file $file The file to get information from
232 * @return content
233 */
234 public function create_content_from_file(\context $context, int $userid, stored_file $file): ?content {
235 global $USER;
236 if (empty($userid)) {
237 $userid = $USER->id;
238 }
239 // Get the contenttype to manage given file's extension.
240 $filename = $file->get_filename();
241 $extension = $this->get_extension($filename);
242 $plugin = $this->get_extension_supporter($extension, $context);
243 $classname = '\\contenttype_'.$plugin.'\\contenttype';
244 $record = new \stdClass();
245 $record->name = $filename;
246 $record->usercreated = $userid;
247 $contentype = new $classname($context);
3dfbd5a1 248 $content = $contentype->upload_content($file, $record);
48bcb1f2
AA
249 $event = \core\event\contentbank_content_uploaded::create_from_record($content->get_content());
250 $event->trigger();
251 return $content;
252 }
f2a9bb6e
AA
253
254 /**
255 * Delete content bank content by context.
256 *
257 * @param context $context The context to delete content from.
258 * @return bool
259 */
260 public function delete_contents(context $context): bool {
261 global $DB;
262
263 $result = true;
264 $records = $DB->get_records('contentbank_content', ['contextid' => $context->id]);
265 foreach ($records as $record) {
266 $contenttypeclass = "\\$record->contenttype\\contenttype";
267 if (class_exists($contenttypeclass)) {
268 $contenttype = new $contenttypeclass($context);
269 $contentclass = "\\$record->contenttype\\content";
270 $content = new $contentclass($record);
271 if (!$contenttype->delete_content($content)) {
272 $result = false;
273 }
274 }
275 }
276 return $result;
277 }
278
279 /**
280 * Move content bank content from a context to another.
281 *
282 * @param context $from The context to get content from.
283 * @param context $to The context to move content to.
284 * @return bool
285 */
286 public function move_contents(context $from, context $to): bool {
287 global $DB;
288
289 $result = true;
290 $records = $DB->get_records('contentbank_content', ['contextid' => $from->id]);
291 foreach ($records as $record) {
292 $contenttypeclass = "\\$record->contenttype\\contenttype";
293 if (class_exists($contenttypeclass)) {
294 $contenttype = new $contenttypeclass($from);
295 $contentclass = "\\$record->contenttype\\content";
296 $content = new $contentclass($record);
297 if (!$contenttype->move_content($content, $to)) {
298 $result = false;
299 }
300 }
301 }
302 return $result;
303 }
75f58cbf
VDF
304
305 /**
306 * Get the list of content types that have the requested feature.
307 *
308 * @param string $feature Feature code e.g CAN_UPLOAD.
309 * @param null|\context $context Optional context to check the permission to use the feature.
310 * @param bool $enabled Whether check only the enabled content types or all of them.
311 *
312 * @return string[] List of content types where the user has permission to access the feature.
313 */
314 public function get_contenttypes_with_capability_feature(string $feature, \context $context = null, bool $enabled = true): array {
315 $contenttypes = [];
316 // Check enabled content types or all of them.
317 if ($enabled) {
318 $contenttypestocheck = $this->get_enabled_content_types();
319 } else {
320 $plugins = core_plugin_manager::instance()->get_plugins_of_type('contenttype');
321 foreach ($plugins as $plugin) {
322 $contenttypeclassname = "\\{$plugin->type}_{$plugin->name}\\contenttype";
323 $contenttypestocheck[$contenttypeclassname] = $plugin->name;
324 }
325 }
326
327 foreach ($contenttypestocheck as $classname => $name) {
328 $contenttype = new $classname($context);
329 // The method names that check the features permissions must follow the pattern can_feature.
330 if ($contenttype->{"can_$feature"}()) {
331 $contenttypes[$classname] = $name;
332 }
333 }
334
335 return $contenttypes;
336 }
33b8ca26 337}