Skip to content
24 changes: 24 additions & 0 deletions classes/framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
96 changes: 96 additions & 0 deletions delete_library_page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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/>.
/**
* Responsible for handling the deletion of a library
*
* @package mod_hvp
* @copyright 2016 Joubel AS <contact@joubel.com>
* @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();
}
7 changes: 7 additions & 0 deletions lang/en/hvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
8 changes: 7 additions & 1 deletion library_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -96,7 +102,7 @@
'numLibraryDependencies' => $usage['libraries'],
'upgradeUrl' => $upgradeurl,
'detailsUrl' => null, // Not implemented in Moodle.
'deleteUrl' => null // Not implemented in Moodle.
'deleteUrl' => $deleteurl
);

$i++;
Expand Down