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