forked from h5p/moodle-mod_hvp
/
mod_form.php
82 lines (61 loc) · 2.53 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
<?php
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once ($CFG->dirroot . '/course/moodleform_mod.php');
//require_once ($CFG->dirroot . '/mod/hvp/h5p.php');
class mod_hvp_mod_form extends moodleform_mod {
function definition() {
global $CFG, $DB, $OUTPUT;
$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');
// H5P
// TODO: Use $course->maxbytes ?
$mform->addElement('filepicker', 'h5pfile', get_string('h5pfile', 'hvp'), null, array('maxbytes' => $CFG->maxbytes, 'accepted_types' => '*'));
$mform->addRule('h5pfile', null, 'required', null, 'client');
$this->standard_coursemodule_elements();
$this->add_action_buttons();
}
function data_preprocessing(&$default_values) {
// Aaah.. we meet again h5pfile!
$draftitemid = file_get_submitted_draft_itemid('h5pfile');
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_hvp', 'package', 0);
$default_values['h5pfile'] = $draftitemid;
}
function validation($data, $files) {
global $CFG;
$errors = parent::validation($data, $files);
if (empty($data['h5pfile'])) {
$errors['h5pfile'] = get_string('required');
return $errors;
}
$files = $this->get_draft_files('h5pfile');
if (count($files) < 1) {
$errors['h5pfile'] = get_string('required');
return $errors;
}
$file = reset($files);
$interface = hvp_get_instance('interface');
$path = $CFG->dirroot . '/mod/hvp/files/tmp/' . uniqid('hvp-');
$interface->getUploadedH5pFolderPath($path);
$path .= '.h5p';
$interface->getUploadedH5pPath($path);
$file->copy_content_to($path);
$h5pValidator = hvp_get_instance('validator');
if (! $h5pValidator->isValidPackage()) {
$errors['h5pfile'] = get_string('noth5pfile', 'hvp');
}
return $errors;
}
function get_data() {
$data = parent::get_data();
if (!$data) {
return false;
}
return $data;
}
}