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
);
34 $url = new moodle_url('/calendar/managesubscriptions.php');
35 if ($courseid != SITEID
&& !empty($courseid)) {
36 $url->param('course', $courseid);
37 navigation_node
::override_active_url(new moodle_url('/course/view.php', ['id' => $courseid]));
39 get_string('calendar', 'calendar'),
40 new moodle_url('/calendar/view.php', ['view' => 'month', 'course' => $courseid])
42 } else if ($categoryid) {
43 $url->param('categoryid', $categoryid);
44 navigation_node
::override_active_url(new moodle_url('/course/index.php', ['categoryid' => $categoryid]));
45 $PAGE->set_category_by_id($categoryid);
47 get_string('calendar', 'calendar'),
48 new moodle_url('/calendar/view.php', ['view' => 'month', 'category' => $categoryid])
51 navigation_node
::override_active_url(new moodle_url('/calendar/view.php', ['view' => 'month']));
55 $PAGE->set_pagelayout('admin');
57 if ($courseid != SITEID
&& !empty($courseid)) {
58 // Course ID must be valid and existing.
59 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST
);
60 $courses = array($course->id
=> $course);
63 $courses = calendar_get_default_courses();
65 require_login($course, false);
67 if (!calendar_user_can_add_event($course)) {
68 print_error('errorcannotimport', 'calendar');
70 $PAGE->navbar
->add(get_string('managesubscriptions', 'calendar'), $PAGE->url
);
72 $types = calendar_get_allowed_event_types($courseid);
77 $usedefaultfilters = true;
79 if (!empty($types['site'])) {
80 $searches[] = "(eventtype = 'site')";
81 $usedefaultfilters = false;
84 if (!empty($types['user'])) {
85 $searches[] = "(eventtype = 'user' AND userid = :userid)";
86 $params['userid'] = $USER->id
;
87 $usedefaultfilters = false;
90 if (!empty($courseid) && !empty($types['course'])) {
91 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid = :courseid)";
92 $params +
= ['courseid' => $courseid];
93 $usedefaultfilters = false;
96 if (!empty($types['category'])) {
97 if (!empty($categoryid)) {
98 $searches[] = "(eventtype = 'category' AND categoryid = :categoryid)";
99 $params +
= ['categoryid' => $categoryid];
101 $searches[] = "(eventtype = 'category')";
104 $usedefaultfilters = false;
107 if ($usedefaultfilters) {
108 $searches[] = "(eventtype = 'user' AND userid = :userid)";
109 $params['userid'] = $USER->id
;
111 if (!empty($types['site'])) {
112 $searches[] = "(eventtype = 'site' AND courseid = :siteid)";
113 $params +
= ['siteid' => SITEID
];
116 if (!empty($types['course'])) {
117 $courses = calendar_get_default_courses(null, 'id', true);
118 if (!empty($courses)) {
119 $courseids = array_map(function ($c) {
123 list($courseinsql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED
, 'course');
124 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid {$courseinsql})";
125 $params +
= $courseparams;
129 if (!empty($types['category'])) {
130 list($categoryinsql, $categoryparams) = $DB->get_in_or_equal(
131 array_keys(\core_course_category
::make_categories_list('moodle/category:manage')), SQL_PARAMS_NAMED
, 'category');
132 $searches[] = "(eventtype = 'category' AND categoryid {$categoryinsql})";
133 $params +
= $categoryparams;
137 $sql = "SELECT * FROM {event_subscriptions} WHERE " . implode(' OR ', $searches);;
138 $subscriptions = $DB->get_records_sql($sql, $params);
140 // Print title and header.
141 $PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
142 $heading = get_string('calendar', 'core_calendar');
143 $heading = ($courseid != SITEID
&& !empty($courseid)) ?
"{$heading}: {$COURSE->shortname}" : $heading;
144 $PAGE->set_heading($heading);
146 $renderer = $PAGE->get_renderer('core_calendar');
148 echo $OUTPUT->header();
149 echo $renderer->render_subscriptions_header();
151 // Filter subscriptions which user can't edit.
152 foreach($subscriptions as $subscription) {
153 if (!calendar_can_edit_subscription($subscription)) {
154 unset($subscriptions[$subscription->id
]);
158 // Display a table of subscriptions.
159 if (empty($subscription)) {
160 echo $renderer->render_no_calendar_subscriptions();
162 echo $renderer->subscription_details($courseid, $subscriptions);
164 echo $OUTPUT->footer();