source: moodle/trunk/fuentes/lib/classes/event/user_graded.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 5.0 KB
Line 
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
25namespace core\event;
26defined('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 */
48class 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     * @return user_graded
57     */
58    public static function create_from_grade(\grade_grade $grade) {
59        $event = self::create(array(
60            'context'       => \context_course::instance($grade->grade_item->courseid),
61            'objectid'      => $grade->id,
62            'relateduserid' => $grade->userid,
63            'other'         => array(
64                'itemid'     => $grade->itemid,
65                'overridden' => !empty($grade->overridden),
66                'finalgrade' => $grade->finalgrade),
67        ));
68        $event->grade = $grade;
69        return $event;
70    }
71
72    /**
73     * Get grade object.
74     *
75     * @throws \coding_exception
76     * @return \grade_grade
77     */
78    public function get_grade() {
79        if ($this->is_restored()) {
80            throw new \coding_exception('get_grade() is intended for event observers only');
81        }
82        return $this->grade;
83    }
84
85    /**
86     * Init method.
87     *
88     * @return void
89     */
90    protected function init() {
91        $this->data['crud'] = 'u';
92        $this->data['edulevel'] = self::LEVEL_TEACHING;
93        $this->data['objecttable'] = 'grade_grades';
94    }
95
96    /**
97     * Return localised event name.
98     *
99     * @return string
100     */
101    public static function get_name() {
102        return get_string('eventusergraded', 'core_grades');
103    }
104
105    /**
106     * Returns description of what happened.
107     *
108     * @return string
109     */
110    public function get_description() {
111        return "The user with id '$this->userid' updated the grade with id '$this->objectid' for the user with " .
112            "id '$this->relateduserid' for the grade item with id '{$this->other['itemid']}'.";
113    }
114
115    /**
116     * Get URL related to the action
117     *
118     * @return \moodle_url
119     */
120    public function get_url() {
121        return new \moodle_url('/grade/edit/tree/grade.php', array(
122            'courseid' => $this->courseid,
123            'itemid'   => $this->other['itemid'],
124            'userid'   => $this->relateduserid,
125        ));
126    }
127
128    /**
129     * Return legacy log info.
130     *
131     * @return null|array of parameters to be passed to legacy add_to_log() function.
132     */
133    public function get_legacy_logdata() {
134        $user = $this->get_record_snapshot('user', $this->relateduserid);
135        $fullname = fullname($user);
136        $info = $this->grade->grade_item->itemname . ': ' . $fullname;
137        $url = '/report/grader/index.php?id=' . $this->courseid;
138
139        return array($this->courseid, 'grade', 'update', $url, $info);
140    }
141
142    /**
143     * Custom validation.
144     *
145     * @throws \coding_exception when validation does not pass.
146     * @return void
147     */
148    protected function validate_data() {
149        parent::validate_data();
150
151        if (!isset($this->relateduserid)) {
152            throw new \coding_exception('The \'relateduserid\' must be set.');
153        }
154
155        if (!isset($this->other['itemid'])) {
156            throw new \coding_exception('The \'itemid\' value must be set in other.');
157        }
158    }
159
160    public static function get_objectid_mapping() {
161        return array('db' => 'grade_grades', 'restore' => 'grade_grades');
162    }
163
164    public static function get_other_mapping() {
165        $othermapped = array();
166        $othermapped['itemid'] = array('db' => 'grade_items', 'restore' => 'grade_item');
167
168        return $othermapped;
169    }
170}
Note: See TracBrowser for help on using the repository browser.