MDL-63724 messaging: Prefix field with the table alias
[moodle.git] / calendar / managesubscriptions.php
blob3bf03dfe19e751a17b69fcaca159d58bcd06bb18
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(null, ['courseid' => $course->id]);
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_allowed_event_types($courseid);
110 $searches = [];
111 $params = [];
113 $usedefaultfilters = true;
114 if (!empty($courseid) && $courseid == SITEID && !empty($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) && !empty($types['course'])) {
122 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid = :courseid)";
123 $params += ['courseid' => $courseid];
124 $usedefaultfilters = false;
127 if (!empty($categoryid) && !empty($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 (!empty($types['site'])) {
138 $searches[] = "(eventtype = 'site' AND courseid = :siteid)";
139 $params += ['siteid' => SITEID];
142 if (!empty($types['course'])) {
143 $courses = calendar_get_default_courses(null, 'id', true);
144 if (!empty($courses)) {
145 $courseids = array_map(function ($c) {
146 return $c->id;
147 }, $courses);
149 list($courseinsql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, 'course');
150 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid {$courseinsql})";
151 $params += $courseparams;
155 if (!empty($types['category'])) {
156 list($categoryinsql, $categoryparams) = $DB->get_in_or_equal(
157 array_keys(\core_course_category::make_categories_list('moodle/category:manage')), SQL_PARAMS_NAMED, 'category');
158 $searches[] = "(eventtype = 'category' AND categoryid {$categoryinsql})";
159 $params += $categoryparams;
163 $sql = "SELECT * FROM {event_subscriptions} WHERE " . implode(' OR ', $searches);;
164 $subscriptions = $DB->get_records_sql($sql, $params);
166 // Print title and header.
167 $PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
168 $PAGE->set_heading($course->fullname);
170 $renderer = $PAGE->get_renderer('core_calendar');
172 echo $OUTPUT->header();
174 // Filter subscriptions which user can't edit.
175 foreach($subscriptions as $subscription) {
176 if (!calendar_can_edit_subscription($subscription)) {
177 unset($subscriptions[$subscription->id]);
181 // Display a table of subscriptions.
182 echo $renderer->subscription_details($courseid, $subscriptions, $importresults);
183 // Display the add subscription form.
184 $form->display();
185 echo $OUTPUT->footer();