MDL-40897 avoid extra DB queries in groups_get_activity_groupmode()
[moodle.git] / mod / url / mod_form.php
blob5abbce4f283a51f9d867bbe87bb30eefbd295a60
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 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
49 $this->add_intro_editor($config->requiremodintro);
51 //-------------------------------------------------------
52 $mform->addElement('header', 'content', get_string('contentheader', 'url'));
53 $mform->addElement('url', 'externalurl', get_string('externalurl', 'url'), array('size'=>'60'), array('usefilepicker'=>true));
54 $mform->setType('externalurl', PARAM_URL);
55 $mform->addRule('externalurl', null, 'required', null, 'client');
56 $mform->setExpanded('content');
58 //-------------------------------------------------------
59 $mform->addElement('header', 'optionssection', get_string('appearance'));
61 if ($this->current->instance) {
62 $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
63 } else {
64 $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
66 if (count($options) == 1) {
67 $mform->addElement('hidden', 'display');
68 $mform->setType('display', PARAM_INT);
69 reset($options);
70 $mform->setDefault('display', key($options));
71 } else {
72 $mform->addElement('select', 'display', get_string('displayselect', 'url'), $options);
73 $mform->setDefault('display', $config->display);
74 $mform->addHelpButton('display', 'displayselect', 'url');
77 if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
78 $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'url'), array('size'=>3));
79 if (count($options) > 1) {
80 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
82 $mform->setType('popupwidth', PARAM_INT);
83 $mform->setDefault('popupwidth', $config->popupwidth);
85 $mform->addElement('text', 'popupheight', get_string('popupheight', 'url'), array('size'=>3));
86 if (count($options) > 1) {
87 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
89 $mform->setType('popupheight', PARAM_INT);
90 $mform->setDefault('popupheight', $config->popupheight);
93 if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
94 array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
95 array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
96 $mform->addElement('checkbox', 'printheading', get_string('printheading', 'url'));
97 $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
98 $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
99 $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
100 $mform->setDefault('printheading', $config->printheading);
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);
109 //-------------------------------------------------------
110 $mform->addElement('header', 'parameterssection', get_string('parametersheader', 'url'));
111 $mform->addElement('static', 'parametersinfo', '', get_string('parametersheader_help', 'url'));
113 if (empty($this->current->parameters)) {
114 $parcount = 5;
115 } else {
116 $parcount = 5 + count(unserialize($this->current->parameters));
117 $parcount = ($parcount > 100) ? 100 : $parcount;
119 $options = url_get_variable_options($config);
121 for ($i=0; $i < $parcount; $i++) {
122 $parameter = "parameter_$i";
123 $variable = "variable_$i";
124 $pargroup = "pargoup_$i";
125 $group = array(
126 $mform->createElement('text', $parameter, '', array('size'=>'12')),
127 $mform->createElement('selectgroups', $variable, '', $options),
129 $mform->addGroup($group, $pargroup, get_string('parameterinfo', 'url'), ' ', false);
130 $mform->setType($parameter, PARAM_RAW);
133 //-------------------------------------------------------
134 $this->standard_coursemodule_elements();
136 //-------------------------------------------------------
137 $this->add_action_buttons();
140 function data_preprocessing(&$default_values) {
141 if (!empty($default_values['displayoptions'])) {
142 $displayoptions = unserialize($default_values['displayoptions']);
143 if (isset($displayoptions['printintro'])) {
144 $default_values['printintro'] = $displayoptions['printintro'];
146 if (isset($displayoptions['printheading'])) {
147 $default_values['printheading'] = $displayoptions['printheading'];
149 if (!empty($displayoptions['popupwidth'])) {
150 $default_values['popupwidth'] = $displayoptions['popupwidth'];
152 if (!empty($displayoptions['popupheight'])) {
153 $default_values['popupheight'] = $displayoptions['popupheight'];
156 if (!empty($default_values['parameters'])) {
157 $parameters = unserialize($default_values['parameters']);
158 $i = 0;
159 foreach ($parameters as $parameter=>$variable) {
160 $default_values['parameter_'.$i] = $parameter;
161 $default_values['variable_'.$i] = $variable;
162 $i++;
167 function validation($data, $files) {
168 $errors = parent::validation($data, $files);
170 // Validating Entered url, we are looking for obvious problems only,
171 // teachers are responsible for testing if it actually works.
173 // This is not a security validation!! Teachers are allowed to enter "javascript:alert(666)" for example.
175 // NOTE: do not try to explain the difference between URL and URI, people would be only confused...
177 if (empty($data['externalurl'])) {
178 $errors['externalurl'] = get_string('required');
180 } else {
181 $url = trim($data['externalurl']);
182 if (empty($url)) {
183 $errors['externalurl'] = get_string('required');
185 } else if (preg_match('|^/|', $url)) {
186 // links relative to server root are ok - no validation necessary
188 } else if (preg_match('|^[a-z]+://|i', $url) or preg_match('|^https?:|i', $url) or preg_match('|^ftp:|i', $url)) {
189 // normal URL
190 if (!url_appears_valid_url($url)) {
191 $errors['externalurl'] = get_string('invalidurl', 'url');
194 } else if (preg_match('|^[a-z]+:|i', $url)) {
195 // general URI such as teamspeak, mailto, etc. - it may or may not work in all browsers,
196 // we do not validate these at all, sorry
198 } else {
199 // invalid URI, we try to fix it by adding 'http://' prefix,
200 // relative links are NOT allowed because we display the link on different pages!
201 if (!url_appears_valid_url('http://'.$url)) {
202 $errors['externalurl'] = get_string('invalidurl', 'url');
206 return $errors;