Merge branch 'MDL-67146-master-enfix-2' of git://github.com/mudrd8mz/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
29$context = context_system::instance();
30require_capability('moodle/h5p:updatelibraries', $context);
31
32$pagetitle = get_string('h5pmanage', 'core_h5p');
33$url = new \moodle_url("/h5p/libraries.php");
34
35$PAGE->set_context($context);
36$PAGE->set_url($url);
37$PAGE->set_title($pagetitle);
38$PAGE->set_pagelayout('admin');
39$PAGE->set_heading($pagetitle);
40
41echo $OUTPUT->header();
42echo $OUTPUT->heading($pagetitle);
43echo $OUTPUT->box(get_string('librariesmanagerdescription', 'core_h5p'));
44
45$form = new \core_h5p\form\uploadlibraries_form();
c43f99ca 46$h5pfactory = new \core_h5p\factory();
b57e1c95
AA
47if ($data = $form->get_data()) {
48 require_sesskey();
49
50 // Get the file from the users draft area.
51 $usercontext = context_user::instance($USER->id);
52 $fs = get_file_storage();
53 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data->h5ppackage, 'id',
54 false);
55 $file = reset($files);
56
57 // Validate and save the H5P package.
b57e1c95
AA
58 // Because we are passing skipcontent = true to save_h5p function, the returning value is false in an error
59 // is encountered, null when successfully saving the package without creating the content.
60 if (\core_h5p\helper::save_h5p($h5pfactory, $file, new stdClass(), false, true) === false) {
61 echo $OUTPUT->notification(get_string('invalidpackage', 'core_h5p'), 'error');
62 } else {
63 echo $OUTPUT->notification(get_string('uploadsuccess', 'core_h5p'), 'success');
64 }
65}
66$form->display();
c43f99ca
BB
67
68// Load installed Libraries.
69$framework = $h5pfactory->get_framework();
70$libraries = $framework->loadLibraries();
71$installed = [];
72foreach ($libraries as $libraryname => $versions) {
73 foreach ($versions as $version) {
74 $installed[] = $version;
75 }
76}
77
78if (count($installed)) {
79 echo $OUTPUT->render_from_template('core_h5p/h5plibraries', (object)['contenttypes' => $installed]);
80}
81
b57e1c95 82echo $OUTPUT->footer();