forked from h5p/moodle-mod_hvp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.php
212 lines (178 loc) · 7.3 KB
/
view.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?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/>.
/**
* View H5P Content
*
* @package mod_hvp
* @copyright 2016 Joubel AS <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("../../config.php");
require_once("locallib.php");
$id = required_param('id', PARAM_INT);
$url = new \moodle_url('/mod/hvp/view.php', array('id' => $id));
$PAGE->set_url($url);
if (! $cm = get_coursemodule_from_id('hvp', $id)) {
print_error('invalidcoursemodule');
}
if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
print_error('coursemisconf');
}
require_course_login($course, false, $cm);
// Load H5P Core.
$core = \mod_hvp\framework::instance();
// Load H5P Content.
$content = $core->loadContent($cm->instance);
if ($content === null) {
print_error('invalidhvp');
}
// Log view.
new \mod_hvp\event(
'content', null,
$content['id'], $content['title'],
$content['library']['name'],
$content['library']['majorVersion'] . '.' . $content['library']['minorVersion']
);
$PAGE->set_title(format_string($content['title']));
$PAGE->set_heading($course->fullname);
// Mark viewed by user (if required).
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
// Attach scripts, styles, etc. from core.
$settings = hvp_get_core_assets();
// Display options.
$displayoptions = $core->getDisplayOptionsForView($content['disable'], $content['id']);
// Embed is not supported in Moodle.
$displayoptions[\H5PCore::DISPLAY_OPTION_EMBED] = false;
// Filter content parameters.
$safeparameters = $core->filterParameters($content);
$decodedparams = json_decode($safeparameters);
$hvpoutput = $PAGE->get_renderer('mod_hvp');
$hvpoutput->hvp_alter_filtered_parameters(
$decodedparams,
$content['library']['name'],
$content['library']['majorVersion'],
$content['library']['minorVersion']
);
$safeparameters = json_encode($decodedparams);
$export = '';
if ($displayoptions[\H5PCore::DISPLAY_OPTION_DOWNLOAD] && (!isset($CFG->mod_hvp_export) || $CFG->mod_hvp_export === true)) {
// Find course context.
$context = \context_course::instance($course->id);
$hvppath = "{$CFG->httpswwwroot}/pluginfile.php/{$context->id}/mod_hvp";
$exportfilename = ($content['slug'] ? $content['slug'] . '-' : '') . $content['id'] . '.h5p';
$export = "{$hvppath}/exports/{$exportfilename}";
}
// Find cm context.
$context = \context_module::instance($cm->id);
// Add JavaScript settings for this content.
$cid = 'cid-' . $content['id'];
$settings['contents'][$cid] = array(
'library' => \H5PCore::libraryToString($content['library']),
'jsonContent' => $safeparameters,
'fullScreen' => $content['library']['fullscreen'],
'exportUrl' => $export,
'title' => $content['title'],
'displayOptions' => $displayoptions,
'url' => "{$CFG->httpswwwroot}/mod/hvp/view.php?id={$id}",
'contentUrl' => "{$CFG->httpswwwroot}/pluginfile.php/{$context->id}/mod_hvp/content/" . $content['id'],
'contentUserData' => array(
0 => \mod_hvp\content_user_data::load_pre_loaded_user_data($content['id'])
)
);
// Get assets for this content.
$preloadeddependencies = $core->loadContentDependencies($content['id'], 'preloaded');
$files = $core->getDependenciesFiles($preloadeddependencies);
// Determine embed type.
$embedtype = \H5PCore::determineEmbedType($content['embedType'], $content['library']['embedTypes']);
// Add additional asset files if required.
$hvpoutput->hvp_alter_scripts($files['scripts'], $preloadeddependencies, $embedtype);
$hvpoutput->hvp_alter_styles($files['styles'], $preloadeddependencies, $embedtype);
if ($embedtype === 'div') {
$context = \context_system::instance();
$hvppath = "/pluginfile.php/{$context->id}/mod_hvp";
// Schedule JavaScripts for loading through Moodle.
foreach ($files['scripts'] as $script) {
$url = $script->path . $script->version;
// Add URL prefix if not external.
$isexternal = strpos($script->path, '://');
if ($isexternal === false) {
$url = $hvppath . $url;
}
$settings['loadedJs'][] = $url;
$PAGE->requires->js(new moodle_url($isexternal ? $url : $CFG->httpswwwroot . $url), true);
}
// Schedule stylesheets for loading through Moodle.
foreach ($files['styles'] as $style) {
$url = $style->path . $style->version;
// Add URL prefix if not external.
$isexternal = strpos($style->path, '://');
if ($isexternal === false) {
$url = $hvppath . $url;
}
$settings['loadedCss'][] = $url;
$PAGE->requires->css(new moodle_url($isexternal ? $url : $CFG->httpswwwroot . $url));
}
} else {
// JavaScripts and stylesheets will be loaded through h5p.js.
$settings['contents'][$cid]['scripts'] = $core->getAssetsUrls($files['scripts']);
$settings['contents'][$cid]['styles'] = $core->getAssetsUrls($files['styles']);
}
// XAPI collector token.
$xapiresultsurl = new moodle_url('/mod/hvp/ajax.php',
array(
'token' => \H5PCore::createToken('xapiresult'),
'action' => 'xapiresult'
));
$settings['ajax']['xAPIResult'] = $xapiresultsurl->out(false);
// Print JavaScript settings to page.
$PAGE->requires->data_for_js('H5PIntegration', $settings, true);
// Add xAPI collector script.
$PAGE->requires->js(new moodle_url($CFG->httpswwwroot . '/mod/hvp/xapi-collector.js'), true);
// Print page HTML.
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($content['title']));
echo '<div class="clearer"></div>';
// Print any messages.
\mod_hvp\framework::printMessages('info', \mod_hvp\framework::messages('info'));
\mod_hvp\framework::printMessages('error', \mod_hvp\framework::messages('error'));
// Print intro.
if (trim(strip_tags($content['intro']))) {
echo $OUTPUT->box_start('mod_introbox', 'hvpintro');
echo format_module_intro('hvp', (object) array(
'intro' => $content['intro'],
'introformat' => $content['introformat'],
), $cm->id);
echo $OUTPUT->box_end();
}
// Print H5P Content.
if ($embedtype === 'div') {
echo '<div class="h5p-content" data-content-id="' . $content['id'] . '"></div>';
} else {
echo '<div class="h5p-iframe-wrapper"><iframe id="h5p-iframe-' . $content['id'] .
'" class="h5p-iframe" data-content-id="' . $content['id'] .
'" style="height:1px" src="about:blank" frameBorder="0" scrolling="no"></iframe></div>';
}
// Find cm context.
$context = \context_module::instance($cm->id);
// Trigger module viewed event.
$event = \mod_hvp\event\course_module_viewed::create(array(
'objectid' => $cm->instance,
'context' => $context
));
$event->add_record_snapshot('course_modules', $cm);
$event->trigger();
echo $OUTPUT->footer();