forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-67062 core_h5p: delete libraries
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
Showing
8 changed files
with
546 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.