forked from h5p/moodle-mod_hvp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
194 lines (157 loc) · 6.46 KB
/
lib.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
<?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/>.
/**
* @package mod
* @subpackage hvp
* @copyright 2013 Amendor
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once ($CFG->dirroot . '/mod/hvp/hvp.php');
function hvp_supports($feature) {
switch($feature) {
case FEATURE_GROUPS: return true;
case FEATURE_GROUPINGS: return true;
case FEATURE_GROUPMEMBERSONLY: return true;
case FEATURE_MOD_INTRO: return false;
case FEATURE_COMPLETION_TRACKS_VIEWS: return false;
case FEATURE_COMPLETION_HAS_RULES: return false;
case FEATURE_GRADE_HAS_GRADE: return false;
case FEATURE_GRADE_OUTCOMES: return false;
case FEATURE_BACKUP_MOODLE2: return false;
case FEATURE_SHOW_DESCRIPTION: return false;
default: return null;
}
}
function hvp_add_instance($hvp) {
global $DB;
$hvp->id = $DB->insert_record('hvp', $hvp);
$h5pStorage = hvp_get_instance('storage');
$library_updated = $h5pStorage->savePackage($hvp->id);
return $hvp->id;
}
function hvp_update_instance($hvp) {
global $DB;
$hvp->id = $hvp->instance;
$result = $DB->update_record('hvp', $hvp);
$h5pStorage = hvp_get_instance('storage');
$library_updated = $h5pStorage->updatePackage($hvp->id);
return $result;
}
function hvp_delete_instance($id) {
global $DB;
if (! $hvp = $DB->get_record('hvp', array('id' => "$id"))) {
return false;
}
$result = true;
$h5pStorage = hvp_get_instance('storage');
$h5pStorage->deletePackage($hvp->id);
if (! $DB->delete_records('hvp', array('id' => "$hvp->id"))) {
$result = false;
}
debugging('Deleted h5p ' . $hvp->id . ': ' . $result, DEBUG_DEVELOPER);
return $result;
}
function hvp_get_hvp($hvpid) {
global $DB;
$hvp = $DB->get_record_sql('SELECT h.id, h.name, hc.content, hc.embed_type, hc.library_id, hl.machine_name, hl.major_version, hl.minor_version, hl.embed_types, hl.fullscreen
FROM {hvp} h
JOIN {hvp_contents} hc ON hc.id = h.id
JOIN {hvp_libraries} hl ON hl.id = hc.library_id
WHERE hc.id = ?', array($hvpid));
if ($hvp) {
return $hvp;
}
return false;
}
function hvp_get_file_paths($hvp) {
global $CFG, $DB;
$filepaths = array(
'preloadedJs' => array(),
'preloadedCss' => array(),
);
$libraries = $DB->get_records_sql('SELECT hl.id, hl.machine_name, hl.major_version, hl.minor_version, hl.preloaded_css, hl.preloaded_js, hcl.drop_css
FROM {hvp_contents_libraries} hcl
JOIN {hvp_libraries} hl ON hcl.library_id = hl.id
WHERE hcl.id = ? AND hcl.preloaded = 1', array($hvp->id));
$path = '/mod/hvp/files';
$h5pcore = hvp_get_instance('core');
foreach ($libraries as $library) {
// core only supports assoc. arrays.
$librarydata = array(
'machineName' => $library->machine_name,
'majorVersion' => $library->major_version,
'minorVersion' => $library->minor_version,
);
if (!empty($library->preloaded_js)) {
foreach (explode(',', $library->preloaded_js) as $scriptpath) {
$filepaths['preloadedJs'][] = $path . '/libraries/' . $h5pcore->libraryToString($librarydata, TRUE) . '/' . trim($scriptpath);
}
}
if (!empty($library->preloaded_css) && !$library->drop_css) {
foreach (explode(',', $library->preloaded_css) as $stylepath) {
$filepaths['preloadedCss'][] = $path . '/libraries/' . $h5pcore->libraryToString($librarydata, TRUE) . '/' . trim($stylepath);
}
}
}
return $filepaths;
}
function hvp_add_scripts_and_styles($hvp, $embedtype) {
global $CFG, $PAGE;
foreach (H5PCore::$styles as $style) {
$PAGE->requires->css('/mod/hvp/library/' . $style);
}
$PAGE->requires->js('/mod/hvp/hvp.js', true);
$PAGE->requires->string_for_js('fullscreen', 'hvp');
foreach (H5PCore::$scripts as $script) {
$PAGE->requires->js('/mod/hvp/library/' . $script, true);
}
$settings = array(
'content' => array(
'cid-' . $hvp->id => array(
'jsonContent' => $hvp->content,
'fullScreen' => $hvp->fullscreen
),
),
'contentPath' => $CFG->wwwroot . '/mod/hvp/files/content/',
'exportEnabled' => FALSE,
'libraryPath' => $CFG->wwwroot . '/mod/hvp/files/libraries/',
);
$filepaths = hvp_get_file_paths($hvp);
foreach ($filepaths['preloadedJs'] as $script) {
$PAGE->requires->js($script, true);
$settings['loadedJs'][] = $CFG->wwwroot . $script;
}
if ($embedtype === 'div') {
foreach ($filepaths['preloadedCss'] as $style) {
$PAGE->requires->css($style);
$settings['loadedCss'][] = $CFG->wwwroot . $style;
}
}
else {
$settings['core']['scripts'] = array();
$settings['core']['styles'] = array();
foreach (H5PCore::$styles as $style) {
$settings['core']['styles'][] = '/mod/hvp/library/' . $style;
}
$settings['core']['scripts'][] = '/mod/hvp/hvp.js';
foreach (H5PCore::$scripts as $script) {
$settings['core']['scripts'][] = '/mod/hvp/library/' . $script;
}
$settings['content']['cid-' . $hvp->id]['scripts'] = $filepaths['preloadedJs'];
$settings['content']['cid-' . $hvp->id]['styles'] = $filepaths['preloadedCss'];
}
$PAGE->requires->data_for_js('hvp', $settings, true);
}