2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Allows the user to manage calendar subscriptions.
20 * @copyright 2012 Jonathan Harker
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
29 require_once($CFG->libdir.'/formslib.php');
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
36 class calendar_addsubscription_form extends moodleform {
39 * Defines the form used to add calendar subscriptions.
41 public function definition() {
42 $mform = $this->_form;
43 $courseid = optional_param('course', 0, PARAM_INT);
45 $mform->addElement('header', 'addsubscriptionform', get_string('importcalendarheading', 'calendar'));
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);
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);
60 $mform->addElement('text', 'url', get_string('importfromurl', 'calendar'), array('maxsize' => '255', 'size' => '50'));
61 // Cannot set as PARAM_URL since we need to allow webcal:// protocol.
62 $mform->setType('url', PARAM_RAW);
65 $mform->addElement('filepicker', 'importfile', get_string('importfromfile', 'calendar'));
67 $mform->disabledIf('url', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_FILE);
68 $mform->disabledIf('importfile', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_URL);
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);
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);
83 if (!empty($groups) and is_array($groups)) {
84 $groupoptions = array();
85 foreach ($groups as $group) {
86 $groupoptions[$group->id] = $group->name;
88 $mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions);
89 $mform->setType('groupid', PARAM_INT);
90 $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
93 $mform->addElement('hidden', 'course');
94 $mform->addElement('hidden', 'sesskey', sesskey());
95 $mform->addElement('submit', 'add', get_string('add'));
99 * Validates the returned data.
102 * @param array $files
105 public function validation($data, $files) {
106 $errors = parent::validation($data, $files);
108 if (empty($url) && empty($data['importfile'])) {
109 if (!empty($data['importfrom']) && $data['importfrom'] == CALENDAR_IMPORT_FROM_FILE) {
110 $errors['importfile'] = get_string('errorrequiredurlorfile', 'calendar');
112 $errors['url'] = get_string('errorrequiredurlorfile', 'calendar');
114 } elseif (!empty($url)) {
115 // Url is webcal protocol which is not accepted by PARAM_URL.
116 if (stripos($url, "webcal://") === 0) {
117 $url = substr($url, strlen("webcal://"));
119 if (clean_param($url, PARAM_URL) !== $url) {
120 $errors['url'] = get_string('invalidurl', 'error');