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 311 and 402] [Versions 311 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       * Return legacy log info.
 136       *
 137       * @return null|array of parameters to be passed to legacy add_to_log() function.
 138       */
 139      public function get_legacy_logdata() {
 140          $user = $this->get_record_snapshot('user', $this->relateduserid);
 141          $fullname = fullname($user);
 142          $info = $this->grade->grade_item->itemname . ': ' . $fullname;
 143          $url = '/report/grader/index.php?id=' . $this->courseid;
 144  
 145          return array($this->courseid, 'grade', 'update', $url, $info);
 146      }
 147  
 148      /**
 149       * Custom validation.
 150       *
 151       * @throws \coding_exception when validation does not pass.
 152       * @return void
 153       */
 154      protected function validate_data() {
 155          parent::validate_data();
 156  
 157          if (!isset($this->relateduserid)) {
 158              throw new \coding_exception('The \'relateduserid\' must be set.');
 159          }
 160  
 161          if (!isset($this->other['itemid'])) {
 162              throw new \coding_exception('The \'itemid\' value must be set in other.');
 163          }
 164      }
 165  
 166      public static function get_objectid_mapping() {
 167          return array('db' => 'grade_grades', 'restore' => 'grade_grades');
 168      }
 169  
 170      public static function get_other_mapping() {
 171          $othermapped = array();
 172          $othermapped['itemid'] = array('db' => 'grade_items', 'restore' => 'grade_item');
 173  
 174          return $othermapped;
 175      }
 176  }