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