Commit | Line | Data |
---|---|---|
349f98ad PS |
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 | * New messaging manager class. | |
19 | * | |
20 | * @package core_message | |
21 | * @since Moodle 2.8 | |
22 | * @copyright 2014 Totara Learning Solutions Ltd {@link http://www.totaralms.com/} | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | * @author Petr Skoda <petr.skoda@totaralms.com> | |
25 | */ | |
26 | ||
27 | namespace core\message; | |
28 | ||
29 | defined('MOODLE_INTERNAL') || die(); | |
30 | ||
31 | /** | |
32 | * Class used for various messaging related stuff. | |
33 | * | |
34 | * Note: Do NOT use directly in your code, it is intended to be used from core code only. | |
35 | * | |
36 | * @access private | |
37 | * | |
38 | * @package core_message | |
39 | * @since Moodle 2.8 | |
40 | * @copyright 2014 Totara Learning Solutions Ltd {@link http://www.totaralms.com/} | |
41 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
42 | * @author Petr Skoda <petr.skoda@totaralms.com> | |
43 | */ | |
44 | class manager { | |
45 | /** @var array buffer of pending messages */ | |
46 | protected static $buffer = array(); | |
47 | ||
48 | /** | |
49 | * Do the message sending. | |
50 | * | |
51 | * NOTE: to be used from message_send() only. | |
52 | * | |
a29bcf78 | 53 | * @todo MDL-55449 Drop support for stdClass in Moodle 3.6 |
cc350fd9 | 54 | * @param \core\message\message $eventdata fully prepared event data for processors |
349f98ad PS |
55 | * @param \stdClass $savemessage the message saved in 'message' table |
56 | * @param array $processorlist list of processors for target user | |
883ce421 | 57 | * @return int $messageid the id from 'messages' (false is not returned) |
349f98ad | 58 | */ |
c6f45ef8 | 59 | public static function send_message($eventdata, \stdClass $savemessage, array $processorlist) { |
349f98ad | 60 | global $CFG; |
c6f45ef8 | 61 | |
a29bcf78 | 62 | // TODO MDL-55449 Drop support for stdClass in Moodle 3.6. |
c6f45ef8 AA |
63 | if (!($eventdata instanceof \stdClass) && !($eventdata instanceof message)) { |
64 | // Not a valid object. | |
65 | throw new \coding_exception('Message should be of type stdClass or \core\message\message'); | |
66 | } | |
67 | ||
a29bcf78 | 68 | // TODO MDL-55449 Drop support for stdClass in Moodle 3.6. |
cc350fd9 AD |
69 | if ($eventdata instanceof \stdClass) { |
70 | if (!isset($eventdata->courseid)) { | |
71 | $eventdata->courseid = null; | |
72 | } | |
73 | ||
0e8b5160 | 74 | debugging('eventdata as \stdClass is deprecated. Please use \core\message\message instead.', DEBUG_DEVELOPER); |
cc350fd9 AD |
75 | } |
76 | ||
349f98ad PS |
77 | require_once($CFG->dirroot.'/message/lib.php'); // This is most probably already included from messagelib.php file. |
78 | ||
79 | if (empty($processorlist)) { | |
376a79c2 MN |
80 | // Trigger event for sending a message or notification - we need to do this before marking as read! |
81 | if ($eventdata->notification) { | |
82 | \core\event\notification_sent::create_from_ids( | |
83 | $eventdata->userfrom->id, | |
84 | $eventdata->userto->id, | |
85 | $savemessage->id, | |
86 | $eventdata->courseid | |
87 | )->trigger(); | |
88 | } else { // Must be a message. | |
883ce421 MN |
89 | \core\event\message_sent::create_from_ids( |
90 | $eventdata->userfrom->id, | |
91 | $eventdata->userto->id, | |
92 | $savemessage->id, | |
93 | $eventdata->courseid | |
cc350fd9 | 94 | )->trigger(); |
883ce421 | 95 | } |
349f98ad | 96 | |
883ce421 | 97 | if ($eventdata->notification or empty($CFG->messaging)) { |
349f98ad PS |
98 | // If they have deselected all processors and its a notification mark it read. The user doesn't want to be bothered. |
99 | // The same goes if the messaging is completely disabled. | |
883ce421 | 100 | if ($eventdata->notification) { |
548936a6 MN |
101 | $savemessage->timeread = null; |
102 | \core_message\api::mark_notification_as_read($savemessage); | |
883ce421 | 103 | } else { |
548936a6 | 104 | \core_message\api::mark_message_as_read($eventdata->userto->id, $savemessage); |
883ce421 | 105 | } |
349f98ad PS |
106 | } |
107 | ||
883ce421 | 108 | return $savemessage->id; |
349f98ad PS |
109 | } |
110 | ||
111 | // Let the manager do the sending or buffering when db transaction in progress. | |
112 | return self::send_message_to_processors($eventdata, $savemessage, $processorlist); | |
113 | } | |
114 | ||
115 | /** | |
116 | * Send message to message processors. | |
117 | * | |
c6f45ef8 | 118 | * @param \stdClass|\core\message\message $eventdata |
349f98ad PS |
119 | * @param \stdClass $savemessage |
120 | * @param array $processorlist | |
121 | * @return int $messageid | |
122 | */ | |
c6f45ef8 AA |
123 | protected static function send_message_to_processors($eventdata, \stdClass $savemessage, array |
124 | $processorlist) { | |
349f98ad PS |
125 | global $CFG, $DB; |
126 | ||
127 | // We cannot communicate with external systems in DB transactions, | |
128 | // buffer the messages if necessary. | |
129 | ||
130 | if ($DB->is_transaction_started()) { | |
131 | // We need to clone all objects so that devs may not modify it from outside later. | |
132 | $eventdata = clone($eventdata); | |
133 | $eventdata->userto = clone($eventdata->userto); | |
134 | $eventdata->userfrom = clone($eventdata->userfrom); | |
135 | ||
136 | // Conserve some memory the same was as $USER setup does. | |
137 | unset($eventdata->userto->description); | |
138 | unset($eventdata->userfrom->description); | |
139 | ||
140 | self::$buffer[] = array($eventdata, $savemessage, $processorlist); | |
141 | return $savemessage->id; | |
142 | } | |
143 | ||
349f98ad | 144 | foreach ($processorlist as $procname) { |
c6f45ef8 AA |
145 | // Let new messaging class add custom content based on the processor. |
146 | $proceventdata = ($eventdata instanceof message) ? $eventdata->get_eventobject_for_processor($procname) : $eventdata; | |
685daf1a MN |
147 | $stdproc = new \stdClass(); |
148 | $stdproc->name = $procname; | |
149 | $processor = \core_message\api::get_processed_processor_object($stdproc); | |
150 | if (!$processor->object->send_message($proceventdata)) { | |
349f98ad | 151 | debugging('Error calling message processor ' . $procname); |
349f98ad PS |
152 | } |
153 | } | |
154 | ||
376a79c2 MN |
155 | // Trigger event for sending a message or notification - we need to do this before marking as read! |
156 | if ($eventdata->notification) { | |
157 | \core\event\notification_sent::create_from_ids( | |
158 | $eventdata->userfrom->id, | |
159 | $eventdata->userto->id, | |
160 | $savemessage->id, | |
161 | $eventdata->courseid | |
162 | )->trigger(); | |
163 | } else { // Must be a message. | |
883ce421 MN |
164 | \core\event\message_sent::create_from_ids( |
165 | $eventdata->userfrom->id, | |
166 | $eventdata->userto->id, | |
167 | $savemessage->id, | |
168 | $eventdata->courseid | |
cc350fd9 | 169 | )->trigger(); |
883ce421 | 170 | } |
349f98ad | 171 | |
883ce421 MN |
172 | // If messaging is disabled and they previously had forum notifications handled by the popup processor |
173 | // or any processor that puts a row in message_working then the notification will remain forever | |
174 | // unread. To prevent this mark the message read if messaging is disabled. | |
175 | if (empty($CFG->messaging) && $eventdata->notification) { | |
548936a6 | 176 | \core_message\api::mark_notification_as_read($savemessage); |
883ce421 | 177 | } |
349f98ad | 178 | |
883ce421 MN |
179 | // If there is no more processors that want to process this we can mark the message as read. |
180 | if ($DB->count_records('message_working', array('unreadmessageid' => $savemessage->id)) == 0) { | |
181 | if ($eventdata->notification) { | |
548936a6 MN |
182 | $savemessage->timeread = null; |
183 | \core_message\api::mark_notification_as_read($savemessage); | |
883ce421 | 184 | } else { |
548936a6 | 185 | \core_message\api::mark_message_as_read($eventdata->userto->id, $savemessage); |
883ce421 | 186 | } |
349f98ad PS |
187 | } |
188 | ||
883ce421 | 189 | return $savemessage->id; |
349f98ad PS |
190 | } |
191 | ||
192 | /** | |
193 | * Notification from DML layer. | |
194 | * | |
195 | * Note: to be used from DML layer only. | |
196 | */ | |
197 | public static function database_transaction_commited() { | |
198 | if (!self::$buffer) { | |
199 | return; | |
200 | } | |
201 | self::process_buffer(); | |
202 | } | |
203 | ||
204 | /** | |
205 | * Notification from DML layer. | |
206 | * | |
207 | * Note: to be used from DML layer only. | |
208 | */ | |
209 | public static function database_transaction_rolledback() { | |
210 | self::$buffer = array(); | |
211 | } | |
212 | ||
213 | /** | |
214 | * Sent out any buffered messages if necessary. | |
215 | */ | |
216 | protected static function process_buffer() { | |
217 | // Reset the buffer first in case we get exception from processor. | |
218 | $messages = self::$buffer; | |
219 | self::$buffer = array(); | |
220 | ||
221 | foreach ($messages as $message) { | |
222 | list($eventdata, $savemessage, $processorlist) = $message; | |
223 | self::send_message_to_processors($eventdata, $savemessage, $processorlist); | |
224 | } | |
225 | } | |
226 | } |