Merge branch 'MDL-67062-master' of git://github.com/sarjona/moodle

This commit is contained in:
Eloy Lafuente (stronk7)
2020-02-26 22:44:47 +01:00
8 changed files with 550 additions and 26 deletions
+106
View File
@@ -0,0 +1,106 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains API class for the H5P area.
*
* @package core_h5p
* @copyright 2020 Sara Arjona <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_h5p;
defined('MOODLE_INTERNAL') || die();
/**
* Contains API class for the H5P area.
*
* @copyright 2020 Sara Arjona <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class api {
/**
* Delete a library and also all the libraries depending on it and the H5P contents using it. For the H5P content, only the
* database entries in {h5p} are removed (the .h5p files are not removed in order to let users to deploy them again).
*
* @param factory $factory The H5P factory.
* @param \stdClass $library The library to delete.
*/
public static function delete_library(factory $factory, \stdClass $library): void {
global $DB;
// Get the H5P contents using this library, to remove them from DB. The .h5p files won't be removed
// so they will be displayed by the player next time a user with the proper permissions accesses it.
$sql = 'SELECT DISTINCT hcl.h5pid
FROM {h5p_contents_libraries} hcl
WHERE hcl.libraryid = :libraryid';
$params = ['libraryid' => $library->id];
$h5pcontents = $DB->get_records_sql($sql, $params);
foreach ($h5pcontents as $h5pcontent) {
$factory->get_framework()->deleteContentData($h5pcontent->h5pid);
}
$fs = $factory->get_core()->fs;
$framework = $factory->get_framework();
// Delete the library from the file system.
$fs->delete_library(array('libraryId' => $library->id));
// Delete also the cache assets to rebuild them next time.
$framework->deleteCachedAssets($library->id);
// Remove library data from database.
$DB->delete_records('h5p_library_dependencies', array('libraryid' => $library->id));
$DB->delete_records('h5p_libraries', array('id' => $library->id));
// Remove the libraries using this library.
$requiredlibraries = self::get_dependent_libraries($library->id);
foreach ($requiredlibraries as $requiredlibrary) {
self::delete_library($factory, $requiredlibrary);
}
}
/**
* Get all the libraries using a defined library.
*
* @param int $libraryid The library to get its dependencies.
* @return array List of libraryid with all the libraries required by a defined library.
*/
public static function get_dependent_libraries(int $libraryid): array {
global $DB;
$sql = 'SELECT DISTINCT hl.*
FROM {h5p_library_dependencies} hld
JOIN {h5p_libraries} hl ON hl.id = hld.libraryid
WHERE hld.requiredlibraryid = :libraryid';
$params = ['libraryid' => $libraryid];
return $DB->get_records_sql($sql, $params);
}
/**
* Get a library from an identifier.
*
* @param int $libraryid The library identifier.
* @return \stdClass The library object having the library identifier defined.
* @throws dml_exception A DML specific exception is thrown if the libraryid doesn't exist.
*/
public static function get_library(int $libraryid): \stdClass {
global $DB;
return $DB->get_record('h5p_libraries', ['id' => $libraryid], '*', MUST_EXIST);
}
}
+6 -9
View File
@@ -1133,15 +1133,8 @@ class framework implements \H5PFrameworkInterface {
* @param stdClass $library Library object with id, name, major version and minor version
*/
public function deleteLibrary($library) {
global $DB;
$fs = new \core_h5p\file_storage();
// Delete the library from the file system.
$fs->delete_library(array('libraryId' => $library->id));
// Remove library data from database.
$DB->delete_records('h5p_library_dependencies', array('libraryid' => $library->id));
$DB->delete_records('h5p_libraries', array('id' => $library->id));
$factory = new \core_h5p\factory();
\core_h5p\api::delete_library($factory, $library);
}
/**
@@ -1461,6 +1454,10 @@ class framework implements \H5PFrameworkInterface {
list($sql, $params) = $DB->get_in_or_equal($hashes, SQL_PARAMS_NAMED);
// Remove all invalid keys.
$DB->delete_records_select('h5p_libraries_cachedassets', 'hash ' . $sql, $params);
// Remove also the cachedassets files.
$fs = new file_storage();
$fs->deleteCachedAssets($hashes);
}
return $hashes;
+99
View File
@@ -0,0 +1,99 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains class core_h5p\output\libraries
*
* @package core_h5p
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_h5p\output;
defined('MOODLE_INTERNAL') || die();
use renderable;
use templatable;
use renderer_base;
use stdClass;
use moodle_url;
use action_menu;
use action_menu_link;
use pix_icon;
/**
* Class to help display H5P library management table.
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class libraries implements renderable, templatable {
/** @var H5P factory */
protected $factory;
/** @var H5P library list */
protected $libraries;
/**
* Constructor.
*
* @param factory $factory The H5P factory
* @param array $libraries array of h5p libraries records
*/
public function __construct(\core_h5p\factory $factory, array $libraries) {
$this->factory = $factory;
$this->libraries = $libraries;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output
* @return stdClass
*/
public function export_for_template(renderer_base $output) {
$installed = [];
$filestorage = $this->factory->get_core()->fs;
foreach ($this->libraries as $libraryname => $versions) {
foreach ($versions as $version) {
// Get the icon URL.
$version->icon = $filestorage->get_icon_url(
$version->id,
$version->machine_name,
$version->major_version,
$version->minor_version
);
// Get the action menu options.
$actionmenu = new action_menu();
$actionmenu->set_menu_trigger(get_string('actions', 'core_h5p'));
$actionmenu->set_alignment(action_menu::TL, action_menu::BL);
$actionmenu->prioritise = true;
$actionmenu->add_primary_action(new action_menu_link(
new moodle_url('/h5p/libraries.php', ['deletelibrary' => $version->id]),
new pix_icon('t/delete', get_string('deletelibraryversion', 'core_h5p')),
get_string('deletelibraryversion', 'core_h5p')
));
$version->actionmenu = $actionmenu->export_for_template($output);
$installed[] = $version;
}
}
$r = new stdClass();
$r->contenttypes = $installed;
return $r;
}
}
+30 -17
View File
@@ -26,6 +26,9 @@ require_once(__DIR__ . '/../config.php');
require_login(null, false);
$deletelibrary = optional_param('deletelibrary', null, PARAM_INT);
$confirm = optional_param('confirm', false, PARAM_BOOL);
$context = context_system::instance();
require_capability('moodle/h5p:updatelibraries', $context);
@@ -38,12 +41,34 @@ $PAGE->set_pagelayout('admin');
$PAGE->set_title("$SITE->shortname: " . $pagetitle);
$PAGE->set_heading($SITE->fullname);
$h5pfactory = new \core_h5p\factory();
if ($deletelibrary) {
$library = \core_h5p\api::get_library($deletelibrary);
if ($confirm) {
require_sesskey();
\core_h5p\api::delete_library($h5pfactory, $library);
redirect(new moodle_url('/h5p/libraries.php'));
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('deleting', 'core_h5p'));
echo $OUTPUT->confirm(
get_string('deletelibraryconfirm', 'core_h5p', [
'name' => format_string($library->title),
'version' => format_string($library->majorversion . '.' . $library->minorversion . '.' . $library->patchversion),
]),
new moodle_url($PAGE->url, ['deletelibrary' => $deletelibrary, 'confirm' => 1]),
new moodle_url('/h5p/libraries.php')
);
echo $OUTPUT->footer();
die();
}
echo $OUTPUT->header();
echo $OUTPUT->heading($pagetitle);
echo $OUTPUT->box(get_string('librariesmanagerdescription', 'core_h5p'));
$form = new \core_h5p\form\uploadlibraries_form();
$h5pfactory = new \core_h5p\factory();
if ($data = $form->get_data()) {
require_sesskey();
@@ -55,7 +80,7 @@ if ($data = $form->get_data()) {
$file = reset($files);
// Validate and save the H5P package.
// Because we are passing skipcontent = true to save_h5p function, the returning value is false in an error
// Because we are passing skipcontent = true to save_h5p function, the returning value is false if an error
// is encountered, null when successfully saving the package without creating the content.
if (\core_h5p\helper::save_h5p($h5pfactory, $file, new stdClass(), false, true) === false) {
echo $OUTPUT->notification(get_string('invalidpackage', 'core_h5p'), 'error');
@@ -67,23 +92,11 @@ $form->display();
// Load installed Libraries.
$framework = $h5pfactory->get_framework();
$filestorage = $h5pfactory->get_core()->fs;
$libraries = $framework->loadLibraries();
$installed = [];
foreach ($libraries as $libraryname => $versions) {
foreach ($versions as $version) {
$version->icon = $filestorage->get_icon_url(
$version->id,
$version->machine_name,
$version->major_version,
$version->minor_version
);
$installed[] = $version;
}
}
if (count($installed)) {
echo $OUTPUT->render_from_template('core_h5p/h5plibraries', (object)['contenttypes' => $installed]);
if (!empty($libraries)) {
$libs = new \core_h5p\output\libraries($h5pfactory, $libraries);
echo $OUTPUT->render_from_template('core_h5p/h5plibraries', $libs->export_for_template($OUTPUT));
}
echo $OUTPUT->footer();
+12
View File
@@ -69,6 +69,7 @@
<tr>
<th>{{#str}}description, core{{/str}}</th>
<th>{{#str}}version, core{{/str}}</th>
<th aria-label="{{#str}}actions, core_h5p{{/str}}"></th>
</tr>
</thead>
<tbody>
@@ -87,6 +88,11 @@
{{{ title }}}
</td>
<td>{{{ major_version }}}.{{{ minor_version }}}.{{{ patch_version }}}</td>
<td>
{{#actionmenu}}
{{>core/action_menu}}
{{/actionmenu}}
</td>
</tr>
{{/runnable}}
{{/contenttypes}}
@@ -101,6 +107,7 @@
<tr>
<th>{{#str}}description, core{{/str}}</th>
<th>{{#str}}version, core{{/str}}</th>
<th aria-label="{{#str}}actions, core_h5p{{/str}}"></th>
</tr>
</thead>
<tbody>
@@ -111,6 +118,11 @@
{{{ title }}}
</td>
<td>{{{ major_version }}}.{{{ minor_version }}}.{{{ patch_version }}}</td>
<td>
{{#actionmenu}}
{{>core/action_menu}}
{{/actionmenu}}
</td>
</tr>
{{/runnable}}
{{/contenttypes}}
+276
View File
@@ -0,0 +1,276 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Testing the H5P API.
*
* @package core_h5p
* @category test
* @copyright 2020 Sara Arjona <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types = 1);
namespace core_h5p;
defined('MOODLE_INTERNAL') || die();
/**
* Test class covering the H5P API.
*
* @package core_h5p
* @copyright 2020 Sara Arjona <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class api_testcase extends \advanced_testcase {
/**
* Test the behaviour of delete_library().
*
* @dataProvider delete_library_provider
* @param string $libraryname Machine name of the library to delete.
* @param int $expectedh5p Total of H5P contents expected after deleting the library.
* @param int $expectedlibraries Total of H5P libraries expected after deleting the library.
* @param int $expectedcontents Total of H5P content_libraries expected after deleting the library.
* @param int $expecteddependencies Total of H5P library dependencies expected after deleting the library.
*/
public function test_delete_library(string $libraryname, int $expectedh5p, int $expectedlibraries,
int $expectedcontents, int $expecteddependencies): void {
global $DB;
$this->setRunTestInSeparateProcess(true);
$this->resetAfterTest();
// Generate h5p related data.
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$generator->generate_h5p_data();
$generator->create_library_record('H5P.TestingLibrary', 'TestingLibrary', 1, 0);
// Check the current content in H5P tables is the expected.
$counth5p = $DB->count_records('h5p');
$counth5plibraries = $DB->count_records('h5p_libraries');
$counth5pcontents = $DB->count_records('h5p_contents_libraries');
$counth5pdependencies = $DB->count_records('h5p_library_dependencies');
$this->assertSame(1, $counth5p);
$this->assertSame(7, $counth5plibraries);
$this->assertSame(5, $counth5pcontents);
$this->assertSame(7, $counth5pdependencies);
// Delete this library.
$factory = new factory();
$library = $DB->get_record('h5p_libraries', ['machinename' => $libraryname]);
if ($library) {
api::delete_library($factory, $library);
}
// Check the expected libraries and content have been removed.
$counth5p = $DB->count_records('h5p');
$counth5plibraries = $DB->count_records('h5p_libraries');
$counth5pcontents = $DB->count_records('h5p_contents_libraries');
$counth5pdependencies = $DB->count_records('h5p_library_dependencies');
$this->assertSame($expectedh5p, $counth5p);
$this->assertSame($expectedlibraries, $counth5plibraries);
$this->assertSame($expectedcontents, $counth5pcontents);
$this->assertSame($expecteddependencies, $counth5pdependencies);
}
/**
* Data provider for test_delete_library().
*
* @return array
*/
public function delete_library_provider(): array {
return [
'Delete MainLibrary' => [
'MainLibrary',
0,
6,
0,
4,
],
'Delete Library1' => [
'Library1',
0,
5,
0,
1,
],
'Delete Library2' => [
'Library2',
0,
4,
0,
1,
],
'Delete Library3' => [
'Library3',
0,
4,
0,
0,
],
'Delete Library4' => [
'Library4',
0,
4,
0,
1,
],
'Delete Library5' => [
'Library5',
0,
3,
0,
0,
],
'Delete a library without dependencies' => [
'H5P.TestingLibrary',
1,
6,
5,
7,
],
'Delete unexisting library' => [
'LibraryX',
1,
7,
5,
7,
],
];
}
/**
* Test the behaviour of get_dependent_libraries().
*
* @dataProvider get_dependent_libraries_provider
* @param string $libraryname Machine name of the library to delete.
* @param int $expectedvalue Total of H5P required libraries expected.
*/
public function test_get_dependent_libraries(string $libraryname, int $expectedvalue): void {
global $DB;
$this->resetAfterTest();
// Generate h5p related data.
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$generator->generate_h5p_data();
$generator->create_library_record('H5P.TestingLibrary', 'TestingLibrary', 1, 0);
// Get required libraries.
$library = $DB->get_record('h5p_libraries', ['machinename' => $libraryname], 'id');
if ($library) {
$libraries = api::get_dependent_libraries((int)$library->id);
} else {
$libraries = [];
}
$this->assertCount($expectedvalue, $libraries);
}
/**
* Data provider for test_get_dependent_libraries().
*
* @return array
*/
public function get_dependent_libraries_provider(): array {
return [
'Main library of a content' => [
'MainLibrary',
0,
],
'Library1' => [
'Library1',
1,
],
'Library2' => [
'Library2',
2,
],
'Library without dependencies' => [
'H5P.TestingLibrary',
0,
],
'Unexisting library' => [
'LibraryX',
0,
],
];
}
/**
* Test the behaviour of get_library().
*
* @dataProvider get_library_provider
* @param string $libraryname Machine name of the library to delete.
* @param bool $emptyexpected Wether the expected result is empty or not.
*/
public function test_get_library(string $libraryname, bool $emptyexpected): void {
global $DB;
$this->resetAfterTest();
// Generate h5p related data.
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$generator->generate_h5p_data();
$generator->create_library_record('H5P.TestingLibrary', 'TestingLibrary', 1, 0);
// Get the library identifier.
$library = $DB->get_record('h5p_libraries', ['machinename' => $libraryname], 'id');
if ($library) {
$result = api::get_library((int)$library->id);
} else {
$result = null;
}
if ($emptyexpected) {
$this->assertEmpty($result);
} else {
$this->assertEquals($library->id, $result->id);
$this->assertEquals($libraryname, $result->machinename);
}
}
/**
* Data provider for test_get_library().
*
* @return array
*/
public function get_library_provider(): array {
return [
'Main library of a content' => [
'MainLibrary',
false,
],
'Library1' => [
'Library1',
false,
],
'Library without dependencies' => [
'H5P.TestingLibrary',
false,
],
'Unexisting library' => [
'LibraryX',
true,
],
];
}
}
+17
View File
@@ -44,3 +44,20 @@ Feature: Upload and list H5P libraries and content types installed
And I click on "Installed H5P libraries" "link"
And I should see "1.3" in the "Question" "table_row"
And I should see "1.4"
@javascript
Scenario: Delete H5P library.
Given I log in as "admin"
And I navigate to "H5P > Manage H5P content types" in site administration
And I upload "h5p/tests/fixtures/filltheblanks.h5p" file to "H5P content type" filemanager
And I click on "Upload H5P content types" "button" in the "#fitem_id_submitbutton" "css_element"
And I wait until the page is ready
And I click on "Installed H5P libraries" "link"
When I click on "Delete version" "link" in the "H5P.FontIcons" "table_row"
And I press "Continue"
And I click on "Installed H5P content types" "link"
Then I should not see "Fill in the Blanks"
And I click on "Installed H5P libraries" "link"
And I should not see "H5P.FontIcons"
And I should not see "Joubel UI"
And I should see "Transition"
+4
View File
@@ -23,6 +23,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['actions'] = 'Actions';
$string['addedandupdatedpp'] = 'Added {$a->%new} new H5P libraries and updated {$a->%old} old ones.';
$string['addedandupdatedps'] = 'Added {$a->%new} new H5P libraries and updated {$a->%old} old one.';
$string['addedandupdatedsp'] = 'Added {$a->%new} new H5P library and updated {$a->%old} old ones.';
@@ -67,6 +68,9 @@ $string['couldNotParseJSONFromZip'] = 'Unable to parse JSON from the package: {$
$string['couldNotReadFileFromZip'] = 'Unable to read file from the package: {$a->%fileName}';
$string['creativecommons'] = 'Creative Commons';
$string['date'] = 'Date';
$string['deletelibraryconfirm'] = '<p>Are you sure you want to delete version <em>\'{$a->version}\'</em> from library <em>\'{$a->name}\'</em>? It will remove the library and all its uses.</p><p>This operation can not be undone.</p>';
$string['deletelibraryversion'] = 'Delete version';
$string['deleting'] = 'Deleting a library';
$string['description'] = 'Description';
$string['disablefullscreen'] = 'Disable fullscreen';
$string['download'] = 'Download';