mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
index.php
165 lines (149 loc) · 6.86 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This page lets users to manage rules for a given course.
*
* @package tool_monitor
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/monitor/lib.php');
$courseid = optional_param('courseid', 0, PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
$cmid = optional_param('cmid', 0, PARAM_INT);
$ruleid = optional_param('ruleid', 0, PARAM_INT);
$subscriptionid = optional_param('subscriptionid', 0, PARAM_INT);
$confirm = optional_param('confirm', false, PARAM_BOOL);
$choose = false;
// Validate course id.
if (empty($courseid)) {
require_login();
$context = context_system::instance();
// check system level capability.
if (!has_capability('tool/monitor:subscribe', $context)) {
// If not system level then check to see if they have access to any course level rules.
if (tool_monitor_can_subscribe()) {
// Make them choose a course.
$choose = true;
} else {
// return error.
print_error('rulenopermission', 'tool_monitor');
}
}
} else {
// They might want to see rules for this course.
$course = get_course($courseid);
require_login($course);
$context = context_course::instance($course->id);
// Check for caps.
require_capability('tool/monitor:subscribe', $context);
}
if (!get_config('tool_monitor', 'enablemonitor')) {
// This should never happen as the this page does not appear in navigation when the tool is disabled.
throw new coding_exception('Event monitoring is disabled');
}
// Use the user context here so that the header shows user information.
$PAGE->set_context(context_user::instance($USER->id));
// Set up the page.
$indexurl = new moodle_url('/admin/tool/monitor/index.php', array('courseid' => $courseid));
$PAGE->set_url($indexurl);
$PAGE->set_pagelayout('report');
$PAGE->set_title(get_string('managesubscriptions', 'tool_monitor'));
$PAGE->set_heading(fullname($USER));
$settingsnode = $PAGE->settingsnav->find('monitor', null);
if ($settingsnode) {
$settingsnode->make_active();
}
// Create/delete subscription if needed.
if (!empty($action)) {
require_sesskey();
switch ($action) {
case 'subscribe' :
$rule = \tool_monitor\rule_manager::get_rule($ruleid);
$rule->subscribe_user($courseid, $cmid);
echo $OUTPUT->header();
echo $OUTPUT->notification(get_string('subcreatesuccess', 'tool_monitor'), 'notifysuccess');
break;
case 'unsubscribe' :
// If the subscription does not exist, then redirect back as the subscription must have already been deleted.
if (!$subscription = $DB->record_exists('tool_monitor_subscriptions', array('id' => $subscriptionid))) {
redirect(new moodle_url('/admin/tool/monitor/index.php', array('courseid' => $courseid)));
}
// Set the URLs.
$confirmurl = new moodle_url('/admin/tool/monitor/index.php', array('subscriptionid' => $subscriptionid,
'courseid' => $courseid, 'action' => 'unsubscribe', 'confirm' => true,
'sesskey' => sesskey()));
$cancelurl = new moodle_url('/admin/tool/monitor/index.php', array('subscriptionid' => $subscriptionid,
'courseid' => $courseid, 'sesskey' => sesskey()));
if ($confirm) {
\tool_monitor\subscription_manager::delete_subscription($subscriptionid);
echo $OUTPUT->header();
echo $OUTPUT->notification(get_string('subdeletesuccess', 'tool_monitor'), 'notifysuccess');
} else {
$subscription = \tool_monitor\subscription_manager::get_subscription($subscriptionid);
echo $OUTPUT->header();
echo $OUTPUT->confirm(get_string('subareyousure', 'tool_monitor', $subscription->get_name($context)),
$confirmurl, $cancelurl);
echo $OUTPUT->footer();
exit();
}
break;
default:
}
} else {
echo $OUTPUT->header();
}
$renderer = $PAGE->get_renderer('tool_monitor', 'managesubs');
// Render the course selector.
$totalrules = \tool_monitor\rule_manager::count_rules_by_courseid($courseid);
$rules = new \tool_monitor\output\managesubs\rules('toolmonitorrules', $indexurl, $courseid);
$usercourses = $rules->get_user_courses_select($choose);
// There must be user courses otherwise we wouldn't make it this far.
echo $renderer->render($usercourses);
// Render the current subscriptions list.
$totalsubs = \tool_monitor\subscription_manager::count_user_subscriptions();
if (!empty($totalsubs) && !$choose) {
// Show the subscriptions section only if there are subscriptions.
$subs = new \tool_monitor\output\managesubs\subs('toolmonitorsubs', $indexurl, $courseid);
echo $OUTPUT->heading(get_string('currentsubscriptions', 'tool_monitor'), 3);
echo $renderer->render($subs);
}
// Render the potential rules list.
if (!$choose) {
echo $OUTPUT->heading(get_string('rulescansubscribe', 'tool_monitor'), 3);
echo $renderer->render($rules);
}
// Check if the user can manage the course rules we are viewing.
$canmanagerules = has_capability('tool/monitor:managerules', $context);
if (empty($totalrules)) {
// No rules present. Show a link to manage rules page if permissions permit.
echo html_writer::start_div();
echo html_writer::tag('span', get_string('norules', 'tool_monitor'));
if ($canmanagerules) {
$manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
$a = html_writer::link($manageurl, get_string('managerules', 'tool_monitor'));
$link = " ";
$link .= html_writer::tag('span', get_string('manageruleslink', 'tool_monitor', $a));
echo $link;
}
echo html_writer::end_div();
} else if ($canmanagerules) {
$manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
echo $renderer->render_rules_link($manageurl);
}
echo $OUTPUT->footer();