2009-07-07 02:26:36 +00:00
|
|
|
<?php
|
2008-04-18 19:30:28 +00:00
|
|
|
|
2009-07-07 02:26:36 +00:00
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
2008-04-18 19:30:28 +00:00
|
|
|
|
|
|
|
|
2008-06-02 16:06:33 +00:00
|
|
|
require_once '../../../config.php';
|
2008-04-18 19:30:28 +00:00
|
|
|
require_once $CFG->libdir.'/gradelib.php';
|
|
|
|
require_once $CFG->dirroot.'/grade/lib.php';
|
|
|
|
// require_once $CFG->dirroot.'/grade/report/grader/ajaxlib.php';
|
|
|
|
// require_once $CFG->dirroot.'/grade/report/grader/lib.php';
|
|
|
|
|
2009-08-27 08:23:10 +00:00
|
|
|
$courseid = required_param('id', PARAM_INT); // course id
|
2008-04-18 19:30:28 +00:00
|
|
|
$userid = optional_param('userid', false, PARAM_INT);
|
|
|
|
$itemid = optional_param('itemid', false, PARAM_INT);
|
|
|
|
$type = optional_param('type', false, PARAM_ALPHA);
|
|
|
|
$action = optional_param('action', false, PARAM_ALPHA);
|
|
|
|
$newvalue = optional_param('newvalue', false, PARAM_MULTILANG);
|
|
|
|
|
2009-10-29 08:37:37 +00:00
|
|
|
/// basic access checks
|
|
|
|
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
|
|
|
|
print_error('nocourseid');
|
|
|
|
}
|
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $course->id);
|
|
|
|
require_login($course);
|
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
switch ($action) {
|
|
|
|
case 'update':
|
2009-10-29 09:01:58 +00:00
|
|
|
if (!confirm_sesskey()) {
|
|
|
|
break;
|
|
|
|
}
|
2009-10-29 08:37:37 +00:00
|
|
|
require_capability('moodle/grade:edit', $context);
|
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
if (!empty($userid) && !empty($itemid) && $newvalue !== false && !empty($type)) {
|
|
|
|
// Save the grade or feedback
|
|
|
|
if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) { // we must verify course id here!
|
2008-05-14 08:17:40 +00:00
|
|
|
print_error('invalidgradeitmeid');
|
2008-04-18 19:30:28 +00:00
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
/**
|
|
|
|
* Code copied from grade/report/grader/lib.php line 187+
|
|
|
|
*/
|
|
|
|
$warnings = array();
|
|
|
|
$finalvalue = null;
|
|
|
|
$finalgrade = null;
|
|
|
|
$feedback = null;
|
|
|
|
$json_object = new stdClass();
|
|
|
|
// Pre-process grade
|
|
|
|
if ($type == 'value' || $type == 'scale') {
|
|
|
|
$feedback = false;
|
|
|
|
$feedbackformat = false;
|
|
|
|
if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
|
|
|
|
if ($newvalue == -1) { // -1 means no grade
|
|
|
|
$finalgrade = null;
|
|
|
|
} else {
|
|
|
|
$finalgrade = $newvalue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$finalgrade = unformat_float($newvalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
$errorstr = '';
|
|
|
|
// Warn if the grade is out of bounds.
|
|
|
|
if (is_null($finalgrade)) {
|
|
|
|
// ok
|
|
|
|
} else if ($finalgrade < $grade_item->grademin) {
|
|
|
|
$errorstr = 'lessthanmin';
|
|
|
|
} else if ($finalgrade > $grade_item->grademax) {
|
|
|
|
$errorstr = 'morethanmax';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($errorstr) {
|
2008-06-03 16:10:57 +00:00
|
|
|
$user = $DB->get_record('user', array('id' => $userid), 'id, firstname, lastname');
|
2008-04-18 19:30:28 +00:00
|
|
|
$gradestr = new object();
|
|
|
|
$gradestr->username = fullname($user);
|
|
|
|
$gradestr->itemname = $grade_item->get_name();
|
|
|
|
$json_object->message = get_string($errorstr, 'grades', $gradestr);
|
|
|
|
$json_object->result = "error";
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$finalvalue = $finalgrade;
|
|
|
|
|
|
|
|
} else if ($type == 'feedback') {
|
|
|
|
$finalgrade = false;
|
|
|
|
$trimmed = trim($newvalue);
|
|
|
|
if (empty($trimmed)) {
|
|
|
|
$feedback = NULL;
|
|
|
|
} else {
|
2008-06-09 16:53:30 +00:00
|
|
|
$feedback = $newvalue;
|
2008-04-18 19:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$finalvalue = $feedback;
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
if (!empty($json_object->result) && $json_object->result == 'error') {
|
|
|
|
echo json_encode($json_object);
|
|
|
|
die();
|
|
|
|
} else {
|
|
|
|
$json_object->gradevalue = $finalvalue;
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
if ($grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE)) {
|
|
|
|
$json_object->result = 'success';
|
|
|
|
$json_object->message = false;
|
|
|
|
} else {
|
|
|
|
$json_object->result = 'error';
|
|
|
|
$json_object->message = "TO BE LOCALISED: Failure to update final grade!";
|
|
|
|
echo json_encode();
|
|
|
|
die();
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
// Get row data
|
|
|
|
$sql = "SELECT gg.id, gi.id AS itemid, gi.scaleid AS scale, gg.userid AS userid, finalgrade, gg.overridden AS overridden "
|
2008-06-02 16:06:33 +00:00
|
|
|
. "FROM {grade_grades} gg, {grade_items} gi WHERE "
|
2008-06-03 16:10:57 +00:00
|
|
|
. "gi.courseid = ? AND gg.itemid = gi.id AND gg.userid = ?";
|
|
|
|
$records = $DB->get_records_sql($sql, array($courseid, $userid));
|
2008-04-18 19:30:28 +00:00
|
|
|
$json_object->row = $records;
|
|
|
|
echo json_encode($json_object);
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$json_object = new stdClass();
|
|
|
|
$json_object->result = "error";
|
2008-06-02 16:06:33 +00:00
|
|
|
$json_object->message = "Missing parameter to ajax UPDATE callback: \n" .
|
2008-04-18 19:30:28 +00:00
|
|
|
" userid: $userid,\n itemid: $itemid\n, type: $type\n, newvalue: $newvalue";
|
|
|
|
echo json_encode($json_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|