Merge branch 'MDL-25708' of git://github.com/stronk7/moodle
[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
22  * @subpackage resource
23  * @copyright  2009 Petr Skoda  {@link http://skodak.org}
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die;
29 require_once($CFG->dirroot.'/course/moodleform_mod.php');
30 require_once($CFG->dirroot.'/mod/resource/locallib.php');
31 require_once($CFG->libdir.'/filelib.php');
33 class mod_resource_mod_form extends moodleform_mod {
34     function definition() {
35         global $CFG, $DB;
36         $mform =& $this->_form;
38         $config = get_config('resource');
40         if ($this->current->instance and $this->current->tobemigrated) {
41             // resource not migrated yet
42             $resoruce_old = $DB->get_record('resource_old', array('oldid'=>$this->current->instance));
43             $mform->addElement('static', 'warning', '', get_string('notmigrated', 'resource', $resoruce_old->type));
44             $mform->addElement('cancel');
45             $this->standard_hidden_coursemodule_elements();
46             return;
47         }
49         //-------------------------------------------------------
50         $mform->addElement('header', 'general', get_string('general', 'form'));
51         $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
52         if (!empty($CFG->formatstringstriptags)) {
53             $mform->setType('name', PARAM_TEXT);
54         } else {
55             $mform->setType('name', PARAM_CLEANHTML);
56         }
57         $mform->addRule('name', null, 'required', null, 'client');
58         $this->add_intro_editor($config->requiremodintro);
60         //-------------------------------------------------------
61         $mform->addElement('header', 'contentsection', get_string('contentheader', 'resource'));
63         $filemanager_options = array();
64         // 3 == FILE_EXTERNAL & FILE_INTERNAL
65         // These two constant names are defined in repository/lib.php
66         $filemanager_options['return_types'] = 3;
67         $filemanager_options['accepted_types'] = '*';
68         $filemanager_options['maxbytes'] = 0;
69         $filemanager_options['maxfiles'] = -1;
70         $filemanager_options['mainfile'] = true;
72         $mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $filemanager_options);
74         //-------------------------------------------------------
75         $mform->addElement('header', 'optionssection', get_string('optionsheader', 'resource'));
77         if ($this->current->instance) {
78             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
79         } else {
80             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
81         }
83         if (count($options) == 1) {
84             $mform->addElement('hidden', 'display');
85             $mform->setType('display', PARAM_INT);
86             reset($options);
87             $mform->setDefault('display', key($options));
88         } else {
89             $mform->addElement('select', 'display', get_string('displayselect', 'resource'), $options);
90             $mform->setDefault('display', $config->display);
91             $mform->setAdvanced('display', $config->display_adv);
92             $mform->addHelpButton('display', 'displayselect', 'resource');
93         }
95         if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
96             $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
97             if (count($options) > 1) {
98                 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
99             }
100             $mform->setType('popupwidth', PARAM_INT);
101             $mform->setDefault('popupwidth', $config->popupwidth);
102             $mform->setAdvanced('popupwidth', $config->popupwidth_adv);
104             $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
105             if (count($options) > 1) {
106                 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
107             }
108             $mform->setType('popupheight', PARAM_INT);
109             $mform->setDefault('popupheight', $config->popupheight);
110             $mform->setAdvanced('popupheight', $config->popupheight_adv);
111         }
113         if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
114           array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
115           array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
116             $mform->addElement('checkbox', 'printheading', get_string('printheading', 'resource'));
117             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
118             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
119             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
120             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
121             $mform->setDefault('printheading', $config->printheading);
122             $mform->setAdvanced('printheading', $config->printheading_adv);
124             $mform->addElement('checkbox', 'printintro', get_string('printintro', 'resource'));
125             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
126             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
127             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
128             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
129             $mform->setDefault('printintro', $config->printintro);
130             $mform->setAdvanced('printintro', $config->printintro_adv);
131         }
133         $options = array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly'));
134         $mform->addElement('select', 'filterfiles', get_string('filterfiles', 'resource'), $options);
135         $mform->setDefault('filterfiles', $config->filterfiles);
136         $mform->setAdvanced('filterfiles', $config->filterfiles_adv);
138         // add legacy files flag only if used
139         if (isset($this->current->legacyfiles) and $this->current->legacyfiles != RESOURCELIB_LEGACYFILES_NO) {
140             $options = array(RESOURCELIB_LEGACYFILES_DONE   => get_string('legacyfilesdone', 'resource'),
141                              RESOURCELIB_LEGACYFILES_ACTIVE => get_string('legacyfilesactive', 'resource'));
142             $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'resource'), $options);
143             $mform->setAdvanced('legacyfiles', 1);
144         }
146         //-------------------------------------------------------
147         $this->standard_coursemodule_elements();
149         //-------------------------------------------------------
150         $this->add_action_buttons();
152         //-------------------------------------------------------
153         $mform->addElement('hidden', 'revision');
154         $mform->setType('revision', PARAM_INT);
155         $mform->setDefault('revision', 1);
156     }
158     function data_preprocessing(&$default_values) {
159         if ($this->current->instance and !$this->current->tobemigrated) {
160             $draftitemid = file_get_submitted_draft_itemid('files');
161             file_prepare_draft_area($draftitemid, $this->context->id, 'mod_resource', 'content', 0, array('subdirs'=>true));
162             $default_values['files'] = $draftitemid;
163         }
164         if (!empty($default_values['displayoptions'])) {
165             $displayoptions = unserialize($default_values['displayoptions']);
166             if (isset($displayoptions['printintro'])) {
167                 $default_values['printintro'] = $displayoptions['printintro'];
168             }
169             if (isset($displayoptions['printheading'])) {
170                 $default_values['printheading'] = $displayoptions['printheading'];
171             }
172             if (!empty($displayoptions['popupwidth'])) {
173                 $default_values['popupwidth'] = $displayoptions['popupwidth'];
174             }
175             if (!empty($displayoptions['popupheight'])) {
176                 $default_values['popupheight'] = $displayoptions['popupheight'];
177             }
178         }
179     }
181     function definition_after_data() {
182         if ($this->current->instance and $this->current->tobemigrated) {
183             // resource not migrated yet
184             return;
185         }
187         parent::definition_after_data();
188     }
190     function validation($data, $files) {
191         global $USER;
193         $errors = parent::validation($data, $files);
195         $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
196         $fs = get_file_storage();
197         if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['files'], 'sortorder, id', false)) {
198             $errors['files'] = get_string('required');
199             return $errors;
200         }
201         if (count($files) == 1) {
202             // no need to select main file if only one picked
203             return $errors;
204         } else if(count($files) > 1) {
205             $mainfile = false;
206             foreach($files as $file) {
207                 if ($file->get_sortorder() == 1) {
208                     $mainfile = true;
209                     break;
210                 }
211             }
212             // set a default main file
213             if (!$mainfile) {
214                 $file = reset($files);
215                 file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(),
216                                    $file->get_filepath(), $file->get_filename(), 1);
217             }
218         }
219         return $errors;
220     }