Merge branch 'MDL-57742_master' of git://github.com/markn86/moodle
[moodle.git] / lib / classes / event / notification_sent.php
blobf00fb51227b38c5f67cec47794b65e2a146cda95
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.
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/>.
17 /**
18 * Notification sent event.
20 * @package core
21 * @copyright 2018 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core\event;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Notification sent event class.
32 * @property-read array $other {
33 * Extra information about event.
35 * - int courseid: the id of the related course.
36 * }
38 * @package core
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 {
44 /**
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)) {
58 $userfromid = 0;
61 // If the courseid is null, then set it to the site id.
62 if (is_null($courseid)) {
63 $courseid = SITEID;
66 $event = self::create(
68 'objectid' => $notificationid,
69 'userid' => $userfromid,
70 'context' => \context_system::instance(),
71 'relateduserid' => $usertoid,
72 'other' => [
73 'courseid' => $courseid
78 return $event;
81 /**
82 * Init method.
84 protected function init() {
85 $this->data['objecttable'] = 'notifications';
86 $this->data['crud'] = 'c';
87 $this->data['edulevel'] = self::LEVEL_OTHER;
90 /**
91 * Returns localised general event name.
93 * @return string
95 public static function get_name() {
96 return get_string('eventnotificationsent', 'message');
99 /**
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.
111 * @return string
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'.";
123 * Custom validation.
125 * @throws \coding_exception
126 * @return void
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);
147 return $othermapped;