Merge branch 'wip-MDL-29678-m21' of git://github.com/samhemelryk/moodle into MOODLE_2...
[moodle.git] / mod / url / mod_form.php
blob47d42512bb963cc90205c3f1f59645bf26ba6544
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.
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 * URL configuration form
21 * @package mod
22 * @subpackage url
23 * @copyright 2009 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die;
29 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
30 require_once($CFG->dirroot.'/mod/url/locallib.php');
32 class mod_url_mod_form extends moodleform_mod {
33 function definition() {
34 global $CFG, $DB;
35 $mform = $this->_form;
37 $config = get_config('url');
39 //-------------------------------------------------------
40 $mform->addElement('header', 'general', get_string('general', 'form'));
41 $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
42 if (!empty($CFG->formatstringstriptags)) {
43 $mform->setType('name', PARAM_TEXT);
44 } else {
45 $mform->setType('name', PARAM_CLEANHTML);
47 $mform->addRule('name', null, 'required', null, 'client');
48 $this->add_intro_editor($config->requiremodintro);
50 //-------------------------------------------------------
51 $mform->addElement('header', 'content', get_string('contentheader', 'url'));
52 $mform->addElement('url', 'externalurl', get_string('externalurl', 'url'), array('size'=>'60'), array('usefilepicker'=>true));
53 $mform->addRule('externalurl', null, 'required', null, 'client');
54 //-------------------------------------------------------
55 $mform->addElement('header', 'optionssection', get_string('optionsheader', 'url'));
57 if ($this->current->instance) {
58 $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
59 } else {
60 $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
62 if (count($options) == 1) {
63 $mform->addElement('hidden', 'display');
64 $mform->setType('display', PARAM_INT);
65 reset($options);
66 $mform->setDefault('display', key($options));
67 } else {
68 $mform->addElement('select', 'display', get_string('displayselect', 'url'), $options);
69 $mform->setDefault('display', $config->display);
70 $mform->setAdvanced('display', $config->display_adv);
71 $mform->addHelpButton('display', 'displayselect', 'url');
74 if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
75 $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'url'), array('size'=>3));
76 if (count($options) > 1) {
77 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
79 $mform->setType('popupwidth', PARAM_INT);
80 $mform->setDefault('popupwidth', $config->popupwidth);
81 $mform->setAdvanced('popupwidth', $config->popupwidth_adv);
83 $mform->addElement('text', 'popupheight', get_string('popupheight', 'url'), array('size'=>3));
84 if (count($options) > 1) {
85 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
87 $mform->setType('popupheight', PARAM_INT);
88 $mform->setDefault('popupheight', $config->popupheight);
89 $mform->setAdvanced('popupheight', $config->popupheight_adv);
92 if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
93 array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
94 array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
95 $mform->addElement('checkbox', 'printheading', get_string('printheading', 'url'));
96 $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
97 $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
98 $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
99 $mform->setDefault('printheading', $config->printheading);
100 $mform->setAdvanced('printheading', $config->printheading_adv);
102 $mform->addElement('checkbox', 'printintro', get_string('printintro', 'url'));
103 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
104 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
105 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
106 $mform->setDefault('printintro', $config->printintro);
107 $mform->setAdvanced('printintro', $config->printintro_adv);
110 //-------------------------------------------------------
111 $mform->addElement('header', 'parameterssection', get_string('parametersheader', 'url'));
112 $mform->addElement('static', 'parametersinfo', '', get_string('parametersheader_help', 'url'));
113 $mform->setAdvanced('parametersinfo');
115 if (empty($this->current->parameters)) {
116 $parcount = 5;
117 } else {
118 $parcount = 5 + count(unserialize($this->current->parameters));
119 $parcount = ($parcount > 100) ? 100 : $parcount;
121 $options = url_get_variable_options($config);
123 for ($i=0; $i < $parcount; $i++) {
124 $parameter = "parameter_$i";
125 $variable = "variable_$i";
126 $pargroup = "pargoup_$i";
127 $group = array(
128 $mform->createElement('text', $parameter, '', array('size'=>'12')),
129 $mform->createElement('selectgroups', $variable, '', $options),
131 $mform->addGroup($group, $pargroup, get_string('parameterinfo', 'url'), ' ', false);
132 $mform->setAdvanced($pargroup);
135 //-------------------------------------------------------
136 $this->standard_coursemodule_elements();
138 //-------------------------------------------------------
139 $this->add_action_buttons();
142 function data_preprocessing(&$default_values) {
143 if (!empty($default_values['displayoptions'])) {
144 $displayoptions = unserialize($default_values['displayoptions']);
145 if (isset($displayoptions['printintro'])) {
146 $default_values['printintro'] = $displayoptions['printintro'];
148 if (isset($displayoptions['printheading'])) {
149 $default_values['printheading'] = $displayoptions['printheading'];
151 if (!empty($displayoptions['popupwidth'])) {
152 $default_values['popupwidth'] = $displayoptions['popupwidth'];
154 if (!empty($displayoptions['popupheight'])) {
155 $default_values['popupheight'] = $displayoptions['popupheight'];
158 if (!empty($default_values['parameters'])) {
159 $parameters = unserialize($default_values['parameters']);
160 $i = 0;
161 foreach ($parameters as $parameter=>$variable) {
162 $default_values['parameter_'.$i] = $parameter;
163 $default_values['variable_'.$i] = $variable;
164 $i++;
169 function validation($data, $files) {
170 $errors = parent::validation($data, $files);
172 // Validating Entered url, we are looking for obvious problems only,
173 // teachers are responsible for testing if it actually works.
175 // This is not a security validation!! Teachers are allowed to enter "javascript:alert(666)" for example.
177 // NOTE: do not try to explain the difference between URL and URI, people would be only confused...
179 if (empty($data['externalurl'])) {
180 $errors['externalurl'] = get_string('required');
182 } else {
183 $url = trim($data['externalurl']);
184 if (empty($url)) {
185 $errors['externalurl'] = get_string('required');
187 } else if (preg_match('|^/|', $url)) {
188 // links relative to server root are ok - no validation necessary
190 } else if (preg_match('|^[a-z]+://|i', $url) or preg_match('|^https?:|i', $url) or preg_match('|^ftp:|i', $url)) {
191 // normal URL
192 if (!url_appears_valid_url($url)) {
193 $errors['externalurl'] = get_string('invalidurl', 'url');
196 } else if (preg_match('|^[a-z]+:|i', $url)) {
197 // general URI such as teamspeak, mailto, etc. - it may or may not work in all browsers,
198 // we do not validate these at all, sorry
200 } else {
201 // invalid URI, we try to fix it by adding 'http://' prefix,
202 // relative links are NOT allowed because we display the link on different pages!
203 if (!url_appears_valid_url('http://'.$url)) {
204 $errors['externalurl'] = get_string('invalidurl', 'url');
208 return $errors;