forked from iandavidwild/moodle-gradebook-marking-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax_callbacks.php
143 lines (123 loc) · 5.55 KB
/
ajax_callbacks.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
<?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->libdir.'/gradelib.php';
require_once $CFG->dirroot.'/grade/lib.php';
$courseid = required_param('id', PARAM_INT); // course id
$userid = optional_param('userid', false, PARAM_INT);
$itemid = optional_param('itemid', false, PARAM_INT);
$type = optional_param('type', false, PARAM_ALPHA);
$action = optional_param('action', false, PARAM_ALPHA);
$newvalue = optional_param('newvalue', false, PARAM_MULTILANG);
/// basic access checks
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
$context = get_context_instance(CONTEXT_COURSE, $course->id);
require_login($course);
switch ($action) {
case 'update':
if (!confirm_sesskey()) {
break;
}
require_capability('moodle/grade:edit', $context);
if (!empty($userid) && !empty($itemid) && $newvalue !== false && !empty($type)) {
// Save the grade or feedback
if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) { // we must verify course id here!
print_error('invalidgradeitmeid');
}
/**
* Code copied from grade/report/grader/lib.php line 187+
*/
$warnings = array();
$finalvalue = null;
$finalgrade = null;
$feedback = null;
$json_object = new stdClass();
// Pre-process grade
if ($type == 'value' || $type == 'scale') {
$feedback = false;
$feedbackformat = false;
if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
if ($newvalue == -1) { // -1 means no grade
$finalgrade = null;
} else {
$finalgrade = $newvalue;
}
} else {
$finalgrade = unformat_float($newvalue);
}
$errorstr = '';
// Warn if the grade is out of bounds.
if (is_null($finalgrade)) {
// ok
} else if ($finalgrade < $grade_item->grademin) {
$errorstr = 'lessthanmin';
} else if ($finalgrade > $grade_item->grademax) {
$errorstr = 'morethanmax';
}
if ($errorstr) {
// IDW 20/08/2012 Don't show a user's full name. It should be anonymous
$user = $DB->get_record('user', array('id' => $userid), 'id, username');
$gradestr = new stdClass();
$gradestr->username = $user->username;
$gradestr->itemname = $grade_item->get_name();
$json_object->message = get_string($errorstr, 'grades', $gradestr);
$json_object->result = "error";
}
$finalvalue = $finalgrade;
} else if ($type == 'feedback') {
$finalgrade = false;
$trimmed = trim($newvalue);
if (empty($trimmed)) {
$feedback = NULL;
} else {
$feedback = $newvalue;
}
$finalvalue = $feedback;
}
if (!empty($json_object->result) && $json_object->result == 'error') {
echo json_encode($json_object);
die();
} else {
$json_object->gradevalue = $finalvalue;
if ($grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE)) {
$json_object->result = 'success';
$json_object->message = false;
} else {
$json_object->result = 'error';
$json_object->message = "TO BE LOCALISED: Failure to update final grade!";
echo json_encode($json_object);
die();
}
// Get row data
$sql = "SELECT gg.id, gi.id AS itemid, gi.scaleid AS scale, gg.userid AS userid, finalgrade, gg.overridden AS overridden "
. "FROM {grade_grades} gg, {grade_items} gi WHERE "
. "gi.courseid = ? AND gg.itemid = gi.id AND gg.userid = ?";
$records = $DB->get_records_sql($sql, array($courseid, $userid));
$json_object->row = $records;
echo json_encode($json_object);
die();
}
} else {
$json_object = new stdClass();
$json_object->result = "error";
$json_object->message = "Missing parameter to ajax UPDATE callback: \n" .
" userid: $userid,\n itemid: $itemid\n, type: $type\n, newvalue: $newvalue";
echo json_encode($json_object);
}
break;
}