Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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                      $userfieldsapi = \core_user\fields::for_name();
  95                      $user = $DB->get_record('user', array('id' => $userid), 'id' . $userfieldsapi->get_sql()->selects);
  96                      $gradestr = new stdClass();
  97                      $gradestr->username = fullname($user);
  98                      $gradestr->itemname = $grade_item->get_name();
  99                      $json_object->message = get_string($errorstr, 'grades', $gradestr);
 100                      $json_object->result = "error";
 101  
 102                  }
 103  
 104                  $finalvalue = $finalgrade;
 105  
 106              } else if ($type == 'feedback') {
 107                  $finalgrade = false;
 108                  $trimmed = trim($newvalue);
 109                  if (empty($trimmed)) {
 110                      $feedback = NULL;
 111                  } else {
 112                      $feedback = $newvalue;
 113                  }
 114  
 115                  $finalvalue = $feedback;
 116              }
 117  
 118              if (!empty($json_object->result) && $json_object->result == 'error') {
 119                  echo json_encode($json_object);
 120                  die();
 121              } else {
 122                  $json_object->gradevalue = $finalvalue;
 123  
 124                  if ($grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE)) {
 125                      $json_object->result = 'success';
 126                      $json_object->message = false;
 127                  } else {
 128                      $json_object->result = 'error';
 129                      $json_object->message = "TO BE LOCALISED: Failure to update final grade!";
 130                      echo json_encode($json_object);
 131                      die();
 132                  }
 133  
 134                  // Get row data
 135                  $sql = "SELECT gg.id, gi.id AS itemid, gi.scaleid AS scale, gg.userid AS userid, finalgrade, gg.overridden AS overridden "
 136                       . "FROM {grade_grades} gg, {grade_items} gi WHERE "
 137                       . "gi.courseid = ? AND gg.itemid = gi.id AND gg.userid = ?";
 138                  $records = $DB->get_records_sql($sql, array($courseid, $userid));
 139                  $json_object->row = $records;
 140                  echo json_encode($json_object);
 141                  die();
 142              }
 143          } else {
 144              $json_object = new stdClass();
 145              $json_object->result = "error";
 146              $json_object->message = "Missing parameter to ajax UPDATE callback: \n" .
 147                                      "  userid: $userid,\n  itemid: $itemid\n,  type: $type\n,  newvalue: $newvalue";
 148              echo json_encode($json_object);
 149          }
 150  
 151          break;
 152  }
 153  
 154