2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
30 require_once($CFG->dirroot.'/course/moodleform_mod.php');
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 {
42 * Defines forms elements
44 public function definition(): void {
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);
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.
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 // Add a link to the Content Bank if the user can access.
77 $course = $this->get_course();
78 $context = context_course::instance($course->id);
79 if (has_capability('moodle/contentbank:access', $context)) {
80 $url = new moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
81 $msg = get_string('usecontentbank', 'mod_h5pactivity', $url->out());
82 $msg .= ' '.$OUTPUT->help_icon('contentbank', 'mod_h5pactivity');
83 $mform->addElement('static', 'contentbank', '', $msg);
86 // H5P displaying options.
87 $factory = new \core_h5p\factory();
88 $core = $factory->get_core();
89 $displayoptions = (array) \core_h5p\helper::decode_display_options($core);
90 $mform->addElement('header', 'h5pdisplay', get_string('h5pdisplay', 'mod_h5pactivity'));
91 foreach ($displayoptions as $key => $value) {
92 $name = get_string('display'.$key, 'mod_h5pactivity');
93 $fieldname = "displayopt[$key]";
94 $mform->addElement('checkbox', $fieldname, $name);
95 $mform->setType($fieldname, PARAM_BOOL);
98 // Add standard grading elements.
99 $this->standard_grading_coursemodule_elements();
102 $mform->addElement('header', 'h5pattempts', get_string('h5pattempts', 'mod_h5pactivity'));
104 $mform->addElement('static', 'trackingwarning', '', get_string('tracking_messages', 'mod_h5pactivity'));
106 $options = [1 => get_string('yes'), 0 => get_string('no')];
107 $mform->addElement('select', 'enabletracking', get_string('enabletracking', 'mod_h5pactivity'), $options);
108 $mform->setDefault('enabletracking', 1);
110 $options = manager::get_grading_methods();
111 $mform->addElement('select', 'grademethod', get_string('grade_grademethod', 'mod_h5pactivity'), $options);
112 $mform->setType('grademethod', PARAM_INT);
113 $mform->hideIf('grademethod', 'enabletracking', 'neq', 1);
114 $mform->disabledIf('grademethod', 'grade[modgrade_type]', 'neq', 'point');
115 $mform->addHelpButton('grademethod', 'grade_grademethod', 'mod_h5pactivity');
117 $options = manager::get_review_modes();
118 $mform->addElement('select', 'reviewmode', get_string('review_mode', 'mod_h5pactivity'), $options);
119 $mform->setType('reviewmode', PARAM_INT);
120 $mform->hideIf('reviewmode', 'enabletracking', 'notchecked');
122 // Add standard elements.
123 $this->standard_coursemodule_elements();
125 // Add standard buttons.
126 $this->add_action_buttons();
130 * Enforce validation rules here
132 * @param array $data array of ("fieldname"=>value) of submitted data
133 * @param array $files array of uploaded files "element_name"=>tmp_file_path
136 public function validation($data, $files) {
138 $errors = parent::validation($data, $files);
140 if (empty($data['packagefile'])) {
141 $errors['packagefile'] = get_string('required');
144 $draftitemid = file_get_submitted_draft_itemid('packagefile');
146 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_h5pactivity', 'packagefilecheck', null,
147 ['subdirs' => 0, 'maxfiles' => 1]);
149 // Get file from users draft area.
150 $usercontext = context_user::instance($USER->id);
151 $fs = get_file_storage();
152 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false);
154 if (count($files) < 1) {
155 $errors['packagefile'] = get_string('required');
158 $file = reset($files);
159 if (!$file->is_external_file() && !empty($data['updatefreq'])) {
160 // Make sure updatefreq is not set if using normal local file.
161 $errors['updatefreq'] = get_string('updatefreq_error', 'mod_h5pactivity');
169 * Enforce defaults here.
171 * @param array $defaultvalues Form defaults
174 public function data_preprocessing(&$defaultvalues) {
176 $draftitemid = file_get_submitted_draft_itemid('packagefile');
177 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_h5pactivity',
178 'package', 0, ['subdirs' => 0, 'maxfiles' => 1]);
179 $defaultvalues['packagefile'] = $draftitemid;
181 // H5P display options.
182 $factory = new \core_h5p\factory();
183 $core = $factory->get_core();
184 if (isset($defaultvalues['displayoptions'])) {
185 $currentdisplay = $defaultvalues['displayoptions'];
186 $displayoptions = (array) \core_h5p\helper::decode_display_options($core, $currentdisplay);
188 $displayoptions = (array) \core_h5p\helper::decode_display_options($core);
190 foreach ($displayoptions as $key => $value) {
191 $fieldname = "displayopt[$key]";
192 $defaultvalues[$fieldname] = $value;
197 * Allows modules to modify the data returned by form get_data().
198 * This method is also called in the bulk activity completion form.
200 * Only available on moodleform_mod.
202 * @param stdClass $data passed by reference
204 public function data_postprocessing($data) {
205 parent::data_postprocessing($data);
207 $factory = new \core_h5p\factory();
208 $core = $factory->get_core();
209 if (isset($data->displayopt)) {
210 $config = (object) $data->displayopt;
212 $config = \core_h5p\helper::decode_display_options($core);
214 $data->displayoptions = \core_h5p\helper::get_display_options($core, $config);
216 if (!isset($data->enabletracking)) {
217 $data->enabletracking = 0;