MDL-22548 temporary workaround for horrible usability in mod/resource edit interface
[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 if (!defined('MOODLE_INTERNAL')) {
27     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
28 }
30 require_once($CFG->dirroot.'/course/moodleform_mod.php');
31 require_once($CFG->dirroot.'/mod/resource/locallib.php');
32 require_once($CFG->libdir.'/filelib.php');
34 class mod_resource_mod_form extends moodleform_mod {
35     function definition() {
36         global $CFG, $DB;
37         $mform =& $this->_form;
39         $config = get_config('resource');
41         if ($this->current->instance and $this->current->tobemigrated) {
42             // resource not migrated yet
43             $resoruce_old = $DB->get_record('resource_old', array('oldid'=>$this->current->instance));
44             $mform->addElement('static', 'warning', '', get_string('notmigrated', 'resource', $resoruce_old->type));
45             $mform->addElement('cancel');
46             $this->standard_hidden_coursemodule_elements();
47             return;
48         }
50         //-------------------------------------------------------
51         $mform->addElement('header', 'general', get_string('general', 'form'));
52         $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
53         if (!empty($CFG->formatstringstriptags)) {
54             $mform->setType('name', PARAM_TEXT);
55         } else {
56             $mform->setType('name', PARAM_CLEAN);
57         }
58         $mform->addRule('name', null, 'required', null, 'client');
59         $this->add_intro_editor($config->requiremodintro);
61         //-------------------------------------------------------
62         $mform->addElement('header', 'contentsection', get_string('contentheader', 'resource'));
64         $filemanager_options = array();
65         // 3 == FILE_EXTERNAL & FILE_INTERNAL
66         // These two constant names are defined in repository/lib.php
67         $filemanager_options['return_types'] = 3;
68         $filemanager_options['accepted_types'] = '*';
69         $filemanager_options['maxbytes'] = 0;
70         $filemanager_options['maxfiles'] = -1;
71         $filemanager_options['mainfile'] = 'mainfile';
73         $mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $filemanager_options);
75         //TODO: ohlala, it should be hidden when JS file manager loaded, the main file should be somehow highlighted directly in editor
76         $mform->addElement('text', 'mainfile', get_string('areamainfile', 'repository'));
77         $mform->setType('mainfile', PARAM_PATH);
79         //-------------------------------------------------------
80         $mform->addElement('header', 'optionssection', get_string('optionsheader', 'resource'));
82         if ($this->current->instance) {
83             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
84         } else {
85             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
86         }
88         if (count($options) == 1) {
89             $mform->addElement('hidden', 'display');
90             $mform->setType('display', PARAM_INT);
91             reset($options);
92             $mform->setDefault('display', key($options));
93         } else {
94             $mform->addElement('select', 'display', get_string('displayselect', 'resource'), $options);
95             $mform->setDefault('display', $config->display);
96             $mform->setAdvanced('display', $config->display_adv);
97         }
99         if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
100             $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
101             if (count($options) > 1) {
102                 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
103             }
104             $mform->setType('popupwidth', PARAM_INT);
105             $mform->setDefault('popupwidth', $config->popupwidth);
106             $mform->setAdvanced('popupwidth', $config->popupwidth_adv);
108             $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
109             if (count($options) > 1) {
110                 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
111             }
112             $mform->setType('popupheight', PARAM_INT);
113             $mform->setDefault('popupheight', $config->popupheight);
114             $mform->setAdvanced('popupheight', $config->popupheight_adv);
115         }
117         if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
118           array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
119           array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
120             $mform->addElement('checkbox', 'printheading', get_string('printheading', 'resource'));
121             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
122             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
123             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
124             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
125             $mform->setDefault('printheading', $config->printheading);
126             $mform->setAdvanced('printheading', $config->printheading_adv);
128             $mform->addElement('checkbox', 'printintro', get_string('printintro', 'resource'));
129             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
130             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
131             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
132             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
133             $mform->setDefault('printintro', $config->printintro);
134             $mform->setAdvanced('printintro', $config->printintro_adv);
135         }
137         $options = array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly'));
138         $mform->addElement('select', 'filterfiles', get_string('filterfiles', 'resource'), $options);
139         $mform->setDefault('filterfiles', $config->filterfiles);
140         $mform->setAdvanced('filterfiles', $config->filterfiles_adv);
142         // add legacy files flag only if used
143         if (isset($this->current->legacyfiles) and $this->current->legacyfiles != RESOURCELIB_LEGACYFILES_NO) {
144             $options = array(RESOURCE_LEGACYYFILES_DONE     => get_string('legacyfilesdone', 'resource'),
145                              RESOURCELIB_LEGACYFILES_ACTIVE => get_string('legacyfilesactive', 'resource'));
146             $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'resource'), $options);
147             $mform->setAdvanced('legacyfiles', 1);
148         }
150         //-------------------------------------------------------
151         $this->standard_coursemodule_elements();
153         //-------------------------------------------------------
154         $this->add_action_buttons();
156         //-------------------------------------------------------
157         $mform->addElement('hidden', 'revision');
158         $mform->setType('revision', PARAM_INT);
159         $mform->setDefault('revision', 1);
160     }
162     function data_preprocessing(&$default_values) {
163         if ($this->current->instance and !$this->current->tobemigrated) {
164             $draftitemid = file_get_submitted_draft_itemid('files');
165             file_prepare_draft_area($draftitemid, $this->context->id, 'resource_content', 0, array('subdirs'=>true));
166             $default_values['files'] = $draftitemid;
167         }
168         if (!empty($default_values['displayoptions'])) {
169             $displayoptions = unserialize($default_values['displayoptions']);
170             if (isset($displayoptions['printintro'])) {
171                 $default_values['printintro'] = $displayoptions['printintro'];
172             }
173             if (isset($displayoptions['printheading'])) {
174                 $default_values['printheading'] = $displayoptions['printheading'];
175             }
176             if (!empty($displayoptions['popupwidth'])) {
177                 $default_values['popupwidth'] = $displayoptions['popupwidth'];
178             }
179             if (!empty($displayoptions['popupheight'])) {
180                 $default_values['popupheight'] = $displayoptions['popupheight'];
181             }
182         }
183     }
185     function definition_after_data() {
186         if ($this->current->instance and $this->current->tobemigrated) {
187             // resource not migrated yet
188             return;
189         }
191         parent::definition_after_data();
192     }
194     function validation($data, $files) {
195         global $USER;
197         $errors = parent::validation($data, $files);
199         $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
200         $fs = get_file_storage();
201         if (!$files = $fs->get_area_files($usercontext->id, 'user_draft', $data['files'], 'id', false)) {
202             $errors['files'] = get_string('required');
203             return $errors;
204         }
205         if (count($files) == 1) {
206             // no need to select main file if only one picked
207             return $errors;
208         }
209         $filepaths = array();
210         foreach ($files as $file) {
211             $filepaths[] = $file->get_filepath().$file->get_filename();
212         }
213         if (!in_array($data['mainfile'], $filepaths)) {
214             $errors['mainfile'] = 'Please type correct main file path'; //TODO: will not be needed, do NOT localize!
215         }
216         return $errors;
217     }