MDL-57343 behat: Ensure we wait enough for page to be fully loaded
[moodle.git] / calendar / managesubscriptions_form.php
blob12ab3ac5b740e54d3f394578e0a9c57f2a0c4e8e
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);
63 $mform->setForceLtr('url');
65 // Poll interval
66 $choices = calendar_get_pollinterval_choices();
67 $mform->addElement('select', 'pollinterval', get_string('pollinterval', 'calendar'), $choices);
68 $mform->setDefault('pollinterval', 604800);
69 $mform->addHelpButton('pollinterval', 'pollinterval', 'calendar');
70 $mform->setType('pollinterval', PARAM_INT);
72 // Import file
73 $mform->addElement('filepicker', 'importfile', get_string('importfromfile', 'calendar'), null, array('accepted_types' => '.ics'));
75 // Disable appropriate elements depending on import from value.
76 $mform->disabledIf('pollinterval', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_FILE);
77 $mform->disabledIf('url', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_FILE);
78 $mform->disabledIf('importfile', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_URL);
80 // Eventtype: 0 = user, 1 = global, anything else = course ID.
81 list($choices, $groups) = calendar_get_eventtype_choices($courseid);
82 $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $choices);
83 $mform->addRule('eventtype', get_string('required'), 'required');
84 $mform->setType('eventtype', PARAM_ALPHA);
86 if (!empty($groups) and is_array($groups)) {
87 $groupoptions = array();
88 foreach ($groups as $group) {
89 $groupoptions[$group->id] = $group->name;
91 $mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions);
92 $mform->setType('groupid', PARAM_INT);
93 $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
96 $mform->addElement('hidden', 'course');
97 $mform->setType('course', PARAM_INT);
98 $mform->addElement('submit', 'add', get_string('add'));
102 * Validates the returned data.
104 * @param array $data
105 * @param array $files
106 * @return array
108 public function validation($data, $files) {
109 global $USER;
111 $errors = parent::validation($data, $files);
113 if ($data['importfrom'] == CALENDAR_IMPORT_FROM_FILE) {
114 if (empty($data['importfile'])) {
115 $errors['importfile'] = get_string('errorrequiredurlorfile', 'calendar');
116 } else {
117 // Make sure the file area is not empty and contains only one file.
118 $draftitemid = $data['importfile'];
119 $fs = get_file_storage();
120 $usercontext = context_user::instance($USER->id);
121 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false);
122 if (count($files) !== 1) {
123 $errors['importfile'] = get_string('errorrequiredurlorfile', 'calendar');
126 } else if (($data['importfrom'] == CALENDAR_IMPORT_FROM_URL)) {
127 // Clean input calendar url.
128 $url = clean_param($data['url'], PARAM_URL);
129 if (empty($url) || ($url !== $data['url'])) {
130 $errors['url'] = get_string('invalidurl', 'error');
132 } else {
133 // Shouldn't happen.
134 $errors['url'] = get_string('errorrequiredurlorfile', 'calendar');
137 return $errors;
140 public function definition_after_data() {
141 $mform =& $this->_form;
143 $mform->applyFilter('url', 'calendar_addsubscription_form::strip_webcal');
144 $mform->applyFilter('url', 'trim');
148 * Replace webcal:// urls with http:// as
149 * curl does not understand this protocol
151 * @param string @url url to examine
152 * @return string url with webcal:// replaced
154 public static function strip_webcal($url) {
155 if (strpos($url, 'webcal://') === 0) {
156 $url = str_replace('webcal://', 'http://', $url);
158 return $url;