Merge branch 'MDL-79017-39' of https://github.com/paulholden/moodle into MOODLE_39_STABLE
[moodle.git] / h5p / libraries.php
blob8d784f6643fbd57f01c0f926f4f2b0ed95ed94b6
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Manage H5P libraries settings page.
20 * @package core_h5p
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);
32 $context = context_system::instance();
33 require_capability('moodle/h5p:updatelibraries', $context);
35 $pagetitle = get_string('h5pmanage', 'core_h5p');
36 $url = new \moodle_url("/h5p/libraries.php");
38 $PAGE->set_context($context);
39 $PAGE->set_url($url);
40 $PAGE->set_pagelayout('admin');
41 $PAGE->set_title("$SITE->shortname: " . $pagetitle);
42 $PAGE->set_heading($SITE->fullname);
44 $h5pfactory = new \core_h5p\factory();
45 if ($deletelibrary) {
46 $library = \core_h5p\api::get_library($deletelibrary);
47 if ($confirm) {
48 require_sesskey();
49 \core_h5p\api::delete_library($h5pfactory, $library);
50 redirect(new moodle_url('/h5p/libraries.php'));
53 echo $OUTPUT->header();
54 echo $OUTPUT->heading(get_string('deleting', 'core_h5p'));
55 echo $OUTPUT->confirm(
56 get_string('deletelibraryconfirm', 'core_h5p', [
57 'name' => format_string($library->title),
58 'version' => format_string($library->majorversion . '.' . $library->minorversion . '.' . $library->patchversion),
59 ]),
60 new moodle_url($PAGE->url, ['deletelibrary' => $deletelibrary, 'confirm' => 1]),
61 new moodle_url('/h5p/libraries.php')
63 echo $OUTPUT->footer();
64 die();
67 echo $OUTPUT->header();
68 echo $OUTPUT->heading($pagetitle);
69 echo $OUTPUT->box(get_string('librariesmanagerdescription', 'core_h5p'));
71 $form = new \core_h5p\form\uploadlibraries_form();
72 if ($data = $form->get_data()) {
73 require_sesskey();
75 // Get the file from the users draft area.
76 $usercontext = context_user::instance($USER->id);
77 $fs = get_file_storage();
78 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data->h5ppackage, 'id',
79 false);
80 $file = reset($files);
82 // Validate and save the H5P package.
83 // Because we are passing skipcontent = true to save_h5p function, the returning value is false if an error
84 // is encountered, null when successfully saving the package without creating the content.
85 if (\core_h5p\helper::save_h5p($h5pfactory, $file, new stdClass(), false, true) === false) {
86 echo $OUTPUT->notification(get_string('invalidpackage', 'core_h5p'), 'error');
87 } else {
88 echo $OUTPUT->notification(get_string('uploadsuccess', 'core_h5p'), 'success');
91 $form->display();
93 // Load installed Libraries.
94 $framework = $h5pfactory->get_framework();
95 $libraries = $framework->loadLibraries();
97 if (!empty($libraries)) {
98 $libs = new \core_h5p\output\libraries($h5pfactory, $libraries);
99 echo $OUTPUT->render_from_template('core_h5p/h5plibraries', $libs->export_for_template($OUTPUT));
102 echo $OUTPUT->footer();