2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Content type manager class
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
25 namespace core_contentbank
;
27 use core\event\contentbank_content_created
;
28 use core\event\contentbank_content_deleted
;
29 use core\event\contentbank_content_viewed
;
34 * Content type manager class
36 * @package core_contentbank
37 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 abstract class contenttype
{
42 /** @var string Constant representing whether the plugin implements uploading feature */
43 const CAN_UPLOAD
= 'upload';
45 /** @var string Constant representing whether the plugin implements edition feature */
46 const CAN_EDIT
= 'edit';
49 * @var string Constant representing whether the plugin implements download feature
52 const CAN_DOWNLOAD
= 'download';
55 * @var string Constant representing whether the plugin implements copy feature
58 const CAN_COPY
= 'copy';
60 /** @var \context This contenttype's context. **/
61 protected $context = null;
64 * Content type constructor
66 * @param \context $context Optional context to check (default null)
68 public function __construct(\context
$context = null) {
69 if (empty($context)) {
70 $context = \context_system
::instance();
72 $this->context
= $context;
76 * Fills content_bank table with appropiate information.
78 * @throws dml_exception A DML specific exception is thrown for any creation error.
79 * @param \stdClass $record An optional content record compatible object (default null)
80 * @return content Object with content bank information.
82 public function create_content(\stdClass
$record = null): content
{
83 global $USER, $DB, $CFG;
85 $entry = new \
stdClass();
86 if (isset($record->visibility
)) {
87 $entry->visibility
= $record->visibility
;
89 $usercreated = $record->usercreated ??
$USER->id
;
90 $entry->visibility
= get_user_preferences('core_contentbank_visibility',
91 $CFG->defaultpreference_core_contentbank_visibility
, $usercreated);
93 $entry->contenttype
= $this->get_contenttype_name();
94 $entry->contextid
= $this->context
->id
;
95 $entry->name
= $record->name ??
'';
96 $entry->usercreated
= $record->usercreated ??
$USER->id
;
97 $entry->timecreated
= time();
98 $entry->usermodified
= $entry->usercreated
;
99 $entry->timemodified
= $entry->timecreated
;
100 $entry->configdata
= $record->configdata ??
'';
101 $entry->instanceid
= $record->instanceid ??
0;
102 $entry->id
= $DB->insert_record('contentbank_content', $entry);
103 $classname = '\\'.$entry->contenttype
.'\\content';
104 $content = new $classname($entry);
105 // Trigger an event for creating the content.
106 $event = contentbank_content_created
::create_from_record($content->get_content());
112 * Create a new content from an uploaded file.
114 * @throws file_exception If file operations fail
115 * @throws dml_exception if the content creation fails
116 * @param stored_file $file the uploaded file
117 * @param \stdClass|null $record an optional content record
118 * @return content Object with content bank information.
120 public function upload_content(stored_file
$file, \stdClass
$record = null): content
{
121 if (empty($record)) {
122 $record = new \
stdClass();
123 $record->name
= $file->get_filename();
125 $content = $this->create_content($record);
127 $content->import_file($file);
128 } catch (\moodle_exception
$e) {
129 $this->delete_content($content);
130 throw new \
moodle_exception($e->errorcode
);
137 * Replace a content using an uploaded file.
139 * @throws file_exception If file operations fail
140 * @throws dml_exception if the content creation fails
141 * @param stored_file $file the uploaded file
142 * @param content $content the original content record
143 * @return content Object with the updated content bank information.
145 public function replace_content(stored_file
$file, content
$content): content
{
146 $content->import_file($file);
147 $content->update_content();
152 * Delete this content from the content_bank.
153 * This method can be overwritten by the plugins if they need to delete specific information.
155 * @param content $content The content to delete.
156 * @return boolean true if the content has been deleted; false otherwise.
158 public function delete_content(content
$content): bool {
161 // Delete the file if it exists.
162 if ($file = $content->get_file()) {
166 // Delete the contentbank DB entry.
167 $result = $DB->delete_records('contentbank_content', ['id' => $content->get_id()]);
169 // Trigger an event for deleting this content.
170 $record = $content->get_content();
171 $event = contentbank_content_deleted
::create([
172 'objectid' => $content->get_id(),
173 'relateduserid' => $record->usercreated
,
174 'context' => \context
::instance_by_id($record->contextid
),
176 'contenttype' => $content->get_content_type(),
177 'name' => $content->get_name()
180 $event->add_record_snapshot('contentbank_content', $record);
187 * Rename this content from the content_bank.
188 * This method can be overwritten by the plugins if they need to change some other specific information.
190 * @param content $content The content to rename.
191 * @param string $name The name of the content.
192 * @return boolean true if the content has been renamed; false otherwise.
194 public function rename_content(content
$content, string $name): bool {
195 return $content->set_name($name);
199 * Move content to another context.
200 * This method can be overwritten by the plugins if they need to change some other specific information.
202 * @param content $content The content to rename.
203 * @param \context $context The new context.
204 * @return boolean true if the content has been renamed; false otherwise.
206 public function move_content(content
$content, \context
$context): bool {
207 return $content->set_contextid($context->id
);
211 * Returns the contenttype name of this content.
213 * @return string Content type of the current instance
215 public function get_contenttype_name(): string {
216 $classname = get_class($this);
217 $contenttype = explode('\\', $classname);
218 return array_shift($contenttype);
222 * Returns the plugin name of the current instance.
224 * @return string Plugin name of the current instance
226 public function get_plugin_name(): string {
227 $contenttype = $this->get_contenttype_name();
228 $plugin = explode('_', $contenttype);
229 return array_pop($plugin);
233 * Returns the URL where the content will be visualized.
235 * @param content $content The content to be displayed.
236 * @return string URL where to visualize the given content.
238 public function get_view_url(content
$content): string {
239 return new moodle_url('/contentbank/view.php', ['id' => $content->get_id()]);
243 * Returns the HTML content to add to view.php visualizer.
245 * @param content $content The content to be displayed.
246 * @return string HTML code to include in view.php.
248 public function get_view_content(content
$content): string {
249 // Trigger an event for viewing this content.
250 $event = contentbank_content_viewed
::create_from_record($content->get_content());
257 * Returns the URL to download the content.
260 * @param content $content The content to be downloaded.
261 * @return string URL with the content to download.
263 public function get_download_url(content
$content): string {
265 $file = $content->get_file();
267 $url = \moodle_url
::make_pluginfile_url(
268 $file->get_contextid(),
269 $file->get_component(),
270 $file->get_filearea(),
272 $file->get_filepath(),
273 $file->get_filename()
275 $downloadurl = $url->out(false);
282 * Returns the HTML code to render the icon for content bank contents.
284 * @param content $content The content to be displayed.
285 * @return string HTML code to render the icon
287 public function get_icon(content
$content): string {
289 return $OUTPUT->image_url('f/unknown-64', 'moodle')->out(false);
293 * Returns user has access capability for the main content bank and the content itself (base on is_access_allowed from plugin).
295 * @return bool True if content could be accessed. False otherwise.
297 final public function can_access(): bool {
298 $classname = 'contenttype/'.$this->get_plugin_name();
299 $capability = $classname.":access";
300 $hascapabilities = has_capability('moodle/contentbank:access', $this->context
)
301 && has_capability($capability, $this->context
);
302 return $hascapabilities && $this->is_access_allowed();
306 * Returns user has access capability for the content itself.
308 * @return bool True if content could be accessed. False otherwise.
310 protected function is_access_allowed(): bool {
311 // Plugins can overwrite this function to add any check they need.
316 * Returns the user has permission to upload new content.
318 * @return bool True if content could be uploaded. False otherwise.
320 final public function can_upload(): bool {
321 if (!$this->is_feature_supported(self
::CAN_UPLOAD
)) {
324 if (!$this->can_access()) {
328 $classname = 'contenttype/'.$this->get_plugin_name();
329 $uploadcap = $classname.':upload';
330 $hascapabilities = has_capability('moodle/contentbank:upload', $this->context
)
331 && has_capability($uploadcap, $this->context
);
332 return $hascapabilities && $this->is_upload_allowed();
336 * Returns plugin allows uploading.
338 * @return bool True if plugin allows uploading. False otherwise.
340 protected function is_upload_allowed(): bool {
341 // Plugins can overwrite this function to add any check they need.
346 * Check if the user can delete this content.
348 * @param content $content The content to be deleted.
349 * @return bool True if content could be uploaded. False otherwise.
351 final public function can_delete(content
$content): bool {
354 if ($this->context
->id
!= $content->get_content()->contextid
) {
355 // The content has to have exactly the same context as this contenttype.
359 $hascapability = has_capability('moodle/contentbank:deleteanycontent', $this->context
);
360 if ($content->get_content()->usercreated
== $USER->id
) {
361 // This content has been created by the current user; check if she can delete her content.
362 $hascapability = $hascapability ||
has_capability('moodle/contentbank:deleteowncontent', $this->context
);
365 return $hascapability && $this->is_delete_allowed($content);
369 * Returns if content allows deleting.
371 * @param content $content The content to be deleted.
372 * @return bool True if content allows uploading. False otherwise.
374 protected function is_delete_allowed(content
$content): bool {
375 // Plugins can overwrite this function to add any check they need.
380 * Check if the user can managed this content.
382 * @param content $content The content to be managed.
383 * @return bool True if content could be managed. False otherwise.
385 public final function can_manage(content
$content): bool {
388 if ($this->context
->id
!= $content->get_content()->contextid
) {
389 // The content has to have exactly the same context as this contenttype.
393 // Check main contentbank management permission.
394 $hascapability = has_capability('moodle/contentbank:manageanycontent', $this->context
);
395 if ($content->get_content()->usercreated
== $USER->id
) {
396 // This content has been created by the current user; check if they can manage their content.
397 $hascapability = $hascapability ||
has_capability('moodle/contentbank:manageowncontent', $this->context
);
400 return $hascapability && $this->is_manage_allowed($content);
404 * Returns if content allows managing.
406 * @param content $content The content to be managed.
407 * @return bool True if content allows uploading. False otherwise.
409 protected function is_manage_allowed(content
$content): bool {
410 // Plugins can overwrite this function to add any check they need.
415 * Returns whether or not the user has permission to use the editor.
416 * This function will be called with the content to be edited as parameter,
417 * or null when is checking permission to create a new content using the editor.
419 * @param content $content The content to be edited or null when creating a new content.
420 * @return bool True if the user can edit content. False otherwise.
422 final public function can_edit(?content
$content = null): bool {
423 if (!$this->is_feature_supported(self
::CAN_EDIT
)) {
427 if (!$this->can_access()) {
431 if (!is_null($content) && !$this->can_manage($content)) {
435 $classname = 'contenttype/'.$this->get_plugin_name();
437 $editioncap = $classname.':useeditor';
438 $hascapabilities = has_all_capabilities(['moodle/contentbank:useeditor', $editioncap], $this->context
);
439 return $hascapabilities && $this->is_edit_allowed($content);
443 * Returns plugin allows edition.
445 * @param content $content The content to be edited.
446 * @return bool True if plugin allows edition. False otherwise.
448 protected function is_edit_allowed(?content
$content): bool {
449 // Plugins can overwrite this function to add any check they need.
454 * Returns whether or not the user has permission to download the content.
457 * @param content $content The content to be downloaded.
458 * @return bool True if the user can download the content. False otherwise.
460 final public function can_download(content
$content): bool {
461 if (!$this->is_feature_supported(self
::CAN_DOWNLOAD
)) {
465 if (!$this->can_access()) {
469 $hascapability = has_capability('moodle/contentbank:downloadcontent', $this->context
);
470 return $hascapability && $this->is_download_allowed($content);
474 * Returns whether or not the user has permission to copy the content.
477 * @param content $content The content to be copied.
478 * @return bool True if the user can copy the content. False otherwise.
480 final public function can_copy(content
$content): bool {
483 if (!$this->is_feature_supported(self
::CAN_COPY
)) {
487 if (!$this->can_access()) {
491 if (!$this->is_copy_allowed($content)) {
495 $hascapability = has_capability('moodle/contentbank:copyanycontent', $this->context
);
496 if (!$hascapability && ($content->get_content()->usercreated
== $USER->id
)) {
497 $hascapability = has_capability('moodle/contentbank:copycontent', $this->context
);
499 return $hascapability;
503 * Returns plugin allows downloading.
506 * @param content $content The content to be downloaed.
507 * @return bool True if plugin allows downloading. False otherwise.
509 protected function is_download_allowed(content
$content): bool {
510 // Plugins can overwrite this function to add any check they need.
515 * Returns plugin allows copying.
518 * @param content $content The content to be copied.
519 * @return bool True if plugin allows copying. False otherwise.
521 protected function is_copy_allowed(content
$content): bool {
522 // Plugins can overwrite this function to add any check they need.
527 * Returns the plugin supports the feature.
529 * @param string $feature Feature code e.g CAN_UPLOAD
530 * @return bool True if content could be uploaded. False otherwise.
532 final public function is_feature_supported(string $feature): bool {
533 return in_array($feature, $this->get_implemented_features());
537 * Return an array of implemented features by the plugins.
541 abstract protected function get_implemented_features(): array;
544 * Return an array of extensions the plugins could manage.
548 abstract public function get_manageable_extensions(): array;
551 * Returns the list of different types of the given content type.
553 * A content type can have one or more options for creating content. This method will report all of them or only the content
554 * type itself if it has no other options.
556 * @return array An object for each type:
557 * - string typename: descriptive name of the type.
558 * - string typeeditorparams: params required by this content type editor.
559 * - url typeicon: this type icon.
561 abstract public function get_contenttype_types(): array;