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');
56 $PAGE->set_secondary_navigation(false);
58 if ($courseid != SITEID
&& !empty($courseid)) {
59 // Course ID must be valid and existing.
60 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST
);
61 $courses = array($course->id
=> $course);
64 $courses = calendar_get_default_courses();
66 require_login($course, false);
68 if (!calendar_user_can_add_event($course)) {
69 print_error('errorcannotimport', 'calendar');
71 $PAGE->navbar
->add(get_string('managesubscriptions', 'calendar'), $PAGE->url
);
73 $types = calendar_get_allowed_event_types($courseid);
78 $usedefaultfilters = true;
80 if (!empty($types['site'])) {
81 $searches[] = "(eventtype = 'site')";
82 $usedefaultfilters = false;
85 if (!empty($types['user'])) {
86 $searches[] = "(eventtype = 'user' AND userid = :userid)";
87 $params['userid'] = $USER->id
;
88 $usedefaultfilters = false;
91 if (!empty($courseid) && !empty($types['course'])) {
92 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid = :courseid)";
93 $params +
= ['courseid' => $courseid];
94 $usedefaultfilters = false;
97 if (!empty($types['category'])) {
98 if (!empty($categoryid)) {
99 $searches[] = "(eventtype = 'category' AND categoryid = :categoryid)";
100 $params +
= ['categoryid' => $categoryid];
102 $searches[] = "(eventtype = 'category')";
105 $usedefaultfilters = false;
108 if ($usedefaultfilters) {
109 $searches[] = "(eventtype = 'user' AND userid = :userid)";
110 $params['userid'] = $USER->id
;
112 if (!empty($types['site'])) {
113 $searches[] = "(eventtype = 'site' AND courseid = :siteid)";
114 $params +
= ['siteid' => SITEID
];
117 if (!empty($types['course'])) {
118 $courses = calendar_get_default_courses(null, 'id', true);
119 if (!empty($courses)) {
120 $courseids = array_map(function ($c) {
124 list($courseinsql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED
, 'course');
125 $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid {$courseinsql})";
126 $params +
= $courseparams;
130 if (!empty($types['category'])) {
131 list($categoryinsql, $categoryparams) = $DB->get_in_or_equal(
132 array_keys(\core_course_category
::make_categories_list('moodle/category:manage')), SQL_PARAMS_NAMED
, 'category');
133 $searches[] = "(eventtype = 'category' AND categoryid {$categoryinsql})";
134 $params +
= $categoryparams;
138 $sql = "SELECT * FROM {event_subscriptions} WHERE " . implode(' OR ', $searches);;
139 $subscriptions = $DB->get_records_sql($sql, $params);
141 // Print title and header.
142 $PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
143 $heading = get_string('calendar', 'core_calendar');
144 $heading = ($courseid != SITEID
&& !empty($courseid)) ?
"{$heading}: {$COURSE->shortname}" : $heading;
145 $PAGE->set_heading($heading);
147 $renderer = $PAGE->get_renderer('core_calendar');
149 echo $OUTPUT->header();
150 echo $renderer->render_subscriptions_header();
152 // Filter subscriptions which user can't edit.
153 foreach($subscriptions as $subscription) {
154 if (!calendar_can_edit_subscription($subscription)) {
155 unset($subscriptions[$subscription->id
]);
159 // Display a table of subscriptions.
160 if (empty($subscription)) {
161 echo $renderer->render_no_calendar_subscriptions();
163 echo $renderer->subscription_details($courseid, $subscriptions);
165 echo $OUTPUT->footer();