Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]
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 namespace core_calendar\local\event\forms; 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 require_once($CFG->libdir . '/formslib.php'); 29 30 /** 31 * Form for adding a subscription to a Moodle course calendar. 32 * @copyright 2012 Jonathan Harker 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class managesubscriptions extends \moodleform { 36 37 use eventtype; 38 39 /** 40 * Defines the form used to add calendar subscriptions. 41 */ 42 public function definition() { 43 global $PAGE; 44 $mform = $this->_form; 45 $eventtypes = calendar_get_allowed_event_types(); 46 if (in_array(true, $eventtypes, true) === false) { 47 print_error('nopermissiontoupdatecalendar'); 48 } 49 50 // Name. 51 $mform->addElement('text', 'name', get_string('subscriptionname', 'calendar'), array('maxsize' => '255', 'size' => '40')); 52 $mform->addRule('name', get_string('required'), 'required', null, 'client'); 53 $mform->setType('name', PARAM_TEXT); 54 55 // Import from (url | importfile). 56 $choices = array(CALENDAR_IMPORT_FROM_FILE => get_string('importfromfile', 'calendar'), 57 CALENDAR_IMPORT_FROM_URL => get_string('importfromurl', 'calendar')); 58 $mform->addElement('select', 'importfrom', get_string('importcalendarfrom', 'calendar'), $choices); 59 $mform->setDefault('importfrom', CALENDAR_IMPORT_FROM_URL); 60 61 // URL. 62 $mform->addElement('text', 'url', get_string('importfromurl', 'calendar'), array('maxsize' => '255', 'size' => '50')); 63 // Cannot set as PARAM_URL since we need to allow webcal:// protocol. 64 $mform->setType('url', PARAM_RAW); 65 $mform->setForceLtr('url'); 66 67 // Poll interval 68 $choices = calendar_get_pollinterval_choices(); 69 $mform->addElement('select', 'pollinterval', get_string('pollinterval', 'calendar'), $choices); 70 $mform->setDefault('pollinterval', 604800); 71 $mform->addHelpButton('pollinterval', 'pollinterval', 'calendar'); 72 $mform->setType('pollinterval', PARAM_INT); 73 74 // Import file 75 $mform->addElement('filepicker', 'importfile', get_string('importfromfile', 'calendar'), null, array('accepted_types' => '.ics')); 76 77 // Disable appropriate elements depending on import from value. 78 $mform->hideIf('pollinterval', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_FILE); 79 $mform->hideIf('url', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_FILE); 80 $mform->hideIf('importfile', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_URL); 81 82 // Add the select elements for the available event types. 83 $this->add_event_type_elements($mform, $eventtypes); 84 85 // Eventtype: 0 = user, 1 = site, anything else = course ID. 86 $mform->addElement('submit', 'add', get_string('importcalendar', 'calendar')); 87 88 // Add the javascript required to enhance this mform. 89 $PAGE->requires->js_call_amd('core_calendar/event_form', 'init', [$mform->getAttribute('id')]); 90 } 91 92 /** 93 * Validates the returned data. 94 * 95 * @param array $data 96 * @param array $files 97 * @return array 98 */ 99 public function validation($data, $files) { 100 global $USER; 101 102 $errors = parent::validation($data, $files); 103 104 $eventtype = isset($data['eventtype']) ? $data['eventtype'] : null; 105 $coursekey = ($eventtype == 'group') ? 'groupcourseid' : 'courseid'; 106 $courseid = (!empty($data[$coursekey])) ? $data[$coursekey] : null; 107 $eventtypes = calendar_get_allowed_event_types($courseid); 108 109 if (empty($eventtype) || !isset($eventtypes[$eventtype])) { 110 $errors['eventtype'] = get_string('invalideventtype', 'calendar'); 111 } 112 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'); 124 } 125 } 126 } else if (($data['importfrom'] == CALENDAR_IMPORT_FROM_URL)) { 127 if (empty($data['url'])) { 128 $errors['url'] = get_string('errorrequiredurlorfile', 'calendar'); 129 } else { 130 // Clean input calendar url. 131 $url = clean_param($data['url'], PARAM_URL); 132 try { 133 calendar_get_icalendar($url); 134 } catch (\moodle_exception $e) { 135 $errors['url'] = get_string('errorinvalidicalurl', 'calendar'); 136 } 137 } 138 } else { 139 // Shouldn't happen. 140 $errors['url'] = get_string('errorrequiredurlorfile', 'calendar'); 141 } 142 143 // Validate course/category event types (ensure appropriate field is also filled in). 144 if ($eventtype === 'course' && empty($data['courseid'])) { 145 $errors['courseid'] = get_string('selectacourse'); 146 } else if ($eventtype === 'category' && empty($data['categoryid'])) { 147 $errors['categoryid'] = get_string('required'); 148 } 149 150 return $errors; 151 } 152 153 public function definition_after_data() { 154 $mform =& $this->_form; 155 156 $mform->applyFilter('url', static::class . '::strip_webcal'); 157 $mform->applyFilter('url', 'trim'); 158 } 159 160 /** 161 * Replace webcal:// urls with http:// as 162 * curl does not understand this protocol 163 * 164 * @param string @url url to examine 165 * @return string url with webcal:// replaced 166 */ 167 public static function strip_webcal($url) { 168 if (strpos($url, 'webcal://') === 0) { 169 $url = str_replace('webcal://', 'http://', $url); 170 } 171 return $url; 172 } 173 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body