message MDLSITE-1042 prevented messages being automatically marked as read if the...
[moodle.git] / lib / messagelib.php
CommitLineData
ce02a9bf 1<?php
3b120e46 2
ce02a9bf 3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
3b120e46 17
18/**
120b3758 19 * messagelib.php - Contains generic messaging functions for the message system
3b120e46 20 *
78bfb562
PS
21 * @package core
22 * @subpackage message
23 * @copyright Luis Rodrigues and Martin Dougiamas
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3b120e46 25 */
26
78bfb562
PS
27defined('MOODLE_INTERNAL') || die();
28
3b120e46 29/**
7c7d3afa 30 * Called when a message provider wants to send a message.
3b120e46 31 * This functions checks the user's processor configuration to send the given type of message,
32 * then tries to send it.
7c7d3afa
PS
33 *
34 * Required parameter $eventdata structure:
35 * modulename -
36 * userfrom
37 * userto
38 * subject
39 * fullmessage - the full message in a given format
40 * fullmessageformat - the format if the full message (FORMAT_MOODLE, FORMAT_HTML, ..)
41 * fullmessagehtml - the full version (the message processor will choose with one to use)
42 * smallmessage - the small version of the message
43 *
44 * @param object $eventdata information about the message (modulename, userfrom, userto, ...)
3b120e46 45 * @return boolean success
46 */
7c7d3afa 47function message_send($eventdata) {
3b120e46 48 global $CFG, $DB;
49
7c7d3afa
PS
50 //TODO: this function is very slow and inefficient, it would be a major bottleneck in cron processing, this has to be improved in 2.0
51 // probably we could add two parameters with user messaging preferences and we could somehow preload/cache them in cron
e484041b
PS
52
53 //TODO: we need to solve problems with database transactions here somehow, for now we just prevent transactions - sorry
54 $DB->transactions_forbidden();
7c7d3afa 55
1560760f 56 //after how long inactive should the user be considered logged off?
3b120e46 57 if (isset($CFG->block_online_users_timetosee)) {
58 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
59 } else {
1560760f 60 $timetoshowusers = 300;//5 minutes
3b120e46 61 }
62
1560760f
AD
63 // Work out if the user is logged in or not
64 if ((time() - $timetoshowusers) < $eventdata->userto->lastaccess) {
3b120e46 65 $userstate = 'loggedin';
1560760f
AD
66 } else {
67 $userstate = 'loggedoff';
3b120e46 68 }
117bd748 69
1560760f 70 // Create the message object
365a5941 71 $savemessage = new stdClass();
3b120e46 72 $savemessage->useridfrom = $eventdata->userfrom->id;
73 $savemessage->useridto = $eventdata->userto->id;
74 $savemessage->subject = $eventdata->subject;
75 $savemessage->fullmessage = $eventdata->fullmessage;
76 $savemessage->fullmessageformat = $eventdata->fullmessageformat;
77 $savemessage->fullmessagehtml = $eventdata->fullmessagehtml;
78 $savemessage->smallmessage = $eventdata->smallmessage;
79 $savemessage->timecreated = time();
80
1560760f
AD
81 // Find out what processors are defined currently
82 // When a user doesn't have settings none gets return, if he doesn't want contact "" gets returned
83 $preferencename = 'message_provider_'.$eventdata->component.'_'.$eventdata->name.'_'.$userstate;
84 $processor = get_user_preferences($preferencename, NULL, $eventdata->userto->id);
117bd748 85
7c7d3afa
PS
86 if ($processor == NULL) { //this user never had a preference, save default
87 if (!message_set_default_message_preferences($eventdata->userto)) {
818dc096 88 print_error('cannotsavemessageprefs', 'message');
2044a2b2 89 }
7c7d3afa
PS
90 if ($userstate == 'loggedin') {
91 $processor = 'popup';
2044a2b2 92 }
7c7d3afa
PS
93 if ($userstate == 'loggedoff') {
94 $processor = 'email';
2044a2b2 95 }
96 }
3b120e46 97
d3674ef7 98 // if we are supposed to do something with this message
2044a2b2 99 // No processor for this message, mark it as read
100 if ($processor == "") { //this user cleared all the preferences
117bd748 101 $savemessage->timeread = time();
3b120e46 102 $DB->insert_record('message_read', $savemessage);
103
104 } else { // Process the message
3b120e46 105 /// Store unread message just in case we can not send it
117bd748
PS
106 $savemessage->id = $DB->insert_record('message', $savemessage);
107
3b120e46 108 /// Try to deliver the message to each processor
109 $processorlist = explode(',', $processor);
110 foreach ($processorlist as $procname) {
111 $processorfile = $CFG->dirroot. '/message/output/'.$procname.'/message_output_'.$procname.'.php';
112
117bd748 113 if (is_readable($processorfile)) {
7c7d3afa 114 include_once($processorfile); // defines $module with version etc
3b120e46 115 $processclass = 'message_output_' . $procname;
117bd748
PS
116
117 if (class_exists($processclass)) {
3b120e46 118 $pclass = new $processclass();
119
7c7d3afa 120 if (!$pclass->send_message($savemessage)) {
3b120e46 121 debugging('Error calling message processor '.$procname);
122 return false;
117bd748 123 }
3b120e46 124 }
125 } else {
126 debugging('Error calling message processor '.$procname);
127 return false;
128 }
129 }
1560760f
AD
130
131 $savemessage->timeread = time();
132 $messageid = $savemessage->id;
133 unset($savemessage->id);
134
5683b9e5
AD
135 //MDLSITE-1042 only the popup processor adds rows to message_working
136 //if the popup processor isnt selected messages will be marked read now and may not be seen
137 //For example, if the receiver has an incorrect email address configured or all processors deselected
138 //
1560760f 139 //if there is no more processors that want to process this we can move message to message_read
5683b9e5
AD
140 //if ( $DB->count_records('message_working', array('unreadmessageid' => $messageid)) == 0){
141 //$DB->insert_record('message_read', $savemessage);
142 //$DB->delete_records('message', array('id' => $messageid));
143 //}
3b120e46 144 }
145
146 return true;
147}
148
120b3758 149
150/**
151 * This code updates the message_providers table with the current set of providers
17da2e6f 152 * @param $component - examples: 'moodle', 'mod_forum', 'block_quiz_results'
120b3758 153 * @return boolean
154 */
155function message_update_providers($component='moodle') {
156 global $DB;
157
158 // load message providers from files
159 $fileproviders = message_get_providers_from_file($component);
160
161 // load message providers from the database
162 $dbproviders = message_get_providers_from_db($component);
163
164 foreach ($fileproviders as $messagename => $fileprovider) {
165
166 if (!empty($dbproviders[$messagename])) { // Already exists in the database
117bd748 167
120b3758 168 if ($dbproviders[$messagename]->capability == $fileprovider['capability']) { // Same, so ignore
169 // exact same message provider already present in db, ignore this entry
170 unset($dbproviders[$messagename]);
171 continue;
172
173 } else { // Update existing one
365a5941 174 $provider = new stdClass();
7c7d3afa
PS
175 $provider->id = $dbproviders[$messagename]->id;
176 $provider->capability = $fileprovider['capability'];
120b3758 177 $DB->update_record('message_providers', $provider);
178 unset($dbproviders[$messagename]);
179 continue;
180 }
181
182 } else { // New message provider, add it
183
365a5941 184 $provider = new stdClass();
120b3758 185 $provider->name = $messagename;
186 $provider->component = $component;
187 $provider->capability = $fileprovider['capability'];
188
189 $DB->insert_record('message_providers', $provider);
190 }
191 }
192
193 foreach ($dbproviders as $dbprovider) { // Delete old ones
194 $DB->delete_records('message_providers', array('id' => $dbprovider->id));
195 }
196
197 return true;
198}
199
200/**
201 * Returns the active providers for the current user, based on capability
202 * @return array of message providers
203 */
204function message_get_my_providers() {
205 global $DB;
206
207 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
208
209 $providers = $DB->get_records('message_providers');
210
211 // Remove all the providers we aren't allowed to see now
212 foreach ($providers as $providerid => $provider) {
213 if (!empty($provider->capability)) {
214 if (!has_capability($provider->capability, $systemcontext)) {
215 unset($providers[$providerid]); // Not allowed to see this
216 }
217 }
218 }
219
220 return $providers;
221}
222
223/**
224 * Gets the message providers that are in the database for this component.
225 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
226 * @return array of message providers
227 *
228 * INTERNAL - to be used from messagelib only
229 */
230function message_get_providers_from_db($component) {
231 global $DB;
232
7c7d3afa 233 return $DB->get_records('message_providers', array('component'=>$component), '', 'name, id, component, capability'); // Name is unique per component
120b3758 234}
235
236/**
237 * Loads the messages definitions for the component (from file). If no
238 * messages are defined for the component, we simply return an empty array.
17da2e6f 239 * @param $component - examples: 'moodle', 'mod_forum', 'block_quiz_results'
120b3758 240 * @return array of message providerss or empty array if not exists
241 *
242 * INTERNAL - to be used from messagelib only
243 */
244function message_get_providers_from_file($component) {
17da2e6f 245 $defpath = get_component_directory($component).'/db/messages.php';
120b3758 246
247 $messageproviders = array();
248
249 if (file_exists($defpath)) {
250 require($defpath);
251 }
252
253 foreach ($messageproviders as $name => $messageprovider) { // Fix up missing values if required
254 if (empty($messageprovider['capability'])) {
255 $messageproviders[$name]['capability'] = NULL;
256 }
257 }
258
259 return $messageproviders;
260}
261
262/**
117bd748 263 * Remove all message providers
120b3758 264 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
265 */
266function message_uninstall($component) {
03dd0575 267 global $DB;
120b3758 268 return $DB->delete_records('message_providers', array('component' => $component));
269}
270
2044a2b2 271/**
272 * Set default message preferences.
273 * @param $user - User to set message preferences
274 */
7c7d3afa 275function message_set_default_message_preferences($user) {
2044a2b2 276 global $DB;
117bd748 277
2044a2b2 278 $providers = $DB->get_records('message_providers');
279 $preferences = array();
7c7d3afa
PS
280 foreach ($providers as $providerid => $provider) {
281 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_loggedin'] = 'popup';
282 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_loggedoff'] = 'email';
117bd748 283 }
7c7d3afa 284 return set_user_preferences($preferences, $user->id);
2044a2b2 285}