Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/grade/report/grader/ajax_callbacks.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 
00019 require_once '../../../config.php';
00020 require_once $CFG->libdir.'/gradelib.php';
00021 require_once $CFG->dirroot.'/grade/lib.php';
00022 // require_once $CFG->dirroot.'/grade/report/grader/ajaxlib.php';
00023 // require_once $CFG->dirroot.'/grade/report/grader/lib.php';
00024 
00025 $courseid = required_param('id', PARAM_INT);                   // course id
00026 $userid = optional_param('userid', false, PARAM_INT);
00027 $itemid = optional_param('itemid', false, PARAM_INT);
00028 $type = optional_param('type', false, PARAM_ALPHA);
00029 $action = optional_param('action', false, PARAM_ALPHA);
00030 $newvalue = optional_param('newvalue', false, PARAM_MULTILANG);
00031 
00033 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
00034     print_error('nocourseid');
00035 }
00036 $context = get_context_instance(CONTEXT_COURSE, $course->id);
00037 require_login($course);
00038 
00039 switch ($action) {
00040     case 'update':
00041         if (!confirm_sesskey()) {
00042             break;
00043         }
00044         require_capability('moodle/grade:edit', $context);
00045 
00046         if (!empty($userid) && !empty($itemid) && $newvalue !== false && !empty($type)) {
00047             // Save the grade or feedback
00048             if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) { // we must verify course id here!
00049                 print_error('invalidgradeitmeid');
00050             }
00051 
00055             $warnings = array();
00056             $finalvalue = null;
00057             $finalgrade = null;
00058             $feedback = null;
00059             $json_object = new stdClass();
00060             // Pre-process grade
00061             if ($type == 'value' || $type == 'scale') {
00062                 $feedback = false;
00063                 $feedbackformat = false;
00064                 if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
00065                     if ($newvalue == -1) { // -1 means no grade
00066                         $finalgrade = null;
00067                     } else {
00068                         $finalgrade = $newvalue;
00069                     }
00070                 } else {
00071                     $finalgrade = unformat_float($newvalue);
00072                 }
00073 
00074                 $errorstr = '';
00075                 // Warn if the grade is out of bounds.
00076                 if (is_null($finalgrade)) {
00077                     // ok
00078                 } else if ($finalgrade < $grade_item->grademin) {
00079                     $errorstr = 'lessthanmin';
00080                 } else if ($finalgrade > $grade_item->grademax) {
00081                     $errorstr = 'morethanmax';
00082                 }
00083 
00084                 if ($errorstr) {
00085                     $user = $DB->get_record('user', array('id' => $userid), 'id, firstname, lastname');
00086                     $gradestr = new stdClass();
00087                     $gradestr->username = fullname($user);
00088                     $gradestr->itemname = $grade_item->get_name();
00089                     $json_object->message = get_string($errorstr, 'grades', $gradestr);
00090                     $json_object->result = "error";
00091 
00092                 }
00093 
00094                 $finalvalue = $finalgrade;
00095 
00096             } else if ($type == 'feedback') {
00097                 $finalgrade = false;
00098                 $trimmed = trim($newvalue);
00099                 if (empty($trimmed)) {
00100                     $feedback = NULL;
00101                 } else {
00102                     $feedback = $newvalue;
00103                 }
00104 
00105                 $finalvalue = $feedback;
00106             }
00107 
00108             if (!empty($json_object->result) && $json_object->result == 'error') {
00109                 echo json_encode($json_object);
00110                 die();
00111             } else {
00112                 $json_object->gradevalue = $finalvalue;
00113 
00114                 if ($grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE)) {
00115                     $json_object->result = 'success';
00116                     $json_object->message = false;
00117                 } else {
00118                     $json_object->result = 'error';
00119                     $json_object->message = "TO BE LOCALISED: Failure to update final grade!";
00120                     echo json_encode($json_object);
00121                     die();
00122                 }
00123 
00124                 // Get row data
00125                 $sql = "SELECT gg.id, gi.id AS itemid, gi.scaleid AS scale, gg.userid AS userid, finalgrade, gg.overridden AS overridden "
00126                      . "FROM {grade_grades} gg, {grade_items} gi WHERE "
00127                      . "gi.courseid = ? AND gg.itemid = gi.id AND gg.userid = ?";
00128                 $records = $DB->get_records_sql($sql, array($courseid, $userid));
00129                 $json_object->row = $records;
00130                 echo json_encode($json_object);
00131                 die();
00132             }
00133         } else {
00134             $json_object = new stdClass();
00135             $json_object->result = "error";
00136             $json_object->message = "Missing parameter to ajax UPDATE callback: \n" .
00137                                     "  userid: $userid,\n  itemid: $itemid\n,  type: $type\n,  newvalue: $newvalue";
00138             echo json_encode($json_object);
00139         }
00140 
00141         break;
00142 }
00143 
00144 
 All Data Structures Namespaces Files Functions Variables Enumerations