forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
edit.php
115 lines (100 loc) · 3.81 KB
/
edit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Open the editor to modify an H5P content from a given H5P URL.
*
* @package core_h5p
* @copyright 2021 Sara Arjona <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../config.php');
require_once("$CFG->libdir/formslib.php");
require_once("$CFG->libdir/filestorage/file_storage.php");
require_login(null, false);
$contenturl = required_param('url', PARAM_LOCALURL);
$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
// If no returnurl is defined, use local_referer.
if (empty($returnurl)) {
$returnurl = get_local_referer(false);
if (empty($returnurl)) {
// If local referer is empty, returnurl will be set to default site page.
$returnurl = new \moodle_url('/');
}
}
$contentid = null;
$isreferenced = false;
$context = \context_system::instance();
if (!empty($contenturl)) {
list($originalfile, $h5p, $file) = \core_h5p\api::get_original_content_from_pluginfile_url($contenturl);
$isreferenced = ($file !== false);
if ($originalfile) {
// Check if the user can edit the content behind the given URL.
if (\core_h5p\api::can_edit_content($originalfile)) {
if (!$h5p) {
// This H5P file hasn't been deployed yet, so it should be saved to create the entry into the H5P DB.
\core_h5p\local\library\autoloader::register();
$factory = new \core_h5p\factory();
$config = new \stdClass();
$onlyupdatelibs = !\core_h5p\helper::can_update_library($originalfile);
$contentid = \core_h5p\helper::save_h5p($factory, $originalfile, $config, $onlyupdatelibs, false);
} else {
// The H5P content exists. Update the contentid value.
$contentid = $h5p->id;
}
}
if ($file) {
list($context, $course, $cm) = get_context_info_array($file->get_contextid());
if ($course) {
$context = \context_course::instance($course->id);
}
} else {
list($context, $course, $cm) = get_context_info_array($originalfile->get_contextid());
if ($course) {
$context = \context_course::instance($course->id);
}
}
}
}
if (empty($contentid)) {
throw new \moodle_exception('error:emptycontentid', 'core_h5p', $returnurl);
}
$pagetitle = get_string('h5peditor', 'core_h5p');
$url = new \moodle_url("/h5p/edit.php");
$PAGE->set_context($context);
$PAGE->set_url($url);
$PAGE->set_title($pagetitle);
$PAGE->set_heading($pagetitle);
$PAGE->set_pagelayout('course');
$values = [
'id' => $contentid,
'contenturl' => $contenturl,
'returnurl' => $returnurl,
];
$form = new \core_h5p\form\editcontent_form(null, $values);
if ($form->is_cancelled()) {
redirect($returnurl);
} else if ($data = $form->get_data()) {
$form->save_h5p($data);
if (!empty($returnurl)) {
redirect($returnurl);
}
}
echo $OUTPUT->header();
if ($isreferenced) {
echo $OUTPUT->notification(get_string('contentinuse', 'core_h5p'), 'info');
}
$form->display();
echo $OUTPUT->footer();