1 |
efrain |
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 |
* Share H5P Content on the Hub
|
|
|
18 |
*
|
|
|
19 |
* @package mod_hvp
|
|
|
20 |
* @copyright 2020 Joubel AS <contact@joubel.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
require_once("../../config.php");
|
|
|
24 |
require_once("locallib.php");
|
|
|
25 |
|
|
|
26 |
global $PAGE, $DB, $CFG, $OUTPUT;
|
|
|
27 |
|
|
|
28 |
$id = required_param('id', PARAM_INT);
|
|
|
29 |
|
|
|
30 |
// Verify course context.
|
|
|
31 |
$cm = get_coursemodule_from_id('hvp', $id);
|
|
|
32 |
if (!$cm) {
|
|
|
33 |
print_error('invalidcoursemodule');
|
|
|
34 |
}
|
|
|
35 |
$course = $DB->get_record('course', array('id' => $cm->course));
|
|
|
36 |
if (!$course) {
|
|
|
37 |
print_error('coursemisconf');
|
|
|
38 |
}
|
|
|
39 |
require_course_login($course, true, $cm);
|
|
|
40 |
$context = context_module::instance($cm->id);
|
|
|
41 |
require_capability('mod/hvp:share', $context);
|
|
|
42 |
|
|
|
43 |
// Check if Hub registered, if not redirect to hub registration.
|
|
|
44 |
if (empty(get_config('mod_hvp', 'site_uuid')) || empty(get_config('mod_hvp', 'hub_secret'))) {
|
|
|
45 |
if (!has_capability('mod/hvp:contenthubregistration', \context_system::instance())) {
|
|
|
46 |
print_error('nohubregistration', 'mod_hvp');
|
|
|
47 |
}
|
|
|
48 |
redirect(new moodle_url('/mod/hvp/content_hub_registration.php'));
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
// Try to load existing content from the Hub.
|
|
|
52 |
$core = \mod_hvp\framework::instance();
|
|
|
53 |
$content = $core->loadContent($cm->instance);
|
|
|
54 |
|
|
|
55 |
$action = optional_param('action', '', PARAM_TEXT);
|
|
|
56 |
if ($action) {
|
|
|
57 |
// Prepare to do a Hub Action.
|
|
|
58 |
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
|
59 |
http_response_code(405);
|
|
|
60 |
}
|
|
|
61 |
$token = required_param('_token', PARAM_RAW);
|
|
|
62 |
if (!\H5PCore::validToken('share_' . $id, $token)) {
|
|
|
63 |
print_error('invalidtoken', 'mod_hvp');
|
|
|
64 |
}
|
|
|
65 |
if (empty($content['contentHubId']) || $content['shared'] !== '1') {
|
|
|
66 |
print_error('contentnotshared', 'mod_hvp');
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
$core = \mod_hvp\framework::instance();
|
|
|
70 |
if ($action === 'sync') {
|
|
|
71 |
// Sync content already shared on the Hub.
|
|
|
72 |
$exporturl = hvp_create_hub_export_url($cm->id, $content);
|
|
|
73 |
if ($core->hubSyncContent($content['contentHubId'], $exporturl)) {
|
|
|
74 |
$core->h5pF->updateContentFields($content['id'], array('synced' => \H5PContentHubSyncStatus::WAITING));
|
|
|
75 |
}
|
|
|
76 |
} else if ($action === 'unpublish') {
|
|
|
77 |
// Unpublish content already shared on the Hub.
|
|
|
78 |
if ($core->hubUnpublishContent($content['contentHubId'])) {
|
|
|
79 |
$core->h5pF->updateContentFields($content['id'], array('shared' => 0));
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
redirect(new moodle_url('/mod/hvp/view.php', ['id' => $id]));
|
|
|
83 |
exit;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$hubcontent = !empty($content['contentHubId']) ? $core->hubRetrieveContent($content['contentHubId']) : null;
|
|
|
87 |
if (empty($content['contentHubId'])) {
|
|
|
88 |
// Try to populate with license from content.
|
|
|
89 |
$license = !empty($content['metadata']['license']) ? $content['metadata']['license'] : null;
|
|
|
90 |
$licenseversion = !empty($license) && !empty($content['metadata']['licenseVersion']) ? $content['metadata']['licenseVersion'] : null;
|
|
|
91 |
$showcopyrightwarning = false;
|
|
|
92 |
|
|
|
93 |
if ($license === 'U') {
|
|
|
94 |
$license = null;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if ($license === 'C') {
|
|
|
98 |
$license = null;
|
|
|
99 |
$showcopyrightwarning = true;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
$hubcontent = [
|
|
|
103 |
'license' => $license,
|
|
|
104 |
'licenseVersion' => $licenseversion,
|
|
|
105 |
'showCopyrightWarning' => $showcopyrightwarning,
|
|
|
106 |
];
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
// Prepare settings for the UI.
|
|
|
110 |
$locale = \mod_hvp\framework::get_language();
|
|
|
111 |
$settings = [
|
|
|
112 |
'token' => \H5PCore::createToken('share_' . $id),
|
|
|
113 |
'publishURL' => (new \moodle_url('/mod/hvp/ajax.php', array('action' => 'share', 'id' => $id)))->out(false),
|
|
|
114 |
'returnURL' => (new \moodle_url('/mod/hvp/view.php', array('id' => $id)))->out(false),
|
|
|
115 |
'l10n' => $core->getLocalization(),
|
|
|
116 |
'metadata' => json_decode($core->getUpdatedContentHubMetadataCache($locale)),
|
|
|
117 |
'title' => html_entity_decode($cm->name, ENT_QUOTES),
|
|
|
118 |
'contentType' => "{$content['library']['name']} {$content['library']['majorVersion']}.{$content['library']['minorVersion']}",
|
|
|
119 |
'language' => $locale,
|
|
|
120 |
'hubContent' => $hubcontent,
|
|
|
121 |
'context' => empty($content['shared']) ? 'share' : 'edit',
|
|
|
122 |
];
|
|
|
123 |
|
|
|
124 |
// Configure page.
|
|
|
125 |
$PAGE->set_url(new \moodle_url('/mod/hvp/share.php', array('id' => $id)));
|
|
|
126 |
$PAGE->set_title(format_string($cm->name));
|
|
|
127 |
$PAGE->set_heading($course->fullname);
|
|
|
128 |
|
|
|
129 |
// Load JavaScript and styles.
|
|
|
130 |
$PAGE->requires->js(new \moodle_url(\mod_hvp\view_assets::getsiteroot() . '/mod/hvp/library/js/h5p-hub-sharing.js'), true);
|
|
|
131 |
foreach (\H5PCore::$styles as $style) {
|
|
|
132 |
$PAGE->requires->css(new \moodle_url(\mod_hvp\view_assets::getsiteroot() . "/mod/hvp/library/{$style}"));
|
|
|
133 |
}
|
|
|
134 |
$PAGE->requires->css(new \moodle_url(\mod_hvp\view_assets::getsiteroot() . '/mod/hvp/library/styles/h5p-hub-sharing.css'));
|
|
|
135 |
|
|
|
136 |
// Print page HTML.
|
|
|
137 |
echo $OUTPUT->header();
|
|
|
138 |
echo $OUTPUT->heading(format_string($cm->name));
|
|
|
139 |
|
|
|
140 |
?>
|
|
|
141 |
<div id="h5p-hub-share">
|
|
|
142 |
<div class="loading-screen">
|
|
|
143 |
<?php echo get_string('javascriptloading', 'hvp'); ?>
|
|
|
144 |
</div>
|
|
|
145 |
</div>
|
|
|
146 |
<script>
|
|
|
147 |
H5PHub.createSharingUI(document.getElementById('h5p-hub-share'), <?php echo json_encode($settings); ?>);
|
|
|
148 |
</script>
|
|
|
149 |
<?php
|
|
|
150 |
|
|
|
151 |
echo $OUTPUT->footer();
|