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 |
/**
|
|
|
18 |
* \mod_hvp\upload_libraries_form class
|
|
|
19 |
*
|
|
|
20 |
* @package mod_hvp
|
|
|
21 |
* @copyright 2016 Joubel AS
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace mod_hvp;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
// Load moodleform class.
|
|
|
30 |
require_once("$CFG->libdir/formslib.php");
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Form to upload new H5P libraries and upgrade existing once
|
|
|
34 |
*
|
|
|
35 |
* @package mod_hvp
|
|
|
36 |
* @copyright 2016 Joubel AS
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
*/
|
|
|
39 |
class upload_libraries_form extends \moodleform {
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Define form elements
|
|
|
43 |
*/
|
|
|
44 |
public function definition() {
|
|
|
45 |
global $CFG, $OUTPUT;
|
|
|
46 |
|
|
|
47 |
// Get form.
|
|
|
48 |
$mform = $this->_form;
|
|
|
49 |
|
|
|
50 |
// Add File Picker.
|
|
|
51 |
$mform->addElement('filepicker', 'h5pfile', get_string('h5pfile', 'hvp'), null,
|
|
|
52 |
array('maxbytes' => $CFG->maxbytes, 'accepted_types' => '*.h5p'));
|
|
|
53 |
|
|
|
54 |
// Add options.
|
|
|
55 |
$mform->addElement('checkbox',
|
|
|
56 |
'onlyupdate',
|
|
|
57 |
get_string('options', 'hvp'),
|
|
|
58 |
get_string('onlyupdate', 'hvp'),
|
|
|
59 |
array('group' => 1)
|
|
|
60 |
);
|
|
|
61 |
$mform->setType('onlyupdate', PARAM_BOOL);
|
|
|
62 |
$mform->setDefault('onlyupdate', false);
|
|
|
63 |
|
|
|
64 |
$mform->addElement('checkbox',
|
|
|
65 |
'disablefileextensioncheck',
|
|
|
66 |
'',
|
|
|
67 |
get_string('disablefileextensioncheck', 'hvp'),
|
|
|
68 |
array('group' => 1)
|
|
|
69 |
);
|
|
|
70 |
$mform->setType('disablefileextensioncheck', PARAM_BOOL);
|
|
|
71 |
$mform->setDefault('disablefileextensioncheck', false);
|
|
|
72 |
|
|
|
73 |
$notification = $OUTPUT->notification(
|
|
|
74 |
get_string('disablefileextensioncheckwarning', 'hvp'),
|
|
|
75 |
'notifymessage'
|
|
|
76 |
);
|
|
|
77 |
$mform->addElement('static', '', '', $notification);
|
|
|
78 |
|
|
|
79 |
// Upload button.
|
|
|
80 |
$this->add_action_buttons(false, get_string('upload', 'hvp'));
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Preprocess incoming data
|
|
|
85 |
*
|
|
|
86 |
* @param array $defaultvalues default values for form
|
|
|
87 |
*/
|
|
|
88 |
public function data_preprocessing(&$defaultvalues) {
|
|
|
89 |
// Aaah.. we meet again h5pfile!.
|
|
|
90 |
$draftitemid = file_get_submitted_draft_itemid('h5pfile');
|
|
|
91 |
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_hvp', 'package', 0);
|
|
|
92 |
$defaultvalues['h5pfile'] = $draftitemid;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Validate incoming data
|
|
|
97 |
*
|
|
|
98 |
* @param array $data array of ("fieldname"=>value) of submitted data
|
|
|
99 |
* @param array $files array of uploaded files "element_name"=>tmp_file_path
|
|
|
100 |
* @return array of "element_name"=>"error_description" if there are errors,
|
|
|
101 |
* or an empty array if everything is OK (true allowed for backwards compatibility too).
|
|
|
102 |
*/
|
|
|
103 |
public function validation($data, $files) {
|
|
|
104 |
global $CFG;
|
|
|
105 |
$errors = array();
|
|
|
106 |
|
|
|
107 |
// Check for file.
|
|
|
108 |
if (empty($data['h5pfile'])) {
|
|
|
109 |
$errors['h5pfile'] = get_string('required');
|
|
|
110 |
return $errors;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
$files = $this->get_draft_files('h5pfile');
|
|
|
114 |
if (count($files) < 1) {
|
|
|
115 |
$errors['h5pfile'] = get_string('required');
|
|
|
116 |
return $errors;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
// Add file so that core framework can find it.
|
|
|
120 |
$file = reset($files);
|
|
|
121 |
$interface = \mod_hvp\framework::instance('interface');
|
|
|
122 |
|
|
|
123 |
$path = $CFG->tempdir . uniqid('/hvp-');
|
|
|
124 |
$interface->getUploadedH5pFolderPath($path);
|
|
|
125 |
$path .= '.h5p';
|
|
|
126 |
$interface->getUploadedH5pPath($path);
|
|
|
127 |
$file->copy_content_to($path);
|
|
|
128 |
|
|
|
129 |
// Validate package.
|
|
|
130 |
$h5pvalidator = \mod_hvp\framework::instance('validator');
|
|
|
131 |
if (!$h5pvalidator->isValidPackage(true, isset($data['onlyupdate']))) {
|
|
|
132 |
// Errors while validating the package.
|
|
|
133 |
$errors = array_map(function ($message) {
|
|
|
134 |
return $message->message;
|
|
|
135 |
}, framework::messages('error'));
|
|
|
136 |
|
|
|
137 |
$messages = array_merge(framework::messages('info'), $errors);
|
|
|
138 |
$errors['h5pfile'] = implode('<br/>', $messages);
|
|
|
139 |
}
|
|
|
140 |
return $errors;
|
|
|
141 |
}
|
|
|
142 |
}
|