Skip to content

Commit

Permalink
MDL-67062 core_h5p: delete libraries
Browse files Browse the repository at this point in the history
New feature to let admins to remove H5P libraries/content types.

Thanks Ferran Recio for your contribution with the renderer!

AMOS BEGIN
 CPY [actions,core],[actions,core_h5p]
AMOS END
  • Loading branch information
sarjona committed Feb 26, 2020
1 parent 2b8d973 commit 64a2bd1
Show file tree
Hide file tree
Showing 8 changed files with 546 additions and 28 deletions.
106 changes: 106 additions & 0 deletions h5p/classes/api.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 2 additions & 11 deletions h5p/classes/framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -1133,17 +1133,8 @@ public function unlockDependencyStorage() {
* @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));
// Delete also the cache assets to rebuild them next time.
$this->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));
$factory = new \core_h5p\factory();
\core_h5p\api::delete_library($factory, $library);
}

/**
Expand Down
99 changes: 99 additions & 0 deletions h5p/classes/output/libraries.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
47 changes: 30 additions & 17 deletions h5p/libraries.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

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);

Expand All @@ -38,12 +41,34 @@
$PAGE->set_pagelayout('admin');
$PAGE->set_heading($pagetitle);

$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();

Expand All @@ -55,7 +80,7 @@
$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');
Expand All @@ -67,23 +92,11 @@

// 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 changes: 12 additions & 0 deletions h5p/templates/h5plibraries.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -87,6 +88,11 @@
{{{ title }}}
</td>
<td>{{{ major_version }}}.{{{ minor_version }}}.{{{ patch_version }}}</td>
<td>
{{#actionmenu}}
{{>core/action_menu}}
{{/actionmenu}}
</td>
</tr>
{{/runnable}}
{{/contenttypes}}
Expand All @@ -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>
Expand All @@ -111,6 +118,11 @@
{{{ title }}}
</td>
<td>{{{ major_version }}}.{{{ minor_version }}}.{{{ patch_version }}}</td>
<td>
{{#actionmenu}}
{{>core/action_menu}}
{{/actionmenu}}
</td>
</tr>
{{/runnable}}
{{/contenttypes}}
Expand Down
Loading

0 comments on commit 64a2bd1

Please sign in to comment.