MDL-51267 mod_url: make the url and resource forms friendlier
[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  {@link http://skodak.org}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die;
28 require_once($CFG->dirroot.'/course/moodleform_mod.php');
29 require_once($CFG->dirroot.'/mod/resource/locallib.php');
30 require_once($CFG->libdir.'/filelib.php');
32 class mod_resource_mod_form extends moodleform_mod {
33     function definition() {
34         global $CFG, $DB;
35         $mform =& $this->_form;
37         $config = get_config('resource');
39         if ($this->current->instance and $this->current->tobemigrated) {
40             // resource not migrated yet
41             $resource_old = $DB->get_record('resource_old', array('oldid'=>$this->current->instance));
42             $mform->addElement('static', 'warning', '', get_string('notmigrated', 'resource', $resource_old->type));
43             $mform->addElement('cancel');
44             $this->standard_hidden_coursemodule_elements();
45             return;
46         }
48         //-------------------------------------------------------
49         $mform->addElement('header', 'general', get_string('general', 'form'));
50         $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
51         if (!empty($CFG->formatstringstriptags)) {
52             $mform->setType('name', PARAM_TEXT);
53         } else {
54             $mform->setType('name', PARAM_CLEANHTML);
55         }
56         $mform->addRule('name', null, 'required', null, 'client');
57         $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
58         $this->standard_intro_elements(null, array('rows' => 5));
60         $filemanager_options = array();
61         $filemanager_options['accepted_types'] = '*';
62         $filemanager_options['maxbytes'] = 0;
63         $filemanager_options['maxfiles'] = -1;
64         $filemanager_options['mainfile'] = true;
66         $mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $filemanager_options);
68         // add legacy files flag only if used
69         if (isset($this->current->legacyfiles) and $this->current->legacyfiles != RESOURCELIB_LEGACYFILES_NO) {
70             $options = array(RESOURCELIB_LEGACYFILES_DONE   => get_string('legacyfilesdone', 'resource'),
71                              RESOURCELIB_LEGACYFILES_ACTIVE => get_string('legacyfilesactive', 'resource'));
72             $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'resource'), $options);
73         }
75         //-------------------------------------------------------
76         $mform->addElement('header', 'optionssection', get_string('appearance'));
78         if ($this->current->instance) {
79             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
80         } else {
81             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
82         }
84         if (count($options) == 1) {
85             $mform->addElement('hidden', 'display');
86             $mform->setType('display', PARAM_INT);
87             reset($options);
88             $mform->setDefault('display', key($options));
89         } else {
90             $mform->addElement('select', 'display', get_string('displayselect', 'resource'), $options);
91             $mform->setDefault('display', $config->display);
92             $mform->addHelpButton('display', 'displayselect', 'resource');
93         }
95         $mform->addElement('checkbox', 'showsize', get_string('showsize', 'resource'));
96         $mform->setDefault('showsize', $config->showsize);
97         $mform->addHelpButton('showsize', 'showsize', 'resource');
98         $mform->addElement('checkbox', 'showtype', get_string('showtype', 'resource'));
99         $mform->setDefault('showtype', $config->showtype);
100         $mform->addHelpButton('showtype', 'showtype', 'resource');
101         $mform->addElement('checkbox', 'showdate', get_string('showdate', 'resource'));
102         $mform->setDefault('showdate', $config->showdate);
103         $mform->addHelpButton('showdate', 'showdate', 'resource');
105         if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
106             $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
107             if (count($options) > 1) {
108                 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
109             }
110             $mform->setType('popupwidth', PARAM_INT);
111             $mform->setDefault('popupwidth', $config->popupwidth);
112             $mform->setAdvanced('popupwidth', true);
114             $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
115             if (count($options) > 1) {
116                 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
117             }
118             $mform->setType('popupheight', PARAM_INT);
119             $mform->setDefault('popupheight', $config->popupheight);
120             $mform->setAdvanced('popupheight', true);
121         }
123         if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
124           array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
125           array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
126             $mform->addElement('checkbox', 'printintro', get_string('printintro', 'resource'));
127             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
128             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
129             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
130             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
131             $mform->setDefault('printintro', $config->printintro);
132         }
134         $options = array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly'));
135         $mform->addElement('select', 'filterfiles', get_string('filterfiles', 'resource'), $options);
136         $mform->setDefault('filterfiles', $config->filterfiles);
137         $mform->setAdvanced('filterfiles', true);
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, 'mod_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 (!empty($displayoptions['popupwidth'])) {
163                 $default_values['popupwidth'] = $displayoptions['popupwidth'];
164             }
165             if (!empty($displayoptions['popupheight'])) {
166                 $default_values['popupheight'] = $displayoptions['popupheight'];
167             }
168             if (!empty($displayoptions['showsize'])) {
169                 $default_values['showsize'] = $displayoptions['showsize'];
170             } else {
171                 // Must set explicitly to 0 here otherwise it will use system
172                 // default which may be 1.
173                 $default_values['showsize'] = 0;
174             }
175             if (!empty($displayoptions['showtype'])) {
176                 $default_values['showtype'] = $displayoptions['showtype'];
177             } else {
178                 $default_values['showtype'] = 0;
179             }
180             if (!empty($displayoptions['showdate'])) {
181                 $default_values['showdate'] = $displayoptions['showdate'];
182             } else {
183                 $default_values['showdate'] = 0;
184             }
185         }
186     }
188     function definition_after_data() {
189         if ($this->current->instance and $this->current->tobemigrated) {
190             // resource not migrated yet
191             return;
192         }
194         parent::definition_after_data();
195     }
197     function validation($data, $files) {
198         global $USER;
200         $errors = parent::validation($data, $files);
202         $usercontext = context_user::instance($USER->id);
203         $fs = get_file_storage();
204         if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['files'], 'sortorder, id', false)) {
205             $errors['files'] = get_string('required');
206             return $errors;
207         }
208         if (count($files) == 1) {
209             // no need to select main file if only one picked
210             return $errors;
211         } else if(count($files) > 1) {
212             $mainfile = false;
213             foreach($files as $file) {
214                 if ($file->get_sortorder() == 1) {
215                     $mainfile = true;
216                     break;
217                 }
218             }
219             // set a default main file
220             if (!$mainfile) {
221                 $file = reset($files);
222                 file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(),
223                                    $file->get_filepath(), $file->get_filename(), 1);
224             }
225         }
226         return $errors;
227     }