PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/calendar/managesubscriptions_form.php

https://bitbucket.org/moodle/moodle
PHP | 160 lines | 84 code | 22 blank | 54 comment | 14 complexity | f32ac0ff8d63e9447116681d2cc8c997 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-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. * Allows the user to manage calendar subscriptions.
  18. *
  19. * @copyright 2012 Jonathan Harker
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package calendar
  22. */
  23. if (!defined('MOODLE_INTERNAL')) {
  24. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  25. }
  26. require_once($CFG->libdir.'/formslib.php');
  27. /**
  28. * Form for adding a subscription to a Moodle course calendar.
  29. * @copyright 2012 Jonathan Harker
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class calendar_addsubscription_form extends moodleform {
  33. /**
  34. * Defines the form used to add calendar subscriptions.
  35. */
  36. public function definition() {
  37. $mform = $this->_form;
  38. $courseid = optional_param('course', 0, PARAM_INT);
  39. $mform->addElement('header', 'addsubscriptionform', get_string('importcalendarheading', 'calendar'));
  40. // Name.
  41. $mform->addElement('text', 'name', get_string('subscriptionname', 'calendar'), array('maxsize' => '255', 'size' => '40'));
  42. $mform->addRule('name', get_string('required'), 'required');
  43. $mform->setType('name', PARAM_TEXT);
  44. // Import from (url | importfile).
  45. $mform->addElement('html', get_string('importfrominstructions', 'calendar'));
  46. $choices = array(CALENDAR_IMPORT_FROM_FILE => get_string('importfromfile', 'calendar'),
  47. CALENDAR_IMPORT_FROM_URL => get_string('importfromurl', 'calendar'));
  48. $mform->addElement('select', 'importfrom', get_string('importcalendarfrom', 'calendar'), $choices);
  49. $mform->setDefault('importfrom', CALENDAR_IMPORT_FROM_URL);
  50. // URL.
  51. $mform->addElement('text', 'url', get_string('importfromurl', 'calendar'), array('maxsize' => '255', 'size' => '50'));
  52. // Cannot set as PARAM_URL since we need to allow webcal:// protocol.
  53. $mform->setType('url', PARAM_RAW);
  54. $mform->setForceLtr('url');
  55. // Poll interval
  56. $choices = calendar_get_pollinterval_choices();
  57. $mform->addElement('select', 'pollinterval', get_string('pollinterval', 'calendar'), $choices);
  58. $mform->setDefault('pollinterval', 604800);
  59. $mform->addHelpButton('pollinterval', 'pollinterval', 'calendar');
  60. $mform->setType('pollinterval', PARAM_INT);
  61. // Import file
  62. $mform->addElement('filepicker', 'importfile', get_string('importfromfile', 'calendar'), null, array('accepted_types' => '.ics'));
  63. // Disable appropriate elements depending on import from value.
  64. $mform->disabledIf('pollinterval', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_FILE);
  65. $mform->disabledIf('url', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_FILE);
  66. $mform->disabledIf('importfile', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_URL);
  67. // Eventtype: 0 = user, 1 = global, anything else = course ID.
  68. list($choices, $groups) = calendar_get_eventtype_choices($courseid);
  69. $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $choices);
  70. $mform->addRule('eventtype', get_string('required'), 'required');
  71. $mform->setType('eventtype', PARAM_ALPHA);
  72. if (!empty($groups) and is_array($groups)) {
  73. $groupoptions = array();
  74. foreach ($groups as $group) {
  75. $groupoptions[$group->id] = $group->name;
  76. }
  77. $mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions);
  78. $mform->setType('groupid', PARAM_INT);
  79. $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
  80. }
  81. $mform->addElement('hidden', 'course');
  82. $mform->setType('course', PARAM_INT);
  83. $mform->addElement('submit', 'add', get_string('add'));
  84. }
  85. /**
  86. * Validates the returned data.
  87. *
  88. * @param array $data
  89. * @param array $files
  90. * @return array
  91. */
  92. public function validation($data, $files) {
  93. global $USER;
  94. $errors = parent::validation($data, $files);
  95. if ($data['importfrom'] == CALENDAR_IMPORT_FROM_FILE) {
  96. if (empty($data['importfile'])) {
  97. $errors['importfile'] = get_string('errorrequiredurlorfile', 'calendar');
  98. } else {
  99. // Make sure the file area is not empty and contains only one file.
  100. $draftitemid = $data['importfile'];
  101. $fs = get_file_storage();
  102. $usercontext = context_user::instance($USER->id);
  103. $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false);
  104. if (count($files) !== 1) {
  105. $errors['importfile'] = get_string('errorrequiredurlorfile', 'calendar');
  106. }
  107. }
  108. } else if (($data['importfrom'] == CALENDAR_IMPORT_FROM_URL)) {
  109. // Clean input calendar url.
  110. $url = clean_param($data['url'], PARAM_URL);
  111. if (empty($url) || ($url !== $data['url'])) {
  112. $errors['url'] = get_string('invalidurl', 'error');
  113. }
  114. } else {
  115. // Shouldn't happen.
  116. $errors['url'] = get_string('errorrequiredurlorfile', 'calendar');
  117. }
  118. return $errors;
  119. }
  120. public function definition_after_data() {
  121. $mform =& $this->_form;
  122. $mform->applyFilter('url', 'calendar_addsubscription_form::strip_webcal');
  123. $mform->applyFilter('url', 'trim');
  124. }
  125. /**
  126. * Replace webcal:// urls with http:// as
  127. * curl does not understand this protocol
  128. *
  129. * @param string @url url to examine
  130. * @return string url with webcal:// replaced
  131. */
  132. public static function strip_webcal($url) {
  133. if (strpos($url, 'webcal://') === 0) {
  134. $url = str_replace('webcal://', 'http://', $url);
  135. }
  136. return $url;
  137. }
  138. }