Skip to content

Commit

Permalink
Merge branch 'MDL-68909-master' of git://github.com/sarjona/moodle in…
Browse files Browse the repository at this point in the history
…to master
  • Loading branch information
stronk7 authored and sarjona committed Sep 17, 2020
2 parents 5e124b9 + 67a1115 commit e390bab
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 24 deletions.
4 changes: 2 additions & 2 deletions h5p/classes/editor.php
Expand Up @@ -382,15 +382,15 @@ private function add_assets_to_page(): void {

// 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,
Expand Down
44 changes: 34 additions & 10 deletions h5p/classes/file_storage.php
Expand Up @@ -48,7 +48,12 @@ class file_storage implements \H5PFileStorage {
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';

/**
Expand Down Expand Up @@ -331,10 +336,22 @@ public function getContent($filepath) {
* @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()
Expand All @@ -357,8 +374,8 @@ public function saveFile($file, $contentid) {
*/
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;
Expand Down Expand Up @@ -791,15 +808,22 @@ private function get_itemid_for_file(string $filearea, string $filepath, string
* @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;
}
Expand All @@ -824,8 +848,8 @@ private function move_file(string $sourcefile, int $contentid): void {
// 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
Expand Down
1 change: 0 additions & 1 deletion h5p/lib.php
Expand Up @@ -94,7 +94,6 @@ function core_h5p_pluginfile($course, $cm, $context, string $filearea, array $ar
}
$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;
Expand Down
3 changes: 3 additions & 0 deletions h5p/tests/editor_test.php
Expand Up @@ -182,6 +182,9 @@ public function test_set_library() {
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();
Expand Down
22 changes: 17 additions & 5 deletions h5p/tests/generator/lib.php
Expand Up @@ -414,22 +414,34 @@ public function create_content_types(array $typestonotinstall, core $core): arra
* @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);

if (($filearea === 'content') && ($contentid == 0)) {
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,
);
Expand Down
18 changes: 12 additions & 6 deletions h5p/tests/h5p_file_storage_test.php
Expand Up @@ -625,6 +625,7 @@ public function get_icon_url_provider(): array {
*/
public function test_get_file(): void {

$this->setAdminUser();
$file = 'img/fake.png';
$h5pcontentid = 3;

Expand All @@ -641,9 +642,9 @@ public function test_get_file(): void {
$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);
Expand Down Expand Up @@ -692,6 +693,9 @@ public function test_move_file(): void {
*/
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).'/';
Expand All @@ -700,9 +704,9 @@ public function test_cloneContentFile(): void {
$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,
Expand Down Expand Up @@ -731,7 +735,9 @@ public function test_cloneContentFile(): void {
$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;
Expand Down
10 changes: 10 additions & 0 deletions lib/db/upgrade.php
Expand Up @@ -2684,5 +2684,15 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2021052500.06);
}

if ($oldversion < 2021052500.13) {
// Remove all the files with component='core_h5p' and filearea='editor' because they won't be used anymore.
$fs = get_file_storage();
$syscontext = context_system::instance();
$fs->delete_area_files($syscontext->id, 'core_h5p', 'editor');

// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.13);
}

return true;
}
2 changes: 2 additions & 0 deletions lib/upgrade.txt
Expand Up @@ -42,6 +42,8 @@ information provided here is intended especially for developers.
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().
Expand Down

0 comments on commit e390bab

Please sign in to comment.