Merge branch 'MDL-79368-main' of https://github.com/roland04/moodle
[moodle.git] / h5p / libraries.php
blobbebe0cf1f10dd3c325123c42af3f6ee9bdeb5d6d
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);
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);
40 $PAGE->set_url($url);
41 $PAGE->set_pagelayout('admin');
42 $PAGE->set_title($pagetitle);
43 $PAGE->set_heading($SITE->fullname);
45 $h5pfactory = new \core_h5p\factory();
46 if ($deletelibrary) {
47 $library = \core_h5p\api::get_library($deletelibrary);
48 if ($confirm) {
49 require_sesskey();
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),
60 ]),
61 new moodle_url($PAGE->url, ['deletelibrary' => $deletelibrary, 'confirm' => 1]),
62 new moodle_url('/h5p/libraries.php')
64 echo $OUTPUT->footer();
65 die();
68 if (!is_null($action)) {
69 require_sesskey();
71 if ($action == 'enable' || $action == 'disable') {
72 // If action is enable or disable, library id is required too.
73 $libraryid = required_param('id', PARAM_INT);
75 \core_h5p\api::set_library_enabled($libraryid, ($action == 'enable'));
79 echo $OUTPUT->header();
80 echo $OUTPUT->heading($pagetitle);
81 echo $OUTPUT->box(get_string('librariesmanagerdescription', 'core_h5p'));
83 $form = new \core_h5p\form\uploadlibraries_form();
84 if ($data = $form->get_data()) {
85 require_sesskey();
87 // Get the file from the users draft area.
88 $usercontext = context_user::instance($USER->id);
89 $fs = get_file_storage();
90 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data->h5ppackage, 'id',
91 false);
92 $file = reset($files);
94 // Validate and save the H5P package.
95 // Because we are passing skipcontent = true to save_h5p function, the returning value is false if an error
96 // is encountered, null when successfully saving the package without creating the content.
97 if (\core_h5p\helper::save_h5p($h5pfactory, $file, new stdClass(), false, true) === false) {
98 echo $OUTPUT->notification(get_string('invalidpackage', 'core_h5p'), 'error');
99 } else {
100 echo $OUTPUT->notification(get_string('uploadsuccess', 'core_h5p'), 'success');
103 $form->display();
105 // Load installed Libraries.
106 $framework = $h5pfactory->get_framework();
107 $libraries = $framework->loadLibraries();
109 if (!empty($libraries)) {
110 $libs = new \core_h5p\output\libraries($h5pfactory, $libraries);
111 echo $OUTPUT->render_from_template('core_h5p/h5plibraries', $libs->export_for_template($OUTPUT));
114 echo $OUTPUT->footer();