MDL-51267 mod_url: make the url and resource forms friendlier
[moodle.git] / mod / url / mod_form.php
CommitLineData
58a27a74 1<?php
2
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 * URL configuration form
20 *
11e41f1a 21 * @package mod_url
2b641d15
PS
22 * @copyright 2009 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
58a27a74 24 */
25
2b641d15 26defined('MOODLE_INTERNAL') || die;
bfebaf64 27
58a27a74 28require_once ($CFG->dirroot.'/course/moodleform_mod.php');
29require_once($CFG->dirroot.'/mod/url/locallib.php');
30
31class mod_url_mod_form extends moodleform_mod {
32 function definition() {
33 global $CFG, $DB;
34 $mform = $this->_form;
35
36 $config = get_config('url');
37
38 //-------------------------------------------------------
39 $mform->addElement('header', 'general', get_string('general', 'form'));
40 $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
41 if (!empty($CFG->formatstringstriptags)) {
42 $mform->setType('name', PARAM_TEXT);
43 } else {
b8ea3041 44 $mform->setType('name', PARAM_CLEANHTML);
58a27a74 45 }
46 $mform->addRule('name', null, 'required', null, 'client');
a74cd331 47 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
e5dd8e3b 48 $mform->addElement('url', 'externalurl', get_string('externalurl', 'url'), array('size'=>'60'), array('usefilepicker'=>true));
47164284 49 $mform->setType('externalurl', PARAM_RAW_TRIMMED);
ec3c9dad 50 $mform->addRule('externalurl', null, 'required', null, 'client');
819e1ff0 51 $this->standard_intro_elements(null, array('rows' => 5));
5c5317ae 52
58a27a74 53 //-------------------------------------------------------
9cf26d08 54 $mform->addElement('header', 'optionssection', get_string('appearance'));
58a27a74 55
56 if ($this->current->instance) {
57 $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
58 } else {
59 $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
60 }
61 if (count($options) == 1) {
62 $mform->addElement('hidden', 'display');
d18e0fe6 63 $mform->setType('display', PARAM_INT);
58a27a74 64 reset($options);
65 $mform->setDefault('display', key($options));
66 } else {
67 $mform->addElement('select', 'display', get_string('displayselect', 'url'), $options);
68 $mform->setDefault('display', $config->display);
a4330d14 69 $mform->addHelpButton('display', 'displayselect', 'url');
58a27a74 70 }
71
72 if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
73 $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'url'), array('size'=>3));
74 if (count($options) > 1) {
75 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
76 }
77 $mform->setType('popupwidth', PARAM_INT);
78 $mform->setDefault('popupwidth', $config->popupwidth);
58a27a74 79
80 $mform->addElement('text', 'popupheight', get_string('popupheight', 'url'), array('size'=>3));
81 if (count($options) > 1) {
82 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
83 }
84 $mform->setType('popupheight', PARAM_INT);
85 $mform->setDefault('popupheight', $config->popupheight);
58a27a74 86 }
87
88 if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
89 array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
90 array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
58a27a74 91 $mform->addElement('checkbox', 'printintro', get_string('printintro', 'url'));
92 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
93 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
94 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
95 $mform->setDefault('printintro', $config->printintro);
58a27a74 96 }
97
98 //-------------------------------------------------------
99 $mform->addElement('header', 'parameterssection', get_string('parametersheader', 'url'));
f08e5e98 100 $mform->addElement('static', 'parametersinfo', '', get_string('parametersheader_help', 'url'));
58a27a74 101
102 if (empty($this->current->parameters)) {
103 $parcount = 5;
104 } else {
105 $parcount = 5 + count(unserialize($this->current->parameters));
106 $parcount = ($parcount > 100) ? 100 : $parcount;
107 }
108 $options = url_get_variable_options($config);
109
110 for ($i=0; $i < $parcount; $i++) {
111 $parameter = "parameter_$i";
112 $variable = "variable_$i";
113 $pargroup = "pargoup_$i";
114 $group = array(
115 $mform->createElement('text', $parameter, '', array('size'=>'12')),
116 $mform->createElement('selectgroups', $variable, '', $options),
117 );
118 $mform->addGroup($group, $pargroup, get_string('parameterinfo', 'url'), ' ', false);
914077b4 119 $mform->setType($parameter, PARAM_RAW);
58a27a74 120 }
121
122 //-------------------------------------------------------
123 $this->standard_coursemodule_elements();
124
125 //-------------------------------------------------------
126 $this->add_action_buttons();
127 }
128
129 function data_preprocessing(&$default_values) {
130 if (!empty($default_values['displayoptions'])) {
131 $displayoptions = unserialize($default_values['displayoptions']);
132 if (isset($displayoptions['printintro'])) {
133 $default_values['printintro'] = $displayoptions['printintro'];
134 }
58a27a74 135 if (!empty($displayoptions['popupwidth'])) {
136 $default_values['popupwidth'] = $displayoptions['popupwidth'];
137 }
138 if (!empty($displayoptions['popupheight'])) {
139 $default_values['popupheight'] = $displayoptions['popupheight'];
140 }
141 }
142 if (!empty($default_values['parameters'])) {
143 $parameters = unserialize($default_values['parameters']);
144 $i = 0;
145 foreach ($parameters as $parameter=>$variable) {
146 $default_values['parameter_'.$i] = $parameter;
147 $default_values['variable_'.$i] = $variable;
148 $i++;
149 }
150 }
151 }
152
4373200f
AKA
153 function validation($data, $files) {
154 $errors = parent::validation($data, $files);
48f69e41
PS
155
156 // Validating Entered url, we are looking for obvious problems only,
157 // teachers are responsible for testing if it actually works.
158
159 // This is not a security validation!! Teachers are allowed to enter "javascript:alert(666)" for example.
160
161 // NOTE: do not try to explain the difference between URL and URI, people would be only confused...
162
47164284
MG
163 if (!empty($data['externalurl'])) {
164 $url = $data['externalurl'];
165 if (preg_match('|^/|', $url)) {
48f69e41
PS
166 // links relative to server root are ok - no validation necessary
167
168 } else if (preg_match('|^[a-z]+://|i', $url) or preg_match('|^https?:|i', $url) or preg_match('|^ftp:|i', $url)) {
169 // normal URL
170 if (!url_appears_valid_url($url)) {
171 $errors['externalurl'] = get_string('invalidurl', 'url');
172 }
173
174 } else if (preg_match('|^[a-z]+:|i', $url)) {
175 // general URI such as teamspeak, mailto, etc. - it may or may not work in all browsers,
176 // we do not validate these at all, sorry
177
178 } else {
179 // invalid URI, we try to fix it by adding 'http://' prefix,
180 // relative links are NOT allowed because we display the link on different pages!
181 if (!url_appears_valid_url('http://'.$url)) {
182 $errors['externalurl'] = get_string('invalidurl', 'url');
183 }
184 }
4373200f
AKA
185 }
186 return $errors;
187 }
188
58a27a74 189}