forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messagelib.php
593 lines (516 loc) · 25.2 KB
/
messagelib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Functions for interacting with the message system
*
* @package core_message
* @copyright 2008 Luis Rodrigues and Martin Dougiamas
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(dirname(dirname(__FILE__)) . '/message/lib.php');
/**
* Called when a message provider wants to send a message.
* This functions checks the message recipient's message processor configuration then
* sends the message to the configured processors
*
* Required parameters of the $eventdata object:
* component string component name. must exist in message_providers
* name string message type name. must exist in message_providers
* userfrom object|int the user sending the message
* userto object|int the message recipient
* subject string the message subject
* fullmessage string the full message in a given format
* fullmessageformat int the format if the full message (FORMAT_MOODLE, FORMAT_HTML, ..)
* fullmessagehtml string the full version (the message processor will choose with one to use)
* smallmessage string the small version of the message
*
* Optional parameters of the $eventdata object:
* notification bool should the message be considered as a notification rather than a personal message
* contexturl string if this is a notification then you can specify a url to view the event. For example the forum post the user is being notified of.
* contexturlname string the display text for contexturl
*
* @category message
* @param object $eventdata information about the message (component, userfrom, userto, ...)
* @return mixed the integer ID of the new message or false if there was a problem with a processor
*/
function message_send($eventdata) {
global $CFG, $DB;
//new message ID to return
$messageid = false;
//TODO: we need to solve problems with database transactions here somehow, for now we just prevent transactions - sorry
$DB->transactions_forbidden();
if (is_number($eventdata->userto)) {
$eventdata->userto = $DB->get_record('user', array('id' => $eventdata->userto));
}
if (is_int($eventdata->userfrom)) {
$eventdata->userfrom = $DB->get_record('user', array('id' => $eventdata->userfrom));
}
if (!isset($eventdata->userto->auth) or !isset($eventdata->userto->suspended) or !isset($eventdata->userto->deleted)) {
$eventdata->userto = $DB->get_record('user', array('id' => $eventdata->userto->id));
}
//after how long inactive should the user be considered logged off?
if (isset($CFG->block_online_users_timetosee)) {
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
} else {
$timetoshowusers = 300;//5 minutes
}
// Work out if the user is logged in or not
if (!empty($eventdata->userto->lastaccess) && (time()-$timetoshowusers) < $eventdata->userto->lastaccess) {
$userstate = 'loggedin';
} else {
$userstate = 'loggedoff';
}
// Create the message object
$savemessage = new stdClass();
$savemessage->useridfrom = $eventdata->userfrom->id;
$savemessage->useridto = $eventdata->userto->id;
$savemessage->subject = $eventdata->subject;
$savemessage->fullmessage = $eventdata->fullmessage;
$savemessage->fullmessageformat = $eventdata->fullmessageformat;
$savemessage->fullmessagehtml = $eventdata->fullmessagehtml;
$savemessage->smallmessage = $eventdata->smallmessage;
if (!empty($eventdata->notification)) {
$savemessage->notification = $eventdata->notification;
} else {
$savemessage->notification = 0;
}
if (!empty($eventdata->contexturl)) {
$savemessage->contexturl = $eventdata->contexturl;
} else {
$savemessage->contexturl = null;
}
if (!empty($eventdata->contexturlname)) {
$savemessage->contexturlname = $eventdata->contexturlname;
} else {
$savemessage->contexturlname = null;
}
$savemessage->timecreated = time();
if (PHPUNIT_TEST and class_exists('phpunit_util')) {
// Add some more tests to make sure the normal code can actually work.
$componentdir = get_component_directory($eventdata->component);
if (!$componentdir or !is_dir($componentdir)) {
throw new coding_exception('Invalid component specified in message-send(): '.$eventdata->component);
}
if (!file_exists("$componentdir/db/messages.php")) {
throw new coding_exception("$eventdata->component does not contain db/messages.php necessary for message_send()");
}
$messageproviders = null;
include("$componentdir/db/messages.php");
if (!isset($messageproviders[$eventdata->name])) {
throw new coding_exception("Missing messaging defaults for event '$eventdata->name' in '$eventdata->component' messages.php file");
}
unset($componentdir);
unset($messageproviders);
// Now ask phpunit if it wants to catch this message.
if (phpunit_util::is_redirecting_messages()) {
$savemessage->timeread = time();
$messageid = $DB->insert_record('message_read', $savemessage);
$message = $DB->get_record('message_read', array('id'=>$messageid));
phpunit_util::message_sent($message);
return $messageid;
}
}
// Fetch enabled processors
$processors = get_message_processors(true);
// Fetch default (site) preferences
$defaultpreferences = get_message_output_default_preferences();
// Preset variables
$processorlist = array();
$preferencebase = $eventdata->component.'_'.$eventdata->name;
// Fill in the array of processors to be used based on default and user preferences
foreach ($processors as $processor) {
// First find out permissions
$defaultpreference = $processor->name.'_provider_'.$preferencebase.'_permitted';
if (isset($defaultpreferences->{$defaultpreference})) {
$permitted = $defaultpreferences->{$defaultpreference};
} else {
//MDL-25114 They supplied an $eventdata->component $eventdata->name combination which doesn't
//exist in the message_provider table (thus there is no default settings for them)
$preferrormsg = get_string('couldnotfindpreference', 'message', $defaultpreference);
throw new coding_exception($preferrormsg,'blah');
}
// Find out if user has configured this output
// Some processors cannot function without settings from the user
$userisconfigured = $processor->object->is_user_configured($eventdata->userto);
// DEBUG: notify if we are forcing unconfigured output
if ($permitted == 'forced' && !$userisconfigured) {
debugging('Attempt to force message delivery to user who has "'.$processor->name.'" output unconfigured', DEBUG_NORMAL);
}
// Warn developers that necessary data is missing regardless of how the processors are configured
if (!isset($eventdata->userto->emailstop)) {
debugging('userto->emailstop is not set. Retrieving it from the user table');
$eventdata->userto->emailstop = $DB->get_field('user', 'emailstop', array('id'=>$eventdata->userto->id));
}
// Populate the list of processors we will be using
if ($permitted == 'forced' && $userisconfigured) {
// An admin is forcing users to use this message processor. Use this processor unconditionally.
$processorlist[] = $processor->name;
} else if ($permitted == 'permitted' && $userisconfigured && !$eventdata->userto->emailstop) {
// User has not disabled notifications
// See if user set any notification preferences, otherwise use site default ones
$userpreferencename = 'message_provider_'.$preferencebase.'_'.$userstate;
if ($userpreference = get_user_preferences($userpreferencename, null, $eventdata->userto->id)) {
if (in_array($processor->name, explode(',', $userpreference))) {
$processorlist[] = $processor->name;
}
} else if (isset($defaultpreferences->{$userpreferencename})) {
if (in_array($processor->name, explode(',', $defaultpreferences->{$userpreferencename}))) {
$processorlist[] = $processor->name;
}
}
}
}
if (empty($processorlist) && $savemessage->notification) {
//if they have deselected all processors and its a notification mark it read. The user doesnt want to be bothered
$savemessage->timeread = time();
$messageid = $DB->insert_record('message_read', $savemessage);
} else { // Process the message
// Store unread message just in case we can not send it
$messageid = $savemessage->id = $DB->insert_record('message', $savemessage);
$eventdata->savedmessageid = $savemessage->id;
// Try to deliver the message to each processor
if (!empty($processorlist)) {
foreach ($processorlist as $procname) {
if (!$processors[$procname]->object->send_message($eventdata)) {
debugging('Error calling message processor '.$procname);
$messageid = false;
}
}
//if messaging is disabled and they previously had forum notifications handled by the popup processor
//or any processor that puts a row in message_working then the notification will remain forever
//unread. To prevent this mark the message read if messaging is disabled
if (empty($CFG->messaging)) {
require_once($CFG->dirroot.'/message/lib.php');
$messageid = message_mark_message_read($savemessage, time());
} else if ( $DB->count_records('message_working', array('unreadmessageid' => $savemessage->id)) == 0){
//if there is no more processors that want to process this we can move message to message_read
require_once($CFG->dirroot.'/message/lib.php');
$messageid = message_mark_message_read($savemessage, time(), true);
}
}
}
return $messageid;
}
/**
* Updates the message_providers table with the current set of message providers
*
* @param string $component For example 'moodle', 'mod_forum' or 'block_quiz_results'
* @return boolean True on success
*/
function message_update_providers($component='moodle') {
global $DB;
// load message providers from files
$fileproviders = message_get_providers_from_file($component);
// load message providers from the database
$dbproviders = message_get_providers_from_db($component);
foreach ($fileproviders as $messagename => $fileprovider) {
if (!empty($dbproviders[$messagename])) { // Already exists in the database
// check if capability has changed
if ($dbproviders[$messagename]->capability == $fileprovider['capability']) { // Same, so ignore
// exact same message provider already present in db, ignore this entry
unset($dbproviders[$messagename]);
continue;
} else { // Update existing one
$provider = new stdClass();
$provider->id = $dbproviders[$messagename]->id;
$provider->capability = $fileprovider['capability'];
$DB->update_record('message_providers', $provider);
unset($dbproviders[$messagename]);
continue;
}
} else { // New message provider, add it
$provider = new stdClass();
$provider->name = $messagename;
$provider->component = $component;
$provider->capability = $fileprovider['capability'];
$transaction = $DB->start_delegated_transaction();
$DB->insert_record('message_providers', $provider);
message_set_default_message_preference($component, $messagename, $fileprovider);
$transaction->allow_commit();
}
}
foreach ($dbproviders as $dbprovider) { // Delete old ones
$DB->delete_records('message_providers', array('id' => $dbprovider->id));
$DB->delete_records_select('config_plugins', "plugin = 'message' AND ".$DB->sql_like('name', '?', false), array("%_provider_{$component}_{$dbprovider->name}_%"));
$DB->delete_records_select('user_preferences', $DB->sql_like('name', '?', false), array("message_provider_{$component}_{$dbprovider->name}_%"));
}
return true;
}
/**
* This function populates default message preferences for all existing providers
* when the new message processor is added.
*
* @param string $processorname The name of message processor plugin (e.g. 'email', 'jabber')
* @throws invalid_parameter_exception if $processorname does not exist in the database
*/
function message_update_processors($processorname) {
global $DB;
// validate if our processor exists
$processor = $DB->get_records('message_processors', array('name' => $processorname));
if (empty($processor)) {
throw new invalid_parameter_exception();
}
$providers = $DB->get_records_sql('SELECT DISTINCT component FROM {message_providers}');
$transaction = $DB->start_delegated_transaction();
foreach ($providers as $provider) {
// load message providers from files
$fileproviders = message_get_providers_from_file($provider->component);
foreach ($fileproviders as $messagename => $fileprovider) {
message_set_default_message_preference($provider->component, $messagename, $fileprovider, $processorname);
}
}
$transaction->allow_commit();
}
/**
* Setting default messaging preferences for particular message provider
*
* @param string $component The name of component (e.g. moodle, mod_forum, etc.)
* @param string $messagename The name of message provider
* @param array $fileprovider The value of $messagename key in the array defined in plugin messages.php
* @param string $processorname The optional name of message processor
*/
function message_set_default_message_preference($component, $messagename, $fileprovider, $processorname='') {
global $DB;
// Fetch message processors
$condition = null;
// If we need to process a particular processor, set the select condition
if (!empty($processorname)) {
$condition = array('name' => $processorname);
}
$processors = $DB->get_records('message_processors', $condition);
// load default messaging preferences
$defaultpreferences = get_message_output_default_preferences();
// Setting default preference
$componentproviderbase = $component.'_'.$messagename;
$loggedinpref = array();
$loggedoffpref = array();
// set 'permitted' preference first for each messaging processor
foreach ($processors as $processor) {
$preferencename = $processor->name.'_provider_'.$componentproviderbase.'_permitted';
// if we do not have this setting yet, set it
if (!isset($defaultpreferences->{$preferencename})) {
// determine plugin default settings
$plugindefault = 0;
if (isset($fileprovider['defaults'][$processor->name])) {
$plugindefault = $fileprovider['defaults'][$processor->name];
}
// get string values of the settings
list($permitted, $loggedin, $loggedoff) = translate_message_default_setting($plugindefault, $processor->name);
// store default preferences for current processor
set_config($preferencename, $permitted, 'message');
// save loggedin/loggedoff settings
if ($loggedin) {
$loggedinpref[] = $processor->name;
}
if ($loggedoff) {
$loggedoffpref[] = $processor->name;
}
}
}
// now set loggedin/loggedoff preferences
if (!empty($loggedinpref)) {
$preferencename = 'message_provider_'.$componentproviderbase.'_loggedin';
if (isset($defaultpreferences->{$preferencename})) {
// We have the default preferences for this message provider, which
// likely means that we have been adding a new processor. Add defaults
// to exisitng preferences.
$loggedinpref = array_merge($loggedinpref, explode(',', $defaultpreferences->{$preferencename}));
}
set_config($preferencename, join(',', $loggedinpref), 'message');
}
if (!empty($loggedoffpref)) {
$preferencename = 'message_provider_'.$componentproviderbase.'_loggedoff';
if (isset($defaultpreferences->{$preferencename})) {
// We have the default preferences for this message provider, which
// likely means that we have been adding a new processor. Add defaults
// to exisitng preferences.
$loggedoffpref = array_merge($loggedoffpref, explode(',', $defaultpreferences->{$preferencename}));
}
set_config($preferencename, join(',', $loggedoffpref), 'message');
}
}
/**
* This function has been deprecated please use {@link message_get_providers_for_user()} instead.
*
* Returns the active providers for the current user, based on capability
*
* @see message_get_providers_for_user()
* @deprecated since 2.1
* @todo Remove in 2.5 (MDL-34454)
* @return array An array of message providers
*/
function message_get_my_providers() {
global $USER;
debugging('message_get_my_providers is deprecated please update your code', DEBUG_DEVELOPER);
return message_get_providers_for_user($USER->id);
}
/**
* Returns the active providers for the user specified, based on capability
*
* @param int $userid id of user
* @return array An array of message providers
*/
function message_get_providers_for_user($userid) {
global $DB, $CFG;
$providers = get_message_providers();
// Ensure user is not allowed to configure instantmessage if it is globally disabled.
if (!$CFG->messaging) {
foreach ($providers as $providerid => $provider) {
if ($provider->name == 'instantmessage') {
unset($providers[$providerid]);
break;
}
}
}
// If the component is an enrolment plugin, check it is enabled
foreach ($providers as $providerid => $provider) {
list($type, $name) = normalize_component($provider->component);
if ($type == 'enrol' && !enrol_is_enabled($name)) {
unset($providers[$providerid]);
}
}
// Now we need to check capabilities. We need to eliminate the providers
// where the user does not have the corresponding capability anywhere.
// Here we deal with the common simple case of the user having the
// capability in the system context. That handles $CFG->defaultuserroleid.
// For the remaining providers/capabilities, we need to do a more complex
// query involving all overrides everywhere.
$unsureproviders = array();
$unsurecapabilities = array();
$systemcontext = context_system::instance();
foreach ($providers as $providerid => $provider) {
if (empty($provider->capability) || has_capability($provider->capability, $systemcontext, $userid)) {
// The provider is relevant to this user.
continue;
}
$unsureproviders[$providerid] = $provider;
$unsurecapabilities[$provider->capability] = 1;
unset($providers[$providerid]);
}
if (empty($unsureproviders)) {
// More complex checks are not required.
return $providers;
}
// Now check the unsure capabilities.
list($capcondition, $params) = $DB->get_in_or_equal(
array_keys($unsurecapabilities), SQL_PARAMS_NAMED);
$params['userid'] = $userid;
$sql = "SELECT DISTINCT rc.capability, 1
FROM {role_assignments} ra
JOIN {context} actx ON actx.id = ra.contextid
JOIN {role_capabilities} rc ON rc.roleid = ra.roleid
JOIN {context} cctx ON cctx.id = rc.contextid
WHERE ra.userid = :userid
AND rc.capability $capcondition
AND rc.permission > 0
AND (".$DB->sql_concat('actx.path', "'/'")." LIKE ".$DB->sql_concat('cctx.path', "'/%'").
" OR ".$DB->sql_concat('cctx.path', "'/'")." LIKE ".$DB->sql_concat('actx.path', "'/%'").")";
if (!empty($CFG->defaultfrontpageroleid)) {
$frontpagecontext = context_course::instance(SITEID);
list($capcondition2, $params2) = $DB->get_in_or_equal(
array_keys($unsurecapabilities), SQL_PARAMS_NAMED);
$params = array_merge($params, $params2);
$params['frontpageroleid'] = $CFG->defaultfrontpageroleid;
$params['frontpagepathpattern'] = $frontpagecontext->path . '/';
$sql .= "
UNION
SELECT DISTINCT rc.capability, 1
FROM {role_capabilities} rc
JOIN {context} cctx ON cctx.id = rc.contextid
WHERE rc.roleid = :frontpageroleid
AND rc.capability $capcondition2
AND rc.permission > 0
AND ".$DB->sql_concat('cctx.path', "'/'")." LIKE :frontpagepathpattern";
}
$relevantcapabilities = $DB->get_records_sql_menu($sql, $params);
// Add back any providers based on the detailed capability check.
foreach ($unsureproviders as $providerid => $provider) {
if (array_key_exists($provider->capability, $relevantcapabilities)) {
$providers[$providerid] = $provider;
}
}
return $providers;
}
/**
* Gets the message providers that are in the database for this component.
*
* This is an internal function used within messagelib.php
*
* @see message_update_providers()
* @param string $component A moodle component like 'moodle', 'mod_forum', 'block_quiz_results'
* @return array An array of message providers
*/
function message_get_providers_from_db($component) {
global $DB;
return $DB->get_records('message_providers', array('component'=>$component), '', 'name, id, component, capability'); // Name is unique per component
}
/**
* Loads the messages definitions for a component from file
*
* If no messages are defined for the component, return an empty array.
* This is an internal function used within messagelib.php
*
* @see message_update_providers()
* @see message_update_processors()
* @param string $component A moodle component like 'moodle', 'mod_forum', 'block_quiz_results'
* @return array An array of message providers or empty array if not exists
*/
function message_get_providers_from_file($component) {
$defpath = get_component_directory($component).'/db/messages.php';
$messageproviders = array();
if (file_exists($defpath)) {
require($defpath);
}
foreach ($messageproviders as $name => $messageprovider) { // Fix up missing values if required
if (empty($messageprovider['capability'])) {
$messageproviders[$name]['capability'] = NULL;
}
if (empty($messageprovider['defaults'])) {
$messageproviders[$name]['defaults'] = array();
}
}
return $messageproviders;
}
/**
* Remove all message providers for particular component and corresponding settings
*
* @param string $component A moodle component like 'moodle', 'mod_forum', 'block_quiz_results'
* @return void
*/
function message_provider_uninstall($component) {
global $DB;
$transaction = $DB->start_delegated_transaction();
$DB->delete_records('message_providers', array('component' => $component));
$DB->delete_records_select('config_plugins', "plugin = 'message' AND ".$DB->sql_like('name', '?', false), array("%_provider_{$component}_%"));
$DB->delete_records_select('user_preferences', $DB->sql_like('name', '?', false), array("message_provider_{$component}_%"));
$transaction->allow_commit();
}
/**
* Uninstall a message processor
*
* @param string $name A message processor name like 'email', 'jabber'
*/
function message_processor_uninstall($name) {
global $DB;
$transaction = $DB->start_delegated_transaction();
$DB->delete_records('message_processors', array('name' => $name));
$DB->delete_records_select('config_plugins', "plugin = ?", array("message_{$name}"));
// delete permission preferences only, we do not care about loggedin/loggedoff
// defaults, they will be removed on the next attempt to update the preferences
$DB->delete_records_select('config_plugins', "plugin = 'message' AND ".$DB->sql_like('name', '?', false), array("{$name}_provider_%"));
$transaction->allow_commit();
}