PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/url/mod_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 194 lines | 128 code | 28 blank | 38 comment | 29 complexity | c7c2cbd20593f0e24b632256cfa795f1 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * URL configuration form
  18. *
  19. * @package mod
  20. * @subpackage url
  21. * @copyright 2009 Petr Skoda {@link http://skodak.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die;
  25. require_once ($CFG->dirroot.'/course/moodleform_mod.php');
  26. require_once($CFG->dirroot.'/mod/url/locallib.php');
  27. class mod_url_mod_form extends moodleform_mod {
  28. function definition() {
  29. global $CFG, $DB;
  30. $mform = $this->_form;
  31. $config = get_config('url');
  32. //-------------------------------------------------------
  33. $mform->addElement('header', 'general', get_string('general', 'form'));
  34. $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
  35. if (!empty($CFG->formatstringstriptags)) {
  36. $mform->setType('name', PARAM_TEXT);
  37. } else {
  38. $mform->setType('name', PARAM_CLEANHTML);
  39. }
  40. $mform->addRule('name', null, 'required', null, 'client');
  41. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  42. $this->add_intro_editor($config->requiremodintro);
  43. //-------------------------------------------------------
  44. $mform->addElement('header', 'content', get_string('contentheader', 'url'));
  45. $mform->addElement('url', 'externalurl', get_string('externalurl', 'url'), array('size'=>'60'), array('usefilepicker'=>true));
  46. $mform->setType('externalurl', PARAM_RAW_TRIMMED);
  47. $mform->addRule('externalurl', null, 'required', null, 'client');
  48. $mform->setExpanded('content');
  49. //-------------------------------------------------------
  50. $mform->addElement('header', 'optionssection', get_string('appearance'));
  51. if ($this->current->instance) {
  52. $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
  53. } else {
  54. $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
  55. }
  56. if (count($options) == 1) {
  57. $mform->addElement('hidden', 'display');
  58. $mform->setType('display', PARAM_INT);
  59. reset($options);
  60. $mform->setDefault('display', key($options));
  61. } else {
  62. $mform->addElement('select', 'display', get_string('displayselect', 'url'), $options);
  63. $mform->setDefault('display', $config->display);
  64. $mform->addHelpButton('display', 'displayselect', 'url');
  65. }
  66. if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
  67. $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'url'), array('size'=>3));
  68. if (count($options) > 1) {
  69. $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  70. }
  71. $mform->setType('popupwidth', PARAM_INT);
  72. $mform->setDefault('popupwidth', $config->popupwidth);
  73. $mform->addElement('text', 'popupheight', get_string('popupheight', 'url'), array('size'=>3));
  74. if (count($options) > 1) {
  75. $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  76. }
  77. $mform->setType('popupheight', PARAM_INT);
  78. $mform->setDefault('popupheight', $config->popupheight);
  79. }
  80. if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
  81. array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
  82. array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
  83. $mform->addElement('checkbox', 'printintro', get_string('printintro', 'url'));
  84. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
  85. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
  86. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
  87. $mform->setDefault('printintro', $config->printintro);
  88. }
  89. //-------------------------------------------------------
  90. $mform->addElement('header', 'parameterssection', get_string('parametersheader', 'url'));
  91. $mform->addElement('static', 'parametersinfo', '', get_string('parametersheader_help', 'url'));
  92. if (empty($this->current->parameters)) {
  93. $parcount = 5;
  94. } else {
  95. $parcount = 5 + count(unserialize($this->current->parameters));
  96. $parcount = ($parcount > 100) ? 100 : $parcount;
  97. }
  98. $options = url_get_variable_options($config);
  99. for ($i=0; $i < $parcount; $i++) {
  100. $parameter = "parameter_$i";
  101. $variable = "variable_$i";
  102. $pargroup = "pargoup_$i";
  103. $group = array(
  104. $mform->createElement('text', $parameter, '', array('size'=>'12')),
  105. $mform->createElement('selectgroups', $variable, '', $options),
  106. );
  107. $mform->addGroup($group, $pargroup, get_string('parameterinfo', 'url'), ' ', false);
  108. $mform->setType($parameter, PARAM_RAW);
  109. }
  110. //-------------------------------------------------------
  111. $this->standard_coursemodule_elements();
  112. //-------------------------------------------------------
  113. $this->add_action_buttons();
  114. }
  115. function data_preprocessing(&$default_values) {
  116. if (!empty($default_values['displayoptions'])) {
  117. $displayoptions = unserialize($default_values['displayoptions']);
  118. if (isset($displayoptions['printintro'])) {
  119. $default_values['printintro'] = $displayoptions['printintro'];
  120. }
  121. if (!empty($displayoptions['popupwidth'])) {
  122. $default_values['popupwidth'] = $displayoptions['popupwidth'];
  123. }
  124. if (!empty($displayoptions['popupheight'])) {
  125. $default_values['popupheight'] = $displayoptions['popupheight'];
  126. }
  127. }
  128. if (!empty($default_values['parameters'])) {
  129. $parameters = unserialize($default_values['parameters']);
  130. $i = 0;
  131. foreach ($parameters as $parameter=>$variable) {
  132. $default_values['parameter_'.$i] = $parameter;
  133. $default_values['variable_'.$i] = $variable;
  134. $i++;
  135. }
  136. }
  137. }
  138. function validation($data, $files) {
  139. $errors = parent::validation($data, $files);
  140. // Validating Entered url, we are looking for obvious problems only,
  141. // teachers are responsible for testing if it actually works.
  142. // This is not a security validation!! Teachers are allowed to enter "javascript:alert(666)" for example.
  143. // NOTE: do not try to explain the difference between URL and URI, people would be only confused...
  144. if (!empty($data['externalurl'])) {
  145. $url = $data['externalurl'];
  146. if (preg_match('|^/|', $url)) {
  147. // links relative to server root are ok - no validation necessary
  148. } else if (preg_match('|^[a-z]+://|i', $url) or preg_match('|^https?:|i', $url) or preg_match('|^ftp:|i', $url)) {
  149. // normal URL
  150. if (!url_appears_valid_url($url)) {
  151. $errors['externalurl'] = get_string('invalidurl', 'url');
  152. }
  153. } else if (preg_match('|^[a-z]+:|i', $url)) {
  154. // general URI such as teamspeak, mailto, etc. - it may or may not work in all browsers,
  155. // we do not validate these at all, sorry
  156. } else {
  157. // invalid URI, we try to fix it by adding 'http://' prefix,
  158. // relative links are NOT allowed because we display the link on different pages!
  159. if (!url_appears_valid_url('http://'.$url)) {
  160. $errors['externalurl'] = get_string('invalidurl', 'url');
  161. }
  162. }
  163. }
  164. return $errors;
  165. }
  166. }