Merge branch 'MDL-61033-master' of https://github.com/nashtechdev01/moodle
[moodle.git] / mod / resource / mod_form.php
CommitLineData
26de8d35 1<?php
26de8d35 2
aa54ed7b 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/>.
17
18/**
19 * Resource configuration form
20 *
11e41f1a 21 * @package mod_resource
702ab58c
PS
22 * @copyright 2009 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
aa54ed7b 24 */
25
702ab58c 26defined('MOODLE_INTERNAL') || die;
bfebaf64 27
aa54ed7b 28require_once($CFG->dirroot.'/course/moodleform_mod.php');
29require_once($CFG->dirroot.'/mod/resource/locallib.php');
30require_once($CFG->libdir.'/filelib.php');
26de8d35 31
aa54ed7b 32class mod_resource_mod_form extends moodleform_mod {
26de8d35 33 function definition() {
c18269c7 34 global $CFG, $DB;
26de8d35 35 $mform =& $this->_form;
36
aa54ed7b 37 $config = get_config('resource');
26de8d35 38
aa54ed7b 39 if ($this->current->instance and $this->current->tobemigrated) {
40 // resource not migrated yet
c6c9a3bc
DM
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));
aa54ed7b 43 $mform->addElement('cancel');
44 $this->standard_hidden_coursemodule_elements();
45 return;
46 }
26de8d35 47
aa54ed7b 48 //-------------------------------------------------------
26de8d35 49 $mform->addElement('header', 'general', get_string('general', 'form'));
9217695a 50 $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
8eb1d25f 51 if (!empty($CFG->formatstringstriptags)) {
52 $mform->setType('name', PARAM_TEXT);
53 } else {
b8ea3041 54 $mform->setType('name', PARAM_CLEANHTML);
8eb1d25f 55 }
26de8d35 56 $mform->addRule('name', null, 'required', null, 'client');
a74cd331 57 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
bc577c54
SL
58 $this->standard_intro_elements();
59 $element = $mform->getElement('introeditor');
60 $attributes = $element->getAttributes();
61 $attributes['rows'] = 5;
62 $element->setAttributes($attributes);
b2aea6c6 63 $filemanager_options = array();
b2aea6c6 64 $filemanager_options['accepted_types'] = '*';
b01c3e84
DC
65 $filemanager_options['maxbytes'] = 0;
66 $filemanager_options['maxfiles'] = -1;
f79321f1 67 $filemanager_options['mainfile'] = true;
bc6f7235 68
b01c3e84 69 $mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $filemanager_options);
bc6f7235 70
f7224e60
FM
71 // add legacy files flag only if used
72 if (isset($this->current->legacyfiles) and $this->current->legacyfiles != RESOURCELIB_LEGACYFILES_NO) {
73 $options = array(RESOURCELIB_LEGACYFILES_DONE => get_string('legacyfilesdone', 'resource'),
74 RESOURCELIB_LEGACYFILES_ACTIVE => get_string('legacyfilesactive', 'resource'));
75 $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'resource'), $options);
76 }
77
aa54ed7b 78 //-------------------------------------------------------
f7224e60 79 $mform->addElement('header', 'optionssection', get_string('appearance'));
aa54ed7b 80
81 if ($this->current->instance) {
82 $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
83 } else {
84 $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
85 }
86
87 if (count($options) == 1) {
88 $mform->addElement('hidden', 'display');
d18e0fe6 89 $mform->setType('display', PARAM_INT);
aa54ed7b 90 reset($options);
91 $mform->setDefault('display', key($options));
92 } else {
93 $mform->addElement('select', 'display', get_string('displayselect', 'resource'), $options);
94 $mform->setDefault('display', $config->display);
a4330d14 95 $mform->addHelpButton('display', 'displayselect', 'resource');
aa54ed7b 96 }
97
a2c57666 98 $mform->addElement('checkbox', 'showsize', get_string('showsize', 'resource'));
99 $mform->setDefault('showsize', $config->showsize);
a2c57666 100 $mform->addHelpButton('showsize', 'showsize', 'resource');
101 $mform->addElement('checkbox', 'showtype', get_string('showtype', 'resource'));
102 $mform->setDefault('showtype', $config->showtype);
a2c57666 103 $mform->addHelpButton('showtype', 'showtype', 'resource');
ba8b641d
AH
104 $mform->addElement('checkbox', 'showdate', get_string('showdate', 'resource'));
105 $mform->setDefault('showdate', $config->showdate);
106 $mform->addHelpButton('showdate', 'showdate', 'resource');
a2c57666 107
aa54ed7b 108 if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
109 $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
110 if (count($options) > 1) {
111 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
112 }
113 $mform->setType('popupwidth', PARAM_INT);
114 $mform->setDefault('popupwidth', $config->popupwidth);
b5472cb8 115 $mform->setAdvanced('popupwidth', true);
aa54ed7b 116
117 $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
118 if (count($options) > 1) {
119 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
120 }
121 $mform->setType('popupheight', PARAM_INT);
122 $mform->setDefault('popupheight', $config->popupheight);
b5472cb8 123 $mform->setAdvanced('popupheight', true);
aa54ed7b 124 }
125
126 if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
127 array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
128 array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
aa54ed7b 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);
aa54ed7b 135 }
26de8d35 136
aa54ed7b 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);
b5472cb8 140 $mform->setAdvanced('filterfiles', true);
aa54ed7b 141
142 //-------------------------------------------------------
42f103be 143 $this->standard_coursemodule_elements();
26de8d35 144
aa54ed7b 145 //-------------------------------------------------------
26de8d35 146 $this->add_action_buttons();
aa54ed7b 147
148 //-------------------------------------------------------
149 $mform->addElement('hidden', 'revision');
150 $mform->setType('revision', PARAM_INT);
151 $mform->setDefault('revision', 1);
152 }
153
154 function data_preprocessing(&$default_values) {
155 if ($this->current->instance and !$this->current->tobemigrated) {
156 $draftitemid = file_get_submitted_draft_itemid('files');
64f93798 157 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_resource', 'content', 0, array('subdirs'=>true));
aa54ed7b 158 $default_values['files'] = $draftitemid;
159 }
160 if (!empty($default_values['displayoptions'])) {
161 $displayoptions = unserialize($default_values['displayoptions']);
162 if (isset($displayoptions['printintro'])) {
163 $default_values['printintro'] = $displayoptions['printintro'];
164 }
aa54ed7b 165 if (!empty($displayoptions['popupwidth'])) {
166 $default_values['popupwidth'] = $displayoptions['popupwidth'];
167 }
168 if (!empty($displayoptions['popupheight'])) {
169 $default_values['popupheight'] = $displayoptions['popupheight'];
170 }
a2c57666 171 if (!empty($displayoptions['showsize'])) {
172 $default_values['showsize'] = $displayoptions['showsize'];
173 } else {
174 // Must set explicitly to 0 here otherwise it will use system
175 // default which may be 1.
176 $default_values['showsize'] = 0;
177 }
178 if (!empty($displayoptions['showtype'])) {
179 $default_values['showtype'] = $displayoptions['showtype'];
180 } else {
181 $default_values['showtype'] = 0;
182 }
ba8b641d
AH
183 if (!empty($displayoptions['showdate'])) {
184 $default_values['showdate'] = $displayoptions['showdate'];
185 } else {
186 $default_values['showdate'] = 0;
187 }
aa54ed7b 188 }
26de8d35 189 }
190
aa54ed7b 191 function definition_after_data() {
192 if ($this->current->instance and $this->current->tobemigrated) {
193 // resource not migrated yet
194 return;
195 }
196
197 parent::definition_after_data();
26de8d35 198 }
199
aa54ed7b 200 function validation($data, $files) {
201 global $USER;
202
203 $errors = parent::validation($data, $files);
204
bf0f06b1 205 $usercontext = context_user::instance($USER->id);
aa54ed7b 206 $fs = get_file_storage();
64f93798 207 if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['files'], 'sortorder, id', false)) {
aa54ed7b 208 $errors['files'] = get_string('required');
209 return $errors;
210 }
211 if (count($files) == 1) {
212 // no need to select main file if only one picked
213 return $errors;
f79321f1 214 } else if(count($files) > 1) {
f79321f1
DC
215 $mainfile = false;
216 foreach($files as $file) {
217 if ($file->get_sortorder() == 1) {
218 $mainfile = true;
219 break;
220 }
221 }
c0cc6daf 222 // set a default main file
f79321f1 223 if (!$mainfile) {
c0cc6daf 224 $file = reset($files);
64f93798 225 file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(),
c0cc6daf 226 $file->get_filepath(), $file->get_filename(), 1);
f79321f1 227 }
aa54ed7b 228 }
229 return $errors;
230 }
26de8d35 231}