MDL-14967 Bug in DB upgrade for get_coursemodule_from_instance: named params were...
[moodle.git] / grade / report / grader / ajax_callbacks.php
CommitLineData
b244b9b7 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
7// http://moodle.org //
8// //
9// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
10// //
11// This program is free software; you can redistribute it and/or modify //
12// it under the terms of the GNU General Public License as published by //
13// the Free Software Foundation; either version 2 of the License, or //
14// (at your option) any later version. //
15// //
16// This program is distributed in the hope that it will be useful, //
17// but WITHOUT ANY WARRANTY; without even the implied warranty of //
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19// GNU General Public License for more details: //
20// //
21// http://www.gnu.org/copyleft/gpl.html //
22// //
23///////////////////////////////////////////////////////////////////////////
24
25
26require_once '../../../config.php';
27require_once $CFG->libdir.'/gradelib.php';
28require_once $CFG->dirroot.'/grade/lib.php';
29// require_once $CFG->dirroot.'/grade/report/grader/ajaxlib.php';
30// require_once $CFG->dirroot.'/grade/report/grader/lib.php';
31
32$courseid = required_param('id'); // course id
33$userid = optional_param('userid', false, PARAM_INT);
34$itemid = optional_param('itemid', false, PARAM_INT);
35$type = optional_param('type', false, PARAM_ALPHA);
36$action = optional_param('action', false, PARAM_ALPHA);
37$newvalue = optional_param('newvalue', false, PARAM_MULTILANG);
38
39switch ($action) {
40 case 'update':
41 if (!empty($userid) && !empty($itemid) && $newvalue !== false && !empty($type)) {
42 // Save the grade or feedback
43 if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) { // we must verify course id here!
14398fd6 44 print_error('invalidgradeitmeid');
b244b9b7 45 }
46
47 /**
48 * Code copied from grade/report/grader/lib.php line 187+
49 */
50 $warnings = array();
51 $finalvalue = null;
52 $finalgrade = null;
53 $feedback = null;
54 $json_object = new stdClass();
55 // Pre-process grade
56 if ($type == 'value' || $type == 'scale') {
57 $feedback = false;
58 $feedbackformat = false;
59 if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
60 if ($newvalue == -1) { // -1 means no grade
61 $finalgrade = null;
62 } else {
63 $finalgrade = $newvalue;
64 }
65 } else {
66 $finalgrade = unformat_float($newvalue);
67 }
68
69 $errorstr = '';
70 // Warn if the grade is out of bounds.
71 if (is_null($finalgrade)) {
72 // ok
73 } else if ($finalgrade < $grade_item->grademin) {
74 $errorstr = 'lessthanmin';
75 } else if ($finalgrade > $grade_item->grademax) {
76 $errorstr = 'morethanmax';
77 }
78
79 if ($errorstr) {
80 $user = get_record('user', 'id', $userid, '', '', '', '', 'id, firstname, lastname');
81 $gradestr = new object();
82 $gradestr->username = fullname($user);
83 $gradestr->itemname = $grade_item->get_name();
84 $json_object->message = get_string($errorstr, 'grades', $gradestr);
85 $json_object->result = "error";
86
87 }
88
89 $finalvalue = $finalgrade;
90
91 } else if ($type == 'feedback') {
92 $finalgrade = false;
93 $trimmed = trim($newvalue);
94 if (empty($trimmed)) {
95 $feedback = NULL;
96 } else {
97 $feedback = stripslashes($newvalue);
98 }
99
100 $finalvalue = $feedback;
101 }
102
103 if (!empty($json_object->result) && $json_object->result == 'error') {
104 echo json_encode($json_object);
105 die();
106 } else {
107 $json_object->gradevalue = $finalvalue;
108
109 if ($grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE)) {
110 $json_object->result = 'success';
111 $json_object->message = false;
112 } else {
113 $json_object->result = 'error';
114 $json_object->message = "TO BE LOCALISED: Failure to update final grade!";
115 echo json_encode();
116 die();
117 }
118
119 // Get row data
120 $sql = "SELECT gg.id, gi.id AS itemid, gi.scaleid AS scale, gg.userid AS userid, finalgrade, gg.overridden AS overridden "
121 . "FROM {$CFG->prefix}grade_grades gg, {$CFG->prefix}grade_items gi WHERE "
122 . "gi.courseid = $courseid AND gg.itemid = gi.id AND gg.userid = $userid";
123 $records = get_records_sql($sql);
124 $json_object->row = $records;
125 echo json_encode($json_object);
126 die();
127 }
128 } else {
129 $json_object = new stdClass();
130 $json_object->result = "error";
131 $json_object->message = "Missing parameter to ajax UPDATE callback: \n" .
132 " userid: $userid,\n itemid: $itemid\n, type: $type\n, newvalue: $newvalue";
133 echo json_encode($json_object);
134 }
135
136 break;
137}
138
139?>