Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/url/mod_form.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die;
00028 
00029 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
00030 require_once($CFG->dirroot.'/mod/url/locallib.php');
00031 
00032 class mod_url_mod_form extends moodleform_mod {
00033     function definition() {
00034         global $CFG, $DB;
00035         $mform = $this->_form;
00036 
00037         $config = get_config('url');
00038 
00039         //-------------------------------------------------------
00040         $mform->addElement('header', 'general', get_string('general', 'form'));
00041         $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
00042         if (!empty($CFG->formatstringstriptags)) {
00043             $mform->setType('name', PARAM_TEXT);
00044         } else {
00045             $mform->setType('name', PARAM_CLEANHTML);
00046         }
00047         $mform->addRule('name', null, 'required', null, 'client');
00048         $this->add_intro_editor($config->requiremodintro);
00049 
00050         //-------------------------------------------------------
00051         $mform->addElement('header', 'content', get_string('contentheader', 'url'));
00052         $mform->addElement('url', 'externalurl', get_string('externalurl', 'url'), array('size'=>'60'), array('usefilepicker'=>true));
00053         $mform->addRule('externalurl', null, 'required', null, 'client');
00054         //-------------------------------------------------------
00055         $mform->addElement('header', 'optionssection', get_string('optionsheader', 'url'));
00056 
00057         if ($this->current->instance) {
00058             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
00059         } else {
00060             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
00061         }
00062         if (count($options) == 1) {
00063             $mform->addElement('hidden', 'display');
00064             $mform->setType('display', PARAM_INT);
00065             reset($options);
00066             $mform->setDefault('display', key($options));
00067         } else {
00068             $mform->addElement('select', 'display', get_string('displayselect', 'url'), $options);
00069             $mform->setDefault('display', $config->display);
00070             $mform->setAdvanced('display', $config->display_adv);
00071             $mform->addHelpButton('display', 'displayselect', 'url');
00072         }
00073 
00074         if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
00075             $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'url'), array('size'=>3));
00076             if (count($options) > 1) {
00077                 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
00078             }
00079             $mform->setType('popupwidth', PARAM_INT);
00080             $mform->setDefault('popupwidth', $config->popupwidth);
00081             $mform->setAdvanced('popupwidth', $config->popupwidth_adv);
00082 
00083             $mform->addElement('text', 'popupheight', get_string('popupheight', 'url'), array('size'=>3));
00084             if (count($options) > 1) {
00085                 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
00086             }
00087             $mform->setType('popupheight', PARAM_INT);
00088             $mform->setDefault('popupheight', $config->popupheight);
00089             $mform->setAdvanced('popupheight', $config->popupheight_adv);
00090         }
00091 
00092         if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
00093           array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
00094           array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
00095             $mform->addElement('checkbox', 'printheading', get_string('printheading', 'url'));
00096             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
00097             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
00098             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
00099             $mform->setDefault('printheading', $config->printheading);
00100             $mform->setAdvanced('printheading', $config->printheading_adv);
00101 
00102             $mform->addElement('checkbox', 'printintro', get_string('printintro', 'url'));
00103             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
00104             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
00105             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
00106             $mform->setDefault('printintro', $config->printintro);
00107             $mform->setAdvanced('printintro', $config->printintro_adv);
00108         }
00109 
00110         //-------------------------------------------------------
00111         $mform->addElement('header', 'parameterssection', get_string('parametersheader', 'url'));
00112         $mform->addElement('static', 'parametersinfo', '', get_string('parametersheader_help', 'url'));
00113         $mform->setAdvanced('parametersinfo');
00114 
00115         if (empty($this->current->parameters)) {
00116             $parcount = 5;
00117         } else {
00118             $parcount = 5 + count(unserialize($this->current->parameters));
00119             $parcount = ($parcount > 100) ? 100 : $parcount;
00120         }
00121         $options = url_get_variable_options($config);
00122 
00123         for ($i=0; $i < $parcount; $i++) {
00124             $parameter = "parameter_$i";
00125             $variable  = "variable_$i";
00126             $pargroup = "pargoup_$i";
00127             $group = array(
00128                 $mform->createElement('text', $parameter, '', array('size'=>'12')),
00129                 $mform->createElement('selectgroups', $variable, '', $options),
00130             );
00131             $mform->addGroup($group, $pargroup, get_string('parameterinfo', 'url'), ' ', false);
00132             $mform->setAdvanced($pargroup);
00133         }
00134 
00135         //-------------------------------------------------------
00136         $this->standard_coursemodule_elements();
00137 
00138         //-------------------------------------------------------
00139         $this->add_action_buttons();
00140     }
00141 
00142     function data_preprocessing(&$default_values) {
00143         if (!empty($default_values['displayoptions'])) {
00144             $displayoptions = unserialize($default_values['displayoptions']);
00145             if (isset($displayoptions['printintro'])) {
00146                 $default_values['printintro'] = $displayoptions['printintro'];
00147             }
00148             if (isset($displayoptions['printheading'])) {
00149                 $default_values['printheading'] = $displayoptions['printheading'];
00150             }
00151             if (!empty($displayoptions['popupwidth'])) {
00152                 $default_values['popupwidth'] = $displayoptions['popupwidth'];
00153             }
00154             if (!empty($displayoptions['popupheight'])) {
00155                 $default_values['popupheight'] = $displayoptions['popupheight'];
00156             }
00157         }
00158         if (!empty($default_values['parameters'])) {
00159             $parameters = unserialize($default_values['parameters']);
00160             $i = 0;
00161             foreach ($parameters as $parameter=>$variable) {
00162                 $default_values['parameter_'.$i] = $parameter;
00163                 $default_values['variable_'.$i]  = $variable;
00164                 $i++;
00165             }
00166         }
00167     }
00168 
00169     function validation($data, $files) {
00170         $errors = parent::validation($data, $files);
00171 
00172         // Validating Entered url, we are looking for obvious problems only,
00173         // teachers are responsible for testing if it actually works.
00174 
00175         // This is not a security validation!! Teachers are allowed to enter "javascript:alert(666)" for example.
00176 
00177         // NOTE: do not try to explain the difference between URL and URI, people would be only confused...
00178 
00179         if (empty($data['externalurl'])) {
00180             $errors['externalurl'] = get_string('required');
00181 
00182         } else {
00183             $url = trim($data['externalurl']);
00184             if (empty($url)) {
00185                 $errors['externalurl'] = get_string('required');
00186 
00187             } else if (preg_match('|^/|', $url)) {
00188                 // links relative to server root are ok - no validation necessary
00189 
00190             } else if (preg_match('|^[a-z]+://|i', $url) or preg_match('|^https?:|i', $url) or preg_match('|^ftp:|i', $url)) {
00191                 // normal URL
00192                 if (!url_appears_valid_url($url)) {
00193                     $errors['externalurl'] = get_string('invalidurl', 'url');
00194                 }
00195 
00196             } else if (preg_match('|^[a-z]+:|i', $url)) {
00197                 // general URI such as teamspeak, mailto, etc. - it may or may not work in all browsers,
00198                 // we do not validate these at all, sorry
00199 
00200             } else {
00201                 // invalid URI, we try to fix it by adding 'http://' prefix,
00202                 // relative links are NOT allowed because we display the link on different pages!
00203                 if (!url_appears_valid_url('http://'.$url)) {
00204                     $errors['externalurl'] = get_string('invalidurl', 'url');
00205                 }
00206             }
00207         }
00208         return $errors;
00209     }
00210 
00211 }
 All Data Structures Namespaces Files Functions Variables Enumerations