2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Manage H5P libraries settings page.
21 * @copyright 2019 Amaia Anabitarte <amaia@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__
. '/../config.php');
27 require_login(null, false);
29 $deletelibrary = optional_param('deletelibrary', null, PARAM_INT
);
30 $confirm = optional_param('confirm', false, PARAM_BOOL
);
31 $action = optional_param('action', null, PARAM_ALPHANUMEXT
);
33 $context = context_system
::instance();
34 require_capability('moodle/h5p:updatelibraries', $context);
36 $pagetitle = get_string('h5pmanage', 'core_h5p');
37 $url = new \
moodle_url("/h5p/libraries.php");
39 $PAGE->set_context($context);
41 $PAGE->set_pagelayout('admin');
42 $PAGE->set_title("$SITE->shortname: " . $pagetitle);
43 $PAGE->set_heading($SITE->fullname
);
45 $h5pfactory = new \core_h5p\factory
();
47 $library = \core_h5p\api
::get_library($deletelibrary);
50 \core_h5p\api
::delete_library($h5pfactory, $library);
51 redirect(new moodle_url('/h5p/libraries.php'));
54 echo $OUTPUT->header();
55 echo $OUTPUT->heading(get_string('deleting', 'core_h5p'));
56 echo $OUTPUT->confirm(
57 get_string('deletelibraryconfirm', 'core_h5p', [
58 'name' => format_string($library->title
),
59 'version' => format_string($library->majorversion
. '.' . $library->minorversion
. '.' . $library->patchversion
),
61 new moodle_url($PAGE->url
, ['deletelibrary' => $deletelibrary, 'confirm' => 1]),
62 new moodle_url('/h5p/libraries.php')
64 echo $OUTPUT->footer();
68 if (!is_null($action)) {
69 if ($action == 'enable' ||
$action == 'disable') {
70 // If action is enable or disable, library id is required too.
71 $libraryid = required_param('id', PARAM_INT
);
73 \core_h5p\api
::set_library_enabled($libraryid, ($action == 'enable'));
77 echo $OUTPUT->header();
78 echo $OUTPUT->heading($pagetitle);
79 echo $OUTPUT->box(get_string('librariesmanagerdescription', 'core_h5p'));
81 $form = new \core_h5p\form\
uploadlibraries_form();
82 if ($data = $form->get_data()) {
85 // Get the file from the users draft area.
86 $usercontext = context_user
::instance($USER->id
);
87 $fs = get_file_storage();
88 $files = $fs->get_area_files($usercontext->id
, 'user', 'draft', $data->h5ppackage
, 'id',
90 $file = reset($files);
92 // Validate and save the H5P package.
93 // Because we are passing skipcontent = true to save_h5p function, the returning value is false if an error
94 // is encountered, null when successfully saving the package without creating the content.
95 if (\core_h5p\helper
::save_h5p($h5pfactory, $file, new stdClass(), false, true) === false) {
96 echo $OUTPUT->notification(get_string('invalidpackage', 'core_h5p'), 'error');
98 echo $OUTPUT->notification(get_string('uploadsuccess', 'core_h5p'), 'success');
103 // Load installed Libraries.
104 $framework = $h5pfactory->get_framework();
105 $libraries = $framework->loadLibraries();
107 if (!empty($libraries)) {
108 $libs = new \core_h5p\output\
libraries($h5pfactory, $libraries);
109 echo $OUTPUT->render_from_template('core_h5p/h5plibraries', $libs->export_for_template($OUTPUT));
112 echo $OUTPUT->footer();