3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Resource configuration form
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
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() {
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();
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
);
54 $mform->setType('name', PARAM_CLEANHTML
);
56 $mform->addRule('name', null, 'required', null, 'client');
57 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
58 $this->add_intro_editor($config->requiremodintro
);
60 //-------------------------------------------------------
61 $mform->addElement('header', 'contentsection', get_string('contentheader', 'resource'));
62 $mform->setExpanded('contentsection');
64 $filemanager_options = array();
65 $filemanager_options['accepted_types'] = '*';
66 $filemanager_options['maxbytes'] = 0;
67 $filemanager_options['maxfiles'] = -1;
68 $filemanager_options['mainfile'] = true;
70 $mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $filemanager_options);
72 // add legacy files flag only if used
73 if (isset($this->current
->legacyfiles
) and $this->current
->legacyfiles
!= RESOURCELIB_LEGACYFILES_NO
) {
74 $options = array(RESOURCELIB_LEGACYFILES_DONE
=> get_string('legacyfilesdone', 'resource'),
75 RESOURCELIB_LEGACYFILES_ACTIVE
=> get_string('legacyfilesactive', 'resource'));
76 $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'resource'), $options);
79 //-------------------------------------------------------
80 $mform->addElement('header', 'optionssection', get_string('appearance'));
82 if ($this->current
->instance
) {
83 $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions
), $this->current
->display
);
85 $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions
));
88 if (count($options) == 1) {
89 $mform->addElement('hidden', 'display');
90 $mform->setType('display', PARAM_INT
);
92 $mform->setDefault('display', key($options));
94 $mform->addElement('select', 'display', get_string('displayselect', 'resource'), $options);
95 $mform->setDefault('display', $config->display
);
96 $mform->addHelpButton('display', 'displayselect', 'resource');
99 $mform->addElement('checkbox', 'showsize', get_string('showsize', 'resource'));
100 $mform->setDefault('showsize', $config->showsize
);
101 $mform->addHelpButton('showsize', 'showsize', 'resource');
102 $mform->addElement('checkbox', 'showtype', get_string('showtype', 'resource'));
103 $mform->setDefault('showtype', $config->showtype
);
104 $mform->addHelpButton('showtype', 'showtype', 'resource');
106 if (array_key_exists(RESOURCELIB_DISPLAY_POPUP
, $options)) {
107 $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
108 if (count($options) > 1) {
109 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP
);
111 $mform->setType('popupwidth', PARAM_INT
);
112 $mform->setDefault('popupwidth', $config->popupwidth
);
113 $mform->setAdvanced('popupwidth', true);
115 $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
116 if (count($options) > 1) {
117 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP
);
119 $mform->setType('popupheight', PARAM_INT
);
120 $mform->setDefault('popupheight', $config->popupheight
);
121 $mform->setAdvanced('popupheight', true);
124 if (array_key_exists(RESOURCELIB_DISPLAY_AUTO
, $options) or
125 array_key_exists(RESOURCELIB_DISPLAY_EMBED
, $options) or
126 array_key_exists(RESOURCELIB_DISPLAY_FRAME
, $options)) {
127 $mform->addElement('checkbox', 'printintro', get_string('printintro', 'resource'));
128 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP
);
129 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD
);
130 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN
);
131 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW
);
132 $mform->setDefault('printintro', $config->printintro
);
135 $options = array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly'));
136 $mform->addElement('select', 'filterfiles', get_string('filterfiles', 'resource'), $options);
137 $mform->setDefault('filterfiles', $config->filterfiles
);
138 $mform->setAdvanced('filterfiles', true);
140 //-------------------------------------------------------
141 $this->standard_coursemodule_elements();
143 //-------------------------------------------------------
144 $this->add_action_buttons();
146 //-------------------------------------------------------
147 $mform->addElement('hidden', 'revision');
148 $mform->setType('revision', PARAM_INT
);
149 $mform->setDefault('revision', 1);
152 function data_preprocessing(&$default_values) {
153 if ($this->current
->instance
and !$this->current
->tobemigrated
) {
154 $draftitemid = file_get_submitted_draft_itemid('files');
155 file_prepare_draft_area($draftitemid, $this->context
->id
, 'mod_resource', 'content', 0, array('subdirs'=>true));
156 $default_values['files'] = $draftitemid;
158 if (!empty($default_values['displayoptions'])) {
159 $displayoptions = unserialize($default_values['displayoptions']);
160 if (isset($displayoptions['printintro'])) {
161 $default_values['printintro'] = $displayoptions['printintro'];
163 if (!empty($displayoptions['popupwidth'])) {
164 $default_values['popupwidth'] = $displayoptions['popupwidth'];
166 if (!empty($displayoptions['popupheight'])) {
167 $default_values['popupheight'] = $displayoptions['popupheight'];
169 if (!empty($displayoptions['showsize'])) {
170 $default_values['showsize'] = $displayoptions['showsize'];
172 // Must set explicitly to 0 here otherwise it will use system
173 // default which may be 1.
174 $default_values['showsize'] = 0;
176 if (!empty($displayoptions['showtype'])) {
177 $default_values['showtype'] = $displayoptions['showtype'];
179 $default_values['showtype'] = 0;
184 function definition_after_data() {
185 if ($this->current
->instance
and $this->current
->tobemigrated
) {
186 // resource not migrated yet
190 parent
::definition_after_data();
193 function validation($data, $files) {
196 $errors = parent
::validation($data, $files);
198 $usercontext = context_user
::instance($USER->id
);
199 $fs = get_file_storage();
200 if (!$files = $fs->get_area_files($usercontext->id
, 'user', 'draft', $data['files'], 'sortorder, id', false)) {
201 $errors['files'] = get_string('required');
204 if (count($files) == 1) {
205 // no need to select main file if only one picked
207 } else if(count($files) > 1) {
209 foreach($files as $file) {
210 if ($file->get_sortorder() == 1) {
215 // set a default main file
217 $file = reset($files);
218 file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(),
219 $file->get_filepath(), $file->get_filename(), 1);