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 new file mode 100644 index 000000000..41d997528 --- /dev/null +++ b/delete_library_page.php @@ -0,0 +1,96 @@ +. +/** + * 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); +$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'); +$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); +$usageNames = $core->h5pF->getLibraryUsageNames($libraryid); +$usageNamesArray = array_keys($usageNames); +$usageNamesString = implode(', ', $usageNamesArray); + +$PAGE->set_heading(get_string('deleteheading', 'hvp', $library->title . ' (' . \H5PCore::libraryVersion($library) . ')')); + +if ($confirm) { + if ($usage['content'] === 0) { + $dependencies = $DB->get_records('hvp_libraries_libraries', array('required_library_id' => $library->id)); + + foreach ($dependencies as $dependency) { + //Get all dependent libraries + $dependentLibrary = $DB->get_record('hvp_libraries', array('id' => $dependency->library_id)); + + if ($dependentLibrary) { + // 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)); + $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', $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', $library->title . ' (' . \H5PCore::libraryVersion($library) . ')'), 'notifyproblem'); + echo $OUTPUT->continue_button(new moodle_url('/mod/hvp/library_list.php')); + echo $OUTPUT->footer(); + } +} else { + // Ask for confirmation + echo $OUTPUT->header(); + echo $OUTPUT->confirm( + 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') + ); + echo $OUTPUT->footer(); +} diff --git a/lang/en/hvp.php b/lang/en/hvp.php index 9101622f8..85906f864 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}'; + // Results / report page. $string['user'] = 'User'; $string['score'] = 'Score'; diff --git a/library_list.php b/library_list.php index 3d950ae16..41f608a44 100644 --- a/library_list.php +++ b/library_list.php @@ -87,6 +87,12 @@ $restrictedurl = 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, @@ -96,7 +102,7 @@ 'numLibraryDependencies' => $usage['libraries'], 'upgradeUrl' => $upgradeurl, 'detailsUrl' => null, // Not implemented in Moodle. - 'deleteUrl' => null // Not implemented in Moodle. + 'deleteUrl' => $deleteurl ); $i++;