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