PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/calendar/managesubscriptions_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 159 lines | 83 code | 22 blank | 54 comment | 14 complexity | 5b8c1997b48333d9c32161ed5f43fd0a 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. * 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. // Poll interval
  55. $choices = calendar_get_pollinterval_choices();
  56. $mform->addElement('select', 'pollinterval', get_string('pollinterval', 'calendar'), $choices);
  57. $mform->setDefault('pollinterval', 604800);
  58. $mform->addHelpButton('pollinterval', 'pollinterval', 'calendar');
  59. $mform->setType('pollinterval', PARAM_INT);
  60. // Import file
  61. $mform->addElement('filepicker', 'importfile', get_string('importfromfile', 'calendar'), null, array('accepted_types' => '.ics'));
  62. // Disable appropriate elements depending on import from value.
  63. $mform->disabledIf('pollinterval', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_FILE);
  64. $mform->disabledIf('url', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_FILE);
  65. $mform->disabledIf('importfile', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_URL);
  66. // Eventtype: 0 = user, 1 = global, anything else = course ID.
  67. list($choices, $groups) = calendar_get_eventtype_choices($courseid);
  68. $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $choices);
  69. $mform->addRule('eventtype', get_string('required'), 'required');
  70. $mform->setType('eventtype', PARAM_ALPHA);
  71. if (!empty($groups) and is_array($groups)) {
  72. $groupoptions = array();
  73. foreach ($groups as $group) {
  74. $groupoptions[$group->id] = $group->name;
  75. }
  76. $mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions);
  77. $mform->setType('groupid', PARAM_INT);
  78. $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
  79. }
  80. $mform->addElement('hidden', 'course');
  81. $mform->setType('course', PARAM_INT);
  82. $mform->addElement('submit', 'add', get_string('add'));
  83. }
  84. /**
  85. * Validates the returned data.
  86. *
  87. * @param array $data
  88. * @param array $files
  89. * @return array
  90. */
  91. public function validation($data, $files) {
  92. global $USER;
  93. $errors = parent::validation($data, $files);
  94. if ($data['importfrom'] == CALENDAR_IMPORT_FROM_FILE) {
  95. if (empty($data['importfile'])) {
  96. $errors['importfile'] = get_string('errorrequiredurlorfile', 'calendar');
  97. } else {
  98. // Make sure the file area is not empty and contains only one file.
  99. $draftitemid = $data['importfile'];
  100. $fs = get_file_storage();
  101. $usercontext = context_user::instance($USER->id);
  102. $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false);
  103. if (count($files) !== 1) {
  104. $errors['importfile'] = get_string('errorrequiredurlorfile', 'calendar');
  105. }
  106. }
  107. } else if (($data['importfrom'] == CALENDAR_IMPORT_FROM_URL)) {
  108. // Clean input calendar url.
  109. $url = clean_param($data['url'], PARAM_URL);
  110. if (empty($url) || ($url !== $data['url'])) {
  111. $errors['url'] = get_string('invalidurl', 'error');
  112. }
  113. } else {
  114. // Shouldn't happen.
  115. $errors['url'] = get_string('errorrequiredurlorfile', 'calendar');
  116. }
  117. return $errors;
  118. }
  119. public function definition_after_data() {
  120. $mform =& $this->_form;
  121. $mform->applyFilter('url', 'calendar_addsubscription_form::strip_webcal');
  122. $mform->applyFilter('url', 'trim');
  123. }
  124. /**
  125. * Replace webcal:// urls with http:// as
  126. * curl does not understand this protocol
  127. *
  128. * @param string @url url to examine
  129. * @return string url with webcal:// replaced
  130. */
  131. public static function strip_webcal($url) {
  132. if (strpos($url, 'webcal://') === 0) {
  133. $url = str_replace('webcal://', 'http://', $url);
  134. }
  135. return $url;
  136. }
  137. }