MDL-67790 core_contentbank: Unit and behat tests for renaming content
[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
27/**
1bb3663c 28 * Content bank class
33b8ca26
AA
29 *
30 * @package core_contentbank
31 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class contentbank {
35
36 /**
37 * Obtains the list of core_contentbank_content objects currently active.
38 *
39 * The list does not include players which are disabled.
40 *
41 * @return string[] Array of contentbank contenttypes.
42 */
43 private function get_enabled_content_types(): array {
44 $enabledtypes = \core\plugininfo\contenttype::get_enabled_plugins();
45 $types = [];
46 foreach ($enabledtypes as $name) {
1bb3663c
SA
47 $contenttypeclassname = "\\contenttype_$name\\contenttype";
48 $contentclassname = "\\contenttype_$name\\content";
49 if (class_exists($contenttypeclassname) && class_exists($contentclassname)) {
33b8ca26
AA
50 $types[] = $name;
51 }
52 }
53 return $types;
54 }
55
56 /**
57 * Obtains an array of supported extensions by active plugins.
58 *
59 * @return array The array with all the extensions supported and the supporting plugin names.
60 */
61 public function load_all_supported_extensions(): array {
62 $extensionscache = \cache::make('core', 'contentbank_enabled_extensions');
63 $supportedextensions = $extensionscache->get('enabled_extensions');
64 if ($supportedextensions === false) {
65 // Load all enabled extensions.
66 $supportedextensions = [];
67 foreach ($this->get_enabled_content_types() as $type) {
68 $classname = "\\contenttype_$type\\contenttype";
1bb3663c
SA
69 $contenttype = new $classname;
70 if ($contenttype->is_feature_supported($contenttype::CAN_UPLOAD)) {
71 $extensions = $contenttype->get_manageable_extensions();
72 foreach ($extensions as $extension) {
73 if (array_key_exists($extension, $supportedextensions)) {
74 $supportedextensions[$extension][] = $type;
75 } else {
76 $supportedextensions[$extension] = [$type];
33b8ca26
AA
77 }
78 }
79 }
80 }
81 $extensionscache->set('enabled_extensions', $supportedextensions);
82 }
83 return $supportedextensions;
84 }
85
86 /**
87 * Obtains an array of supported extensions in the given context.
88 *
89 * @param \context $context Optional context to check (default null)
90 * @return array The array with all the extensions supported and the supporting plugin names.
91 */
92 public function load_context_supported_extensions(\context $context = null): array {
93 $extensionscache = \cache::make('core', 'contentbank_context_extensions');
94
95 $contextextensions = $extensionscache->get($context->id);
96 if ($contextextensions === false) {
97 $contextextensions = [];
98 $supportedextensions = $this->load_all_supported_extensions();
99 foreach ($supportedextensions as $extension => $types) {
100 foreach ($types as $type) {
101 $classname = "\\contenttype_$type\\contenttype";
1bb3663c
SA
102 $contenttype = new $classname($context);
103 if ($contenttype->can_upload()) {
104 $contextextensions[$extension] = $type;
105 break;
33b8ca26
AA
106 }
107 }
108 }
109 $extensionscache->set($context->id, $contextextensions);
110 }
111 return $contextextensions;
112 }
113
114 /**
115 * Obtains a string with all supported extensions by active plugins.
116 * Mainly to use as filepicker options parameter.
117 *
118 * @param \context $context Optional context to check (default null)
119 * @return string A string with all the extensions supported.
120 */
121 public function get_supported_extensions_as_string(\context $context = null) {
122 $supported = $this->load_context_supported_extensions($context);
123 $extensions = array_keys($supported);
124 return implode(',', $extensions);
125 }
126
127 /**
128 * Returns the file extension for a file.
129 *
130 * @param string $filename The name of the file
131 * @return string The extension of the file
132 */
133 public function get_extension(string $filename) {
134 $dot = strrpos($filename, '.');
135 if ($dot === false) {
136 return '';
137 }
138 return strtolower(substr($filename, $dot));
139 }
140
141 /**
142 * Get the first content bank plugin supports a file extension.
143 *
144 * @param string $extension Content file extension
145 * @param \context $context $context Optional context to check (default null)
146 * @return string contenttype name supports the file extension or null if the extension is not supported by any allowed plugin.
147 */
148 public function get_extension_supporter(string $extension, \context $context = null): ?string {
149 $supporters = $this->load_context_supported_extensions($context);
150 if (array_key_exists($extension, $supporters)) {
151 return $supporters[$extension];
152 }
153 return null;
154 }
fb445c76
SA
155
156 /**
157 * Find the contents with %$search% in the contextid defined.
158 * If contextid and search are empty, all contents are returned.
159 * In all the cases, only the contents for the enabled contentbank-type plugins are returned.
160 *
161 * @param string|null $search Optional string to search (for now it will search only into the name).
162 * @param int $contextid Optional contextid to search.
163 * @return array The contents for the enabled contentbank-type plugins having $search as name and placed in $contextid.
164 */
165 public function search_contents(?string $search = null, ?int $contextid = 0): array {
166 global $DB;
167
168 $contents = [];
169
170 // Get only contents for enabled content-type plugins.
171 $contenttypes = array_map(function($contenttypename) {
172 return "contenttype_$contenttypename";
173 }, $this->get_enabled_content_types());
174 if (empty($contenttypes)) {
175 // Early return if there are no content-type plugins enabled.
176 return $contents;
177 }
178
179 list($sqlcontenttypes, $params) = $DB->get_in_or_equal($contenttypes, SQL_PARAMS_NAMED);
180 $sql = " contenttype $sqlcontenttypes ";
181
182 // Filter contents on this context (if defined).
183 if (!empty($contextid)) {
184 $params['contextid'] = $contextid;
185 $sql .= ' AND contextid = :contextid ';
186 }
187
188 // Search for contents having this string (if defined).
189 if (!empty($search)) {
190 $sql .= ' AND ' . $DB->sql_like('name', ':name', false, false);
191 $params['name'] = '%' . $DB->sql_like_escape($search) . '%';
192 }
193
194 $records = $DB->get_records_select('contentbank_content', $sql, $params);
195 foreach ($records as $record) {
196 $contentclass = "\\$record->contenttype\\content";
197 $content = new $contentclass($record);
198 if ($content->is_view_allowed()) {
199 $contents[] = $content;
200 }
201 }
202
203 return $contents;
204 }
33b8ca26 205}