[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 25 if (!defined('MOODLE_INTERNAL')) { 26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page 27 } 28 29 require_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 */ 36 class 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')); 61 // Cannot set as PARAM_URL since we need to allow webcal:// protocol. 62 $mform->setType('url', PARAM_RAW); 63 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); 70 71 // Import file 72 $mform->addElement('filepicker', 'importfile', get_string('importfromfile', 'calendar'), null, array('accepted_types' => '.ics')); 73 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); 78 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); 84 85 if (!empty($groups) and is_array($groups)) { 86 $groupoptions = array(); 87 foreach ($groups as $group) { 88 $groupoptions[$group->id] = $group->name; 89 } 90 $mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions); 91 $mform->setType('groupid', PARAM_INT); 92 $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group'); 93 } 94 95 $mform->addElement('hidden', 'course'); 96 $mform->setType('course', PARAM_INT); 97 $mform->addElement('submit', 'add', get_string('add')); 98 } 99 100 /** 101 * Validates the returned data. 102 * 103 * @param array $data 104 * @param array $files 105 * @return array 106 */ 107 public function validation($data, $files) { 108 global $USER; 109 110 $errors = parent::validation($data, $files); 111 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'); 123 } 124 } 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'); 130 } 131 } else { 132 // Shouldn't happen. 133 $errors['url'] = get_string('errorrequiredurlorfile', 'calendar'); 134 } 135 136 return $errors; 137 } 138 139 public function definition_after_data() { 140 $mform =& $this->_form; 141 142 $mform->applyFilter('url', 'calendar_addsubscription_form::strip_webcal'); 143 $mform->applyFilter('url', 'trim'); 144 } 145 146 /** 147 * Replace webcal:// urls with http:// as 148 * curl does not understand this protocol 149 * 150 * @param string @url url to examine 151 * @return string url with webcal:// replaced 152 */ 153 public static function strip_webcal($url) { 154 if (strpos($url, 'webcal://') === 0) { 155 $url = str_replace('webcal://', 'http://', $url); 156 } 157 return $url; 158 } 159 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |