From 435ed93cb4dc5461c204763d36d2c6e5985c378c Mon Sep 17 00:00:00 2001 From: corinna Date: Tue, 27 Aug 2024 23:52:06 +0200 Subject: [PATCH 1/9] adding frontend delete button --- library_list.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/library_list.php b/library_list.php index 3d950ae16..9acb9b64e 100644 --- a/library_list.php +++ b/library_list.php @@ -86,6 +86,12 @@ $restricted = null; $restrictedurl = null; } + // Check if delete button should be enabled + if ($usage['content'] === 0 && $usage['libraries'] === 0) { + $button = ''; + } else { + $button = ''; + } $settings['libraryList']['listData'][] = array( 'title' => $library->title . ' (' . \H5PCore::libraryVersion($library) . ')', @@ -96,7 +102,7 @@ 'numLibraryDependencies' => $usage['libraries'], 'upgradeUrl' => $upgradeurl, 'detailsUrl' => null, // Not implemented in Moodle. - 'deleteUrl' => null // Not implemented in Moodle. + 'deleteUrl' => $button ); $i++; From b59f19e2651b1cf6f582282c0fb24a3485d77d54 Mon Sep 17 00:00:00 2001 From: corinna Date: Wed, 28 Aug 2024 23:40:00 +0200 Subject: [PATCH 2/9] adding backend functionality --- delete_library_page.php | 61 +++++++++++++++++++++++++++++++++++++++++ library_list.php | 15 +++++----- 2 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 delete_library_page.php diff --git a/delete_library_page.php b/delete_library_page.php new file mode 100644 index 000000000..4324dc4cb --- /dev/null +++ b/delete_library_page.php @@ -0,0 +1,61 @@ +. +/** + * Responsible for handling the deletion of a library + * + * @package mod_hvp + * @copyright 2016 Joubel AS + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once("../../config.php"); +require_once($CFG->libdir.'/adminlib.php'); +require_once("locallib.php"); + +// No guest autologin. +require_login(0, false); + +$libraryid = required_param('library_id', PARAM_INT); +$pageurl = new moodle_url('/mod/hvp/delete_library_page.php', array('library_id' => $libraryid)); +$PAGE->set_url($pageurl); +admin_externalpage_setup('h5plibraries'); +$PAGE->set_title("{$SITE->shortname}: " . get_string('deletelibrary', 'hvp')); + +// Inform Moodle which menu entry currently is active! +$core = \mod_hvp\framework::instance(); +global $DB; + +// Check if the library exists and has no dependencies +$library = $DB->get_record('hvp_libraries', array('id' => $libraryid), '*', MUST_EXIST); +$usage = $core->h5pF->getLibraryUsage($libraryid); + +$PAGE->set_heading(get_string('deleteheading', 'hvp', $library->title . ' (' . \H5PCore::libraryVersion($library) . ')')); + +if ($usage['content'] === 0 && $usage['libraries'] === 0) { + // Delete the library and associated dependencies + $DB->delete_records('hvp_libraries', array('id' => $libraryid)); + $DB->delete_records('hvp_contents_libraries', array('library_id' => $libraryid)); + $DB->delete_records('hvp_libraries_libraries', array('required_library_id' => $libraryid)); + + redirect(new moodle_url('/mod/hvp/library_list.php'), get_string('librarydeleted', 'hvp'), null, \core\output\notification::NOTIFY_SUCCESS); +} else { + // Library cannot be deleted, show error message + echo $OUTPUT->header(); + echo $OUTPUT->notification(get_string('cannotdeletelibrary', 'hvp'), 'notifyproblem'); + echo $OUTPUT->continue_button(new moodle_url('/mod/hvp/library_list.php')); + echo $OUTPUT->footer(); +} + diff --git a/library_list.php b/library_list.php index 9acb9b64e..1ee98a1a5 100644 --- a/library_list.php +++ b/library_list.php @@ -81,16 +81,17 @@ 'restrict' => ($restricted ? 0 : 1), 'library_id' => $library->id )))->out(false); + + // Generate delete URL + $deleteurl = (new moodle_url('/mod/hvp/delete_library_page.php', array( + 'library_id' => $library->id + )))->out(false); + } else { $upgradeurl = null; $restricted = null; $restrictedurl = null; - } - // Check if delete button should be enabled - if ($usage['content'] === 0 && $usage['libraries'] === 0) { - $button = ''; - } else { - $button = ''; + $deleteurl = null; } $settings['libraryList']['listData'][] = array( @@ -102,7 +103,7 @@ 'numLibraryDependencies' => $usage['libraries'], 'upgradeUrl' => $upgradeurl, 'detailsUrl' => null, // Not implemented in Moodle. - 'deleteUrl' => $button + 'deleteUrl' => $deleteurl ); $i++; From f5d3c65e9e1c71445a126d38c6e073627ce0cb79 Mon Sep 17 00:00:00 2001 From: corinna Date: Sun, 1 Sep 2024 22:32:02 +0200 Subject: [PATCH 3/9] Adding a confirmation request before deleting --- delete_library_page.php | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/delete_library_page.php b/delete_library_page.php index 4324dc4cb..dbc95377a 100644 --- a/delete_library_page.php +++ b/delete_library_page.php @@ -29,6 +29,7 @@ require_login(0, false); $libraryid = required_param('library_id', PARAM_INT); +$confirm = optional_param('confirm', 0, PARAM_BOOL); // Confirmation flag $pageurl = new moodle_url('/mod/hvp/delete_library_page.php', array('library_id' => $libraryid)); $PAGE->set_url($pageurl); admin_externalpage_setup('h5plibraries'); @@ -44,18 +45,28 @@ $PAGE->set_heading(get_string('deleteheading', 'hvp', $library->title . ' (' . \H5PCore::libraryVersion($library) . ')')); -if ($usage['content'] === 0 && $usage['libraries'] === 0) { - // Delete the library and associated dependencies - $DB->delete_records('hvp_libraries', array('id' => $libraryid)); - $DB->delete_records('hvp_contents_libraries', array('library_id' => $libraryid)); - $DB->delete_records('hvp_libraries_libraries', array('required_library_id' => $libraryid)); +if ($confirm) { + if ($usage['content'] === 0 && $usage['libraries'] === 0) { + // Delete the library and associated dependencies + $DB->delete_records('hvp_libraries', array('id' => $libraryid)); + $DB->delete_records('hvp_contents_libraries', array('library_id' => $libraryid)); + $DB->delete_records('hvp_libraries_libraries', array('required_library_id' => $libraryid)); - redirect(new moodle_url('/mod/hvp/library_list.php'), get_string('librarydeleted', 'hvp'), null, \core\output\notification::NOTIFY_SUCCESS); + redirect(new moodle_url('/mod/hvp/library_list.php'), get_string('librarydeleted', 'hvp'), null, \core\output\notification::NOTIFY_SUCCESS); + } else { + // Library cannot be deleted, show error message + echo $OUTPUT->header(); + echo $OUTPUT->notification(get_string('cannotdeletelibrary', 'hvp'), 'notifyproblem'); + echo $OUTPUT->continue_button(new moodle_url('/mod/hvp/library_list.php')); + echo $OUTPUT->footer(); + } } else { - // Library cannot be deleted, show error message + // Ask for confirmation echo $OUTPUT->header(); - echo $OUTPUT->notification(get_string('cannotdeletelibrary', 'hvp'), 'notifyproblem'); - echo $OUTPUT->continue_button(new moodle_url('/mod/hvp/library_list.php')); + echo $OUTPUT->confirm( + 'Are you sure you want to delete this library?', + new moodle_url('/mod/hvp/delete_library_page.php', array('library_id' => $libraryid, 'confirm' => 1)), + new moodle_url('/mod/hvp/library_list.php') + ); echo $OUTPUT->footer(); } - From 7f144d2e3517f3e291deed2d9bae06393787311b Mon Sep 17 00:00:00 2001 From: corinna Date: Sat, 14 Sep 2024 17:08:35 +0200 Subject: [PATCH 4/9] Adding delete button for all libraries --- library_list.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/library_list.php b/library_list.php index 1ee98a1a5..41f608a44 100644 --- a/library_list.php +++ b/library_list.php @@ -81,19 +81,18 @@ 'restrict' => ($restricted ? 0 : 1), 'library_id' => $library->id )))->out(false); - - // Generate delete URL - $deleteurl = (new moodle_url('/mod/hvp/delete_library_page.php', array( - 'library_id' => $library->id - )))->out(false); - } else { $upgradeurl = null; $restricted = null; $restrictedurl = null; - $deleteurl = null; } + // Generate delete URL for all libraries + $deleteurl = (new moodle_url('/mod/hvp/delete_library_page.php', array( + 'library_id' => $library->id + )))->out(false); + + $settings['libraryList']['listData'][] = array( 'title' => $library->title . ' (' . \H5PCore::libraryVersion($library) . ')', 'restricted' => $restricted, From 05e4056d91c7fc5e2fd6280ca4a27f37876bab8f Mon Sep 17 00:00:00 2001 From: corinna Date: Sat, 14 Sep 2024 22:31:44 +0200 Subject: [PATCH 5/9] Recursiv deleting for the selected librry and all her dependent libraries --- delete_library_page.php | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/delete_library_page.php b/delete_library_page.php index dbc95377a..0a300e109 100644 --- a/delete_library_page.php +++ b/delete_library_page.php @@ -39,18 +39,39 @@ $core = \mod_hvp\framework::instance(); global $DB; -// Check if the library exists and has no dependencies +// Check if the library exists and has no content dependencies $library = $DB->get_record('hvp_libraries', array('id' => $libraryid), '*', MUST_EXIST); $usage = $core->h5pF->getLibraryUsage($libraryid); $PAGE->set_heading(get_string('deleteheading', 'hvp', $library->title . ' (' . \H5PCore::libraryVersion($library) . ')')); if ($confirm) { - if ($usage['content'] === 0 && $usage['libraries'] === 0) { - // Delete the library and associated dependencies - $DB->delete_records('hvp_libraries', array('id' => $libraryid)); - $DB->delete_records('hvp_contents_libraries', array('library_id' => $libraryid)); - $DB->delete_records('hvp_libraries_libraries', array('required_library_id' => $libraryid)); + if ($usage['content'] === 0) { + $dependencies = $DB->get_records('hvp_libraries_libraries', array('required_library_id' => $library->id)); + + foreach ($dependencies as $dependency) { + //Get all dependet libraries + $dependentLibrary = $DB->get_record('hvp_libraries', array('id' => $dependency->library_id)); + + if ($dependentLibrary) { + // Delete dependent library + \H5PCore::deleteFileTree($core->h5pF->getH5pPath() . '/libraries/' . "{$dependentLibrary->name}-{$dependentLibrary->major_version}.{$dependentLibrary->minor_version}"); + + $DB->delete_records('hvp_libraries_libraries', array('library_id' => $dependentLibrary->id)); + $DB->delete_records('hvp_libraries_languages', array('library_id' => $dependentLibrary->id)); + $DB->delete_records('hvp_libraries', array('id' => $dependentLibrary->id)); + } + } + + // Delete inital library + $librarybase = $core->h5pF->getH5pPath() . '/libraries/'; + $libname = "{$library->name}-{$library->major_version}.{$library->minor_version}"; + \H5PCore::deleteFileTree("{$librarybase}{$libname}"); + + // Remove library data from database. + $DB->delete_records('hvp_libraries_libraries', array('library_id' => $library->id)); + $DB->delete_records('hvp_libraries_languages', array('library_id' => $library->id)); + $DB->delete_records('hvp_libraries', array('id' => $library->id)); redirect(new moodle_url('/mod/hvp/library_list.php'), get_string('librarydeleted', 'hvp'), null, \core\output\notification::NOTIFY_SUCCESS); } else { @@ -64,7 +85,7 @@ // Ask for confirmation echo $OUTPUT->header(); echo $OUTPUT->confirm( - 'Are you sure you want to delete this library?', + 'Are you sure you want to delete this library? You will also delete: ' . $usage['libraries'] . ' more libraries', new moodle_url('/mod/hvp/delete_library_page.php', array('library_id' => $libraryid, 'confirm' => 1)), new moodle_url('/mod/hvp/library_list.php') ); From 10dd71f4c7f49486ac6fca2aabdc3deffe0477bc Mon Sep 17 00:00:00 2001 From: corinna Date: Sat, 14 Sep 2024 22:45:53 +0200 Subject: [PATCH 6/9] Adding language strings --- lang/en/hvp.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lang/en/hvp.php b/lang/en/hvp.php index 8e922bfab..82c0873fe 100644 --- a/lang/en/hvp.php +++ b/lang/en/hvp.php @@ -167,6 +167,13 @@ $string['upgradeerrortoohighversion'] = 'Parameters contain %used while only %supported or earlier are supported.'; $string['upgradeerrornotsupported'] = 'Parameters contain %used which is not supported.'; +// Delete H5P library page labels +$string['deletelibrary'] = 'Delete H5P library'; +$string['deleteheading'] = 'Delete {$a}'; +$string['cannotdeletelibrary'] = 'Could not delete library {$a}'; +$string['librarydeleted'] = 'You have successfully deleted {$a}'; +$string['confirmlibrary'] = 'Are you sure you want to delete this library? You will also delete {$a} more libraries'; + // Results / report page. $string['user'] = 'User'; $string['score'] = 'Score'; From 4f686db9fb7ccb1980f7497cc7b65f3e5b016498 Mon Sep 17 00:00:00 2001 From: corinna Date: Sat, 14 Sep 2024 22:53:00 +0200 Subject: [PATCH 7/9] Remove the hardcoded code --- delete_library_page.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/delete_library_page.php b/delete_library_page.php index 0a300e109..4ebde6e23 100644 --- a/delete_library_page.php +++ b/delete_library_page.php @@ -73,11 +73,11 @@ $DB->delete_records('hvp_libraries_languages', array('library_id' => $library->id)); $DB->delete_records('hvp_libraries', array('id' => $library->id)); - redirect(new moodle_url('/mod/hvp/library_list.php'), get_string('librarydeleted', 'hvp'), null, \core\output\notification::NOTIFY_SUCCESS); + redirect(new moodle_url('/mod/hvp/library_list.php'), get_string('librarydeleted', 'hvp', $library->title . ' (' . \H5PCore::libraryVersion($library) . ')'), null, \core\output\notification::NOTIFY_SUCCESS); } else { // Library cannot be deleted, show error message echo $OUTPUT->header(); - echo $OUTPUT->notification(get_string('cannotdeletelibrary', 'hvp'), 'notifyproblem'); + echo $OUTPUT->notification(get_string('cannotdeletelibrary', 'hvp', $library->title . ' (' . \H5PCore::libraryVersion($library) . ')'), 'notifyproblem'); echo $OUTPUT->continue_button(new moodle_url('/mod/hvp/library_list.php')); echo $OUTPUT->footer(); } @@ -85,7 +85,7 @@ // Ask for confirmation echo $OUTPUT->header(); echo $OUTPUT->confirm( - 'Are you sure you want to delete this library? You will also delete: ' . $usage['libraries'] . ' more libraries', + get_string('confirmlibrary', 'hvp', $usage['libraries']), new moodle_url('/mod/hvp/delete_library_page.php', array('library_id' => $libraryid, 'confirm' => 1)), new moodle_url('/mod/hvp/library_list.php') ); From 8821c08e7c6c43a8e6fb489ca47cd6145cb6d14c Mon Sep 17 00:00:00 2001 From: corinna Date: Sun, 15 Sep 2024 23:38:11 +0200 Subject: [PATCH 8/9] Adding the message which Libraries will also be deleted --- classes/framework.php | 24 ++++++++++++++++++++++++ delete_library_page.php | 7 +++++-- lang/en/hvp.php | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/classes/framework.php b/classes/framework.php index fc7ab69c8..5e0391232 100644 --- a/classes/framework.php +++ b/classes/framework.php @@ -915,6 +915,30 @@ public function getLibraryUsage($id, $skipcontent = false) { ); } + /** + * Implements getLibraryUsageNames + * + * Get the names of dependencies to other libraries + * + * @param int $id + * @return array The array contains the names of the dependet libraries + */ + + public function getLibraryUsageNames($id) { + global $DB; + + // Count the libraries and get their names + $librariesNames = $DB->get_records_sql( + "SELECT rl.title + FROM {hvp_libraries_libraries} ll + JOIN {hvp_libraries} rl ON rl.id = ll.library_id + WHERE ll.required_library_id = ?", array($id) + ); + + // Return a list with the library names + return $librariesNames; + } + /** * Implements getLibraryContentCount */ diff --git a/delete_library_page.php b/delete_library_page.php index 4ebde6e23..2646413f7 100644 --- a/delete_library_page.php +++ b/delete_library_page.php @@ -39,9 +39,12 @@ $core = \mod_hvp\framework::instance(); global $DB; -// Check if the library exists and has no content dependencies +// Check if the library exists and has no dependencies $library = $DB->get_record('hvp_libraries', array('id' => $libraryid), '*', MUST_EXIST); $usage = $core->h5pF->getLibraryUsage($libraryid); +$usageNames = $core->h5pF->getLibraryUsageNames($libraryid); +$usageNamesArray = array_keys($usageNames); +$usageNamesString = implode(', ', $usageNamesArray); $PAGE->set_heading(get_string('deleteheading', 'hvp', $library->title . ' (' . \H5PCore::libraryVersion($library) . ')')); @@ -85,7 +88,7 @@ // Ask for confirmation echo $OUTPUT->header(); echo $OUTPUT->confirm( - get_string('confirmlibrary', 'hvp', $usage['libraries']), + get_string('confirmlibrary', 'hvp', $usageNamesString), new moodle_url('/mod/hvp/delete_library_page.php', array('library_id' => $libraryid, 'confirm' => 1)), new moodle_url('/mod/hvp/library_list.php') ); diff --git a/lang/en/hvp.php b/lang/en/hvp.php index 82c0873fe..d7e8a099a 100644 --- a/lang/en/hvp.php +++ b/lang/en/hvp.php @@ -172,7 +172,7 @@ $string['deleteheading'] = 'Delete {$a}'; $string['cannotdeletelibrary'] = 'Could not delete library {$a}'; $string['librarydeleted'] = 'You have successfully deleted {$a}'; -$string['confirmlibrary'] = 'Are you sure you want to delete this library? You will also delete {$a} more libraries'; +$string['confirmlibrary'] = 'Are you sure you want to delete this library? You will also delete {$a}'; // Results / report page. $string['user'] = 'User'; From 79ce435f8e2b9ccbbdf532b7edc57212b6175d9b Mon Sep 17 00:00:00 2001 From: 5kw-3al-u1 <105639138+5kw-3al-u1@users.noreply.github.com> Date: Mon, 23 Sep 2024 12:07:47 +0200 Subject: [PATCH 9/9] fixing typos in comments --- delete_library_page.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/delete_library_page.php b/delete_library_page.php index 2646413f7..41d997528 100644 --- a/delete_library_page.php +++ b/delete_library_page.php @@ -53,11 +53,11 @@ $dependencies = $DB->get_records('hvp_libraries_libraries', array('required_library_id' => $library->id)); foreach ($dependencies as $dependency) { - //Get all dependet libraries + //Get all dependent libraries $dependentLibrary = $DB->get_record('hvp_libraries', array('id' => $dependency->library_id)); if ($dependentLibrary) { - // Delete dependent library + // Delete dependent libraries \H5PCore::deleteFileTree($core->h5pF->getH5pPath() . '/libraries/' . "{$dependentLibrary->name}-{$dependentLibrary->major_version}.{$dependentLibrary->minor_version}"); $DB->delete_records('hvp_libraries_libraries', array('library_id' => $dependentLibrary->id));