Merge branch 'MDL-62962-master' of git://github.com/junpataleta/moodle
[moodle.git] / calendar / managesubscriptions.php
blob75ddc09f19211d94e7bba704aae39f6e042cd7eb
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');
30 // Required use.
31 $courseid = optional_param('course', null, PARAM_INT);
32 $categoryid = optional_param('category', null, 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 if ($categoryid) {
43 $url->param('categoryid', $categoryid);
45 navigation_node::override_active_url(new moodle_url('/calendar/view.php', array('view' => 'month')));
46 $PAGE->set_url($url);
47 $PAGE->set_pagelayout('admin');
48 $PAGE->navbar->add(get_string('managesubscriptions', 'calendar'));
50 if ($courseid != SITEID && !empty($courseid)) {
51 // Course ID must be valid and existing.
52 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
53 $courses = array($course->id => $course);
54 } else {
55 $course = get_site();
56 $courses = calendar_get_default_courses();
58 require_login($course, false);
60 if (!calendar_user_can_add_event($course)) {
61 print_error('errorcannotimport', 'calendar');
64 $form = new \core_calendar\local\event\forms\managesubscriptions();
65 $form->set_data(array(
66 'course' => $course->id
67 ));
69 $importresults = '';
71 $formdata = $form->get_data();
72 if (!empty($formdata)) {
73 require_sesskey(); // Must have sesskey for all actions.
74 $subscriptionid = calendar_add_subscription($formdata);
75 if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) {
76 // Blank the URL if it's a file import.
77 $formdata->url = '';
78 $calendar = $form->get_file_content('importfile');
79 $ical = new iCalendar();
80 $ical->unserialize($calendar);
81 $importresults = calendar_import_icalendar_events($ical, null, $subscriptionid);
82 } else {
83 try {
84 $importresults = calendar_update_subscription_events($subscriptionid);
85 } catch (moodle_exception $e) {
86 // Delete newly added subscription and show invalid url error.
87 calendar_delete_subscription($subscriptionid);
88 print_error($e->errorcode, $e->module, $PAGE->url);
91 // Redirect to prevent refresh issues.
92 redirect($PAGE->url, $importresults);
93 } else if (!empty($subscriptionid)) {
94 // The user is wanting to perform an action upon an existing subscription.
95 require_sesskey(); // Must have sesskey for all actions.
96 if (calendar_can_edit_subscription($subscriptionid)) {
97 try {
98 $importresults = calendar_process_subscription_row($subscriptionid, $pollinterval, $action);
99 } catch (moodle_exception $e) {
100 // If exception caught, then user should be redirected to page where he/she came from.
101 print_error($e->errorcode, $e->module, $PAGE->url);
103 } else {
104 print_error('nopermissions', 'error', $PAGE->url, get_string('managesubscriptions', 'calendar'));
108 $types = calendar_get_all_allowed_types();
110 $searches = [];
111 $params = [];
113 $usedefaultfilters = true;
114 if (!empty($courseid) && $courseid == SITEID && isset($types['site'])) {
115 $searches[] = "(eventtype = 'site')";
116 $searches[] = "(eventtype = 'user' AND userid = :userid)";
117 $params['userid'] = $USER->id;
118 $usedefaultfilters = false;
121 if (!empty($courseid) && isset($types['course']) && array_key_exists($courseid, $types['course'])) {
122 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid = :courseid)";
123 $params += ['courseid' => $courseid];
124 $usedefaultfilters = false;
127 if (!empty($categoryid) && isset($types['category']) && array_key_exists($categoryid, $types['category'])) {
128 $searches[] = "(eventtype = 'category' AND categoryid = :categoryid)";
129 $params += ['categoryid' => $categoryid];
130 $usedefaultfilters = false;
133 if ($usedefaultfilters) {
134 $searches[] = "(eventtype = 'user' AND userid = :userid)";
135 $params['userid'] = $USER->id;
137 if (isset($types['site'])) {
138 $searches[] = "(eventtype = 'site' AND courseid = :siteid)";
139 $params += ['siteid' => SITEID];
142 if (isset($types['course'])) {
143 list($courseinsql, $courseparams) = $DB->get_in_or_equal(array_keys($types['course']), SQL_PARAMS_NAMED, 'course');
144 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid {$courseinsql})";
145 $params += $courseparams;
148 if (isset($types['category'])) {
149 list($categoryinsql, $categoryparams) = $DB->get_in_or_equal(array_keys($types['category']), SQL_PARAMS_NAMED, 'category');
150 $searches[] = "(eventtype = 'category' AND categoryid {$categoryinsql})";
151 $params += $categoryparams;
155 $sql = "SELECT * FROM {event_subscriptions} WHERE " . implode(' OR ', $searches);;
156 $subscriptions = $DB->get_records_sql($sql, $params);
158 // Print title and header.
159 $PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
160 $PAGE->set_heading($course->fullname);
162 $renderer = $PAGE->get_renderer('core_calendar');
164 echo $OUTPUT->header();
166 // Filter subscriptions which user can't edit.
167 foreach($subscriptions as $subscription) {
168 if (!calendar_can_edit_subscription($subscription)) {
169 unset($subscriptions[$subscription->id]);
173 // Display a table of subscriptions.
174 echo $renderer->subscription_details($courseid, $subscriptions, $importresults);
175 // Display the add subscription form.
176 $form->display();
177 echo $OUTPUT->footer();