MDL-67795 contentbank: add generator
[moodle.git] / contentbank / classes / contenttype.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 type 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
27use coding_exception;
28use moodle_url;
29
30/**
31 * Content type manager class
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 */
37abstract class contenttype {
38
39 /** Plugin implements uploading feature */
40 const CAN_UPLOAD = 'upload';
41
42 /** @var context This content's context. **/
43 protected $context = null;
44
45 /**
46 * Content type constructor
47 *
48 * @param \context $context Optional context to check (default null)
49 */
50 public function __construct(\context $context = null) {
51 if (empty($context)) {
52 $context = \context_system::instance();
53 }
54 $this->context = $context;
55 }
56
57 /**
58 * Fills content_bank table with appropiate information.
59 *
60 * @param stdClass $content An optional content record compatible object (default null)
61 * @return content Object with content bank information.
62 */
63 public function create_content(\stdClass $content = null): ?content {
64 global $USER, $DB;
65
66 $record = new \stdClass();
67 $record->contenttype = $this->get_contenttype_name();
68 $record->contextid = $this->context->id;
69 $record->name = $content->name ?? '';
70 $record->usercreated = $content->usercreated ?? $USER->id;
71 $record->timecreated = time();
72 $record->usermodified = $record->usercreated;
73 $record->timemodified = $record->timecreated;
74 $record->configdata = $content->configdata ?? '';
75 $record->id = $DB->insert_record('contentbank_content', $record);
76 if ($record->id) {
77 $classname = '\\'.$record->contenttype.'\\content';
78 return new $classname($record);
79 }
80 return null;
81 }
82
83 /**
84 * Returns the contenttype name of this content.
85 *
86 * @return string Content type of the current instance
87 */
88 public function get_contenttype_name(): string {
89 $classname = get_class($this);
90 $contenttype = explode('\\', $classname);
91 return array_shift($contenttype);
92 }
93
94 /**
95 * Returns the plugin name of the current instance.
96 *
97 * @return string Plugin name of the current instance
98 */
99 public function get_plugin_name(): string {
100 $contenttype = $this->get_contenttype_name();
101 $plugin = explode('_', $contenttype);
102 return array_pop($plugin);
103 }
104
105 /**
106 * Returns the URL where the content will be visualized.
107 *
108 * @param stdClass $record Th content to be displayed.
109 * @return string URL where to visualize the given content.
110 */
111 public function get_view_url(\stdClass $record): string {
112 return new moodle_url('/contentbank/view.php', ['id' => $record->id]);
113 }
114
115 /**
116 * Returns the HTML content to add to view.php visualizer.
117 *
118 * @param stdClass $record Th content to be displayed.
119 * @return string HTML code to include in view.php.
120 */
121 public function get_view_content(\stdClass $record): string {
122 // Main contenttype class can visualize the content, but plugins could overwrite visualization.
123 return '';
124 }
125
126 /**
127 * Returns the HTML code to render the icon for content bank contents.
128 *
129 * @param string $contentname The contentname to add as alt value to the icon.
130 * @return string HTML code to render the icon
131 */
132 public function get_icon(string $contentname): string {
133 global $OUTPUT;
134 return $OUTPUT->pix_icon('f/unknown-64', $contentname, 'moodle', ['class' => 'iconsize-big']);
135 }
136
137 /**
138 * Returns user has access capability for the main content bank and the content itself (base on is_access_allowed from plugin).
139 *
140 * @return bool True if content could be accessed. False otherwise.
141 */
142 final public function can_access(): bool {
143 $classname = 'contenttype/'.$this->get_plugin_name();
144 $capability = $classname.":access";
145 $hascapabilities = has_capability('moodle/contentbank:access', $this->context)
146 && has_capability($capability, $this->context);
147 return $hascapabilities && $this->is_access_allowed();
148 }
149
150 /**
151 * Returns user has access capability for the content itself.
152 *
153 * @return bool True if content could be accessed. False otherwise.
154 */
155 protected function is_access_allowed(): bool {
156 // Plugins can overwrite this function to add any check they need.
157 return true;
158 }
159
160 /**
161 * Returns the user has permission to upload new content.
162 *
163 * @return bool True if content could be uploaded. False otherwise.
164 */
165 final public function can_upload(): bool {
166 if (!$this->is_feature_supported(self::CAN_UPLOAD)) {
167 return false;
168 }
169 if (!$this->can_access()) {
170 return false;
171 }
172
173 $classname = 'contenttype/'.$this->get_plugin_name();
174 $uploadcap = $classname.':upload';
175 $hascapabilities = has_capability('moodle/contentbank:upload', $this->context)
176 && has_capability($uploadcap, $this->context);
177 return $hascapabilities && $this->is_upload_allowed();
178 }
179
180 /**
181 * Returns plugin allows uploading.
182 *
183 * @return bool True if plugin allows uploading. False otherwise.
184 */
185 protected function is_upload_allowed(): bool {
186 // Plugins can overwrite this function to add any check they need.
187 return true;
188 }
189
190 /**
191 * Returns the plugin supports the feature.
192 *
193 * @param string $feature Feature code e.g CAN_UPLOAD
194 * @return bool True if content could be uploaded. False otherwise.
195 */
196 final public function is_feature_supported(string $feature): bool {
197 return in_array($feature, $this->get_implemented_features());
198 }
199
200 /**
201 * Return an array of implemented features by the plugins.
202 *
203 * @return array
204 */
205 abstract protected function get_implemented_features(): array;
206
207 /**
208 * Return an array of extensions the plugins could manage.
209 *
210 * @return array
211 */
212 abstract public function get_manageable_extensions(): array;
213}