Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

   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   * Grade edited event.
  19   *
  20   * @package    core
  21   * @copyright  2014 Petr Skoda
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\event;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Event triggered after teacher edits manual grade or
  30   * overrides activity/aggregated grade.
  31   *
  32   * Note: use grade_grades_history table if you need to know
  33   *       the history of grades.
  34   *
  35   * @property-read array $other {
  36   *      Extra information about the event.
  37   *
  38   *      - int itemid: grade item id.
  39   *      - bool overridden: (optional) Is this grade override?
  40   *      - float finalgrade: (optional) the final grade value.
  41   * }
  42   *
  43   * @package    core
  44   * @since      Moodle 2.7
  45   * @copyright  2013 Petr Skoda
  46   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  47   */
  48  class user_graded extends base {
  49      /** @var \grade_grade $grade */
  50      protected $grade;
  51  
  52      /**
  53       * Utility method to create new event.
  54       *
  55       * @param \grade_grade $grade
  56       * @param null|int $userid Id of user responsible for this event.
  57       *
  58       * @return user_graded
  59       */
  60      public static function create_from_grade(\grade_grade $grade, $userid = null) {
  61          $gradedata = array(
  62              'context'       => \context_course::instance($grade->grade_item->courseid),
  63              'objectid'      => $grade->id,
  64              'relateduserid' => $grade->userid,
  65              'other'         => array(
  66                  'itemid'     => $grade->itemid,
  67                  'overridden' => !empty($grade->overridden),
  68                  'finalgrade' => $grade->finalgrade),
  69          );
  70          if ($userid !== null) {
  71              $gradedata["userid"] = $userid;
  72          }
  73          $event = self::create($gradedata);
  74          $event->grade = $grade;
  75          return $event;
  76      }
  77  
  78      /**
  79       * Get grade object.
  80       *
  81       * @throws \coding_exception
  82       * @return \grade_grade
  83       */
  84      public function get_grade() {
  85          if ($this->is_restored()) {
  86              throw new \coding_exception('get_grade() is intended for event observers only');
  87          }
  88          return $this->grade;
  89      }
  90  
  91      /**
  92       * Init method.
  93       *
  94       * @return void
  95       */
  96      protected function init() {
  97          $this->data['crud'] = 'u';
  98          $this->data['edulevel'] = self::LEVEL_TEACHING;
  99          $this->data['objecttable'] = 'grade_grades';
 100      }
 101  
 102      /**
 103       * Return localised event name.
 104       *
 105       * @return string
 106       */
 107      public static function get_name() {
 108          return get_string('eventusergraded', 'core_grades');
 109      }
 110  
 111      /**
 112       * Returns description of what happened.
 113       *
 114       * @return string
 115       */
 116      public function get_description() {
 117          return "The user with id '$this->userid' updated the grade with id '$this->objectid' for the user with " .
 118              "id '$this->relateduserid' for the grade item with id '{$this->other['itemid']}'.";
 119      }
 120  
 121      /**
 122       * Get URL related to the action
 123       *
 124       * @return \moodle_url
 125       */
 126      public function get_url() {
 127          return new \moodle_url('/grade/edit/tree/grade.php', array(
 128              'courseid' => $this->courseid,
 129              'itemid'   => $this->other['itemid'],
 130              'userid'   => $this->relateduserid,
 131          ));
 132      }
 133  
 134      /**
 135       * Custom validation.
 136       *
 137       * @throws \coding_exception when validation does not pass.
 138       * @return void
 139       */
 140      protected function validate_data() {
 141          parent::validate_data();
 142  
 143          if (!isset($this->relateduserid)) {
 144              throw new \coding_exception('The \'relateduserid\' must be set.');
 145          }
 146  
 147          if (!isset($this->other['itemid'])) {
 148              throw new \coding_exception('The \'itemid\' value must be set in other.');
 149          }
 150      }
 151  
 152      public static function get_objectid_mapping() {
 153          return array('db' => 'grade_grades', 'restore' => 'grade_grades');
 154      }
 155  
 156      public static function get_other_mapping() {
 157          $othermapped = array();
 158          $othermapped['itemid'] = array('db' => 'grade_items', 'restore' => 'grade_item');
 159  
 160          return $othermapped;
 161      }
 162  }