Merge branch 'MDL-47037-27' of git://github.com/jethac/moodle into MOODLE_27_STABLE
[moodle.git] / calendar / managesubscriptions_form.php
blob769a5b5408ca0bbbc266123cd5c3d4012d4533f6
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.
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/>.
17 /**
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
22 * @package calendar
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');
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
36 class calendar_addsubscription_form extends moodleform {
38 /**
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'));
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);
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);
59 // 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);
64 // Poll interval
65 $choices = calendar_get_pollinterval_choices();
66 $mform->addElement('select', 'pollinterval', get_string('pollinterval', 'calendar'), $choices);
67 $mform->setDefault('pollinterval', 604800);
68 $mform->addHelpButton('pollinterval', 'pollinterval', 'calendar');
69 $mform->setType('pollinterval', PARAM_INT);
71 // Import file
72 $mform->addElement('filepicker', 'importfile', get_string('importfromfile', 'calendar'), null, array('accepted_types' => '.ics'));
74 // Disable appropriate elements depending on import from value.
75 $mform->disabledIf('pollinterval', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_FILE);
76 $mform->disabledIf('url', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_FILE);
77 $mform->disabledIf('importfile', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_URL);
79 // Eventtype: 0 = user, 1 = global, anything else = course ID.
80 list($choices, $groups) = calendar_get_eventtype_choices($courseid);
81 $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $choices);
82 $mform->addRule('eventtype', get_string('required'), 'required');
83 $mform->setType('eventtype', PARAM_ALPHA);
85 if (!empty($groups) and is_array($groups)) {
86 $groupoptions = array();
87 foreach ($groups as $group) {
88 $groupoptions[$group->id] = $group->name;
90 $mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions);
91 $mform->setType('groupid', PARAM_INT);
92 $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
95 $mform->addElement('hidden', 'course');
96 $mform->setType('course', PARAM_INT);
97 $mform->addElement('submit', 'add', get_string('add'));
101 * Validates the returned data.
103 * @param array $data
104 * @param array $files
105 * @return array
107 public function validation($data, $files) {
108 global $USER;
110 $errors = parent::validation($data, $files);
112 if ($data['importfrom'] == CALENDAR_IMPORT_FROM_FILE) {
113 if (empty($data['importfile'])) {
114 $errors['importfile'] = get_string('errorrequiredurlorfile', 'calendar');
115 } else {
116 // Make sure the file area is not empty and contains only one file.
117 $draftitemid = $data['importfile'];
118 $fs = get_file_storage();
119 $usercontext = context_user::instance($USER->id);
120 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false);
121 if (count($files) !== 1) {
122 $errors['importfile'] = get_string('errorrequiredurlorfile', 'calendar');
125 } else if (($data['importfrom'] == CALENDAR_IMPORT_FROM_URL)) {
126 // Clean input calendar url.
127 $url = clean_param($data['url'], PARAM_URL);
128 if (empty($url) || ($url !== $data['url'])) {
129 $errors['url'] = get_string('invalidurl', 'error');
131 } else {
132 // Shouldn't happen.
133 $errors['url'] = get_string('errorrequiredurlorfile', 'calendar');
136 return $errors;
139 public function definition_after_data() {
140 $mform =& $this->_form;
142 $mform->applyFilter('url', 'calendar_addsubscription_form::strip_webcal');
143 $mform->applyFilter('url', 'trim');
147 * Replace webcal:// urls with http:// as
148 * curl does not understand this protocol
150 * @param string @url url to examine
151 * @return string url with webcal:// replaced
153 public static function strip_webcal($url) {
154 if (strpos($url, 'webcal://') === 0) {
155 $url = str_replace('webcal://', 'http://', $url);
157 return $url;