Commit | Line | Data |
---|---|---|
b7a41681 FR |
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 | * The main mod_h5pactivity configuration form. | |
19 | * | |
20 | * @package mod_h5pactivity | |
21 | * @copyright 2020 Ferran Recio <ferran@moodle.com> | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
e28b4069 FR |
25 | use mod_h5pactivity\local\manager; |
26 | ||
b7a41681 FR |
27 | defined('MOODLE_INTERNAL') || die(); |
28 | ||
4d0241e4 | 29 | global $CFG; |
b7a41681 FR |
30 | require_once($CFG->dirroot.'/course/moodleform_mod.php'); |
31 | ||
32 | /** | |
33 | * Module instance settings form. | |
34 | * | |
35 | * @package mod_h5pactivity | |
36 | * @copyright 2020 Ferran Recio <ferran@moodle.com> | |
37 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
38 | */ | |
39 | class mod_h5pactivity_mod_form extends moodleform_mod { | |
40 | ||
41 | /** | |
42 | * Defines forms elements | |
43 | */ | |
44 | public function definition(): void { | |
7f602632 | 45 | global $CFG, $OUTPUT; |
b7a41681 FR |
46 | |
47 | $mform = $this->_form; | |
48 | ||
49 | // Adding the "general" fieldset, where all the common settings are shown. | |
50 | $mform->addElement('header', 'general', get_string('general', 'form')); | |
51 | ||
52 | // Adding the standard "name" field. | |
53 | $mform->addElement('text', 'name', get_string('name'), ['size' => '64']); | |
54 | ||
55 | if (!empty($CFG->formatstringstriptags)) { | |
56 | $mform->setType('name', PARAM_TEXT); | |
57 | } else { | |
58 | $mform->setType('name', PARAM_CLEANHTML); | |
59 | } | |
60 | ||
61 | $mform->addRule('name', null, 'required', null, 'client'); | |
62 | $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); | |
63 | ||
64 | $this->standard_intro_elements(); | |
65 | ||
66 | // Adding the rest of mod_h5pactivity settings, spreading all them into this fieldset. | |
67 | $options = []; | |
68 | $options['accepted_types'] = ['.h5p']; | |
69 | $options['maxbytes'] = 0; | |
70 | $options['maxfiles'] = 1; | |
71 | $options['subdirs'] = 0; | |
72 | ||
73 | $mform->addElement('filemanager', 'packagefile', get_string('package', 'mod_h5pactivity'), null, $options); | |
74 | $mform->addHelpButton('packagefile', 'package', 'mod_h5pactivity'); | |
6305c7b8 | 75 | $mform->addRule('packagefile', null, 'required'); |
b7a41681 | 76 | |
7f602632 | 77 | // Add a link to the Content Bank if the user can access. |
dd75c988 FR |
78 | $course = $this->get_course(); |
79 | $context = context_course::instance($course->id); | |
80 | if (has_capability('moodle/contentbank:access', $context)) { | |
81 | $url = new moodle_url('/contentbank/index.php', ['contextid' => $context->id]); | |
7f602632 FR |
82 | $msg = get_string('usecontentbank', 'mod_h5pactivity', $url->out()); |
83 | $msg .= ' '.$OUTPUT->help_icon('contentbank', 'mod_h5pactivity'); | |
84 | $mform->addElement('static', 'contentbank', '', $msg); | |
85 | } | |
86 | ||
b7a41681 FR |
87 | // H5P displaying options. |
88 | $factory = new \core_h5p\factory(); | |
89 | $core = $factory->get_core(); | |
90 | $displayoptions = (array) \core_h5p\helper::decode_display_options($core); | |
91 | $mform->addElement('header', 'h5pdisplay', get_string('h5pdisplay', 'mod_h5pactivity')); | |
92 | foreach ($displayoptions as $key => $value) { | |
93 | $name = get_string('display'.$key, 'mod_h5pactivity'); | |
94 | $fieldname = "displayopt[$key]"; | |
95 | $mform->addElement('checkbox', $fieldname, $name); | |
96 | $mform->setType($fieldname, PARAM_BOOL); | |
97 | } | |
98 | ||
99 | // Add standard grading elements. | |
100 | $this->standard_grading_coursemodule_elements(); | |
101 | ||
e28b4069 FR |
102 | // Attempt options. |
103 | $mform->addElement('header', 'h5pattempts', get_string('h5pattempts', 'mod_h5pactivity')); | |
104 | ||
105 | $mform->addElement('static', 'trackingwarning', '', get_string('tracking_messages', 'mod_h5pactivity')); | |
106 | ||
107 | $options = [1 => get_string('yes'), 0 => get_string('no')]; | |
108 | $mform->addElement('select', 'enabletracking', get_string('enabletracking', 'mod_h5pactivity'), $options); | |
109 | $mform->setDefault('enabletracking', 1); | |
110 | ||
111 | $options = manager::get_grading_methods(); | |
112 | $mform->addElement('select', 'grademethod', get_string('grade_grademethod', 'mod_h5pactivity'), $options); | |
113 | $mform->setType('grademethod', PARAM_INT); | |
114 | $mform->hideIf('grademethod', 'enabletracking', 'neq', 1); | |
115 | $mform->disabledIf('grademethod', 'grade[modgrade_type]', 'neq', 'point'); | |
116 | $mform->addHelpButton('grademethod', 'grade_grademethod', 'mod_h5pactivity'); | |
117 | ||
9a4307dd FR |
118 | $options = manager::get_review_modes(); |
119 | $mform->addElement('select', 'reviewmode', get_string('review_mode', 'mod_h5pactivity'), $options); | |
120 | $mform->setType('reviewmode', PARAM_INT); | |
121 | $mform->hideIf('reviewmode', 'enabletracking', 'notchecked'); | |
122 | ||
b7a41681 FR |
123 | // Add standard elements. |
124 | $this->standard_coursemodule_elements(); | |
125 | ||
126 | // Add standard buttons. | |
127 | $this->add_action_buttons(); | |
128 | } | |
129 | ||
130 | /** | |
131 | * Enforce validation rules here | |
132 | * | |
133 | * @param array $data array of ("fieldname"=>value) of submitted data | |
134 | * @param array $files array of uploaded files "element_name"=>tmp_file_path | |
135 | * @return array | |
136 | **/ | |
137 | public function validation($data, $files) { | |
138 | global $USER; | |
139 | $errors = parent::validation($data, $files); | |
140 | ||
141 | if (empty($data['packagefile'])) { | |
142 | $errors['packagefile'] = get_string('required'); | |
143 | ||
144 | } else { | |
145 | $draftitemid = file_get_submitted_draft_itemid('packagefile'); | |
146 | ||
147 | file_prepare_draft_area($draftitemid, $this->context->id, 'mod_h5pactivity', 'packagefilecheck', null, | |
148 | ['subdirs' => 0, 'maxfiles' => 1]); | |
149 | ||
150 | // Get file from users draft area. | |
151 | $usercontext = context_user::instance($USER->id); | |
152 | $fs = get_file_storage(); | |
153 | $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false); | |
154 | ||
155 | if (count($files) < 1) { | |
156 | $errors['packagefile'] = get_string('required'); | |
157 | return $errors; | |
158 | } | |
159 | $file = reset($files); | |
160 | if (!$file->is_external_file() && !empty($data['updatefreq'])) { | |
161 | // Make sure updatefreq is not set if using normal local file. | |
162 | $errors['updatefreq'] = get_string('updatefreq_error', 'mod_h5pactivity'); | |
163 | } | |
164 | } | |
165 | ||
166 | return $errors; | |
167 | } | |
168 | ||
169 | /** | |
170 | * Enforce defaults here. | |
171 | * | |
172 | * @param array $defaultvalues Form defaults | |
173 | * @return void | |
174 | **/ | |
175 | public function data_preprocessing(&$defaultvalues) { | |
176 | // H5P file. | |
177 | $draftitemid = file_get_submitted_draft_itemid('packagefile'); | |
178 | file_prepare_draft_area($draftitemid, $this->context->id, 'mod_h5pactivity', | |
179 | 'package', 0, ['subdirs' => 0, 'maxfiles' => 1]); | |
180 | $defaultvalues['packagefile'] = $draftitemid; | |
181 | ||
182 | // H5P display options. | |
183 | $factory = new \core_h5p\factory(); | |
184 | $core = $factory->get_core(); | |
185 | if (isset($defaultvalues['displayoptions'])) { | |
186 | $currentdisplay = $defaultvalues['displayoptions']; | |
187 | $displayoptions = (array) \core_h5p\helper::decode_display_options($core, $currentdisplay); | |
188 | } else { | |
189 | $displayoptions = (array) \core_h5p\helper::decode_display_options($core); | |
190 | } | |
191 | foreach ($displayoptions as $key => $value) { | |
192 | $fieldname = "displayopt[$key]"; | |
193 | $defaultvalues[$fieldname] = $value; | |
194 | } | |
195 | } | |
196 | ||
197 | /** | |
198 | * Allows modules to modify the data returned by form get_data(). | |
199 | * This method is also called in the bulk activity completion form. | |
200 | * | |
201 | * Only available on moodleform_mod. | |
202 | * | |
203 | * @param stdClass $data passed by reference | |
204 | */ | |
205 | public function data_postprocessing($data) { | |
206 | parent::data_postprocessing($data); | |
207 | ||
208 | $factory = new \core_h5p\factory(); | |
209 | $core = $factory->get_core(); | |
210 | if (isset($data->displayopt)) { | |
211 | $config = (object) $data->displayopt; | |
212 | } else { | |
213 | $config = \core_h5p\helper::decode_display_options($core); | |
214 | } | |
215 | $data->displayoptions = \core_h5p\helper::get_display_options($core, $config); | |
e28b4069 FR |
216 | |
217 | if (!isset($data->enabletracking)) { | |
218 | $data->enabletracking = 0; | |
219 | } | |
b7a41681 FR |
220 | } |
221 | } |