2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
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');
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);
44 $url->param('categoryid', $categoryid);
46 navigation_node
::override_active_url(new moodle_url('/calendar/view.php', array('view' => 'month')));
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);
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.
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
;
77 'courseid' => $course->id
,
80 $form = new \core_calendar\local\event\forms\
managesubscriptions(null, $customdata);
81 $form->set_data(array(
82 'course' => $course->id
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.
94 $calendar = $form->get_file_content('importfile');
95 $ical = new iCalendar();
96 $ical->unserialize($calendar);
97 $importresults = calendar_import_icalendar_events($ical, null, $subscriptionid);
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)) {
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
);
120 print_error('nopermissions', 'error', $PAGE->url
, get_string('managesubscriptions', 'calendar'));
124 $types = calendar_get_allowed_event_types($courseid);
129 $usedefaultfilters = true;
130 if (!empty($courseid) && $courseid == SITEID
&& !empty($types['site'])) {
131 $searches[] = "(eventtype = 'site')";
132 $searches[] = "(eventtype = 'user' AND userid = :userid)";
133 $params['userid'] = $USER->id
;
134 $usedefaultfilters = false;
137 if (!empty($courseid) && !empty($types['course'])) {
138 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid = :courseid)";
139 $params +
= ['courseid' => $courseid];
140 $usedefaultfilters = false;
143 if (!empty($categoryid) && !empty($types['category'])) {
144 $searches[] = "(eventtype = 'category' AND categoryid = :categoryid)";
145 $params +
= ['categoryid' => $categoryid];
146 $usedefaultfilters = false;
149 if ($usedefaultfilters) {
150 $searches[] = "(eventtype = 'user' AND userid = :userid)";
151 $params['userid'] = $USER->id
;
153 if (!empty($types['site'])) {
154 $searches[] = "(eventtype = 'site' AND courseid = :siteid)";
155 $params +
= ['siteid' => SITEID
];
158 if (!empty($types['course'])) {
159 $courses = calendar_get_default_courses(null, 'id', true);
160 if (!empty($courses)) {
161 $courseids = array_map(function ($c) {
165 list($courseinsql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED
, 'course');
166 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid {$courseinsql})";
167 $params +
= $courseparams;
171 if (!empty($types['category'])) {
172 list($categoryinsql, $categoryparams) = $DB->get_in_or_equal(
173 array_keys(\core_course_category
::make_categories_list('moodle/category:manage')), SQL_PARAMS_NAMED
, 'category');
174 $searches[] = "(eventtype = 'category' AND categoryid {$categoryinsql})";
175 $params +
= $categoryparams;
179 $sql = "SELECT * FROM {event_subscriptions} WHERE " . implode(' OR ', $searches);;
180 $subscriptions = $DB->get_records_sql($sql, $params);
182 // Print title and header.
183 $PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
184 $PAGE->set_heading($course->fullname
);
186 $renderer = $PAGE->get_renderer('core_calendar');
188 echo $OUTPUT->header();
190 // Filter subscriptions which user can't edit.
191 foreach($subscriptions as $subscription) {
192 if (!calendar_can_edit_subscription($subscription)) {
193 unset($subscriptions[$subscription->id
]);
197 // Display a table of subscriptions.
198 echo $renderer->subscription_details($courseid, $subscriptions, $importresults);
199 // Display the add subscription form.
201 echo $OUTPUT->footer();