forked from iandavidwild/moodle-gradebook-marking-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blindmark.php
290 lines (236 loc) · 10.2 KB
/
blindmark.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
<?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/>.
require_once '../../../config.php';
require_once $CFG->dirroot.'/grade/lib.php';
require_once $CFG->dirroot.'/grade/report/lib.php';
require_once './blindmark_form.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/blindmark.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);
}
// default return url
$gpr = new grade_plugin_return();
$returnurl = $gpr->get_return_url($CFG->wwwroot.'/grade/report.php?id='.$course->id);
// 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');
}
}
$mform = new edit_grade_form(null, array('grade_item'=>$grade_item, 'gpr'=>$gpr));
if ($grade = $DB->get_record('grade_grades', array('itemid' => $grade_item->id, 'userid' => $userid))) {
// always clean existing feedback - grading should not have XSS risk
if (can_use_html_editor()) {
if (empty($grade->feedback)) {
$grade->feedback = '';
} else {
$options = new stdClass();
$options->smiley = false;
$options->filter = false;
$options->noclean = false;
$options->para = false;
$grade->feedback = format_text($grade->feedback, $grade->feedbackformat, $options);
}
$grade->feedbackformat = FORMAT_HTML;
} else {
$grade->feedback = clean_text($grade->feedback, $grade->feedbackformat);
}
$grade->locked = $grade->locked > 0 ? 1:0;
$grade->overridden = $grade->overridden > 0 ? 1:0;
$grade->excluded = $grade->excluded > 0 ? 1:0;
if ($grade->hidden > 1) {
$grade->hiddenuntil = $grade->hidden;
$grade->hidden = 1;
} else {
$grade->hiddenuntil = 0;
}
if ($grade_item->is_hidden()) {
$grade->hidden = 1;
}
if ($grade_item->is_locked()) {
$grade->locked = 1;
}
// normalize the final grade value
if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
if (empty($grade->finalgrade)) {
$grade->finalgrade = -1;
} else {
$grade->finalgrade = (int)$grade->finalgrade;
}
} else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
$grade->finalgrade = format_float($grade->finalgrade, $grade_item->get_decimals());
}
$grade->oldgrade = $grade->finalgrade;
$grade->oldfeedback = $grade->feedback;
$grade->feedback = array('text'=>$grade->feedback, 'format'=>$grade->feedbackformat);
$mform->set_data($grade);
} else {
$grade->feedback = array('text'=>'', 'format'=>FORMAT_HTML);
$mform->set_data(array('itemid'=>$itemid, 'userid'=>$userid, 'locked'=>$grade_item->locked, 'locktime'=>$grade_item->locktime));
}
if ($mform->is_cancelled()) {
redirect($returnurl);
// form processing
} else if ($data = $mform->get_data(false)) {
if (is_array($data->feedback)) {
$data->feedbackformat = $data->feedback['format'];
$data->feedback = $data->feedback['text'];
}
$old_grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true); //might not exist yet
// fix no grade for scales
if (!isset($data->finalgrade) or $data->finalgrade == $data->oldgrade) {
$data->finalgrade = $old_grade_grade->finalgrade;
} else if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
if ($data->finalgrade < 1) {
$data->finalgrade = NULL;
}
} else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
$data->finalgrade = unformat_float($data->finalgrade);
} else {
//this should not happen
$data->finalgrade = $old_grade_grade->finalgrade;
}
// the overriding of feedback is tricky - we have to care about external items only
if (!property_exists($data, 'feedback') or $data->feedback == $data->oldfeedback) {
$data->feedback = $old_grade_grade->feedback;
$data->feedbackformat = $old_grade_grade->feedbackformat;
}
// update final grade or feedback
$grade_item->update_final_grade($data->userid, $data->finalgrade, 'editgrade', $data->feedback, $data->feedbackformat);
$grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true);
$grade_grade->grade_item =& $grade_item; // no db fetching
if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:edit', $context)) {
if (!grade_floats_different($data->finalgrade, $old_grade_grade->finalgrade)
and $data->feedback === $old_grade_grade->feedback) {
// change overridden flag only if grade or feedback not changed
if (!isset($data->overridden)) {
$data->overridden = 0; // checkbox
}
$grade_grade->set_overridden($data->overridden);
}
}
if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:hide', $context)) {
$hidden = empty($data->hidden) ? 0: $data->hidden;
$hiddenuntil = empty($data->hiddenuntil) ? 0: $data->hiddenuntil;
if ($grade_item->is_hidden()) {
if ($old_grade_grade->hidden == 1 and $hiddenuntil == 0) {
//nothing to do - grade was originally hidden, we want to keep it that way
} else {
$grade_grade->set_hidden($hiddenuntil);
}
} else {
if ($hiddenuntil) {
$grade_grade->set_hidden($hiddenuntil);
} else {
$grade_grade->set_hidden($hidden); // checkbox data might be undefined
}
}
}
if (isset($data->locked) and !$grade_item->is_locked()) {
if (($old_grade_grade->locked or $old_grade_grade->locktime)
and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context))) {
//ignore data
} else if ((!$old_grade_grade->locked and !$old_grade_grade->locktime)
and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context))) {
//ignore data
} else {
$grade_grade->set_locktime($data->locktime); //set_lock may reset locktime
$grade_grade->set_locked($data->locked, false, true);
// reload grade in case it was regraded from activity
$grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true);
$grade_grade->grade_item =& $grade_item; // no db fetching
}
}
if (isset($data->excluded) and has_capability('moodle/grade:manage', $context)) {
$grade_grade->set_excluded($data->excluded);
}
// detect cases when we need to do full regrading
if ($old_grade_grade->excluded != $grade_grade->excluded) {
$parent = $grade_item->get_parent_category();
$parent->force_regrading();
} else if ($old_grade_grade->overridden != $grade_grade->overridden and empty($grade_grade->overridden)) { // only when unoverridding
$grade_item->force_regrading();
} else if ($old_grade_grade->locktime != $grade_grade->locktime) {
$grade_item->force_regrading();
}
redirect($returnurl);
}
$strgrades = get_string('grades');
$strmarkingreport = get_string('markingreport', 'gradereport_marking');
$strgradeedit = get_string('editgrade', 'grades');
$struser = get_string('user');
grade_build_nav(__FILE__, $strgradeedit, array('courseid' => $courseid));
/*********** BEGIN OUTPUT *************/
$PAGE->set_title($strgrades . ': ' . $strmarkingreport . ': ' . $strgradeedit);
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
echo $OUTPUT->heading($strgradeedit);
echo $OUTPUT->box_start();
// Form if in edit or add modes
$mform->display();
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
die;