MDL-67786 core_contentbank: New content bank contenttype plugin type
[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/**
18 * Content bank manager class
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/**
28 * Content bank manager class
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) {
47 $classname = "\\contenttype_$name\\contenttype";
48 if (class_exists($classname)) {
49 $types[] = $name;
50 }
51 }
52 return $types;
53 }
54
55 /**
56 * Obtains an array of supported extensions by active plugins.
57 *
58 * @return array The array with all the extensions supported and the supporting plugin names.
59 */
60 public function load_all_supported_extensions(): array {
61 $extensionscache = \cache::make('core', 'contentbank_enabled_extensions');
62 $supportedextensions = $extensionscache->get('enabled_extensions');
63 if ($supportedextensions === false) {
64 // Load all enabled extensions.
65 $supportedextensions = [];
66 foreach ($this->get_enabled_content_types() as $type) {
67 $classname = "\\contenttype_$type\\contenttype";
68 if (class_exists($classname)) {
69 $manager = new $classname;
70 if ($manager->is_feature_supported($manager::CAN_UPLOAD)) {
71 $extensions = $manager->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];
77 }
78 }
79 }
80 }
81 }
82 $extensionscache->set('enabled_extensions', $supportedextensions);
83 }
84 return $supportedextensions;
85 }
86
87 /**
88 * Obtains an array of supported extensions in the given context.
89 *
90 * @param \context $context Optional context to check (default null)
91 * @return array The array with all the extensions supported and the supporting plugin names.
92 */
93 public function load_context_supported_extensions(\context $context = null): array {
94 $extensionscache = \cache::make('core', 'contentbank_context_extensions');
95
96 $contextextensions = $extensionscache->get($context->id);
97 if ($contextextensions === false) {
98 $contextextensions = [];
99 $supportedextensions = $this->load_all_supported_extensions();
100 foreach ($supportedextensions as $extension => $types) {
101 foreach ($types as $type) {
102 $classname = "\\contenttype_$type\\contenttype";
103 if (class_exists($classname)) {
104 $manager = new $classname($context);
105 if ($manager->can_upload()) {
106 $contextextensions[$extension] = $type;
107 break;
108 }
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 *
121 * @param \context $context Optional context to check (default null)
122 * @return string A string with all the extensions supported.
123 */
124 public function get_supported_extensions_as_string(\context $context = null) {
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
148 * @param \context $context $context Optional context to check (default null)
149 * @return string contenttype name supports the file extension or null if the extension is not supported by any allowed plugin.
150 */
151 public function get_extension_supporter(string $extension, \context $context = null): ?string {
152 $supporters = $this->load_context_supported_extensions($context);
153 if (array_key_exists($extension, $supporters)) {
154 return $supporters[$extension];
155 }
156 return null;
157 }
158}