Skip to content

Commit

Permalink
MDL-68448 core_h5p: Add get export information to helper
Browse files Browse the repository at this point in the history
Create a new method in the helper to use in the player,
in the external WS and in the API. Also, add a
new method in API to help to get export information
by other WS.
  • Loading branch information
cescobedo committed May 25, 2020
1 parent 71965a8 commit 14b463c
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 138 deletions.
39 changes: 39 additions & 0 deletions h5p/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,43 @@ public static function get_content_from_pathnamehash(string $pathnamehash): ?\st

return ($h5p) ? $h5p : null;
}

/**
* Return the H5P export information file when the file has been deployed.
* Otherwise, return null if H5P file:
* i) has not been deployed.
* ii) has changed the content.
*
* The information returned will be:
* - filename, filepath, mimetype, filesize, timemodified and fileurl.
*
* @param int $contextid ContextId of the H5P activity.
* @param factory $factory The \core_h5p\factory object.
* @param string $component component
* @param string $filearea file area
* @return array|null Return file info otherwise null.
*/
public static function get_export_info_from_context_id(int $contextid,
factory $factory,
string $component,
string $filearea): ?array {

$core = $factory->get_core();
$fs = get_file_storage();
$files = $fs->get_area_files($contextid, $component, $filearea, 0, 'id', false);
$file = reset($files);

if ($h5p = self::get_content_from_pathnamehash($file->get_pathnamehash())) {
if ($h5p->contenthash == $file->get_contenthash()) {
$content = $core->loadContent($h5p->id);
$slug = $content['slug'] ? $content['slug'] . '-' : '';
$filename = "{$slug}{$content['id']}.h5p";
$deployedfile = helper::get_export_info($filename, null, $factory);

return $deployedfile;
}
}

return null;
}
}
46 changes: 46 additions & 0 deletions h5p/classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,50 @@ public static function parse_js_array(string $jscontent): array {

return $strings;
}

/**
* Get the information related to the H5P export file.
* The information returned will be:
* - filename, filepath, mimetype, filesize, timemodified and fileurl.
*
* @param string $exportfilename The H5P export filename (with slug).
* @param \moodle_url $url The URL of the exported file.
* @param factory $factory The \core_h5p\factory object
* @return array|null The information export file otherwise null.
*/
public static function get_export_info(string $exportfilename, \moodle_url $url = null, ?factory $factory = null): ?array {

if (!$factory) {
$factory = new factory();
}
$core = $factory->get_core();

// Get export file.
if (!$fileh5p = $core->fs->get_export_file($exportfilename)) {
return null;
}

// Build the export info array.
$file = [];
$file['filename'] = $fileh5p->get_filename();
$file['filepath'] = $fileh5p->get_filepath();
$file['mimetype'] = $fileh5p->get_mimetype();
$file['filesize'] = $fileh5p->get_filesize();
$file['timemodified'] = $fileh5p->get_timemodified();

if (!$url) {
$url = \moodle_url::make_webservice_pluginfile_url(
$fileh5p->get_contextid(),
$fileh5p->get_component(),
$fileh5p->get_filearea(),
'',
'',
$fileh5p->get_filename()
);
}

$file['fileurl'] = $url->out(false);

return $file;
}
}
21 changes: 3 additions & 18 deletions h5p/classes/player.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public static function get_embed_url(string $url): \moodle_url {
}

/**
* Return the export file for Mobile App.
* Return the info export file for Mobile App.
*
* @return array
*/
Expand All @@ -467,23 +467,8 @@ public function get_export_file(): array {
$path = $exporturl->out_as_local_url();
$parts = explode('/', $path);
$filename = array_pop($parts);
// Get the the export file.
$systemcontext = \context_system::instance();
$fs = get_file_storage();
$fileh5p = $fs->get_file($systemcontext->id,
\core_h5p\file_storage::COMPONENT,
\core_h5p\file_storage::EXPORT_FILEAREA,
0,
'/',
$filename);
// Get the options that the Mobile App needs.
$file = [];
$file['filename'] = $fileh5p->get_filename();
$file['filepath'] = $fileh5p->get_filepath();
$file['mimetype'] = $fileh5p->get_mimetype();
$file['filesize'] = $fileh5p->get_filesize();
$file['timemodified'] = $fileh5p->get_timemodified();
$file['fileurl'] = $exporturl->out(false);
// Get the required info from the export file to be able to get the export file by third apps.
$file = helper::get_export_info($filename, $exporturl);

return $file;
}
Expand Down
52 changes: 52 additions & 0 deletions h5p/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,56 @@ public function test_delete_content_from_pluginfile_url(): void {
api::delete_content_from_pluginfile_url($url->out(), $factory);
$this->assertEquals(0, $DB->count_records('h5p'));
}

/**
* Test the behaviour of get_export_info_from_context_id().
*/
public function test_get_export_info_from_context_id(): void {
global $DB;

$this->setRunTestInSeparateProcess(true);
$this->resetAfterTest();
$factory = new factory();

// Create the H5P data.
$filename = 'find-the-words.h5p';
$syscontext = \context_system::instance();

// Test scenario 1: H5P exists and deployed.
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$fakeexportfile = $generator->create_export_file($filename,
$syscontext->id,
\core_h5p\file_storage::COMPONENT,
\core_h5p\file_storage::EXPORT_FILEAREA);

$exportfile = api::get_export_info_from_context_id($syscontext->id,
$factory,
\core_h5p\file_storage::COMPONENT,
\core_h5p\file_storage::EXPORT_FILEAREA);
$this->assertEquals($fakeexportfile['filename'], $exportfile['filename']);
$this->assertEquals($fakeexportfile['filepath'], $exportfile['filepath']);
$this->assertEquals($fakeexportfile['filesize'], $exportfile['filesize']);
$this->assertEquals($fakeexportfile['timemodified'], $exportfile['timemodified']);
$this->assertEquals($fakeexportfile['fileurl'], $exportfile['fileurl']);

// Test scenario 2: H5P exist, deployed but the content has changed.
// We need to change the contenthash to simulate the H5P file was changed.
$h5pfile = $DB->get_record('h5p', []);
$h5pfile->contenthash = sha1('testedit');
$DB->update_record('h5p', $h5pfile);
$exportfile = api::get_export_info_from_context_id($syscontext->id,
$factory,
\core_h5p\file_storage::COMPONENT,
\core_h5p\file_storage::EXPORT_FILEAREA);
$this->assertNull($exportfile);

// Tests scenario 3: H5P is not deployed.
// We need to delete the H5P record to simulate the H5P was not deployed.
$DB->delete_records('h5p', ['id' => $h5pfile->id]);
$exportfile = api::get_export_info_from_context_id($syscontext->id,
$factory,
\core_h5p\file_storage::COMPONENT,
\core_h5p\file_storage::EXPORT_FILEAREA);
$this->assertNull($exportfile);
}
}
Loading

0 comments on commit 14b463c

Please sign in to comment.