MDL-63724 core_message: add new group_message_sent event
[moodle.git] / lib / classes / message / manager.php
CommitLineData
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
27namespace core\message;
28
29defined('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 */
44class 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 *
cc350fd9 53 * @param \core\message\message $eventdata fully prepared event data for processors
349f98ad
PS
54 * @param \stdClass $savemessage the message saved in 'message' table
55 * @param array $processorlist list of processors for target user
883ce421 56 * @return int $messageid the id from 'messages' (false is not returned)
349f98ad 57 */
cc4952e9 58 public static function send_message(message $eventdata, \stdClass $savemessage, array $processorlist) {
349f98ad 59 global $CFG;
c6f45ef8 60
349f98ad
PS
61 require_once($CFG->dirroot.'/message/lib.php'); // This is most probably already included from messagelib.php file.
62
63 if (empty($processorlist)) {
376a79c2 64 // Trigger event for sending a message or notification - we need to do this before marking as read!
947d3289 65 self::trigger_message_events($eventdata, $savemessage);
349f98ad 66
883ce421 67 if ($eventdata->notification or empty($CFG->messaging)) {
349f98ad
PS
68 // If they have deselected all processors and its a notification mark it read. The user doesn't want to be bothered.
69 // The same goes if the messaging is completely disabled.
883ce421 70 if ($eventdata->notification) {
548936a6
MN
71 $savemessage->timeread = null;
72 \core_message\api::mark_notification_as_read($savemessage);
883ce421 73 } else {
548936a6 74 \core_message\api::mark_message_as_read($eventdata->userto->id, $savemessage);
883ce421 75 }
349f98ad
PS
76 }
77
883ce421 78 return $savemessage->id;
349f98ad
PS
79 }
80
81 // Let the manager do the sending or buffering when db transaction in progress.
82 return self::send_message_to_processors($eventdata, $savemessage, $processorlist);
83 }
84
85 /**
86 * Send message to message processors.
87 *
c6f45ef8 88 * @param \stdClass|\core\message\message $eventdata
349f98ad
PS
89 * @param \stdClass $savemessage
90 * @param array $processorlist
91 * @return int $messageid
92 */
c6f45ef8
AA
93 protected static function send_message_to_processors($eventdata, \stdClass $savemessage, array
94 $processorlist) {
349f98ad
PS
95 global $CFG, $DB;
96
97 // We cannot communicate with external systems in DB transactions,
98 // buffer the messages if necessary.
349f98ad
PS
99 if ($DB->is_transaction_started()) {
100 // We need to clone all objects so that devs may not modify it from outside later.
101 $eventdata = clone($eventdata);
102 $eventdata->userto = clone($eventdata->userto);
103 $eventdata->userfrom = clone($eventdata->userfrom);
104
105 // Conserve some memory the same was as $USER setup does.
106 unset($eventdata->userto->description);
107 unset($eventdata->userfrom->description);
108
109 self::$buffer[] = array($eventdata, $savemessage, $processorlist);
110 return $savemessage->id;
111 }
112
947d3289
JD
113 // Send the message to processors.
114 self::call_processors($eventdata, $processorlist);
349f98ad 115
376a79c2 116 // Trigger event for sending a message or notification - we need to do this before marking as read!
947d3289 117 self::trigger_message_events($eventdata, $savemessage);
349f98ad 118
0d657cc9
MN
119 if (empty($CFG->messaging)) {
120 // If they have deselected all processors and its a notification mark it read. The user doesn't want to be bothered.
121 // The same goes if the messaging is completely disabled.
883ce421 122 if ($eventdata->notification) {
548936a6
MN
123 $savemessage->timeread = null;
124 \core_message\api::mark_notification_as_read($savemessage);
883ce421 125 } else {
548936a6 126 \core_message\api::mark_message_as_read($eventdata->userto->id, $savemessage);
883ce421 127 }
349f98ad
PS
128 }
129
883ce421 130 return $savemessage->id;
349f98ad
PS
131 }
132
133 /**
134 * Notification from DML layer.
135 *
136 * Note: to be used from DML layer only.
137 */
138 public static function database_transaction_commited() {
139 if (!self::$buffer) {
140 return;
141 }
142 self::process_buffer();
143 }
144
145 /**
146 * Notification from DML layer.
147 *
148 * Note: to be used from DML layer only.
149 */
150 public static function database_transaction_rolledback() {
151 self::$buffer = array();
152 }
153
154 /**
155 * Sent out any buffered messages if necessary.
156 */
157 protected static function process_buffer() {
158 // Reset the buffer first in case we get exception from processor.
159 $messages = self::$buffer;
160 self::$buffer = array();
161
162 foreach ($messages as $message) {
163 list($eventdata, $savemessage, $processorlist) = $message;
164 self::send_message_to_processors($eventdata, $savemessage, $processorlist);
165 }
166 }
947d3289
JD
167
168 /**
169 * Trigger an appropriate message creation event, based on the supplied $eventdata and $savemessage.
170 *
171 * @param message $eventdata the eventdata for the message.
172 * @param \stdClass $savemessage the message record.
173 * @throws \coding_exception
174 */
175 protected static function trigger_message_events(message $eventdata, \stdClass $savemessage) {
176 if ($eventdata->notification) {
177 \core\event\notification_sent::create_from_ids(
178 $eventdata->userfrom->id,
179 $eventdata->userto->id,
180 $savemessage->id,
181 $eventdata->courseid
182 )->trigger();
183 } else { // Must be a message.
184 \core\event\message_sent::create_from_ids(
185 $eventdata->userfrom->id,
186 $eventdata->userto->id,
187 $savemessage->id,
188 $eventdata->courseid
189 )->trigger();
190 }
191 }
192
193 /**
194 * For each processor, call it's send_message() method.
195 *
196 * @param message $eventdata the message object.
197 * @param array $processorlist the list of processors for a single user.
198 */
199 protected static function call_processors(message $eventdata, array $processorlist) {
200 foreach ($processorlist as $procname) {
201 // Let new messaging class add custom content based on the processor.
202 $proceventdata = ($eventdata instanceof message) ? $eventdata->get_eventobject_for_processor($procname) : $eventdata;
203 $stdproc = new \stdClass();
204 $stdproc->name = $procname;
205 $processor = \core_message\api::get_processed_processor_object($stdproc);
206 if (!$processor->object->send_message($proceventdata)) {
207 debugging('Error calling message processor ' . $procname);
208 }
209 }
210 }
349f98ad 211}