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');
75 $mform->addRule('packagefile', null, 'required');
77 // Add a link to the Content Bank if the user can access.
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
]);
82 $msg = get_string('usecontentbank', 'mod_h5pactivity', $url->out());
83 $msg .= ' '.$OUTPUT->help_icon('contentbank', 'mod_h5pactivity');
84 $mform->addElement('static', 'contentbank', '', $msg);
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
);
99 // Add standard grading elements.
100 $this->standard_grading_coursemodule_elements();
103 $mform->addElement('header', 'h5pattempts', get_string('h5pattempts', 'mod_h5pactivity'));
105 $mform->addElement('static', 'trackingwarning', '', get_string('tracking_messages', 'mod_h5pactivity'));
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);
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');
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');
123 // Add standard elements.
124 $this->standard_coursemodule_elements();
126 // Add standard buttons.
127 $this->add_action_buttons();
131 * Enforce validation rules here
133 * @param array $data array of ("fieldname"=>value) of submitted data
134 * @param array $files array of uploaded files "element_name"=>tmp_file_path
137 public function validation($data, $files) {
139 $errors = parent
::validation($data, $files);
141 if (empty($data['packagefile'])) {
142 $errors['packagefile'] = get_string('required');
145 $draftitemid = file_get_submitted_draft_itemid('packagefile');
147 file_prepare_draft_area($draftitemid, $this->context
->id
, 'mod_h5pactivity', 'packagefilecheck', null,
148 ['subdirs' => 0, 'maxfiles' => 1]);
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);
155 if (count($files) < 1) {
156 $errors['packagefile'] = get_string('required');
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');
170 * Enforce defaults here.
172 * @param array $defaultvalues Form defaults
175 public function data_preprocessing(&$defaultvalues) {
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;
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);
189 $displayoptions = (array) \core_h5p\helper
::decode_display_options($core);
191 foreach ($displayoptions as $key => $value) {
192 $fieldname = "displayopt[$key]";
193 $defaultvalues[$fieldname] = $value;
198 * Allows modules to modify the data returned by form get_data().
199 * This method is also called in the bulk activity completion form.
201 * Only available on moodleform_mod.
203 * @param stdClass $data passed by reference
205 public function data_postprocessing($data) {
206 parent
::data_postprocessing($data);
208 $factory = new \core_h5p\factory
();
209 $core = $factory->get_core();
210 if (isset($data->displayopt
)) {
211 $config = (object) $data->displayopt
;
213 $config = \core_h5p\helper
::decode_display_options($core);
215 $data->displayoptions
= \core_h5p\helper
::get_display_options($core, $config);
217 if (!isset($data->enabletracking
)) {
218 $data->enabletracking
= 0;