forked from xAPI-vle/moodle-logstore_xapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
report.php
344 lines (284 loc) · 11.4 KB
/
report.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?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/>.
/**
* Reporting view.
*
* @package logstore_xapi
* @copyright Jerret Fowler <[email protected]>
* Ryan Smith <https://www.linkedin.com/in/ryan-smith-uk/>
* David Pesce <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\output\notification;
require_once(__DIR__ . '/../../../../../config.php');
require_once($CFG->dirroot . '/lib/adminlib.php');
require_once($CFG->dirroot . '/admin/tool/log/store/xapi/lib.php');
require_once($CFG->dirroot . '/admin/tool/log/store/xapi/classes/form/reportfilter_form.php');
// Set context.
$systemcontext = context_system::instance();
// Add require login and only admin allowed to see this page.
require_login(null, false);
// Read parameters.
$id = optional_param('id', XAPI_REPORT_ID_ERROR, PARAM_INT); // This is the report ID.
$run = optional_param('run', false, PARAM_BOOL);
$page = optional_param('page', XAPI_REPORT_STARTING_PAGE, PARAM_INT);
$perpage = optional_param('perpage', XAPI_REPORT_PERPAGE_DEFAULT, PARAM_INT);
$onpage = optional_param('onpage', XAPI_REPORT_ONPAGE_DEFAULT, PARAM_TEXT);
if ($id == XAPI_REPORT_ID_ERROR) {
$run = true;
}
// Set pagination url's parameter.
$urlparams = array(
'id' => $id,
'run' => $run,
'page' => $page,
'perpage' => $perpage,
'onpage' => $onpage
);
$url = new moodle_url('/admin/tool/log/store/xapi/report.php', array('id' => $id));
// Set page parameters.
$PAGE->set_context($systemcontext);
$PAGE->set_url($url);
// Set filter params and defaults.
$eventnames = logstore_xapi_get_event_names_array();
$defaults = array(
'datefrom' => XAPI_REPORT_DATEFROM_DEFAULT,
'dateto' => XAPI_REPORT_DATETO_DEFAULT,
'eventcontext' => XAPI_REPORT_EVENTCONTEXT_DEFAULT,
'eventnames' => XAPI_REPORT_EVENTNAMES_DEFAULT,
'errortype' => XAPI_REPORT_ERROTYPE_DEFAULT,
'resend' => XAPI_REPORT_RESEND_FALSE,
'response' => XAPI_REPORT_RESPONSE_DEFAULT,
'userfullname' => XAPI_REPORT_USERNAME_DEFAULT,
);
// Reread submitted params.
if (!empty($onpage)) {
$formelements = json_decode($onpage);
foreach (array_keys($defaults) as $element) {
if (isset($formelements->$element) && !empty($formelements->$element)) {
$defaults[$element] = $formelements->$element;
}
}
}
$filterparams = [
'defaults' => $defaults,
'reportid' => $id,
'eventnames' => $eventnames,
];
// Parameter settings depends on report id.
$basetable = XAPI_REPORT_SOURCE_FAILED;
$extraselect = 'x.errortype, x.response';
$pagename = 'logstorexapierrorlog';
$canmanage = false;
switch ($id) {
case XAPI_REPORT_ID_ERROR:
$filterparams['errortypes'] = logstore_xapi_get_distinct_options_from_failed_table('errortype');
$filterparams['responses'] = logstore_xapi_get_distinct_options_from_failed_table('response');
require_capability('logstore/xapi:viewerrorlog', $systemcontext);
if (has_capability('logstore/xapi:manageerrors', $systemcontext)) {
$canmanage = true;
}
break;
case XAPI_REPORT_ID_HISTORIC:
$basetable = XAPI_REPORT_SOURCE_HISTORICAL;
$extraselect = 'u.username, x.contextid';
$pagename = 'logstorexapihistoriclog';
if ($run) {
$filterparams['eventcontexts'] = logstore_xapi_get_logstore_standard_context_options();
} else {
$filterparams['eventcontexts'] = [];
}
require_capability('logstore/xapi:managehistoric', $systemcontext);
$canmanage = true;
break;
default:
break;
}
$notifications = array();
$mform = new logstore_xapi\form\tool_logstore_xapi_reportfilter_form($url, $filterparams);
$params = [];
$where = ['1 = 1'];
// If we have submitted data overwrite form elements from form.
if ($fromform = $mform->get_data()) {
// Set onpage because moodle_url function is not handling arrays.
$formelements = clone $fromform;
unset($formelements->resend, $formelements->submitbutton);
$urlparams['onpage'] = json_encode($formelements);
}
if (isset($formelements)) {
if (!empty($formelements->userfullname)) {
$userfullname = $DB->sql_fullname('u.firstname', 'u.lastname');
$where[] = $DB->sql_like($userfullname, ':userfullname', false, false);
$params['userfullname'] = '%' . $DB->sql_like_escape($formelements->userfullname) . '%';
}
if (!empty($formelements->errortype)) {
$where[] = 'x.errortype = :errortype';
$params['errortype'] = $formelements->errortype;
}
if (!empty($formelements->eventcontext)) {
$where[] = 'x.contextid = :eventcontext';
$params['eventcontext'] = $formelements->eventcontext;
}
if (!empty($formelements->eventnames)) {
$eventnames = $formelements->eventnames;
}
if (!empty($formelements->response)) {
$where[] = 'x.response = :response';
$params['response'] = $formelements->response;
}
if (!empty($formelements->datefrom)) {
$where[] = 'x.timecreated >= :datefrom';
$params['datefrom'] = $formelements->datefrom;
}
if (!empty($formelements->dateto)) {
// Set it to end of the day.
$where[] = 'x.timecreated <= :dateto';
$params['dateto'] = $formelements->dateto + (DAYSECS - 1);
}
}
if ($run) {
list($insql, $inparams) = $DB->get_in_or_equal($eventnames, SQL_PARAMS_NAMED, 'evt');
$where[] = "x.eventname $insql";
$params = array_merge($params, $inparams);
if ($id == XAPI_REPORT_ID_HISTORIC) {
$where[] = "NOT EXISTS (SELECT 1 FROM {logstore_xapi_sent_log} lxsl WHERE lxsl.logstorestandardlogid = x.id)";
}
$where = implode(' AND ', $where);
$sql = "SELECT x.id, x.eventname, u.firstname, u.lastname, x.contextid, x.timecreated, $extraselect
FROM {{$basetable}} x
LEFT JOIN {user} u
ON u.id = x.userid
WHERE $where";
// Resend elements.
$canresenderrors = !empty($fromform->resend) && $fromform->resend == XAPI_REPORT_RESEND_TRUE && $canmanage;
if ($canresenderrors) {
$eventids = array_keys($DB->get_records_sql($sql, $params));
if (!empty($eventids)) {
$mover = new \logstore_xapi\log\moveback($eventids, $id);
if ($mover->execute()) {
$notifications[] = new notification(get_string('resendevents:success', 'logstore_xapi'),
notification::NOTIFY_SUCCESS);
} else {
$notifications[] = new notification(get_string('resendevents:failed', 'logstore_xapi'),
notification::NOTIFY_ERROR);
}
}
}
}
// Instantiate a class for populating some form data.
$submitcount = new stdClass();
$submitcount->resend = XAPI_REPORT_RESEND_FALSE;
if ($run) {
// Collect events to create view.
$results = $DB->get_records_sql($sql, $params, $page * $perpage, $perpage);
$sql = "SELECT COUNT(x.id)
FROM {{$basetable}} x
LEFT JOIN {user} u
ON u.id = x.userid
WHERE $where";
$count = $DB->count_records_sql($sql, $params);
} else {
// No results will be showing so count is 0.
$count = 0;
}
// Now we have the count we can set this value for the submit button.
$submitcount->resendselected = get_string('resendevents', 'logstore_xapi', ['count' => $count]);
$mform->set_data($submitcount);
if (!empty($results)) {
$table = new html_table();
$table->head = array();
$table->attributes['class'] = 'admintable generaltable';
if ($id == XAPI_REPORT_ID_ERROR) {
$table->head[] = get_string('type', 'logstore_xapi');
}
$table->head[] = get_string('eventname', 'logstore_xapi');
if ($id == XAPI_REPORT_ID_HISTORIC) {
$table->head[] = get_string('username', 'logstore_xapi');
$table->head[] = get_string('eventcontext', 'logstore_xapi');
}
if ($id == XAPI_REPORT_ID_ERROR) {
$table->head[] = get_string('response', 'logstore_xapi');
}
$table->head[] = get_string('info', 'logstore_xapi');
$table->head[] = get_string('datetimegmt', 'logstore_xapi');
$table->head[] = '';
$table->id = "report";
foreach ($results as $result) {
$row = [];
if ($id == XAPI_REPORT_ID_ERROR) {
$row[] = $result->errortype;
}
$row[] = $result->eventname;
if ($id == XAPI_REPORT_ID_HISTORIC) {
$row[] = $result->username;
if ($context = context::instance_by_id($result->contextid, IGNORE_MISSING)) {
$row[] = $context->get_context_name();
} else {
$row[] = get_string('contextidnolongerexists', 'logstore_xapi', $result->contextid);
}
}
if ($id == XAPI_REPORT_ID_ERROR) {
$response = '';
if (isset($result->response)) {
$response = '<pre>' . json_encode(logstore_xapi_decode_response($result->response), JSON_PRETTY_PRINT) . '</pre>';
} else {
$response = '-';
}
$row[] = $response;
}
$row[] = logstore_xapi_get_info_string($result);
$row[] = userdate($result->timecreated);
// Add container to the individual reply statements.
$replycontainer = \html_writer::start_span('reply-event', ['id' => 'reply-event-id-' . $result->id]);
$replycontainer .= \html_writer::end_span();
$row[] = $replycontainer;
$table->data[] = $row;
}
}
// Set pagination url.
$paginationurl = new moodle_url('/admin/tool/log/store/xapi/report.php', $urlparams);
// Define the page layout and header/breadcrumb.
$PAGE->set_pagelayout('report');
$PAGE->set_title(get_string($pagename, 'logstore_xapi'));
$PAGE->set_heading(get_string($pagename, 'logstore_xapi'));
// Add requested items to the page view.
if ($canmanage && $run) {
$PAGE->requires->js_call_amd('logstore_xapi/replayevents', 'init', [$count, XAPI_REPORT_RESEND_FALSE, XAPI_REPORT_RESEND_TRUE]);
}
$PAGE->requires->css('/admin/tool/log/store/xapi/styles.css');
// Show page.
echo $OUTPUT->header();
if (!empty($notifications)) {
foreach ($notifications as $notification) {
echo $OUTPUT->render($notification);
}
}
echo \html_writer::start_div('', ['id' => 'xapierrorlog']);
echo \html_writer::start_div('', ['id' => 'xapierrorlog_form']);
$mform->display();
echo \html_writer::end_div();
if ($run) {
if (empty($results)) {
echo $OUTPUT->heading(get_string('noerrorsfound', 'logstore_xapi'));
} else {
echo \html_writer::start_div('no-overflow', ['id' => 'xapierrorlog_data']);
echo \html_writer::table($table);
echo \html_writer::end_div();
echo $OUTPUT->paging_bar($count, $page, $perpage, $paginationurl);
}
}
echo \html_writer::end_div();
echo $OUTPUT->footer();