MDL-47162 core_message: debug whenever courseid is missing
[moodle.git] / lib / classes / event / message_sent.php
CommitLineData
ab1bc5d2
MN
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
02a5a4b2
MN
17/**
18 * Message sent 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
ab1bc5d2
MN
29/**
30 * Message sent event class.
31 *
32 * @property-read array $other {
33 * Extra information about event.
34 *
35 * - int messageid: the id of the message.
cc350fd9 36 * - int courseid: the id of the related course.
ab1bc5d2
MN
37 * }
38 *
39 * @package core
3610ad43 40 * @since Moodle 2.7
ab1bc5d2
MN
41 * @copyright 2014 Mark Nelson <markn@moodle.com>
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
ab1bc5d2 44class message_sent extends base {
349f98ad
PS
45 /**
46 * Create event using ids.
a29bcf78 47 * @todo MDL-55449 Make $courseid mandatory in Moodle 3.6
349f98ad
PS
48 * @param int $userfromid
49 * @param int $usertoid
50 * @param int $messageid
a29bcf78 51 * @param int|null $courseid course id the event is related with. Use SITEID if no relation exists.
349f98ad
PS
52 * @return message_sent
53 */
cc350fd9 54 public static function create_from_ids($userfromid, $usertoid, $messageid, $courseid = null) {
349f98ad
PS
55 // We may be sending a message from the 'noreply' address, which means we are not actually sending a
56 // message from a valid user. In this case, we will set the userid to 0.
57 // Check if the userid is valid.
58 if (!\core_user::is_real_user($userfromid)) {
59 $userfromid = 0;
60 }
61
a29bcf78 62 // TODO: MDL-55449 Make $courseid mandatory in Moodle 3.6.
cc350fd9 63 if (is_null($courseid)) {
a29bcf78
EL
64 // Arrived here with not defined $courseid to associate the event with.
65 // Let's default to SITEID and perform debugging so devs are aware. MDL-47162.
cc350fd9 66 $courseid = SITEID;
a29bcf78
EL
67 debugging('message_sent::create_from_ids() needs a $courseid to be passed, nothing was detected. Please, change ' .
68 'the call to include it, using SITEID if the message is unrelated to any real course.', DEBUG_DEVELOPER);
cc350fd9
AD
69 }
70
349f98ad
PS
71 $event = self::create(array(
72 'userid' => $userfromid,
73 'context' => \context_system::instance(),
74 'relateduserid' => $usertoid,
75 'other' => array(
76 // In earlier versions it can either be the id in the 'message_read' or 'message' table.
77 // Now it is always the id from 'message' table. Please note that the record is still moved
78 // to the 'message_read' table later when message marked as read.
cc350fd9
AD
79 'messageid' => $messageid,
80 'courseid' => $courseid
349f98ad
PS
81 )
82 ));
83
84 return $event;
85 }
ab1bc5d2
MN
86
87 /**
88 * Init method.
89 */
90 protected function init() {
91 $this->data['crud'] = 'c';
92 $this->data['edulevel'] = self::LEVEL_OTHER;
93 }
94
95 /**
96 * Returns localised general event name.
97 *
98 * @return string
99 */
100 public static function get_name() {
101 return get_string('eventmessagesent', 'message');
102 }
103
104 /**
105 * Returns relevant URL.
106 *
107 * @return \moodle_url
108 */
109 public function get_url() {
b63f7732 110 return new \moodle_url('/message/index.php', array('user1' => $this->userid, 'user2' => $this->relateduserid));
ab1bc5d2
MN
111 }
112
113 /**
114 * Returns description of what happened.
115 *
116 * @return string
117 */
118 public function get_description() {
53e4c89d
MN
119 // Check if we are sending from a valid user.
120 if (\core_user::is_real_user($this->userid)) {
02915dfc 121 return "The user with id '$this->userid' sent a message to the user with id '$this->relateduserid'.";
f6a4a90e 122 }
53e4c89d 123
02915dfc 124 return "A message was sent by the system to the user with id '$this->relateduserid'.";
ab1bc5d2
MN
125 }
126
127 /**
128 * Return legacy data for add_to_log().
129 *
130 * @return array
131 */
132 protected function get_legacy_logdata() {
f6a4a90e
MN
133 // The add_to_log function was only ever called when we sent a message from one user to another. We do not want
134 // to return the legacy log data if we are sending a system message, so check that the userid is valid.
53e4c89d 135 if (\core_user::is_real_user($this->userid)) {
f6a4a90e
MN
136 return array(SITEID, 'message', 'write', 'index.php?user=' . $this->userid . '&id=' . $this->relateduserid .
137 '&history=1#m' . $this->other['messageid'], $this->userid);
138 }
139
140 return null;
ab1bc5d2
MN
141 }
142
143 /**
144 * Custom validation.
145 *
146 * @throws \coding_exception
147 * @return void
148 */
149 protected function validate_data() {
150 parent::validate_data();
151
152 if (!isset($this->relateduserid)) {
153 throw new \coding_exception('The \'relateduserid\' must be set.');
154 }
155
156 if (!isset($this->other['messageid'])) {
02a5a4b2 157 throw new \coding_exception('The \'messageid\' value must be set in other.');
ab1bc5d2 158 }
cc350fd9
AD
159
160 if (!isset($this->other['courseid'])) {
a29bcf78 161 throw new \coding_exception('The \'courseid\' value must be set in other.');
cc350fd9 162 }
ab1bc5d2 163 }
08174514
DW
164
165 public static function get_objectid_mapping() {
166 // Messages are not backed up, so no need to map them.
167 return false;
168 }
169
170 public static function get_other_mapping() {
ac82a9a8
DW
171 // Messages are not backed up, so no need to map them on restore.
172 $othermapped = array();
173 // The messages table could vary for older events - so cannot be mapped.
174 $othermapped['messageid'] = array('db' => base::NOT_MAPPED, 'restore' => base::NOT_MAPPED);
cc350fd9 175 $othermapped['courseid'] = array('db' => base::NOT_MAPPED, 'restore' => base::NOT_MAPPED);
ac82a9a8 176 return $othermapped;
08174514 177 }
ab1bc5d2 178}