forked from h5p/moodle-mod_hvp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_form.php
324 lines (278 loc) · 12.8 KB
/
mod_form.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?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/>.
/**
* Form for creating new H5P Content
*
* @package mod_hvp
* @copyright 2016 Joubel AS <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/course/moodleform_mod.php');
class mod_hvp_mod_form extends moodleform_mod {
public function definition() {
global $CFG, $COURSE, $PAGE;
$mform =& $this->_form;
// Name.
$mform->addElement('text', 'name', get_string('name'));
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
// Intro.
if (method_exists($this, 'standard_intro_elements')) {
$this->standard_intro_elements();
} else {
$this->add_intro_editor(false, get_string('intro', 'hvp'));
}
// Action.
$h5paction = array();
$h5paction[] = $mform->createElement('radio', 'h5paction', '', get_string('upload', 'hvp'), 'upload');
$h5paction[] = $mform->createElement('radio', 'h5paction', '', get_string('create', 'hvp'), 'create');
$mform->addGroup($h5paction, 'h5pactiongroup', get_string('action', 'hvp'), array('<br/>'), false);
$mform->setDefault('h5paction', 'create');
// Upload.
$mform->addElement('filepicker', 'h5pfile', get_string('h5pfile', 'hvp'), null,
array('maxbytes' => $COURSE->maxbytes, 'accepted_types' => '*'));
// Editor placeholder.
if ($CFG->theme == 'boost' || in_array('boost', $PAGE->theme->parents)) {
$h5peditor = [];
$h5peditor[] = $mform->createElement('html',
'<div class="h5p-editor">' . get_string('javascriptloading', 'hvp') . '</div>');
$mform->addGroup($h5peditor, 'h5peditor', get_string('editor', 'hvp'));
} else {
$mform->addElement('static', 'h5peditor', get_string('editor', 'hvp'),
'<div class="h5p-editor">' . get_string('javascriptloading', 'hvp') . '</div>');
}
// Hidden fields.
$mform->addElement('hidden', 'h5plibrary', '');
$mform->setType('h5plibrary', PARAM_RAW);
$mform->addElement('hidden', 'h5pparams', '');
$mform->setType('h5pparams', PARAM_RAW);
$core = \mod_hvp\framework::instance();
$displayoptions = $core->getDisplayOptionsForEdit();
if (isset($displayoptions[\H5PCore::DISPLAY_OPTION_FRAME])) {
// Display options group.
$mform->addElement('header', 'displayoptions', get_string('displayoptions', 'hvp'));
$mform->addElement('checkbox', \H5PCore::DISPLAY_OPTION_FRAME, get_string('enableframe', 'hvp'));
$mform->setType(\H5PCore::DISPLAY_OPTION_FRAME, PARAM_BOOL);
$mform->setDefault(\H5PCore::DISPLAY_OPTION_FRAME, true);
if (isset($displayoptions[\H5PCore::DISPLAY_OPTION_DOWNLOAD])) {
$mform->addElement('checkbox', \H5PCore::DISPLAY_OPTION_DOWNLOAD, get_string('enabledownload', 'hvp'));
$mform->setType(\H5PCore::DISPLAY_OPTION_DOWNLOAD, PARAM_BOOL);
$mform->setDefault(\H5PCore::DISPLAY_OPTION_DOWNLOAD, $displayoptions[\H5PCore::DISPLAY_OPTION_DOWNLOAD]);
$mform->disabledIf(\H5PCore::DISPLAY_OPTION_DOWNLOAD, 'frame');
}
if (isset($displayoptions[\H5PCore::DISPLAY_OPTION_EMBED])) {
$mform->addElement('checkbox', \H5PCore::DISPLAY_OPTION_EMBED, get_string('enableembed', 'hvp'));
$mform->setType(\H5PCore::DISPLAY_OPTION_EMBED, PARAM_BOOL);
$mform->setDefault(\H5PCore::DISPLAY_OPTION_EMBED, $displayoptions[\H5PCore::DISPLAY_OPTION_EMBED]);
$mform->disabledIf(\H5PCore::DISPLAY_OPTION_EMBED, 'frame');
}
if (isset($displayoptions[\H5PCore::DISPLAY_OPTION_COPYRIGHT])) {
$mform->addElement('checkbox', \H5PCore::DISPLAY_OPTION_COPYRIGHT, get_string('enablecopyright', 'hvp'));
$mform->setType(\H5PCore::DISPLAY_OPTION_COPYRIGHT, PARAM_BOOL);
$mform->setDefault(\H5PCore::DISPLAY_OPTION_COPYRIGHT, $displayoptions[\H5PCore::DISPLAY_OPTION_COPYRIGHT]);
$mform->disabledIf(\H5PCore::DISPLAY_OPTION_COPYRIGHT, 'frame');
}
}
// Grade settings.
$this->standard_grading_coursemodule_elements();
$mform->removeElement('grade');
// Max grade.
$mform->addElement('text', 'maximumgrade', get_string('maximumgrade', 'hvp'));
$mform->setType('maximumgrade', PARAM_INT);
$mform->setDefault('maximumgrade', 10);
// Standard course module settings.
$this->standard_coursemodule_elements();
$this->add_action_buttons();
}
/**
* Sets display options within default values
*
* @param $defaultvalues
*/
private function set_display_options(&$defaultvalues) {
// Individual display options are not stored, must be extracted from disable.
if (isset($defaultvalues['disable'])) {
$h5pcore = \mod_hvp\framework::instance('core');
$displayoptions = $h5pcore->getDisplayOptionsForEdit($defaultvalues['disable']);
if (isset ($displayoptions[\H5PCore::DISPLAY_OPTION_FRAME])) {
$defaultvalues[\H5PCore::DISPLAY_OPTION_FRAME] = $displayoptions[\H5PCore::DISPLAY_OPTION_FRAME];
}
if (isset($displayoptions[\H5PCore::DISPLAY_OPTION_DOWNLOAD])) {
$defaultvalues[\H5PCore::DISPLAY_OPTION_DOWNLOAD] = $displayoptions[\H5PCore::DISPLAY_OPTION_DOWNLOAD];
}
if (isset($displayoptions[\H5PCore::DISPLAY_OPTION_EMBED])) {
$defaultvalues[\H5PCore::DISPLAY_OPTION_EMBED] = $displayoptions[\H5PCore::DISPLAY_OPTION_EMBED];
}
if (isset($displayoptions[\H5PCore::DISPLAY_OPTION_COPYRIGHT])) {
$defaultvalues[\H5PCore::DISPLAY_OPTION_COPYRIGHT] = $displayoptions[\H5PCore::DISPLAY_OPTION_COPYRIGHT];
}
}
}
/**
* Sets max grade in default values from grade item
*
* @param $content
* @param $defaultvalues
*/
private function set_max_grade($content, &$defaultvalues) {
// Set default maxgrade.
if (isset($content) && isset($content['id'])
&& isset($defaultvalues) && isset($defaultvalues['course'])) {
// Get the gradeitem and set maxgrade.
$gradeitem = grade_item::fetch(array(
'itemtype' => 'mod',
'itemmodule' => 'hvp',
'iteminstance' => $content['id'],
'courseid' => $defaultvalues['course']
));
if (isset($gradeitem) && isset($gradeitem->grademax)) {
$defaultvalues['maximumgrade'] = $gradeitem->grademax;
}
}
}
public function data_preprocessing(&$defaultvalues) {
global $DB;
$core = \mod_hvp\framework::instance();
$content = null;
if (!empty($defaultvalues['id'])) {
// Load Content.
$content = $core->loadContent($defaultvalues['id']);
if ($content === null) {
print_error('invalidhvp');
}
}
$this->set_max_grade($content, $defaultvalues);
// Aaah.. we meet again h5pfile!
$draftitemid = file_get_submitted_draft_itemid('h5pfile');
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_hvp', 'package', 0);
$defaultvalues['h5pfile'] = $draftitemid;
$this->set_display_options($defaultvalues);
// Determine default action.
if (!get_config('mod_hvp', 'hub_is_enabled') && $content === null &&
$DB->get_field_sql("SELECT id FROM {hvp_libraries} WHERE runnable = 1", null, IGNORE_MULTIPLE) === false) {
$defaultvalues['h5paction'] = 'upload';
}
// Set editor defaults.
$defaultvalues['h5plibrary'] = ($content === null ? 0 : H5PCore::libraryToString($content['library']));
$defaultvalues['h5pparams'] = ($content === null ? '{}' : $core->filterParameters($content));
// Add required editor assets.
require_once('locallib.php');
\hvp_add_editor_assets($content === null ? null : $defaultvalues['id']);
}
/**
* Validate uploaded H5P
*
* @param $data
* @param $errors
*/
private function validate_upload($data, &$errors) {
global $CFG;
if (empty($data['h5pfile'])) {
// Field missing.
$errors['h5pfile'] = get_string('required');
} else {
$files = $this->get_draft_files('h5pfile');
if (count($files) < 1) {
// No file uploaded.
$errors['h5pfile'] = get_string('required');
} else {
// Prepare to validate package.
$file = reset($files);
$interface = \mod_hvp\framework::instance('interface');
$path = $CFG->tempdir . uniqid('/hvp-');
$interface->getUploadedH5pFolderPath($path);
$path .= '.h5p';
$interface->getUploadedH5pPath($path);
$file->copy_content_to($path);
$h5pvalidator = \mod_hvp\framework::instance('validator');
if (! $h5pvalidator->isValidPackage()) {
// Errors while validating the package.
$errors = array_map(function ($message) {
return $message->message;
}, \mod_hvp\framework::messages('error'));
$messages = array_merge(\mod_hvp\framework::messages('info'), $errors);
$errors['h5pfile'] = implode('<br/>', $messages);
}
}
}
}
/**
* Validate new H5P
*
* @param $data
*/
private function validate_created(&$data, &$errors) {
// Validate library and params used in editor.
$core = \mod_hvp\framework::instance();
// Get library array from string.
$library = H5PCore::libraryFromString($data['h5plibrary']);
if (!$library) {
$errors['h5peditor'] = get_string('librarynotselected', 'hvp');
} else {
// Check that library exists.
$library['libraryId'] = $core->h5pF->getLibraryId($library['machineName'],
$library['majorVersion'],
$library['minorVersion']);
if (!$library['libraryId']) {
$errors['h5peditor'] = get_string('nosuchlibrary', 'hvp');
} else {
$data['h5plibrary'] = $library;
// Verify that parameters are valid.
if (empty($data['h5pparams'])) {
$errors['h5peditor'] = get_string('noparameters', 'hvp');
} else {
$params = json_decode($data['h5pparams']);
if ($params === null) {
$errors['h5peditor'] = get_string('invalidparameters', 'hvp');
} else {
$data['h5pparams'] = $params;
}
}
}
}
}
/**
* Validates editor form
*
* @param array $data
* @param array $files
*
* @return array
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
// Validate max grade as a non-negative numeric value.
if (!is_numeric($data['maximumgrade']) || $data['maximumgrade'] < 0) {
$errors['maximumgrade'] = get_string('maximumgradeerror', 'hvp');
}
if ($data['h5paction'] === 'upload') {
// Validate uploaded H5P file.
$this->validate_upload($data, $errors);
} else {
$this->validate_created($data, $errors);
}
return $errors;
}
public function get_data() {
$data = parent::get_data();
if (!$data) {
return false;
}
return $data;
}
}