// Add JavaScript settings.
$root = $CFG->wwwroot;
- $filespathbase = "{$root}/pluginfile.php/{$context->id}/core_h5p/";
+ $filespathbase = \moodle_url::make_draftfile_url(0, '', '');
$factory = new factory();
$contentvalidator = $factory->get_content_validator();
$editorajaxtoken = core::createToken(editor_ajax::EDITOR_AJAX_TOKEN);
$sesskey = sesskey();
$settings['editor'] = [
- 'filesPath' => $filespathbase . 'editor',
+ 'filesPath' => $filespathbase->out(),
'fileIcon' => [
'path' => $url . 'images/binary-file.png',
'width' => 50,
public const EXPORT_FILEAREA = 'export';
/** The icon filename */
public const ICON_FILENAME = 'icon.svg';
- /** The editor file area */
+
+ /**
+ * The editor file area.
+ * @deprecated since Moodle 3.10 MDL-68909. Please do not use this constant any more.
+ * @todo MDL-69530 This will be deleted in Moodle 4.2.
+ */
public const EDITOR_FILEAREA = 'editor';
/**
* @return int The id of the saved file.
*/
public function saveFile($file, $contentid) {
+ global $USER;
+
+ $context = $this->context->id;
+ $component = self::COMPONENT;
+ $filearea = self::CONTENT_FILEAREA;
+ if ($contentid === 0) {
+ $usercontext = \context_user::instance($USER->id);
+ $context = $usercontext->id;
+ $component = 'user';
+ $filearea = 'draft';
+ }
+
$record = array(
- 'contextid' => $this->context->id,
- 'component' => self::COMPONENT,
- 'filearea' => $contentid === 0 ? self::EDITOR_FILEAREA : self::CONTENT_FILEAREA,
+ 'contextid' => $context,
+ 'component' => $component,
+ 'filearea' => $filearea,
'itemid' => $contentid,
'filepath' => '/' . $file->getType() . 's/',
'filename' => $file->getName()
*/
public function cloneContentFile($file, $fromid, $tocontent): void {
// Determine source filearea and itemid.
- if ($fromid === self::EDITOR_FILEAREA) {
- $sourcefilearea = self::EDITOR_FILEAREA;
+ if ($fromid === 'editor') {
+ $sourcefilearea = 'draft';
$sourceitemid = 0;
} else {
$sourcefilearea = self::CONTENT_FILEAREA;
* @return stored_file|null
*/
private function get_file(string $filearea, int $itemid, string $file): ?stored_file {
- if ($filearea === 'editor') {
+ global $USER;
+
+ $component = self::COMPONENT;
+ $context = $this->context->id;
+ if ($filearea === 'draft') {
$itemid = 0;
+ $component = 'user';
+ $usercontext = \context_user::instance($USER->id);
+ $context = $usercontext->id;
}
$filepath = '/'. dirname($file). '/';
$filename = basename($file);
// Load file.
- $existingfile = $this->fs->get_file($this->context->id, self::COMPONENT, $filearea, $itemid, $filepath, $filename);
+ $existingfile = $this->fs->get_file($context, $component, $filearea, $itemid, $filepath, $filename);
if (!$existingfile) {
return null;
}
// Create file record for content.
$record = array(
'contextid' => $this->context->id,
- 'component' => self::COMPONENT,
- 'filearea' => $contentid > 0 ? self::CONTENT_FILEAREA : self::EDITOR_FILEAREA,
+ 'component' => $contentid > 0 ? self::COMPONENT : 'user',
+ 'filearea' => $contentid > 0 ? self::CONTENT_FILEAREA : 'draft',
'itemid' => $contentid > 0 ? $contentid : 0,
'filepath' => '/' . $foldername . '/',
'filename' => $filename
}
$itemid = array_shift($args);
break;
- case \core_h5p\file_storage::EDITOR_FILEAREA:
case \core_h5p\file_storage::CACHED_ASSETS_FILEAREA:
case \core_h5p\file_storage::EXPORT_FILEAREA:
$itemid = 0;
public function test_add_editor_to_form() {
global $PAGE, $CFG;
+ $this->resetAfterTest();
+ $this->setAdminUser();
+
// Get form data.
$form = $this->get_test_form();
$mform = $form->getform();
* @throws coding_exception
*/
public function create_content_file(string $file, string $filearea, int $contentid = 0): stored_file {
+ global $USER;
+
$filepath = '/'.dirname($file).'/';
$filename = basename($file);
throw new coding_exception('Files belonging to an H5P content must specify the H5P content id');
}
- $content = 'fake content';
+ if ($filearea === 'draft') {
+ $usercontext = \context_user::instance($USER->id);
+ $context = $usercontext->id;
+ $component = 'user';
+ $itemid = 0;
+ } else {
+ $systemcontext = context_system::instance();
+ $context = $systemcontext->id;
+ $component = \core_h5p\file_storage::COMPONENT;
+ $itemid = $contentid;
+ }
- $systemcontext = context_system::instance();
+ $content = 'fake content';
$filerecord = array(
- 'contextid' => $systemcontext->id,
- 'component' => \core_h5p\file_storage::COMPONENT,
+ 'contextid' => $context,
+ 'component' => $component,
'filearea' => $filearea,
- 'itemid' => ($filearea === 'editor') ? 0 : $contentid,
+ 'itemid' => $itemid,
'filepath' => $filepath,
'filename' => $filename,
);
*/
public function test_get_file(): void {
+ $this->setAdminUser();
$file = 'img/fake.png';
$h5pcontentid = 3;
$this->assertInstanceOf('stored_file', $contentfile);
// Add a file to editor.
- $this->h5p_generator->create_content_file($file, file_storage::EDITOR_FILEAREA, $h5pcontentid);
+ $this->h5p_generator->create_content_file($file, 'draft', $h5pcontentid);
- $editorfile = $method->invoke(new file_storage(), file_storage::EDITOR_FILEAREA, $h5pcontentid, $file);
+ $editorfile = $method->invoke(new file_storage(), 'draft', $h5pcontentid, $file);
// Check that it returns an instance of store_file.
$this->assertInstanceOf('stored_file', $editorfile);
*/
public function test_cloneContentFile(): void {
+ $admin = get_admin();
+ $usercontext = \context_user::instance($admin->id);
+ $this->setUser($admin);
// Upload a file to the editor.
$file = 'images/fake.jpg';
$filepath = '/'.dirname($file).'/';
$content = 'abcd';
$filerecord = array(
- 'contextid' => $this->h5p_fs_context->id,
- 'component' => file_storage::COMPONENT,
- 'filearea' => file_storage::EDITOR_FILEAREA,
+ 'contextid' => $usercontext->id,
+ 'component' => 'user',
+ 'filearea' => 'draft',
'itemid' => 0,
'filepath' => $filepath,
'filename' => $filename,
$filename = basename($file);
$sourcecontentid = 111;
- $filerecord['filearea'] = 'content';
+ $filerecord['contextid'] = $this->h5p_fs_context->id;
+ $filerecord['component'] = file_storage::COMPONENT;
+ $filerecord['filearea'] = file_storage::CONTENT_FILEAREA;
$filerecord['itemid'] = $sourcecontentid;
$filerecord['filepath'] = $filepath;
$filerecord['filename'] = $filename;
be called before executing a task, and a new function \core\task\manager::get_running_tasks()
returns information about currently-running tasks.
* New library function rename_to_unused_name() to rename a file within its current location.
+* Constant \core_h5p\file_storage::EDITOR_FILEAREA has been deprecated
+ because it's not required any more.
=== 3.9 ===
* Following function has been deprecated, please use \core\task\manager::run_from_cli().