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