Differences Between: [Versions 310 and 400] [Versions 39 and 400]
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 25 use mod_h5pactivity\local\manager; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 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 { 45 global $CFG, $OUTPUT; 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'); 75 $mform->addRule('packagefile', null, 'required'); 76 77 // Add a link to the Content Bank if the user can access. 78 $course = $this->get_course(); 79 $coursecontext = context_course::instance($course->id); 80 if (has_capability('moodle/contentbank:access', $coursecontext)) { 81 $msg = null; 82 $context = $this->get_context(); 83 if ($context instanceof \context_module) { 84 // This is an existing activity. If the H5P file it's a referenced file from the content bank, a link for 85 // displaying this specific content will be used instead of the generic link to the main page of the content bank. 86 $fs = get_file_storage(); 87 $files = $fs->get_area_files($context->id, 'mod_h5pactivity', 'package', 0, 'sortorder, itemid, filepath, 88 filename', false); 89 $file = reset($files); 90 if ($file && $file->get_reference() != null) { 91 $referencedfile = \repository::get_moodle_file($file->get_reference()); 92 if ($referencedfile->get_component() == 'contentbank') { 93 // If the attached file is a referencedfile in the content bank, display a link to open this content. 94 $url = new moodle_url('/contentbank/view.php', ['id' => $referencedfile->get_itemid()]); 95 $msg = get_string('opencontentbank', 'mod_h5pactivity', $url->out()); 96 $msg .= ' '.$OUTPUT->help_icon('contentbank', 'mod_h5pactivity'); 97 } 98 } 99 } 100 if (!isset($msg)) { 101 $url = new moodle_url('/contentbank/index.php', ['contextid' => $coursecontext->id]); 102 $msg = get_string('usecontentbank', 'mod_h5pactivity', $url->out()); 103 $msg .= ' '.$OUTPUT->help_icon('contentbank', 'mod_h5pactivity'); 104 } 105 106 $mform->addElement('static', 'contentbank', '', $msg); 107 } 108 109 // H5P displaying options. 110 $factory = new \core_h5p\factory(); 111 $core = $factory->get_core(); 112 $displayoptions = (array) \core_h5p\helper::decode_display_options($core); 113 $mform->addElement('header', 'h5pdisplay', get_string('h5pdisplay', 'mod_h5pactivity')); 114 foreach ($displayoptions as $key => $value) { 115 $name = get_string('display'.$key, 'mod_h5pactivity'); 116 $fieldname = "displayopt[$key]"; 117 $mform->addElement('checkbox', $fieldname, $name); 118 $mform->setType($fieldname, PARAM_BOOL); 119 } 120 121 // Add standard grading elements. 122 $this->standard_grading_coursemodule_elements(); 123 124 // Attempt options. 125 $mform->addElement('header', 'h5pattempts', get_string('h5pattempts', 'mod_h5pactivity')); 126 127 $mform->addElement('static', 'trackingwarning', '', get_string('tracking_messages', 'mod_h5pactivity')); 128 129 $options = [1 => get_string('yes'), 0 => get_string('no')]; 130 $mform->addElement('select', 'enabletracking', get_string('enabletracking', 'mod_h5pactivity'), $options); 131 $mform->setDefault('enabletracking', 1); 132 133 $options = manager::get_grading_methods(); 134 $mform->addElement('select', 'grademethod', get_string('grade_grademethod', 'mod_h5pactivity'), $options); 135 $mform->setType('grademethod', PARAM_INT); 136 $mform->hideIf('grademethod', 'enabletracking', 'neq', 1); 137 $mform->disabledIf('grademethod', 'grade[modgrade_type]', 'neq', 'point'); 138 $mform->addHelpButton('grademethod', 'grade_grademethod', 'mod_h5pactivity'); 139 140 $options = manager::get_review_modes(); 141 $mform->addElement('select', 'reviewmode', get_string('review_mode', 'mod_h5pactivity'), $options); 142 $mform->setType('reviewmode', PARAM_INT); 143 $mform->hideIf('reviewmode', 'enabletracking', 'neq', 1); 144 145 // Add standard elements. 146 $this->standard_coursemodule_elements(); 147 148 // Add standard buttons. 149 $this->add_action_buttons(); 150 } 151 152 /** 153 * Enforce validation rules here 154 * 155 * @param array $data array of ("fieldname"=>value) of submitted data 156 * @param array $files array of uploaded files "element_name"=>tmp_file_path 157 * @return array 158 **/ 159 public function validation($data, $files) { 160 global $USER; 161 $errors = parent::validation($data, $files); 162 163 if (empty($data['packagefile'])) { 164 $errors['packagefile'] = get_string('required'); 165 166 } else { 167 $draftitemid = file_get_submitted_draft_itemid('packagefile'); 168 169 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_h5pactivity', 'packagefilecheck', null, 170 ['subdirs' => 0, 'maxfiles' => 1]); 171 172 // Get file from users draft area. 173 $usercontext = context_user::instance($USER->id); 174 $fs = get_file_storage(); 175 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false); 176 177 if (count($files) < 1) { 178 $errors['packagefile'] = get_string('required'); 179 return $errors; 180 } 181 $file = reset($files); 182 if (!$file->is_external_file() && !empty($data['updatefreq'])) { 183 // Make sure updatefreq is not set if using normal local file. 184 $errors['updatefreq'] = get_string('updatefreq_error', 'mod_h5pactivity'); 185 } 186 } 187 188 return $errors; 189 } 190 191 /** 192 * Enforce defaults here. 193 * 194 * @param array $defaultvalues Form defaults 195 * @return void 196 **/ 197 public function data_preprocessing(&$defaultvalues) { 198 // H5P file. 199 $draftitemid = file_get_submitted_draft_itemid('packagefile'); 200 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_h5pactivity', 201 'package', 0, ['subdirs' => 0, 'maxfiles' => 1]); 202 $defaultvalues['packagefile'] = $draftitemid; 203 204 // H5P display options. 205 $factory = new \core_h5p\factory(); 206 $core = $factory->get_core(); 207 if (isset($defaultvalues['displayoptions'])) { 208 $currentdisplay = $defaultvalues['displayoptions']; 209 $displayoptions = (array) \core_h5p\helper::decode_display_options($core, $currentdisplay); 210 } else { 211 $displayoptions = (array) \core_h5p\helper::decode_display_options($core); 212 } 213 foreach ($displayoptions as $key => $value) { 214 $fieldname = "displayopt[$key]"; 215 $defaultvalues[$fieldname] = $value; 216 } 217 } 218 219 /** 220 * Allows modules to modify the data returned by form get_data(). 221 * This method is also called in the bulk activity completion form. 222 * 223 * Only available on moodleform_mod. 224 * 225 * @param stdClass $data passed by reference 226 */ 227 public function data_postprocessing($data) { 228 parent::data_postprocessing($data); 229 230 $factory = new \core_h5p\factory(); 231 $core = $factory->get_core(); 232 if (isset($data->displayopt)) { 233 $config = (object) $data->displayopt; 234 } else { 235 $config = \core_h5p\helper::decode_display_options($core); 236 } 237 $data->displayoptions = \core_h5p\helper::get_display_options($core, $config); 238 239 if (!isset($data->enabletracking)) { 240 $data->enabletracking = 0; 241 } 242 } 243 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body