4976370d34ee495ad6a16c992f22c458e0f629d1
[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'));
64         $mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $options);
65         $mform->addElement('static', '', get_string('selectedfile'), '<div id="'.$mainfile.'-label"></div>');
67         //-------------------------------------------------------
68         $mform->addElement('header', 'optionssection', get_string('optionsheader', 'resource'));
70         if ($this->current->instance) {
71             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
72         } else {
73             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
74         }
76         if (count($options) == 1) {
77             $mform->addElement('hidden', 'display');
78             reset($options);
79             $mform->setDefault('display', key($options));
80         } else {
81             $mform->addElement('select', 'display', get_string('displayselect', 'resource'), $options);
82             $mform->setDefault('display', $config->display);
83             $mform->setAdvanced('display', $config->display_adv);
84         }
86         if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
87             $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
88             if (count($options) > 1) {
89                 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
90             }
91             $mform->setType('popupwidth', PARAM_INT);
92             $mform->setDefault('popupwidth', $config->popupwidth);
93             $mform->setAdvanced('popupwidth', $config->popupwidth_adv);
95             $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
96             if (count($options) > 1) {
97                 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
98             }
99             $mform->setType('popupheight', PARAM_INT);
100             $mform->setDefault('popupheight', $config->popupheight);
101             $mform->setAdvanced('popupheight', $config->popupheight_adv);
102         }
104         if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
105           array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
106           array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
107             $mform->addElement('checkbox', 'printheading', get_string('printheading', 'resource'));
108             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
109             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
110             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
111             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
112             $mform->setDefault('printheading', $config->printheading);
113             $mform->setAdvanced('printheading', $config->printheading_adv);
115             $mform->addElement('checkbox', 'printintro', get_string('printintro', 'resource'));
116             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
117             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
118             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
119             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
120             $mform->setDefault('printintro', $config->printintro);
121             $mform->setAdvanced('printintro', $config->printintro_adv);
122         }
124         $options = array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly'));
125         $mform->addElement('select', 'filterfiles', get_string('filterfiles', 'resource'), $options);
126         $mform->setDefault('filterfiles', $config->filterfiles);
127         $mform->setAdvanced('filterfiles', $config->filterfiles_adv);
129         // add legacy files flag only if used
130         if (isset($this->current->legacyfiles) and $this->current->legacyfiles != RESOURCELIB_LEGACYFILES_NO) {
131             $options = array(RESOURCE_LEGACYYFILES_DONE     => get_string('legacyfilesdone', 'resource'),
132                              RESOURCELIB_LEGACYFILES_ACTIVE => get_string('legacyfilesactive', 'resource'));
133             $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'resource'), $options);
134             $mform->setAdvanced('legacyfiles', 1);
135         }
137         //-------------------------------------------------------
138         $this->standard_coursemodule_elements();
140         //-------------------------------------------------------
141         $this->add_action_buttons();
143         //-------------------------------------------------------
144         $mform->addElement('hidden', 'revision');
145         $mform->setType('revision', PARAM_INT);
146         $mform->setDefault('revision', 1);
147     }
149     function data_preprocessing(&$default_values) {
150         if ($this->current->instance and !$this->current->tobemigrated) {
151             $draftitemid = file_get_submitted_draft_itemid('files');
152             file_prepare_draft_area($draftitemid, $this->context->id, 'resource_content', 0, array('subdirs'=>true));
153             $default_values['files'] = $draftitemid;
154         }
155         if (!empty($default_values['displayoptions'])) {
156             $displayoptions = unserialize($default_values['displayoptions']);
157             if (isset($displayoptions['printintro'])) {
158                 $default_values['printintro'] = $displayoptions['printintro'];
159             }
160             if (isset($displayoptions['printheading'])) {
161                 $default_values['printheading'] = $displayoptions['printheading'];
162             }
163             if (!empty($displayoptions['popupwidth'])) {
164                 $default_values['popupwidth'] = $displayoptions['popupwidth'];
165             }
166             if (!empty($displayoptions['popupheight'])) {
167                 $default_values['popupheight'] = $displayoptions['popupheight'];
168             }
169         }
170     }
172     function definition_after_data() {
173         if ($this->current->instance and $this->current->tobemigrated) {
174             // resource not migrated yet
175             return;
176         }
178         parent::definition_after_data();
179     }
181     function validation($data, $files) {
182         global $USER;
184         $errors = parent::validation($data, $files);
186         $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
187         $fs = get_file_storage();
188         if (!$files = $fs->get_area_files($usercontext->id, 'user_draft', $data['files'], 'id', false)) {
189             $errors['files'] = get_string('required');
190             return $errors;
191         }
192         if (count($files) == 1) {
193             // no need to select main file if only one picked
194             return $errors;
195         }
196         $filepaths = array();
197         foreach ($files as $file) {
198             $filepaths[] = $file->get_filepath().$file->get_filename();
199         }
200         if (!in_array($data['mainfile'], $filepaths)) {
201             $errors['mainfile'] = 'Please type correct main file path'; //TODO: will not be needed, do NOT localize!
202         }
203         return $errors;
204     }