MDL-70237 payment: Allow html tags in gateway description
[moodle.git] / calendar / managesubscriptions.php
CommitLineData
e30390a0
SH
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
25require_once('../config.php');
26require_once($CFG->libdir.'/bennu/bennu.inc.php');
27require_once($CFG->dirroot.'/course/lib.php');
28require_once($CFG->dirroot.'/calendar/lib.php');
e30390a0
SH
29
30// Required use.
b9fd5164
AN
31$courseid = optional_param('course', null, PARAM_INT);
32$categoryid = optional_param('category', null, PARAM_INT);
e30390a0
SH
33// Used for processing subscription actions.
34$subscriptionid = optional_param('id', 0, PARAM_INT);
35$pollinterval = optional_param('pollinterval', 0, PARAM_INT);
85354cb7 36$groupcourseid = optional_param('groupcourseid', 0, PARAM_INT);
ee74a2a1 37$action = optional_param('action', '', PARAM_INT);
e30390a0
SH
38
39$url = new moodle_url('/calendar/managesubscriptions.php');
40if ($courseid != SITEID) {
41 $url->param('course', $courseid);
42}
b9fd5164
AN
43if ($categoryid) {
44 $url->param('categoryid', $categoryid);
45}
e30390a0
SH
46navigation_node::override_active_url(new moodle_url('/calendar/view.php', array('view' => 'month')));
47$PAGE->set_url($url);
566889aa 48$PAGE->set_pagelayout('admin');
e30390a0
SH
49$PAGE->navbar->add(get_string('managesubscriptions', 'calendar'));
50
51if ($courseid != SITEID && !empty($courseid)) {
fb1a615d
JP
52 // Course ID must be valid and existing.
53 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
e30390a0
SH
54 $courses = array($course->id => $course);
55} else {
56 $course = get_site();
57 $courses = calendar_get_default_courses();
58}
fb1a615d
JP
59require_login($course, false);
60
e30390a0
SH
61if (!calendar_user_can_add_event($course)) {
62 print_error('errorcannotimport', 'calendar');
63}
64
85354cb7
JP
65// Populate the 'group' select box based on the given 'groupcourseid', if necessary.
66$groups = [];
67if (!empty($groupcourseid)) {
68 require_once($CFG->libdir . '/grouplib.php');
69 $groupcoursedata = groups_get_course_data($groupcourseid);
70 if (!empty($groupcoursedata->groups)) {
71 foreach ($groupcoursedata->groups as $groupid => $groupdata) {
72 $groups[$groupid] = $groupdata->name;
73 }
74 }
75}
76$customdata = [
77 'courseid' => $course->id,
78 'groups' => $groups,
79];
80$form = new \core_calendar\local\event\forms\managesubscriptions(null, $customdata);
e30390a0
SH
81$form->set_data(array(
82 'course' => $course->id
83));
84
85$importresults = '';
86
87$formdata = $form->get_data();
88if (!empty($formdata)) {
89 require_sesskey(); // Must have sesskey for all actions.
85354cb7 90 $subscriptionid = calendar_add_subscription($formdata);
e30390a0
SH
91 if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) {
92 // Blank the URL if it's a file import.
93 $formdata->url = '';
c5a82120 94 $calendar = $form->get_file_content('importfile');
e30390a0
SH
95 $ical = new iCalendar();
96 $ical->unserialize($calendar);
b9fd5164 97 $importresults = calendar_import_icalendar_events($ical, null, $subscriptionid);
e30390a0 98 } else {
f40c37d0
RT
99 try {
100 $importresults = calendar_update_subscription_events($subscriptionid);
101 } catch (moodle_exception $e) {
102 // Delete newly added subscription and show invalid url error.
103 calendar_delete_subscription($subscriptionid);
104 print_error($e->errorcode, $e->module, $PAGE->url);
105 }
e30390a0
SH
106 }
107 // Redirect to prevent refresh issues.
7af29435 108 redirect($PAGE->url, $importresults);
e30390a0
SH
109} else if (!empty($subscriptionid)) {
110 // The user is wanting to perform an action upon an existing subscription.
111 require_sesskey(); // Must have sesskey for all actions.
7a216229 112 if (calendar_can_edit_subscription($subscriptionid)) {
f40c37d0
RT
113 try {
114 $importresults = calendar_process_subscription_row($subscriptionid, $pollinterval, $action);
115 } catch (moodle_exception $e) {
116 // If exception caught, then user should be redirected to page where he/she came from.
117 print_error($e->errorcode, $e->module, $PAGE->url);
118 }
7a216229
AA
119 } else {
120 print_error('nopermissions', 'error', $PAGE->url, get_string('managesubscriptions', 'calendar'));
121 }
e30390a0
SH
122}
123
cf2df619 124$types = calendar_get_allowed_event_types($courseid);
b9fd5164
AN
125
126$searches = [];
127$params = [];
128
129$usedefaultfilters = true;
19bea57b
SL
130
131if (!empty($types['site'])) {
b9fd5164 132 $searches[] = "(eventtype = 'site')";
5d384053
AG
133 $usedefaultfilters = false;
134}
135
136if (!empty($types['user'])) {
b9fd5164
AN
137 $searches[] = "(eventtype = 'user' AND userid = :userid)";
138 $params['userid'] = $USER->id;
139 $usedefaultfilters = false;
140}
141
cf2df619 142if (!empty($courseid) && !empty($types['course'])) {
b9fd5164
AN
143 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid = :courseid)";
144 $params += ['courseid' => $courseid];
145 $usedefaultfilters = false;
146}
147
19bea57b
SL
148if (!empty($types['category'])) {
149 if (!empty($categoryid)) {
150 $searches[] = "(eventtype = 'category' AND categoryid = :categoryid)";
151 $params += ['categoryid' => $categoryid];
152 } else {
153 $searches[] = "(eventtype = 'category')";
154 }
155
b9fd5164
AN
156 $usedefaultfilters = false;
157}
158
159if ($usedefaultfilters) {
160 $searches[] = "(eventtype = 'user' AND userid = :userid)";
161 $params['userid'] = $USER->id;
162
cf2df619 163 if (!empty($types['site'])) {
b9fd5164
AN
164 $searches[] = "(eventtype = 'site' AND courseid = :siteid)";
165 $params += ['siteid' => SITEID];
166 }
167
cf2df619
SL
168 if (!empty($types['course'])) {
169 $courses = calendar_get_default_courses(null, 'id', true);
170 if (!empty($courses)) {
171 $courseids = array_map(function ($c) {
172 return $c->id;
173 }, $courses);
174
175 list($courseinsql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, 'course');
176 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid {$courseinsql})";
177 $params += $courseparams;
178 }
b9fd5164
AN
179 }
180
cf2df619
SL
181 if (!empty($types['category'])) {
182 list($categoryinsql, $categoryparams) = $DB->get_in_or_equal(
442f12f8 183 array_keys(\core_course_category::make_categories_list('moodle/category:manage')), SQL_PARAMS_NAMED, 'category');
b9fd5164
AN
184 $searches[] = "(eventtype = 'category' AND categoryid {$categoryinsql})";
185 $params += $categoryparams;
186 }
187}
188
189$sql = "SELECT * FROM {event_subscriptions} WHERE " . implode(' OR ', $searches);;
e30390a0
SH
190$subscriptions = $DB->get_records_sql($sql, $params);
191
192// Print title and header.
193$PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
194$PAGE->set_heading($course->fullname);
e30390a0
SH
195
196$renderer = $PAGE->get_renderer('core_calendar');
197
198echo $OUTPUT->header();
9fd0cb9c
RT
199
200// Filter subscriptions which user can't edit.
201foreach($subscriptions as $subscription) {
202 if (!calendar_can_edit_subscription($subscription)) {
203 unset($subscriptions[$subscription->id]);
204 }
205}
206
e30390a0
SH
207// Display a table of subscriptions.
208echo $renderer->subscription_details($courseid, $subscriptions, $importresults);
209// Display the add subscription form.
210$form->display();
c5a82120 211echo $OUTPUT->footer();