MDL-20418 Entirely removed the duplication of code by refactoring the way the grader...
[moodle.git] / grade / report / grader / ajax_callbacks.php
CommitLineData
e060e33d 1<?php
b244b9b7 2
e060e33d 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.
14//
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/>.
b244b9b7 17
18
d24832f9 19require_once '../../../config.php';
b244b9b7 20require_once $CFG->libdir.'/gradelib.php';
21require_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';
24
6472929b 25$courseid = required_param('id', PARAM_INT); // course id
b244b9b7 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);
31
32switch ($action) {
33 case 'update':
34 if (!empty($userid) && !empty($itemid) && $newvalue !== false && !empty($type)) {
35 // Save the grade or feedback
36 if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) { // we must verify course id here!
14398fd6 37 print_error('invalidgradeitmeid');
b244b9b7 38 }
d24832f9 39
b244b9b7 40 /**
41 * Code copied from grade/report/grader/lib.php line 187+
42 */
43 $warnings = array();
44 $finalvalue = null;
45 $finalgrade = null;
46 $feedback = null;
47 $json_object = new stdClass();
48 // Pre-process grade
49 if ($type == 'value' || $type == 'scale') {
50 $feedback = false;
51 $feedbackformat = false;
52 if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
53 if ($newvalue == -1) { // -1 means no grade
54 $finalgrade = null;
55 } else {
56 $finalgrade = $newvalue;
57 }
58 } else {
59 $finalgrade = unformat_float($newvalue);
60 }
61
62 $errorstr = '';
63 // Warn if the grade is out of bounds.
64 if (is_null($finalgrade)) {
65 // ok
66 } else if ($finalgrade < $grade_item->grademin) {
67 $errorstr = 'lessthanmin';
68 } else if ($finalgrade > $grade_item->grademax) {
69 $errorstr = 'morethanmax';
70 }
71
72 if ($errorstr) {
5c75a0a3 73 $user = $DB->get_record('user', array('id' => $userid), 'id, firstname, lastname');
b244b9b7 74 $gradestr = new object();
75 $gradestr->username = fullname($user);
76 $gradestr->itemname = $grade_item->get_name();
77 $json_object->message = get_string($errorstr, 'grades', $gradestr);
78 $json_object->result = "error";
d24832f9 79
b244b9b7 80 }
81
82 $finalvalue = $finalgrade;
83
84 } else if ($type == 'feedback') {
85 $finalgrade = false;
86 $trimmed = trim($newvalue);
87 if (empty($trimmed)) {
88 $feedback = NULL;
89 } else {
294ce987 90 $feedback = $newvalue;
b244b9b7 91 }
92
93 $finalvalue = $feedback;
94 }
d24832f9 95
b244b9b7 96 if (!empty($json_object->result) && $json_object->result == 'error') {
97 echo json_encode($json_object);
98 die();
99 } else {
100 $json_object->gradevalue = $finalvalue;
d24832f9 101
b244b9b7 102 if ($grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE)) {
103 $json_object->result = 'success';
104 $json_object->message = false;
105 } else {
106 $json_object->result = 'error';
107 $json_object->message = "TO BE LOCALISED: Failure to update final grade!";
108 echo json_encode();
109 die();
110 }
d24832f9 111
b244b9b7 112 // Get row data
113 $sql = "SELECT gg.id, gi.id AS itemid, gi.scaleid AS scale, gg.userid AS userid, finalgrade, gg.overridden AS overridden "
d24832f9 114 . "FROM {grade_grades} gg, {grade_items} gi WHERE "
5c75a0a3 115 . "gi.courseid = ? AND gg.itemid = gi.id AND gg.userid = ?";
116 $records = $DB->get_records_sql($sql, array($courseid, $userid));
b244b9b7 117 $json_object->row = $records;
118 echo json_encode($json_object);
119 die();
120 }
121 } else {
122 $json_object = new stdClass();
123 $json_object->result = "error";
d24832f9 124 $json_object->message = "Missing parameter to ajax UPDATE callback: \n" .
b244b9b7 125 " userid: $userid,\n itemid: $itemid\n, type: $type\n, newvalue: $newvalue";
126 echo json_encode($json_object);
127 }
128
129 break;
130}
131
132?>