Merge branch 'MDL-68241-master' of git://github.com/ferranrecio/moodle
[moodle.git] / mod / h5pactivity / mod_form.php
blobd3c20d0dc4b620a229db0d037d0d5c0c1d50e011
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.
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/>.
17 /**
18 * The main mod_h5pactivity configuration form.
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
25 use mod_h5pactivity\local\manager;
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot.'/course/moodleform_mod.php');
32 /**
33 * Module instance settings form.
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
39 class mod_h5pactivity_mod_form extends moodleform_mod {
41 /**
42 * Defines forms elements
44 public function definition(): void {
45 global $CFG;
47 $mform = $this->_form;
49 // Adding the "general" fieldset, where all the common settings are shown.
50 $mform->addElement('header', 'general', get_string('general', 'form'));
52 // Adding the standard "name" field.
53 $mform->addElement('text', 'name', get_string('name'), ['size' => '64']);
55 if (!empty($CFG->formatstringstriptags)) {
56 $mform->setType('name', PARAM_TEXT);
57 } else {
58 $mform->setType('name', PARAM_CLEANHTML);
61 $mform->addRule('name', null, 'required', null, 'client');
62 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
64 $this->standard_intro_elements();
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;
73 $mform->addElement('filemanager', 'packagefile', get_string('package', 'mod_h5pactivity'), null, $options);
74 $mform->addHelpButton('packagefile', 'package', 'mod_h5pactivity');
76 // H5P displaying options.
77 $factory = new \core_h5p\factory();
78 $core = $factory->get_core();
79 $displayoptions = (array) \core_h5p\helper::decode_display_options($core);
80 $mform->addElement('header', 'h5pdisplay', get_string('h5pdisplay', 'mod_h5pactivity'));
81 foreach ($displayoptions as $key => $value) {
82 $name = get_string('display'.$key, 'mod_h5pactivity');
83 $fieldname = "displayopt[$key]";
84 $mform->addElement('checkbox', $fieldname, $name);
85 $mform->setType($fieldname, PARAM_BOOL);
88 // Add standard grading elements.
89 $this->standard_grading_coursemodule_elements();
91 // Attempt options.
92 $mform->addElement('header', 'h5pattempts', get_string('h5pattempts', 'mod_h5pactivity'));
94 $mform->addElement('static', 'trackingwarning', '', get_string('tracking_messages', 'mod_h5pactivity'));
96 $options = [1 => get_string('yes'), 0 => get_string('no')];
97 $mform->addElement('select', 'enabletracking', get_string('enabletracking', 'mod_h5pactivity'), $options);
98 $mform->setDefault('enabletracking', 1);
100 $options = manager::get_grading_methods();
101 $mform->addElement('select', 'grademethod', get_string('grade_grademethod', 'mod_h5pactivity'), $options);
102 $mform->setType('grademethod', PARAM_INT);
103 $mform->hideIf('grademethod', 'enabletracking', 'neq', 1);
104 $mform->disabledIf('grademethod', 'grade[modgrade_type]', 'neq', 'point');
105 $mform->addHelpButton('grademethod', 'grade_grademethod', 'mod_h5pactivity');
107 // Add standard elements.
108 $this->standard_coursemodule_elements();
110 // Add standard buttons.
111 $this->add_action_buttons();
115 * Enforce validation rules here
117 * @param array $data array of ("fieldname"=>value) of submitted data
118 * @param array $files array of uploaded files "element_name"=>tmp_file_path
119 * @return array
121 public function validation($data, $files) {
122 global $USER;
123 $errors = parent::validation($data, $files);
125 if (empty($data['packagefile'])) {
126 $errors['packagefile'] = get_string('required');
128 } else {
129 $draftitemid = file_get_submitted_draft_itemid('packagefile');
131 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_h5pactivity', 'packagefilecheck', null,
132 ['subdirs' => 0, 'maxfiles' => 1]);
134 // Get file from users draft area.
135 $usercontext = context_user::instance($USER->id);
136 $fs = get_file_storage();
137 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false);
139 if (count($files) < 1) {
140 $errors['packagefile'] = get_string('required');
141 return $errors;
143 $file = reset($files);
144 if (!$file->is_external_file() && !empty($data['updatefreq'])) {
145 // Make sure updatefreq is not set if using normal local file.
146 $errors['updatefreq'] = get_string('updatefreq_error', 'mod_h5pactivity');
150 return $errors;
154 * Enforce defaults here.
156 * @param array $defaultvalues Form defaults
157 * @return void
159 public function data_preprocessing(&$defaultvalues) {
160 // H5P file.
161 $draftitemid = file_get_submitted_draft_itemid('packagefile');
162 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_h5pactivity',
163 'package', 0, ['subdirs' => 0, 'maxfiles' => 1]);
164 $defaultvalues['packagefile'] = $draftitemid;
166 // H5P display options.
167 $factory = new \core_h5p\factory();
168 $core = $factory->get_core();
169 if (isset($defaultvalues['displayoptions'])) {
170 $currentdisplay = $defaultvalues['displayoptions'];
171 $displayoptions = (array) \core_h5p\helper::decode_display_options($core, $currentdisplay);
172 } else {
173 $displayoptions = (array) \core_h5p\helper::decode_display_options($core);
175 foreach ($displayoptions as $key => $value) {
176 $fieldname = "displayopt[$key]";
177 $defaultvalues[$fieldname] = $value;
182 * Allows modules to modify the data returned by form get_data().
183 * This method is also called in the bulk activity completion form.
185 * Only available on moodleform_mod.
187 * @param stdClass $data passed by reference
189 public function data_postprocessing($data) {
190 parent::data_postprocessing($data);
192 $factory = new \core_h5p\factory();
193 $core = $factory->get_core();
194 if (isset($data->displayopt)) {
195 $config = (object) $data->displayopt;
196 } else {
197 $config = \core_h5p\helper::decode_display_options($core);
199 $data->displayoptions = \core_h5p\helper::get_display_options($core, $config);
201 if (!isset($data->enabletracking)) {
202 $data->enabletracking = 0;