MDL-22388 Added some checks to kill these scripts dead with an unequivocal notice...
[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'));
63         $mainfile = 'mainfile';
65         $mform->addElement('text', $mainfile, get_string('selectedfile'));
66         $mform->setType($mainfile, PARAM_PATH);
67         $mform->addRule($mainfile, null, 'required', null, 'client');
69         $filemanager_options = array();
70         // 3 == FILE_EXTERNAL & FILE_INTERNAL
71         // These two constant names are defined in repository/lib.php
72         $filemanager_options['return_types'] = 3;
73         $filemanager_options['accepted_types'] = '*';
74         $filemanager_options['maxbytes'] = 0;
75         $filemanager_options['maxfiles'] = -1;
76         $filemanager_options['mainfile'] = $mainfile;
78         $mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $filemanager_options);
80         //-------------------------------------------------------
81         $mform->addElement('header', 'optionssection', get_string('optionsheader', 'resource'));
83         if ($this->current->instance) {
84             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
85         } else {
86             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
87         }
89         if (count($options) == 1) {
90             $mform->addElement('hidden', 'display');
91             $mform->setType('display', PARAM_INT);
92             reset($options);
93             $mform->setDefault('display', key($options));
94         } else {
95             $mform->addElement('select', 'display', get_string('displayselect', 'resource'), $options);
96             $mform->setDefault('display', $config->display);
97             $mform->setAdvanced('display', $config->display_adv);
98         }
100         if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
101             $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
102             if (count($options) > 1) {
103                 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
104             }
105             $mform->setType('popupwidth', PARAM_INT);
106             $mform->setDefault('popupwidth', $config->popupwidth);
107             $mform->setAdvanced('popupwidth', $config->popupwidth_adv);
109             $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
110             if (count($options) > 1) {
111                 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
112             }
113             $mform->setType('popupheight', PARAM_INT);
114             $mform->setDefault('popupheight', $config->popupheight);
115             $mform->setAdvanced('popupheight', $config->popupheight_adv);
116         }
118         if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
119           array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
120           array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
121             $mform->addElement('checkbox', 'printheading', get_string('printheading', 'resource'));
122             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
123             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
124             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
125             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
126             $mform->setDefault('printheading', $config->printheading);
127             $mform->setAdvanced('printheading', $config->printheading_adv);
129             $mform->addElement('checkbox', 'printintro', get_string('printintro', 'resource'));
130             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
131             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
132             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
133             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
134             $mform->setDefault('printintro', $config->printintro);
135             $mform->setAdvanced('printintro', $config->printintro_adv);
136         }
138         $options = array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly'));
139         $mform->addElement('select', 'filterfiles', get_string('filterfiles', 'resource'), $options);
140         $mform->setDefault('filterfiles', $config->filterfiles);
141         $mform->setAdvanced('filterfiles', $config->filterfiles_adv);
143         // add legacy files flag only if used
144         if (isset($this->current->legacyfiles) and $this->current->legacyfiles != RESOURCELIB_LEGACYFILES_NO) {
145             $options = array(RESOURCE_LEGACYYFILES_DONE     => get_string('legacyfilesdone', 'resource'),
146                              RESOURCELIB_LEGACYFILES_ACTIVE => get_string('legacyfilesactive', 'resource'));
147             $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'resource'), $options);
148             $mform->setAdvanced('legacyfiles', 1);
149         }
151         //-------------------------------------------------------
152         $this->standard_coursemodule_elements();
154         //-------------------------------------------------------
155         $this->add_action_buttons();
157         //-------------------------------------------------------
158         $mform->addElement('hidden', 'revision');
159         $mform->setType('revision', PARAM_INT);
160         $mform->setDefault('revision', 1);
161     }
163     function data_preprocessing(&$default_values) {
164         if ($this->current->instance and !$this->current->tobemigrated) {
165             $draftitemid = file_get_submitted_draft_itemid('files');
166             file_prepare_draft_area($draftitemid, $this->context->id, 'resource_content', 0, array('subdirs'=>true));
167             $default_values['files'] = $draftitemid;
168         }
169         if (!empty($default_values['displayoptions'])) {
170             $displayoptions = unserialize($default_values['displayoptions']);
171             if (isset($displayoptions['printintro'])) {
172                 $default_values['printintro'] = $displayoptions['printintro'];
173             }
174             if (isset($displayoptions['printheading'])) {
175                 $default_values['printheading'] = $displayoptions['printheading'];
176             }
177             if (!empty($displayoptions['popupwidth'])) {
178                 $default_values['popupwidth'] = $displayoptions['popupwidth'];
179             }
180             if (!empty($displayoptions['popupheight'])) {
181                 $default_values['popupheight'] = $displayoptions['popupheight'];
182             }
183         }
184     }
186     function definition_after_data() {
187         if ($this->current->instance and $this->current->tobemigrated) {
188             // resource not migrated yet
189             return;
190         }
192         parent::definition_after_data();
193     }
195     function validation($data, $files) {
196         global $USER;
198         $errors = parent::validation($data, $files);
200         $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
201         $fs = get_file_storage();
202         if (!$files = $fs->get_area_files($usercontext->id, 'user_draft', $data['files'], 'id', false)) {
203             $errors['files'] = get_string('required');
204             return $errors;
205         }
206         if (count($files) == 1) {
207             // no need to select main file if only one picked
208             return $errors;
209         }
210         $filepaths = array();
211         foreach ($files as $file) {
212             $filepaths[] = $file->get_filepath().$file->get_filename();
213         }
214         if (!in_array($data['mainfile'], $filepaths)) {
215             $errors['mainfile'] = 'Please type correct main file path'; //TODO: will not be needed, do NOT localize!
216         }
217         return $errors;
218     }