2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Notification sent event.
21 * @copyright 2018 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
30 * Notification sent event class.
32 * @property-read array $other {
33 * Extra information about event.
35 * - int courseid: the id of the related course.
39 * @copyright 2018 Mark Nelson <markn@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class notification_sent
extends base
{
45 * Create event using ids.
47 * @param int $userfromid
48 * @param int $usertoid
49 * @param int $notificationid
50 * @param int $courseid course id the event is related with - SITEID if no relation exists.
51 * @return notification_sent
53 public static function create_from_ids($userfromid, $usertoid, $notificationid, $courseid) {
54 // We may be sending a notification from the 'noreply' address, which means we are not actually sending a
55 // notification from a valid user. In this case, we will set the userid to 0.
56 // Check if the userid is valid.
57 if (!\core_user
::is_real_user($userfromid)) {
61 // If the courseid is null, then set it to the site id.
62 if (is_null($courseid)) {
66 $event = self
::create(
68 'objectid' => $notificationid,
69 'userid' => $userfromid,
70 'context' => \context_system
::instance(),
71 'relateduserid' => $usertoid,
73 'courseid' => $courseid
84 protected function init() {
85 $this->data
['objecttable'] = 'notifications';
86 $this->data
['crud'] = 'c';
87 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
91 * Returns localised general event name.
95 public static function get_name() {
96 return get_string('eventnotificationsent', 'message');
100 * Returns relevant URL.
102 * @return \moodle_url
104 public function get_url() {
105 return new \
moodle_url('/message/output/popup/notifications.php', array('notificationid' => $this->objectid
));
109 * Returns description of what happened.
113 public function get_description() {
114 // Check if we are sending from a valid user.
115 if (\core_user
::is_real_user($this->userid
)) {
116 return "The user with id '$this->userid' sent a notification to the user with id '$this->relateduserid'.";
119 return "A notification was sent by the system to the user with id '$this->relateduserid'.";
125 * @throws \coding_exception
128 protected function validate_data() {
129 parent
::validate_data();
131 if (!isset($this->relateduserid
)) {
132 throw new \
coding_exception('The \'relateduserid\' must be set.');
135 if (!isset($this->other
['courseid'])) {
136 throw new \
coding_exception('The \'courseid\' value must be set in other.');
140 public static function get_objectid_mapping() {
141 return array('db' => 'notifications', 'restore' => base
::NOT_MAPPED
);
144 public static function get_other_mapping() {
145 $othermapped = array();
146 $othermapped['courseid'] = array('db' => 'course', 'restore' => base
::NOT_MAPPED
);