MDL-20353 adding missing data types of hidden form fields
[moodle.git] / mod / resource / mod_form.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * Resource configuration form
20  *
21  * @package   mod-resource
22  * @copyright 2009 Petr Skoda (http://skodak.org)
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 require_once($CFG->dirroot.'/course/moodleform_mod.php');
27 require_once($CFG->dirroot.'/mod/resource/locallib.php');
28 require_once($CFG->libdir.'/filelib.php');
30 class mod_resource_mod_form extends moodleform_mod {
31     function definition() {
32         global $CFG, $DB;
33         $mform =& $this->_form;
35         $config = get_config('resource');
37         if ($this->current->instance and $this->current->tobemigrated) {
38             // resource not migrated yet
39             $resoruce_old = $DB->get_record('resource_old', array('oldid'=>$this->current->instance));
40             $mform->addElement('static', 'warning', '', get_string('notmigrated', 'resource', $resoruce_old->type));
41             $mform->addElement('cancel');
42             $this->standard_hidden_coursemodule_elements();
43             return;
44         }
46         //-------------------------------------------------------
47         $mform->addElement('header', 'general', get_string('general', 'form'));
48         $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
49         if (!empty($CFG->formatstringstriptags)) {
50             $mform->setType('name', PARAM_TEXT);
51         } else {
52             $mform->setType('name', PARAM_CLEAN);
53         }
54         $mform->addRule('name', null, 'required', null, 'client');
55         $this->add_intro_editor($config->requiremodintro);
57         //-------------------------------------------------------
58         $mform->addElement('header', 'contentsection', get_string('contentheader', 'resource'));
59         $mainfile = 'mainfile';
61         $options = array('mainfile'=>$mainfile, 'subdirs'=>0, 'maxbytes'=>0, 'maxfiles'=>-1, 'filetypes'=>'*', 'returnvalue'=>'ref_id');
63         $mform->addElement('hidden', $mainfile, '', array('id'=>$mainfile.'-id')); // TODO: what is this?
64         $mform->setType('mainfile', PARAM_PATH);
65         $mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $options);
66         $mform->addElement('static', '', get_string('selectedfile'), '<div id="'.$mainfile.'-label"></div>');
68         //-------------------------------------------------------
69         $mform->addElement('header', 'optionssection', get_string('optionsheader', 'resource'));
71         if ($this->current->instance) {
72             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
73         } else {
74             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
75         }
77         if (count($options) == 1) {
78             $mform->addElement('hidden', 'display');
79             $mform->setType('display', PARAM_INT);
80             reset($options);
81             $mform->setDefault('display', key($options));
82         } else {
83             $mform->addElement('select', 'display', get_string('displayselect', 'resource'), $options);
84             $mform->setDefault('display', $config->display);
85             $mform->setAdvanced('display', $config->display_adv);
86         }
88         if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
89             $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
90             if (count($options) > 1) {
91                 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
92             }
93             $mform->setType('popupwidth', PARAM_INT);
94             $mform->setDefault('popupwidth', $config->popupwidth);
95             $mform->setAdvanced('popupwidth', $config->popupwidth_adv);
97             $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
98             if (count($options) > 1) {
99                 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
100             }
101             $mform->setType('popupheight', PARAM_INT);
102             $mform->setDefault('popupheight', $config->popupheight);
103             $mform->setAdvanced('popupheight', $config->popupheight_adv);
104         }
106         if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
107           array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
108           array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
109             $mform->addElement('checkbox', 'printheading', get_string('printheading', 'resource'));
110             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
111             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
112             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
113             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
114             $mform->setDefault('printheading', $config->printheading);
115             $mform->setAdvanced('printheading', $config->printheading_adv);
117             $mform->addElement('checkbox', 'printintro', get_string('printintro', 'resource'));
118             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
119             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
120             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
121             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
122             $mform->setDefault('printintro', $config->printintro);
123             $mform->setAdvanced('printintro', $config->printintro_adv);
124         }
126         $options = array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly'));
127         $mform->addElement('select', 'filterfiles', get_string('filterfiles', 'resource'), $options);
128         $mform->setDefault('filterfiles', $config->filterfiles);
129         $mform->setAdvanced('filterfiles', $config->filterfiles_adv);
131         // add legacy files flag only if used
132         if (isset($this->current->legacyfiles) and $this->current->legacyfiles != RESOURCELIB_LEGACYFILES_NO) {
133             $options = array(RESOURCE_LEGACYYFILES_DONE     => get_string('legacyfilesdone', 'resource'),
134                              RESOURCELIB_LEGACYFILES_ACTIVE => get_string('legacyfilesactive', 'resource'));
135             $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'resource'), $options);
136             $mform->setAdvanced('legacyfiles', 1);
137         }
139         //-------------------------------------------------------
140         $this->standard_coursemodule_elements();
142         //-------------------------------------------------------
143         $this->add_action_buttons();
145         //-------------------------------------------------------
146         $mform->addElement('hidden', 'revision');
147         $mform->setType('revision', PARAM_INT);
148         $mform->setDefault('revision', 1);
149     }
151     function data_preprocessing(&$default_values) {
152         if ($this->current->instance and !$this->current->tobemigrated) {
153             $draftitemid = file_get_submitted_draft_itemid('files');
154             file_prepare_draft_area($draftitemid, $this->context->id, 'resource_content', 0, array('subdirs'=>true));
155             $default_values['files'] = $draftitemid;
156         }
157         if (!empty($default_values['displayoptions'])) {
158             $displayoptions = unserialize($default_values['displayoptions']);
159             if (isset($displayoptions['printintro'])) {
160                 $default_values['printintro'] = $displayoptions['printintro'];
161             }
162             if (isset($displayoptions['printheading'])) {
163                 $default_values['printheading'] = $displayoptions['printheading'];
164             }
165             if (!empty($displayoptions['popupwidth'])) {
166                 $default_values['popupwidth'] = $displayoptions['popupwidth'];
167             }
168             if (!empty($displayoptions['popupheight'])) {
169                 $default_values['popupheight'] = $displayoptions['popupheight'];
170             }
171         }
172     }
174     function definition_after_data() {
175         if ($this->current->instance and $this->current->tobemigrated) {
176             // resource not migrated yet
177             return;
178         }
180         parent::definition_after_data();
181     }
183     function validation($data, $files) {
184         global $USER;
186         $errors = parent::validation($data, $files);
188         $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
189         $fs = get_file_storage();
190         if (!$files = $fs->get_area_files($usercontext->id, 'user_draft', $data['files'], 'id', false)) {
191             $errors['files'] = get_string('required');
192             return $errors;
193         }
194         if (count($files) == 1) {
195             // no need to select main file if only one picked
196             return $errors;
197         }
198         $filepaths = array();
199         foreach ($files as $file) {
200             $filepaths[] = $file->get_filepath().$file->get_filename();
201         }
202         if (!in_array($data['mainfile'], $filepaths)) {
203             $errors['mainfile'] = 'Please type correct main file path'; //TODO: will not be needed, do NOT localize!
204         }
205         return $errors;
206     }