Merge branch 'master-MDL-66805-search' of https://github.com/kristian-94/moodle
[moodle.git] / calendar / managesubscriptions.php
blob2bc85428dde55bab839911557b29524ee0a2abd3
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 $groupcourseid = optional_param('groupcourseid', 0, PARAM_INT);
37 $action = optional_param('action', '', PARAM_INT);
39 $url = new moodle_url('/calendar/managesubscriptions.php');
40 if ($courseid != SITEID) {
41 $url->param('course', $courseid);
43 if ($categoryid) {
44 $url->param('categoryid', $categoryid);
46 navigation_node::override_active_url(new moodle_url('/calendar/view.php', array('view' => 'month')));
47 $PAGE->set_url($url);
48 $PAGE->set_pagelayout('admin');
49 $PAGE->navbar->add(get_string('managesubscriptions', 'calendar'));
51 if ($courseid != SITEID && !empty($courseid)) {
52 // Course ID must be valid and existing.
53 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
54 $courses = array($course->id => $course);
55 } else {
56 $course = get_site();
57 $courses = calendar_get_default_courses();
59 require_login($course, false);
61 if (!calendar_user_can_add_event($course)) {
62 print_error('errorcannotimport', 'calendar');
65 // Populate the 'group' select box based on the given 'groupcourseid', if necessary.
66 $groups = [];
67 if (!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;
76 $customdata = [
77 'courseid' => $course->id,
78 'groups' => $groups,
80 $form = new \core_calendar\local\event\forms\managesubscriptions(null, $customdata);
81 $form->set_data(array(
82 'course' => $course->id
83 ));
85 $importresults = '';
87 $formdata = $form->get_data();
88 if (!empty($formdata)) {
89 require_sesskey(); // Must have sesskey for all actions.
90 $subscriptionid = calendar_add_subscription($formdata);
91 if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) {
92 // Blank the URL if it's a file import.
93 $formdata->url = '';
94 $calendar = $form->get_file_content('importfile');
95 $ical = new iCalendar();
96 $ical->unserialize($calendar);
97 $importresults = calendar_import_icalendar_events($ical, null, $subscriptionid);
98 } else {
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);
107 // Redirect to prevent refresh issues.
108 redirect($PAGE->url, $importresults);
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.
112 if (calendar_can_edit_subscription($subscriptionid)) {
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);
119 } else {
120 print_error('nopermissions', 'error', $PAGE->url, get_string('managesubscriptions', 'calendar'));
124 $types = calendar_get_allowed_event_types($courseid);
126 $searches = [];
127 $params = [];
129 $usedefaultfilters = true;
130 if (!empty($courseid) && $courseid == SITEID && !empty($types['site'])) {
131 $searches[] = "(eventtype = 'site')";
132 $usedefaultfilters = false;
135 if (!empty($types['user'])) {
136 $searches[] = "(eventtype = 'user' AND userid = :userid)";
137 $params['userid'] = $USER->id;
138 $usedefaultfilters = false;
141 if (!empty($courseid) && !empty($types['course'])) {
142 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid = :courseid)";
143 $params += ['courseid' => $courseid];
144 $usedefaultfilters = false;
147 if (!empty($categoryid) && !empty($types['category'])) {
148 $searches[] = "(eventtype = 'category' AND categoryid = :categoryid)";
149 $params += ['categoryid' => $categoryid];
150 $usedefaultfilters = false;
153 if ($usedefaultfilters) {
154 $searches[] = "(eventtype = 'user' AND userid = :userid)";
155 $params['userid'] = $USER->id;
157 if (!empty($types['site'])) {
158 $searches[] = "(eventtype = 'site' AND courseid = :siteid)";
159 $params += ['siteid' => SITEID];
162 if (!empty($types['course'])) {
163 $courses = calendar_get_default_courses(null, 'id', true);
164 if (!empty($courses)) {
165 $courseids = array_map(function ($c) {
166 return $c->id;
167 }, $courses);
169 list($courseinsql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, 'course');
170 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid {$courseinsql})";
171 $params += $courseparams;
175 if (!empty($types['category'])) {
176 list($categoryinsql, $categoryparams) = $DB->get_in_or_equal(
177 array_keys(\core_course_category::make_categories_list('moodle/category:manage')), SQL_PARAMS_NAMED, 'category');
178 $searches[] = "(eventtype = 'category' AND categoryid {$categoryinsql})";
179 $params += $categoryparams;
183 $sql = "SELECT * FROM {event_subscriptions} WHERE " . implode(' OR ', $searches);;
184 $subscriptions = $DB->get_records_sql($sql, $params);
186 // Print title and header.
187 $PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
188 $PAGE->set_heading($course->fullname);
190 $renderer = $PAGE->get_renderer('core_calendar');
192 echo $OUTPUT->header();
194 // Filter subscriptions which user can't edit.
195 foreach($subscriptions as $subscription) {
196 if (!calendar_can_edit_subscription($subscription)) {
197 unset($subscriptions[$subscription->id]);
201 // Display a table of subscriptions.
202 echo $renderer->subscription_details($courseid, $subscriptions, $importresults);
203 // Display the add subscription form.
204 $form->display();
205 echo $OUTPUT->footer();