weekly release 2.3dev
[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
0c68d916
NC
32/// basic access checks
33if (!$course = $DB->get_record('course', array('id' => $courseid))) {
34 print_error('nocourseid');
35}
36$context = get_context_instance(CONTEXT_COURSE, $course->id);
37require_login($course);
38
b244b9b7 39switch ($action) {
40 case 'update':
9d90b998
NC
41 if (!confirm_sesskey()) {
42 break;
43 }
0c68d916
NC
44 require_capability('moodle/grade:edit', $context);
45
b244b9b7 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!
14398fd6 49 print_error('invalidgradeitmeid');
b244b9b7 50 }
d24832f9 51
b244b9b7 52 /**
53 * Code copied from grade/report/grader/lib.php line 187+
54 */
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;
69 }
70 } else {
71 $finalgrade = unformat_float($newvalue);
72 }
73
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';
82 }
83
84 if ($errorstr) {
5c75a0a3 85 $user = $DB->get_record('user', array('id' => $userid), 'id, firstname, lastname');
ace9051c 86 $gradestr = new stdClass();
b244b9b7 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";
d24832f9 91
b244b9b7 92 }
93
94 $finalvalue = $finalgrade;
95
96 } else if ($type == 'feedback') {
97 $finalgrade = false;
98 $trimmed = trim($newvalue);
99 if (empty($trimmed)) {
100 $feedback = NULL;
101 } else {
294ce987 102 $feedback = $newvalue;
b244b9b7 103 }
104
105 $finalvalue = $feedback;
106 }
d24832f9 107
b244b9b7 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;
d24832f9 113
b244b9b7 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!";
913d9dc3 120 echo json_encode($json_object);
b244b9b7 121 die();
122 }
d24832f9 123
b244b9b7 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 "
d24832f9 126 . "FROM {grade_grades} gg, {grade_items} gi WHERE "
5c75a0a3 127 . "gi.courseid = ? AND gg.itemid = gi.id AND gg.userid = ?";
128 $records = $DB->get_records_sql($sql, array($courseid, $userid));
b244b9b7 129 $json_object->row = $records;
130 echo json_encode($json_object);
131 die();
132 }
133 } else {
134 $json_object = new stdClass();
135 $json_object->result = "error";
d24832f9 136 $json_object->message = "Missing parameter to ajax UPDATE callback: \n" .
b244b9b7 137 " userid: $userid,\n itemid: $itemid\n, type: $type\n, newvalue: $newvalue";
138 echo json_encode($json_object);
139 }
140
141 break;
142}
143
6c3ef410 144