Merge branch 'wip-MDL-27145-master' of git://github.com/samhemelryk/moodle
[moodle.git] / grade / report / grader / ajax_callbacks.php
blob463f1d6e245d0b6bcf814a098f344748278b72ea
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 require_once '../../../config.php';
20 require_once $CFG->libdir.'/gradelib.php';
21 require_once $CFG->dirroot.'/grade/lib.php';
22 // require_once $CFG->dirroot.'/grade/report/grader/ajaxlib.php';
23 // require_once $CFG->dirroot.'/grade/report/grader/lib.php';
25 $courseid = required_param('id', PARAM_INT); // course id
26 $userid = optional_param('userid', false, PARAM_INT);
27 $itemid = optional_param('itemid', false, PARAM_INT);
28 $type = optional_param('type', false, PARAM_ALPHA);
29 $action = optional_param('action', false, PARAM_ALPHA);
30 $newvalue = optional_param('newvalue', false, PARAM_MULTILANG);
32 /// basic access checks
33 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
34 print_error('nocourseid');
36 $context = get_context_instance(CONTEXT_COURSE, $course->id);
37 require_login($course);
39 switch ($action) {
40 case 'update':
41 if (!confirm_sesskey()) {
42 break;
44 require_capability('moodle/grade:edit', $context);
46 if (!empty($userid) && !empty($itemid) && $newvalue !== false && !empty($type)) {
47 // Save the grade or feedback
48 if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) { // we must verify course id here!
49 print_error('invalidgradeitmeid');
52 /**
53 * Code copied from grade/report/grader/lib.php line 187+
55 $warnings = array();
56 $finalvalue = null;
57 $finalgrade = null;
58 $feedback = null;
59 $json_object = new stdClass();
60 // Pre-process grade
61 if ($type == 'value' || $type == 'scale') {
62 $feedback = false;
63 $feedbackformat = false;
64 if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
65 if ($newvalue == -1) { // -1 means no grade
66 $finalgrade = null;
67 } else {
68 $finalgrade = $newvalue;
70 } else {
71 $finalgrade = unformat_float($newvalue);
74 $errorstr = '';
75 // Warn if the grade is out of bounds.
76 if (is_null($finalgrade)) {
77 // ok
78 } else if ($finalgrade < $grade_item->grademin) {
79 $errorstr = 'lessthanmin';
80 } else if ($finalgrade > $grade_item->grademax) {
81 $errorstr = 'morethanmax';
84 if ($errorstr) {
85 $user = $DB->get_record('user', array('id' => $userid), 'id, firstname, lastname');
86 $gradestr = new stdClass();
87 $gradestr->username = fullname($user);
88 $gradestr->itemname = $grade_item->get_name();
89 $json_object->message = get_string($errorstr, 'grades', $gradestr);
90 $json_object->result = "error";
94 $finalvalue = $finalgrade;
96 } else if ($type == 'feedback') {
97 $finalgrade = false;
98 $trimmed = trim($newvalue);
99 if (empty($trimmed)) {
100 $feedback = NULL;
101 } else {
102 $feedback = $newvalue;
105 $finalvalue = $feedback;
108 if (!empty($json_object->result) && $json_object->result == 'error') {
109 echo json_encode($json_object);
110 die();
111 } else {
112 $json_object->gradevalue = $finalvalue;
114 if ($grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE)) {
115 $json_object->result = 'success';
116 $json_object->message = false;
117 } else {
118 $json_object->result = 'error';
119 $json_object->message = "TO BE LOCALISED: Failure to update final grade!";
120 echo json_encode($json_object);
121 die();
124 // Get row data
125 $sql = "SELECT gg.id, gi.id AS itemid, gi.scaleid AS scale, gg.userid AS userid, finalgrade, gg.overridden AS overridden "
126 . "FROM {grade_grades} gg, {grade_items} gi WHERE "
127 . "gi.courseid = ? AND gg.itemid = gi.id AND gg.userid = ?";
128 $records = $DB->get_records_sql($sql, array($courseid, $userid));
129 $json_object->row = $records;
130 echo json_encode($json_object);
131 die();
133 } else {
134 $json_object = new stdClass();
135 $json_object->result = "error";
136 $json_object->message = "Missing parameter to ajax UPDATE callback: \n" .
137 " userid: $userid,\n itemid: $itemid\n, type: $type\n, newvalue: $newvalue";
138 echo json_encode($json_object);
141 break;