Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401]

   1  <?php
   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/>.
  16  
  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   */
  24  
  25  require_once '../../../config.php';
  26  require_once $CFG->libdir.'/gradelib.php';
  27  require_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  
  31  $courseid = required_param('id', PARAM_INT);                   // course id
  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);
  36  $newvalue = optional_param('newvalue', false, PARAM_TEXT);
  37  
  38  /// basic access checks
  39  if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  40      print_error('invalidcourseid');
  41  }
  42  $context = context_course::instance($course->id);
  43  require_login($course);
  44  
  45  switch ($action) {
  46      case 'update':
  47          if (!confirm_sesskey()) {
  48              break;
  49          }
  50          require_capability('moodle/grade:edit', $context);
  51  
  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!
  55                  print_error('invalidgradeitemid');
  56              }
  57  
  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
  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                      }
  91                  }
  92  
  93                  if ($errorstr) {
  94                      $user = $DB->get_record('user', array('id' => $userid), 'id, ' . get_all_user_name_fields(true));
  95                      $gradestr = new stdClass();
  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";
 100  
 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 {
 111                      $feedback = $newvalue;
 112                  }
 113  
 114                  $finalvalue = $feedback;
 115              }
 116  
 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;
 122  
 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!";
 129                      echo json_encode($json_object);
 130                      die();
 131                  }
 132  
 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 "
 135                       . "FROM {grade_grades} gg, {grade_items} gi WHERE "
 136                       . "gi.courseid = ? AND gg.itemid = gi.id AND gg.userid = ?";
 137                  $records = $DB->get_records_sql($sql, array($courseid, $userid));
 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";
 145              $json_object->message = "Missing parameter to ajax UPDATE callback: \n" .
 146                                      "  userid: $userid,\n  itemid: $itemid\n,  type: $type\n,  newvalue: $newvalue";
 147              echo json_encode($json_object);
 148          }
 149  
 150          break;
 151  }
 152  
 153