Merge branch 'MDL-67062-master' of git://github.com/sarjona/moodle
[moodle.git] / h5p / libraries.php
CommitLineData
b57e1c95
AA
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.
13//
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/>.
16
17/**
18 * Manage H5P libraries settings page.
19 *
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
23 */
24
25require_once(__DIR__ . '/../config.php');
26
27require_login(null, false);
28
64a2bd19
SA
29$deletelibrary = optional_param('deletelibrary', null, PARAM_INT);
30$confirm = optional_param('confirm', false, PARAM_BOOL);
31
b57e1c95
AA
32$context = context_system::instance();
33require_capability('moodle/h5p:updatelibraries', $context);
34
35$pagetitle = get_string('h5pmanage', 'core_h5p');
36$url = new \moodle_url("/h5p/libraries.php");
37
38$PAGE->set_context($context);
39$PAGE->set_url($url);
b57e1c95 40$PAGE->set_pagelayout('admin');
f531a302
AA
41$PAGE->set_title("$SITE->shortname: " . $pagetitle);
42$PAGE->set_heading($SITE->fullname);
b57e1c95 43
64a2bd19
SA
44$h5pfactory = new \core_h5p\factory();
45if ($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'));
51 }
52
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')
62 );
63 echo $OUTPUT->footer();
64 die();
65}
66
b57e1c95
AA
67echo $OUTPUT->header();
68echo $OUTPUT->heading($pagetitle);
69echo $OUTPUT->box(get_string('librariesmanagerdescription', 'core_h5p'));
70
71$form = new \core_h5p\form\uploadlibraries_form();
72if ($data = $form->get_data()) {
73 require_sesskey();
74
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);
81
82 // Validate and save the H5P package.
64a2bd19 83 // Because we are passing skipcontent = true to save_h5p function, the returning value is false if an error
b57e1c95
AA
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');
89 }
90}
91$form->display();
c43f99ca
BB
92
93// Load installed Libraries.
94$framework = $h5pfactory->get_framework();
95$libraries = $framework->loadLibraries();
c43f99ca 96
64a2bd19
SA
97if (!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));
c43f99ca
BB
100}
101
b57e1c95 102echo $OUTPUT->footer();