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