Merge branch 'MDL-72646-font-mimetype-311' of https://github.com/doctorlard/moodle...
[moodle.git] / calendar / managesubscriptions.php
blob93f4b96b92781f78be1dff0b981065667d169862
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 && !empty($courseid)) {
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($url, $customdata);
81 $form->set_data(array(
82 'course' => $course->id
83 ));
85 $formdata = $form->get_data();
86 if (!empty($formdata)) {
87 require_sesskey(); // Must have sesskey for all actions.
88 $subscriptionid = calendar_add_subscription($formdata);
89 if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) {
90 // Blank the URL if it's a file import.
91 $formdata->url = '';
92 $calendar = $form->get_file_content('importfile');
93 $ical = new iCalendar();
94 $ical->unserialize($calendar);
95 $importresults = calendar_import_icalendar_events($ical, null, $subscriptionid);
96 } else {
97 try {
98 $importresults = calendar_update_subscription_events($subscriptionid);
99 } catch (moodle_exception $e) {
100 // Delete newly added subscription and show invalid url error.
101 calendar_delete_subscription($subscriptionid);
102 print_error($e->errorcode, $e->module, $PAGE->url);
105 // Redirect to prevent refresh issues.
106 redirect($PAGE->url, $importresults);
107 } else if (!empty($subscriptionid)) {
108 // The user is wanting to perform an action upon an existing subscription.
109 require_sesskey(); // Must have sesskey for all actions.
110 if (calendar_can_edit_subscription($subscriptionid)) {
111 try {
112 $importresults = calendar_process_subscription_row($subscriptionid, $pollinterval, $action);
113 redirect($PAGE->url, $importresults);
114 } catch (moodle_exception $e) {
115 // If exception caught, then user should be redirected to page where he/she came from.
116 print_error($e->errorcode, $e->module, $PAGE->url);
118 } else {
119 print_error('nopermissions', 'error', $PAGE->url, get_string('managesubscriptions', 'calendar'));
123 $types = calendar_get_allowed_event_types($courseid);
125 $searches = [];
126 $params = [];
128 $usedefaultfilters = true;
130 if (!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($types['category'])) {
148 if (!empty($categoryid)) {
149 $searches[] = "(eventtype = 'category' AND categoryid = :categoryid)";
150 $params += ['categoryid' => $categoryid];
151 } else {
152 $searches[] = "(eventtype = 'category')";
155 $usedefaultfilters = false;
158 if ($usedefaultfilters) {
159 $searches[] = "(eventtype = 'user' AND userid = :userid)";
160 $params['userid'] = $USER->id;
162 if (!empty($types['site'])) {
163 $searches[] = "(eventtype = 'site' AND courseid = :siteid)";
164 $params += ['siteid' => SITEID];
167 if (!empty($types['course'])) {
168 $courses = calendar_get_default_courses(null, 'id', true);
169 if (!empty($courses)) {
170 $courseids = array_map(function ($c) {
171 return $c->id;
172 }, $courses);
174 list($courseinsql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, 'course');
175 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid {$courseinsql})";
176 $params += $courseparams;
180 if (!empty($types['category'])) {
181 list($categoryinsql, $categoryparams) = $DB->get_in_or_equal(
182 array_keys(\core_course_category::make_categories_list('moodle/category:manage')), SQL_PARAMS_NAMED, 'category');
183 $searches[] = "(eventtype = 'category' AND categoryid {$categoryinsql})";
184 $params += $categoryparams;
188 $sql = "SELECT * FROM {event_subscriptions} WHERE " . implode(' OR ', $searches);;
189 $subscriptions = $DB->get_records_sql($sql, $params);
191 // Print title and header.
192 $PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
193 $PAGE->set_heading($course->fullname);
195 $renderer = $PAGE->get_renderer('core_calendar');
197 echo $OUTPUT->header();
199 // Filter subscriptions which user can't edit.
200 foreach($subscriptions as $subscription) {
201 if (!calendar_can_edit_subscription($subscription)) {
202 unset($subscriptions[$subscription->id]);
206 // Display a table of subscriptions.
207 echo $renderer->subscription_details($courseid, $subscriptions);
208 // Display the add subscription form.
209 $form->display();
210 echo $OUTPUT->footer();