MDL-40697 core_grades: removed existing user_graded event triggers
[moodle.git] / grade / report / grader / ajax_callbacks.php
CommitLineData
e060e33d 1<?php
e060e33d 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
b244b9b7 16
a153c9f2
AD
17/**
18 * This file receives ajax callbacks for the grader report
19 *
20 * @package gradereport_grader
21 * @copyright 2008 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
b244b9b7 24
d24832f9 25require_once '../../../config.php';
b244b9b7 26require_once $CFG->libdir.'/gradelib.php';
27require_once $CFG->dirroot.'/grade/lib.php';
28// require_once $CFG->dirroot.'/grade/report/grader/ajaxlib.php';
29// require_once $CFG->dirroot.'/grade/report/grader/lib.php';
30
6472929b 31$courseid = required_param('id', PARAM_INT); // course id
b244b9b7 32$userid = optional_param('userid', false, PARAM_INT);
33$itemid = optional_param('itemid', false, PARAM_INT);
34$type = optional_param('type', false, PARAM_ALPHA);
35$action = optional_param('action', false, PARAM_ALPHA);
071e68f9 36$newvalue = optional_param('newvalue', false, PARAM_TEXT);
b244b9b7 37
0c68d916
NC
38/// basic access checks
39if (!$course = $DB->get_record('course', array('id' => $courseid))) {
40 print_error('nocourseid');
41}
d4060472 42$context = context_course::instance($course->id);
0c68d916
NC
43require_login($course);
44
b244b9b7 45switch ($action) {
46 case 'update':
9d90b998
NC
47 if (!confirm_sesskey()) {
48 break;
49 }
0c68d916
NC
50 require_capability('moodle/grade:edit', $context);
51
b244b9b7 52 if (!empty($userid) && !empty($itemid) && $newvalue !== false && !empty($type)) {
53 // Save the grade or feedback
54 if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) { // we must verify course id here!
af4f6172 55 print_error('invalidgradeitemid');
b244b9b7 56 }
d24832f9 57
b244b9b7 58 /**
59 * Code copied from grade/report/grader/lib.php line 187+
60 */
61 $warnings = array();
62 $finalvalue = null;
63 $finalgrade = null;
64 $feedback = null;
65 $json_object = new stdClass();
66 // Pre-process grade
67 if ($type == 'value' || $type == 'scale') {
68 $feedback = false;
69 $feedbackformat = false;
70 if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
71 if ($newvalue == -1) { // -1 means no grade
72 $finalgrade = null;
73 } else {
74 $finalgrade = $newvalue;
75 }
76 } else {
77 $finalgrade = unformat_float($newvalue);
78 }
79
80 $errorstr = '';
81 // Warn if the grade is out of bounds.
82 if (is_null($finalgrade)) {
83 // ok
01da2d36
AD
84 } else {
85 $bounded = $grade_item->bounded_grade($finalgrade);
86 if ($bounded > $finalgrade) {
87 $errorstr = 'lessthanmin';
88 } else if ($bounded < $finalgrade) {
89 $errorstr = 'morethanmax';
90 }
b244b9b7 91 }
92
93 if ($errorstr) {
5c75a0a3 94 $user = $DB->get_record('user', array('id' => $userid), 'id, firstname, lastname');
ace9051c 95 $gradestr = new stdClass();
b244b9b7 96 $gradestr->username = fullname($user);
97 $gradestr->itemname = $grade_item->get_name();
98 $json_object->message = get_string($errorstr, 'grades', $gradestr);
99 $json_object->result = "error";
d24832f9 100
b244b9b7 101 }
102
103 $finalvalue = $finalgrade;
104
105 } else if ($type == 'feedback') {
106 $finalgrade = false;
107 $trimmed = trim($newvalue);
108 if (empty($trimmed)) {
109 $feedback = NULL;
110 } else {
294ce987 111 $feedback = $newvalue;
b244b9b7 112 }
113
114 $finalvalue = $feedback;
115 }
d24832f9 116
b244b9b7 117 if (!empty($json_object->result) && $json_object->result == 'error') {
118 echo json_encode($json_object);
119 die();
120 } else {
121 $json_object->gradevalue = $finalvalue;
d24832f9 122
b244b9b7 123 if ($grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE)) {
124 $json_object->result = 'success';
125 $json_object->message = false;
126 } else {
127 $json_object->result = 'error';
128 $json_object->message = "TO BE LOCALISED: Failure to update final grade!";
913d9dc3 129 echo json_encode($json_object);
b244b9b7 130 die();
131 }
d24832f9 132
b244b9b7 133 // Get row data
134 $sql = "SELECT gg.id, gi.id AS itemid, gi.scaleid AS scale, gg.userid AS userid, finalgrade, gg.overridden AS overridden "
d24832f9 135 . "FROM {grade_grades} gg, {grade_items} gi WHERE "
5c75a0a3 136 . "gi.courseid = ? AND gg.itemid = gi.id AND gg.userid = ?";
137 $records = $DB->get_records_sql($sql, array($courseid, $userid));
b244b9b7 138 $json_object->row = $records;
139 echo json_encode($json_object);
140 die();
141 }
142 } else {
143 $json_object = new stdClass();
144 $json_object->result = "error";
d24832f9 145 $json_object->message = "Missing parameter to ajax UPDATE callback: \n" .
b244b9b7 146 " userid: $userid,\n itemid: $itemid\n, type: $type\n, newvalue: $newvalue";
147 echo json_encode($json_object);
148 }
149
150 break;
151}
152
6c3ef410 153