MDL-57343 behat: Ensure we wait enough for page to be fully loaded
[moodle.git] / calendar / managesubscriptions.php
blob3e4e0e98f693cd8ba703d3b306b57e5ad129bbaa
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 require_once('../config.php');
26 require_once($CFG->libdir.'/bennu/bennu.inc.php');
27 require_once($CFG->dirroot.'/course/lib.php');
28 require_once($CFG->dirroot.'/calendar/lib.php');
29 require_once($CFG->dirroot.'/calendar/managesubscriptions_form.php');
31 // Required use.
32 $courseid = optional_param('course', SITEID, PARAM_INT);
33 // Used for processing subscription actions.
34 $subscriptionid = optional_param('id', 0, PARAM_INT);
35 $pollinterval = optional_param('pollinterval', 0, PARAM_INT);
36 $action = optional_param('action', '', PARAM_INT);
38 $url = new moodle_url('/calendar/managesubscriptions.php');
39 if ($courseid != SITEID) {
40 $url->param('course', $courseid);
42 navigation_node::override_active_url(new moodle_url('/calendar/view.php', array('view' => 'month')));
43 $PAGE->set_url($url);
44 $PAGE->set_pagelayout('admin');
45 $PAGE->navbar->add(get_string('managesubscriptions', 'calendar'));
47 if ($courseid != SITEID && !empty($courseid)) {
48 $course = $DB->get_record('course', array('id' => $courseid));
49 $courses = array($course->id => $course);
50 } else {
51 $course = get_site();
52 $courses = calendar_get_default_courses();
54 require_course_login($course);
55 if (!calendar_user_can_add_event($course)) {
56 print_error('errorcannotimport', 'calendar');
59 $form = new calendar_addsubscription_form(null);
60 $form->set_data(array(
61 'course' => $course->id
62 ));
64 $importresults = '';
66 $formdata = $form->get_data();
67 if (!empty($formdata)) {
68 require_sesskey(); // Must have sesskey for all actions.
69 $subscriptionid = calendar_add_subscription($formdata);
70 if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) {
71 // Blank the URL if it's a file import.
72 $formdata->url = '';
73 $calendar = $form->get_file_content('importfile');
74 $ical = new iCalendar();
75 $ical->unserialize($calendar);
76 $importresults = calendar_import_icalendar_events($ical, $courseid, $subscriptionid);
77 } else {
78 try {
79 $importresults = calendar_update_subscription_events($subscriptionid);
80 } catch (moodle_exception $e) {
81 // Delete newly added subscription and show invalid url error.
82 calendar_delete_subscription($subscriptionid);
83 print_error($e->errorcode, $e->module, $PAGE->url);
86 // Redirect to prevent refresh issues.
87 redirect($PAGE->url, $importresults);
88 } else if (!empty($subscriptionid)) {
89 // The user is wanting to perform an action upon an existing subscription.
90 require_sesskey(); // Must have sesskey for all actions.
91 if (calendar_can_edit_subscription($subscriptionid)) {
92 try {
93 $importresults = calendar_process_subscription_row($subscriptionid, $pollinterval, $action);
94 } catch (moodle_exception $e) {
95 // If exception caught, then user should be redirected to page where he/she came from.
96 print_error($e->errorcode, $e->module, $PAGE->url);
98 } else {
99 print_error('nopermissions', 'error', $PAGE->url, get_string('managesubscriptions', 'calendar'));
103 $sql = 'SELECT *
104 FROM {event_subscriptions}
105 WHERE courseid = :courseid
106 OR (courseid = 0 AND userid = :userid)';
107 $params = array('courseid' => $courseid, 'userid' => $USER->id);
108 $subscriptions = $DB->get_records_sql($sql, $params);
110 // Print title and header.
111 $PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
112 $PAGE->set_heading($course->fullname);
114 $renderer = $PAGE->get_renderer('core_calendar');
116 echo $OUTPUT->header();
118 // Filter subscriptions which user can't edit.
119 foreach($subscriptions as $subscription) {
120 if (!calendar_can_edit_subscription($subscription)) {
121 unset($subscriptions[$subscription->id]);
125 // Display a table of subscriptions.
126 echo $renderer->subscription_details($courseid, $subscriptions, $importresults);
127 // Display the add subscription form.
128 $form->display();
129 echo $OUTPUT->footer();