MDL-69143 contentbank: add import file method to content

This commit is contained in:
Ferran Recio 2020-06-26 19:56:29 +02:00
parent 94fdac9117
commit 3dfbd5a16f
8 changed files with 295 additions and 12 deletions

View file

@ -237,6 +237,42 @@ abstract class content {
return $this->content->configdata;
}
/**
* Import a file as a valid content.
*
* By default, all content has a public file area to interact with the content bank
* repository. This method should be overridden by contentypes which does not simply
* upload to the public file area.
*
* If any, the method will return the final stored_file. This way it can be invoked
* as parent::import_file in case any plugin want to store the file in the public area
* and also parse it.
*
* @throws file_exception If file operations fail
* @param stored_file $file File to store in the content file area.
* @return stored_file|null the stored content file or null if the file is discarted.
*/
public function import_file(stored_file $file): ?stored_file {
$originalfile = $this->get_file();
if ($originalfile) {
$originalfile->replace_file_with($file);
return $originalfile;
} else {
$itemid = $this->get_id();
$fs = get_file_storage();
$filerecord = [
'contextid' => $this->get_contextid(),
'component' => 'contentbank',
'filearea' => 'public',
'itemid' => $this->get_id(),
'filepath' => '/',
'filename' => $file->get_filename(),
'timecreated' => time(),
];
return $fs->create_file_from_storedfile($filerecord, $file);
}
}
/**
* Returns the $file related to this content.
*

View file

@ -224,6 +224,8 @@ class contentbank {
/**
* Create content from a file information.
*
* @throws file_exception If file operations fail
* @throws dml_exception if the content creation fails
* @param \context $context Context where to upload the file and content.
* @param int $userid Id of the user uploading the file.
* @param stored_file $file The file to get information from
@ -243,7 +245,7 @@ class contentbank {
$record->name = $filename;
$record->usercreated = $userid;
$contentype = new $classname($context);
$content = $contentype->create_content($record);
$content = $contentype->upload_content($file, $record);
$event = \core\event\contentbank_content_uploaded::create_from_record($content->get_content());
$event->trigger();
return $content;

View file

@ -27,6 +27,8 @@ namespace core_contentbank;
use core\event\contentbank_content_created;
use core\event\contentbank_content_deleted;
use core\event\contentbank_content_viewed;
use stored_file;
use file_exception;
use moodle_url;
/**
@ -62,10 +64,11 @@ abstract class contenttype {
/**
* Fills content_bank table with appropiate information.
*
* @throws dml_exception A DML specific exception is thrown for any creation error.
* @param \stdClass $record An optional content record compatible object (default null)
* @return content Object with content bank information.
*/
public function create_content(\stdClass $record = null): ?content {
public function create_content(\stdClass $record = null): content {
global $USER, $DB;
$entry = new \stdClass();
@ -79,15 +82,37 @@ abstract class contenttype {
$entry->configdata = $record->configdata ?? '';
$entry->instanceid = $record->instanceid ?? 0;
$entry->id = $DB->insert_record('contentbank_content', $entry);
if ($entry->id) {
$classname = '\\'.$entry->contenttype.'\\content';
$content = new $classname($entry);
// Trigger an event for creating the content.
$event = contentbank_content_created::create_from_record($content->get_content());
$event->trigger();
return $content;
$classname = '\\'.$entry->contenttype.'\\content';
$content = new $classname($entry);
// Trigger an event for creating the content.
$event = contentbank_content_created::create_from_record($content->get_content());
$event->trigger();
return $content;
}
/**
* Create a new content from an uploaded file.
*
* @throws file_exception If file operations fail
* @throws dml_exception if the content creation fails
* @param stored_file $file the uploaded file
* @param \stdClass|null $record an optional content record
* @return content Object with content bank information.
*/
public function upload_content(stored_file $file, \stdClass $record = null): content {
if (empty($record)) {
$record = new \stdClass();
$record->name = $file->get_filename();
}
return null;
$content = $this->create_content($record);
try {
$content->import_file($file);
} catch (file_exception $e) {
$this->delete_content($content);
throw $e;
}
return $content;
}
/**