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
;
35 * Content type manager class
37 * @package core_contentbank
38 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 abstract class contenttype
{
43 /** @var string Constant representing whether the plugin implements uploading feature */
44 const CAN_UPLOAD
= 'upload';
46 /** @var string Constant representing whether the plugin implements edition feature */
47 const CAN_EDIT
= 'edit';
50 * @var string Constant representing whether the plugin implements download feature
53 const CAN_DOWNLOAD
= 'download';
55 /** @var \context This contenttype's context. **/
56 protected $context = null;
59 * Content type constructor
61 * @param \context $context Optional context to check (default null)
63 public function __construct(\context
$context = null) {
64 if (empty($context)) {
65 $context = \context_system
::instance();
67 $this->context
= $context;
71 * Fills content_bank table with appropiate information.
73 * @throws dml_exception A DML specific exception is thrown for any creation error.
74 * @param \stdClass $record An optional content record compatible object (default null)
75 * @return content Object with content bank information.
77 public function create_content(\stdClass
$record = null): content
{
78 global $USER, $DB, $CFG;
80 $entry = new \
stdClass();
81 if (isset($record->visibility
)) {
82 $entry->visibility
= $record->visibility
;
84 $usercreated = $record->usercreated ??
$USER->id
;
85 $entry->visibility
= get_user_preferences('core_contentbank_visibility',
86 $CFG->defaultpreference_core_contentbank_visibility
, $usercreated);
88 $entry->contenttype
= $this->get_contenttype_name();
89 $entry->contextid
= $this->context
->id
;
90 $entry->name
= $record->name ??
'';
91 $entry->usercreated
= $record->usercreated ??
$USER->id
;
92 $entry->timecreated
= time();
93 $entry->usermodified
= $entry->usercreated
;
94 $entry->timemodified
= $entry->timecreated
;
95 $entry->configdata
= $record->configdata ??
'';
96 $entry->instanceid
= $record->instanceid ??
0;
97 $entry->id
= $DB->insert_record('contentbank_content', $entry);
98 $classname = '\\'.$entry->contenttype
.'\\content';
99 $content = new $classname($entry);
100 // Trigger an event for creating the content.
101 $event = contentbank_content_created
::create_from_record($content->get_content());
107 * Create a new content from an uploaded file.
109 * @throws file_exception If file operations fail
110 * @throws dml_exception if the content creation fails
111 * @param stored_file $file the uploaded file
112 * @param \stdClass|null $record an optional content record
113 * @return content Object with content bank information.
115 public function upload_content(stored_file
$file, \stdClass
$record = null): content
{
116 if (empty($record)) {
117 $record = new \
stdClass();
118 $record->name
= $file->get_filename();
120 $content = $this->create_content($record);
122 $content->import_file($file);
123 } catch (Exception
$e) {
124 $this->delete_content($content);
132 * Replace a content using an uploaded file.
134 * @throws file_exception If file operations fail
135 * @throws dml_exception if the content creation fails
136 * @param stored_file $file the uploaded file
137 * @param content $content the original content record
138 * @return content Object with the updated content bank information.
140 public function replace_content(stored_file
$file, content
$content): content
{
141 $content->import_file($file);
142 $content->update_content();
147 * Delete this content from the content_bank.
148 * This method can be overwritten by the plugins if they need to delete specific information.
150 * @param content $content The content to delete.
151 * @return boolean true if the content has been deleted; false otherwise.
153 public function delete_content(content
$content): bool {
156 // Delete the file if it exists.
157 if ($file = $content->get_file()) {
161 // Delete the contentbank DB entry.
162 $result = $DB->delete_records('contentbank_content', ['id' => $content->get_id()]);
164 // Trigger an event for deleting this content.
165 $record = $content->get_content();
166 $event = contentbank_content_deleted
::create([
167 'objectid' => $content->get_id(),
168 'relateduserid' => $record->usercreated
,
169 'context' => \context
::instance_by_id($record->contextid
),
171 'contenttype' => $content->get_content_type(),
172 'name' => $content->get_name()
175 $event->add_record_snapshot('contentbank_content', $record);
182 * Rename this content from the content_bank.
183 * This method can be overwritten by the plugins if they need to change some other specific information.
185 * @param content $content The content to rename.
186 * @param string $name The name of the content.
187 * @return boolean true if the content has been renamed; false otherwise.
189 public function rename_content(content
$content, string $name): bool {
190 return $content->set_name($name);
194 * Move content to another context.
195 * This method can be overwritten by the plugins if they need to change some other specific information.
197 * @param content $content The content to rename.
198 * @param \context $context The new context.
199 * @return boolean true if the content has been renamed; false otherwise.
201 public function move_content(content
$content, \context
$context): bool {
202 return $content->set_contextid($context->id
);
206 * Returns the contenttype name of this content.
208 * @return string Content type of the current instance
210 public function get_contenttype_name(): string {
211 $classname = get_class($this);
212 $contenttype = explode('\\', $classname);
213 return array_shift($contenttype);
217 * Returns the plugin name of the current instance.
219 * @return string Plugin name of the current instance
221 public function get_plugin_name(): string {
222 $contenttype = $this->get_contenttype_name();
223 $plugin = explode('_', $contenttype);
224 return array_pop($plugin);
228 * Returns the URL where the content will be visualized.
230 * @param content $content The content to be displayed.
231 * @return string URL where to visualize the given content.
233 public function get_view_url(content
$content): string {
234 return new moodle_url('/contentbank/view.php', ['id' => $content->get_id()]);
238 * Returns the HTML content to add to view.php visualizer.
240 * @param content $content The content to be displayed.
241 * @return string HTML code to include in view.php.
243 public function get_view_content(content
$content): string {
244 // Trigger an event for viewing this content.
245 $event = contentbank_content_viewed
::create_from_record($content->get_content());
252 * Returns the URL to download the content.
255 * @param content $content The content to be downloaded.
256 * @return string URL with the content to download.
258 public function get_download_url(content
$content): string {
260 $file = $content->get_file();
262 $url = \moodle_url
::make_pluginfile_url(
263 $file->get_contextid(),
264 $file->get_component(),
265 $file->get_filearea(),
267 $file->get_filepath(),
268 $file->get_filename()
270 $downloadurl = $url->out(false);
277 * Returns the HTML code to render the icon for content bank contents.
279 * @param content $content The content to be displayed.
280 * @return string HTML code to render the icon
282 public function get_icon(content
$content): string {
284 return $OUTPUT->image_url('f/unknown-64', 'moodle')->out(false);
288 * Returns user has access capability for the main content bank and the content itself (base on is_access_allowed from plugin).
290 * @return bool True if content could be accessed. False otherwise.
292 final public function can_access(): bool {
293 $classname = 'contenttype/'.$this->get_plugin_name();
294 $capability = $classname.":access";
295 $hascapabilities = has_capability('moodle/contentbank:access', $this->context
)
296 && has_capability($capability, $this->context
);
297 return $hascapabilities && $this->is_access_allowed();
301 * Returns user has access capability for the content itself.
303 * @return bool True if content could be accessed. False otherwise.
305 protected function is_access_allowed(): bool {
306 // Plugins can overwrite this function to add any check they need.
311 * Returns the user has permission to upload new content.
313 * @return bool True if content could be uploaded. False otherwise.
315 final public function can_upload(): bool {
316 if (!$this->is_feature_supported(self
::CAN_UPLOAD
)) {
319 if (!$this->can_access()) {
323 $classname = 'contenttype/'.$this->get_plugin_name();
324 $uploadcap = $classname.':upload';
325 $hascapabilities = has_capability('moodle/contentbank:upload', $this->context
)
326 && has_capability($uploadcap, $this->context
);
327 return $hascapabilities && $this->is_upload_allowed();
331 * Returns plugin allows uploading.
333 * @return bool True if plugin allows uploading. False otherwise.
335 protected function is_upload_allowed(): bool {
336 // Plugins can overwrite this function to add any check they need.
341 * Check if the user can delete this content.
343 * @param content $content The content to be deleted.
344 * @return bool True if content could be uploaded. False otherwise.
346 final public function can_delete(content
$content): bool {
349 if ($this->context
->id
!= $content->get_content()->contextid
) {
350 // The content has to have exactly the same context as this contenttype.
354 $hascapability = has_capability('moodle/contentbank:deleteanycontent', $this->context
);
355 if ($content->get_content()->usercreated
== $USER->id
) {
356 // This content has been created by the current user; check if she can delete her content.
357 $hascapability = $hascapability ||
has_capability('moodle/contentbank:deleteowncontent', $this->context
);
360 return $hascapability && $this->is_delete_allowed($content);
364 * Returns if content allows deleting.
366 * @param content $content The content to be deleted.
367 * @return bool True if content allows uploading. False otherwise.
369 protected function is_delete_allowed(content
$content): bool {
370 // Plugins can overwrite this function to add any check they need.
375 * Check if the user can managed this content.
377 * @param content $content The content to be managed.
378 * @return bool True if content could be managed. False otherwise.
380 public final function can_manage(content
$content): bool {
383 if ($this->context
->id
!= $content->get_content()->contextid
) {
384 // The content has to have exactly the same context as this contenttype.
388 // Check main contentbank management permission.
389 $hascapability = has_capability('moodle/contentbank:manageanycontent', $this->context
);
390 if ($content->get_content()->usercreated
== $USER->id
) {
391 // This content has been created by the current user; check if they can manage their content.
392 $hascapability = $hascapability ||
has_capability('moodle/contentbank:manageowncontent', $this->context
);
395 return $hascapability && $this->is_manage_allowed($content);
399 * Returns if content allows managing.
401 * @param content $content The content to be managed.
402 * @return bool True if content allows uploading. False otherwise.
404 protected function is_manage_allowed(content
$content): bool {
405 // Plugins can overwrite this function to add any check they need.
410 * Returns whether or not the user has permission to use the editor.
411 * This function will be called with the content to be edited as parameter,
412 * or null when is checking permission to create a new content using the editor.
414 * @param content $content The content to be edited or null when creating a new content.
415 * @return bool True if the user can edit content. False otherwise.
417 final public function can_edit(?content
$content = null): bool {
418 if (!$this->is_feature_supported(self
::CAN_EDIT
)) {
422 if (!$this->can_access()) {
426 if (!is_null($content) && !$this->can_manage($content)) {
430 $classname = 'contenttype/'.$this->get_plugin_name();
432 $editioncap = $classname.':useeditor';
433 $hascapabilities = has_all_capabilities(['moodle/contentbank:useeditor', $editioncap], $this->context
);
434 return $hascapabilities && $this->is_edit_allowed($content);
438 * Returns plugin allows edition.
440 * @param content $content The content to be edited.
441 * @return bool True if plugin allows edition. False otherwise.
443 protected function is_edit_allowed(?content
$content): bool {
444 // Plugins can overwrite this function to add any check they need.
449 * Returns whether or not the user has permission to download the content.
452 * @param content $content The content to be downloaded.
453 * @return bool True if the user can download the content. False otherwise.
455 final public function can_download(content
$content): bool {
456 if (!$this->is_feature_supported(self
::CAN_DOWNLOAD
)) {
460 if (!$this->can_access()) {
464 $hascapability = has_capability('moodle/contentbank:downloadcontent', $this->context
);
465 return $hascapability && $this->is_download_allowed($content);
469 * Returns plugin allows downloading.
472 * @param content $content The content to be downloaed.
473 * @return bool True if plugin allows downloading. False otherwise.
475 protected function is_download_allowed(content
$content): bool {
476 // Plugins can overwrite this function to add any check they need.
481 * Returns the plugin supports the feature.
483 * @param string $feature Feature code e.g CAN_UPLOAD
484 * @return bool True if content could be uploaded. False otherwise.
486 final public function is_feature_supported(string $feature): bool {
487 return in_array($feature, $this->get_implemented_features());
491 * Return an array of implemented features by the plugins.
495 abstract protected function get_implemented_features(): array;
498 * Return an array of extensions the plugins could manage.
502 abstract public function get_manageable_extensions(): array;
505 * Returns the list of different types of the given content type.
507 * A content type can have one or more options for creating content. This method will report all of them or only the content
508 * type itself if it has no other options.
510 * @return array An object for each type:
511 * - string typename: descriptive name of the type.
512 * - string typeeditorparams: params required by this content type editor.
513 * - url typeicon: this type icon.
515 abstract public function get_contenttype_types(): array;