forked from iandavidwild/moodle-gradebook-marking-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audit.php
202 lines (169 loc) · 6.84 KB
/
audit.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
<?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/>.
/**
* Script to view grade audit trail. Based on view.php, part of customsql, copyright 2009 The Open University
*
*/
global $CFG, $OUTPUT, $PAGE;
require_once '../../../config.php';
require_once $CFG->dirroot.'/grade/report/marking/auditlib.php';
require_once '../../lib.php';
require_once $CFG->libdir.'/adminlib.php';
require_once $CFG->libdir.'/validateurlsyntax.php';
$courseid = required_param('courseid', PARAM_INT);
$id = optional_param('id', 0, PARAM_INT);
$itemid = optional_param('itemid', 0, PARAM_INT);
$userid = optional_param('userid', 0, PARAM_INT);
$url = new moodle_url('/grade/report/marking/audit.php', array('courseid'=>$courseid));
if ($id !== 0) {
$url->param('id', $id);
}
if ($itemid !== 0) {
$url->param('itemid', $itemid);
}
if ($userid !== 0) {
$url->param('userid', $userid);
}
$PAGE->set_url($url);
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
$PAGE->set_pagelayout('incourse');
require_login($course);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
if (!has_capability('moodle/grade:manage', $context)) {
require_capability('moodle/grade:edit', $context);
}
// security checks!
if (!empty($id)) {
if (!$grade = $DB->get_record('grade_grades', array('id' => $id))) {
print_error('invalidgroupid');
}
if (!empty($itemid) and $itemid != $grade->itemid) {
print_error('invaliditemid');
}
$itemid = $grade->itemid;
if (!empty($userid) and $userid != $grade->userid) {
print_error('invaliduser');
}
$userid = $grade->userid;
unset($grade);
} else if (empty($userid) or empty($itemid)) {
print_error('missinguseranditemid');
}
if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) {
print_error('cannotfindgradeitem');
}
// now verify grading user has access to all groups or is member of the same group when separate groups used in course
if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
if ($groups = groups_get_all_groups($COURSE->id, $userid)) {
$ok = false;
foreach ($groups as $group) {
if (groups_is_member($group->id, $USER->id)) {
$ok = true;
}
}
if (!$ok) {
print_error('cannotgradeuser');
}
} else {
print_error('cannotgradeuser');
}
}
// Start the page.
/// Print header
$reportname = get_string('grade_audit', 'gradereport_marking'); // ... which it isn't, but that will do for now.
print_grade_page_head($COURSE->id, 'report', 'marking', $reportname, false, '');
// the SQL query:
$report =
'SELECT
CASE
WHEN gh.action=1 THEN \'inserted\'
WHEN gh.action=2 THEN \'edited\'
WHEN gh.action=3 THEN \'deleted\'
END AS \'Action\',
FROM_UNIXTIME(gh.timemodified,\'%Y %M %D %h:%i:%s\') AS time,
CONCAT(lu.firstname,\' \', lu.lastname) AS \'Editing User\',
CONCAT(u.firstname,\' \', u.lastname) AS \'Edited User\',
gh.finalgrade AS \'Final Grade\',
gh.feedback AS \'Feedback\'
FROM prefix_grade_grades_history AS gh
JOIN prefix_user AS lu ON lu.id=gh.loggeduser
JOIN prefix_user AS u ON u.id=gh.userid
WHERE u.id=\''.$userid.'\' AND gh.itemid=\''.$itemid.'\' ORDER BY time ASC';
// Clean up on the way in
grade_audit_delete_old_temp_files();
$csvfilename = NULL; // just the name of the file, e.g. XXXXX.csv
$csvfilepath = NULL; // full path to the file
try {
list($csvfilepath, $csvfilename) = grade_audit_generate_csv($report, time());
} catch (Exception $e) {
print_error('queryfailed', 'gradereport_marking', grade_audit_url('index.php'),
$e->getMessage());
}
// output some details about this report...
$course_details = get_string('grade_audit_course', 'gradereport_marking').$courseid;
$user_details = get_string('grade_audit_user', 'gradereport_marking').$userid;
$item_details = get_string('grade_audit_item', 'gradereport_marking').$itemid;
$sql_query = get_string('grade_audit_query', 'gradereport_marking').$report;
debugging(format_text($course_details.', '.$user_details.', '.$item_details.', <br/>'.$sql_query, FORMAT_HTML), DEBUG_DEVELOPER);
$count = 0;
if (is_null($csvfilepath)) {
echo html_writer::tag('p', get_string('noaudittrail', 'gradereport_marking'));
} else {
debugging(format_text('$csvfilepath: '.$csvfilepath, FORMAT_HTML), DEBUG_DEVELOPER);
debugging(format_text('$csvfilename: '.$csvfilename, FORMAT_HTML), DEBUG_DEVELOPER);
if (!is_readable($csvfilepath)) {
echo html_writer::tag('p', get_string('filereaderror', 'gradereport_marking'). $csvfilepath);
} else {
$handle = fopen($csvfilepath, 'r');
$table = new html_table();
$table->head = fgetcsv($handle);
while ($row = fgetcsv($handle)) {
$rowdata = array();
foreach ($row as $value) {
if (validateUrlSyntax($value, 's+H?S?F?E?u-P-a?I?p?f?q?r?')) {
$rowdata[] = '<a href="' . $value . '">' . $value . '</a>';
} else {
$rowdata[] = $value;
}
}
$table->data[] = $rowdata;
$count += 1;
}
fclose($handle);
echo html_writer::table($table);
if ($count >= GRADE_AUDIT_MAX_RECORDS) {
echo html_writer::tag('p', get_string('recordlimitreached', 'gradereport_marking',
GRADE_AUDIT_MAX_RECORDS),
array('class' => 'admin_note'));
}
echo html_writer::start_tag('p').
html_writer::tag('a', get_string('downloadthisreportascsv', 'gradereport_marking'),
array('href' => new moodle_url(grade_audit_url('download.php'),
array('filename' => $csvfilename)))).
html_writer::end_tag('p');
}
}
$imglarrow = html_writer::tag('img', '', array('src' => $OUTPUT->pix_url('t/collapsed_rtl'),
'class' => 'iconsmall',
'alt' => ''));
echo html_writer::start_tag('p').
$OUTPUT->action_link(new moodle_url(grade_audit_url('index.php?id='.$courseid)), $imglarrow.
get_string('backtomarkingreport', 'gradereport_marking')).
html_writer::end_tag('p').
$OUTPUT->footer();
?>