-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlib.php
222 lines (209 loc) · 9.6 KB
/
lib.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?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/>.
/**
* Local plugin "Learning Analytics" - Library
*
* @package local_learning_analytics
* @copyright Lehr- und Forschungsgebiet Ingenieurhydrologie - RWTH Aachen University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// Callback to extend navigation for moodle 3.
/**
* @param global_navigation $nav
* @throws coding_exception
* @throws moodle_exception
*/
function local_learning_analytics_extend_navigation(global_navigation $navigation) {
global $PAGE, $COURSE, $DB;
// Only extend navigation inside courses.
if (isset($COURSE->id) && $COURSE->id !== SITEID) {
// first check if user has the capability assigned
if (!has_capability('local/learning_analytics:view_statistics', $PAGE->context)) {
return;
}
// then check if the settings of the plugin
// status: 'show_if_enabled', 'show_courseids', 'show_always', 'hide_link', 'disable'
$statussetting = get_config('local_learning_analytics', 'status');
if ($statussetting === 'course_customfield') {
$customfieldid = (int) get_config('local_learning_analytics', 'customfieldid');
if (!$customfieldid) { // setup went wrong, this should not happen in reality
return;
}
$record = $DB->get_record('customfield_data', [
'fieldid' => $customfieldid,
'instanceid' => $COURSE->id,
], 'intvalue');
if ($record === false || $record->intvalue !== '1') {
return;
}
} else if ($statussetting === 'hide_link' || $statussetting === 'disable') {
return;
} else if ($statussetting === 'show_always') {
// just show it, don't filter anything
} else if ($statussetting === 'show_courseids') {
// use courseids of this plugin
$courseids = get_config('local_learning_analytics', 'course_ids');
if ($courseids === false || $courseids === '') {
$courseids = [];
} else {
$courseids = array_map('trim', explode(',', $courseids));
}
if (!in_array($COURSE->id, $courseids)) {
return;
}
} else { // default setting: 'show_if_enabled'
// check if the logstore plugin is enabled, otherwise hide link
$logstoresstr = get_config('tool_log', 'enabled_stores');
$logstores = $logstoresstr ? explode(',', $logstoresstr) : [];
if (!in_array('logstore_lanalytics', $logstores)) {
return;
}
// logging is enabled, check logging scope
$logscope = get_config('logstore_lanalytics', 'log_scope');
if ($logscope !== false && $logscope !== '' && $logscope !== 'all') {
// scope is not all -> check if course should be tracked
$courseids = get_config('logstore_lanalytics', 'course_ids');
if ($courseids === false || $courseids === '') {
$courseids = [];
} else {
$courseids = array_map('trim', explode(',', $courseids));
}
if (($logscope === 'include' && !in_array($COURSE->id, $courseids))
|| ($logscope === 'exclude' && in_array($COURSE->id, $courseids))) {
return;
}
}
}
$node = $navigation->find($COURSE->id, navigation_node::TYPE_COURSE);
$settingbeforekey = get_config('local_learning_analytics', 'navigation_position_beforekey');
$beforekey = null;
if ($settingbeforekey === false || $settingbeforekey === '') {
// Find first section node, and add our node before that (to be the last non-section node)
if (is_object($node->children)) {
$children = $node->children->type(navigation_node::TYPE_SECTION);
if (count($children) !== 0) {
$beforekey = reset($children)->key;
}
}
} else { // use setting
$beforekey = $settingbeforekey;
}
if ($node) {
$node->add_node(navigation_node::create(
get_string('navigationlink', 'local_learning_analytics'),
new moodle_url('/local/learning_analytics/index.php/reports/coursedashboard', array('course' => $COURSE->id)),
navigation_node::TYPE_CUSTOM,
null, 'learning_analytics',
new pix_icon('i/report', '')
),
$beforekey
);
}
}
}
// Callback to extend navigation for moodle 4.
/**
* @param global_navigation $nav
* @throws coding_exception
* @throws moodle_exception
*/
function local_learning_analytics_extend_navigation_course(\navigation_node $navigation, \stdClass $course, \context $context) {
global $PAGE, $COURSE, $DB;
// Only extend navigation inside courses.
if (isset($COURSE->id) && $COURSE->id !== SITEID) {
// first check if user has the capability assigned
if (!has_capability('local/learning_analytics:view_statistics', $PAGE->context)) {
return;
}
// then check if the settings of the plugin
// status: 'show_if_enabled', 'show_courseids', 'show_always', 'hide_link', 'disable'
$statussetting = get_config('local_learning_analytics', 'status');
if ($statussetting === 'course_customfield') {
$customfieldid = (int) get_config('local_learning_analytics', 'customfieldid');
if (!$customfieldid) { // setup went wrong, this should not happen in reality
return;
}
$record = $DB->get_record('customfield_data', [
'fieldid' => $customfieldid,
'instanceid' => $COURSE->id,
], 'intvalue');
if ($record === false || $record->intvalue !== '1') {
return;
}
} else if ($statussetting === 'hide_link' || $statussetting === 'disable') {
return;
} else if ($statussetting === 'show_always') {
// just show it, don't filter anything
} else if ($statussetting === 'show_courseids') {
// use courseids of this plugin
$courseids = get_config('local_learning_analytics', 'course_ids');
if ($courseids === false || $courseids === '') {
$courseids = [];
} else {
$courseids = array_map('trim', explode(',', $courseids));
}
if (!in_array($COURSE->id, $courseids)) {
return;
}
} else { // default setting: 'show_if_enabled'
// check if the logstore plugin is enabled, otherwise hide link
$logstoresstr = get_config('tool_log', 'enabled_stores');
$logstores = $logstoresstr ? explode(',', $logstoresstr) : [];
if (!in_array('logstore_lanalytics', $logstores)) {
return;
}
// logging is enabled, check logging scope
$logscope = get_config('logstore_lanalytics', 'log_scope');
if ($logscope !== false && $logscope !== '' && $logscope !== 'all') {
// scope is not all -> check if course should be tracked
$courseids = get_config('logstore_lanalytics', 'course_ids');
if ($courseids === false || $courseids === '') {
$courseids = [];
} else {
$courseids = array_map('trim', explode(',', $courseids));
}
if (($logscope === 'include' && !in_array($COURSE->id, $courseids))
|| ($logscope === 'exclude' && in_array($COURSE->id, $courseids))) {
return;
}
}
}
$node = $navigation;
$settingbeforekey = get_config('local_learning_analytics', 'navigation_position_beforekey');
$beforekey = null;
if ($settingbeforekey === false || $settingbeforekey === '') {
// Find first section node, and add our node before that (to be the last non-section node)
$children = $node->children->type(navigation_node::TYPE_SECTION);
if (count($children) !== 0) {
$beforekey = reset($children)->key;
}
} else { // use setting
$beforekey = $settingbeforekey;
}
if ($node) {
$node->add_node(navigation_node::create(
get_string('navigationlink', 'local_learning_analytics'),
new moodle_url('/local/learning_analytics/index.php/reports/coursedashboard', array('course' => $COURSE->id)),
navigation_node::TYPE_CUSTOM,
null, 'learning_analytics',
new pix_icon('i/report', '')
),
$beforekey
);
}
}
}