MDL-63211 core_message: added new web services
[moodle.git] / message / tests / privacy_provider_test.php
CommitLineData
04cd1b3f
MN
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 * Privacy provider tests.
19 *
20 * @package core_message
21 * @copyright 2018 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25use core_privacy\local\metadata\collection;
26use core_message\privacy\provider;
27use \core_privacy\local\request\writer;
28use \core_privacy\local\request\transform;
29
30defined('MOODLE_INTERNAL') || die();
31
32/**
33 * Privacy provider tests class.
34 *
35 * @package core_message
36 * @copyright 2018 Mark Nelson <markn@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class core_message_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
40
41 /**
42 * Test for provider::get_metadata().
43 */
44 public function test_get_metadata() {
45 $collection = new collection('core_message');
46 $newcollection = provider::get_metadata($collection);
47 $itemcollection = $newcollection->get_collection();
48 $this->assertCount(6, $itemcollection);
49
50 $messagestable = array_shift($itemcollection);
51 $this->assertEquals('messages', $messagestable->get_name());
52
53 $messageuseractionstable = array_shift($itemcollection);
54 $this->assertEquals('message_user_actions', $messageuseractionstable->get_name());
55
56 $messageconversationmemberstable = array_shift($itemcollection);
57 $this->assertEquals('message_conversation_members', $messageconversationmemberstable->get_name());
58
59 $messagecontacts = array_shift($itemcollection);
60 $this->assertEquals('message_contacts', $messagecontacts->get_name());
61
62 $notificationstable = array_shift($itemcollection);
63 $this->assertEquals('notifications', $notificationstable->get_name());
64
65 $usersettings = array_shift($itemcollection);
66 $this->assertEquals('core_message_messageprovider_settings', $usersettings->get_name());
67
68 $privacyfields = $messagestable->get_privacy_fields();
69 $this->assertArrayHasKey('useridfrom', $privacyfields);
70 $this->assertArrayHasKey('conversationid', $privacyfields);
71 $this->assertArrayHasKey('subject', $privacyfields);
72 $this->assertArrayHasKey('fullmessage', $privacyfields);
73 $this->assertArrayHasKey('fullmessageformat', $privacyfields);
74 $this->assertArrayHasKey('fullmessagehtml', $privacyfields);
75 $this->assertArrayHasKey('smallmessage', $privacyfields);
76 $this->assertArrayHasKey('timecreated', $privacyfields);
77 $this->assertEquals('privacy:metadata:messages', $messagestable->get_summary());
78
79 $privacyfields = $messageuseractionstable->get_privacy_fields();
80 $this->assertArrayHasKey('userid', $privacyfields);
81 $this->assertArrayHasKey('messageid', $privacyfields);
82 $this->assertArrayHasKey('action', $privacyfields);
83 $this->assertArrayHasKey('timecreated', $privacyfields);
84 $this->assertEquals('privacy:metadata:message_user_actions', $messageuseractionstable->get_summary());
85
86 $privacyfields = $messageconversationmemberstable->get_privacy_fields();
87 $this->assertArrayHasKey('conversationid', $privacyfields);
88 $this->assertArrayHasKey('userid', $privacyfields);
89 $this->assertArrayHasKey('timecreated', $privacyfields);
90 $this->assertEquals('privacy:metadata:message_conversation_members', $messageconversationmemberstable->get_summary());
91
92 $privacyfields = $messagecontacts->get_privacy_fields();
93 $this->assertArrayHasKey('userid', $privacyfields);
94 $this->assertArrayHasKey('contactid', $privacyfields);
95 $this->assertArrayHasKey('blocked', $privacyfields);
96 $this->assertEquals('privacy:metadata:message_contacts', $messagecontacts->get_summary());
97
98 $privacyfields = $notificationstable->get_privacy_fields();
99 $this->assertArrayHasKey('useridfrom', $privacyfields);
100 $this->assertArrayHasKey('useridto', $privacyfields);
101 $this->assertArrayHasKey('subject', $privacyfields);
102 $this->assertArrayHasKey('fullmessage', $privacyfields);
103 $this->assertArrayHasKey('fullmessageformat', $privacyfields);
104 $this->assertArrayHasKey('fullmessagehtml', $privacyfields);
105 $this->assertArrayHasKey('smallmessage', $privacyfields);
106 $this->assertArrayHasKey('component', $privacyfields);
107 $this->assertArrayHasKey('eventtype', $privacyfields);
108 $this->assertArrayHasKey('contexturl', $privacyfields);
109 $this->assertArrayHasKey('contexturlname', $privacyfields);
110 $this->assertArrayHasKey('timeread', $privacyfields);
111 $this->assertArrayHasKey('timecreated', $privacyfields);
112 $this->assertEquals('privacy:metadata:notifications', $notificationstable->get_summary());
113 }
114
115 /**
116 * Test for provider::export_user_preferences().
117 */
118 public function test_export_user_preferences_no_pref() {
119 $this->resetAfterTest();
120
121 $user = $this->getDataGenerator()->create_user();
122 provider::export_user_preferences($user->id);
123
124 $writer = writer::with_context(\context_system::instance());
125
126 $this->assertFalse($writer->has_any_data());
127 }
128
129 /**
130 * Test for provider::export_user_preferences().
131 */
132 public function test_export_user_preferences() {
133 global $USER;
134
135 $this->resetAfterTest();
136
137 $this->setAdminUser();
138
139 // Create another user to set a preference for who we won't be exporting.
140 $user = $this->getDataGenerator()->create_user();
141
142 // Set some message user preferences.
143 set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $USER->id);
144 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'popup', $USER->id);
145 set_user_preference('message_blocknoncontacts', 1, $USER->id);
146 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'inbound', $user->id);
147
148 // Set an unrelated preference.
bbe6eadd 149 set_user_preference('some_unrelated_preference', 'courses', $USER->id);
04cd1b3f
MN
150
151 provider::export_user_preferences($USER->id);
152
153 $writer = writer::with_context(\context_system::instance());
154
155 $this->assertTrue($writer->has_any_data());
156
157 $prefs = (array) $writer->get_user_preferences('core_message');
158
159 // Check only 3 preferences exist.
160 $this->assertCount(3, $prefs);
161 $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedin', $prefs);
162 $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedoff', $prefs);
163 $this->assertArrayHasKey('message_blocknoncontacts', $prefs);
164
165 foreach ($prefs as $key => $pref) {
166 if ($key == 'message_provider_moodle_instantmessage_loggedin') {
167 $this->assertEquals('airnotifier', $pref->value);
168 } else if ($key == 'message_provider_moodle_instantmessage_loggedoff') {
169 $this->assertEquals('popup', $pref->value);
170 } else {
171 $this->assertEquals(1, $pref->value);
172 }
173 }
174 }
175
176 /**
177 * Test for provider::get_contexts_for_userid().
178 */
179 public function test_get_contexts_for_userid() {
180 $this->resetAfterTest();
181
182 $user = $this->getDataGenerator()->create_user();
183 $contextlist = provider::get_contexts_for_userid($user->id);
184 $this->assertCount(1, $contextlist);
185 $contextforuser = $contextlist->current();
186 $this->assertEquals(SYSCONTEXTID, $contextforuser->id);
187 }
188
189 /**
190 * Test for provider::export_user_data().
191 */
192 public function test_export_for_context_with_contacts() {
193 $this->resetAfterTest();
194
195 // Create users to test with.
196 $user1 = $this->getDataGenerator()->create_user();
197 $user2 = $this->getDataGenerator()->create_user();
198 $user3 = $this->getDataGenerator()->create_user();
199 $user4 = $this->getDataGenerator()->create_user();
200
201 // This user will not be added as a contact.
202 $this->getDataGenerator()->create_user();
203
204 message_add_contact($user2->id, 0, $user1->id);
205 message_add_contact($user3->id, 0, $user1->id);
206 message_add_contact($user4->id, 1, $user1->id);
207
208 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
209
210 $writer = writer::with_context(\context_system::instance());
211
212 $contacts = (array) $writer->get_data([get_string('contacts', 'core_message')]);
823e08bf 213 usort($contacts, ['static', 'sort_contacts']);
04cd1b3f
MN
214
215 $this->assertCount(3, $contacts);
216
217 $contact1 = array_shift($contacts);
218 $this->assertEquals($user2->id, $contact1->contact);
219 $this->assertEquals(get_string('no'), $contact1->blocked);
220
221 $contact2 = array_shift($contacts);
222 $this->assertEquals($user3->id, $contact2->contact);
223 $this->assertEquals(get_string('no'), $contact2->blocked);
224
225 $contact3 = array_shift($contacts);
226 $this->assertEquals($user4->id, $contact3->contact);
227 $this->assertEquals(get_string('yes'), $contact3->blocked);
228 }
229
230 /**
231 * Test for provider::export_user_data().
232 */
233 public function test_export_for_context_with_messages() {
234 global $DB;
235
236 $this->resetAfterTest();
237
238 // Create users to test with.
239 $user1 = $this->getDataGenerator()->create_user();
240 $user2 = $this->getDataGenerator()->create_user();
241 $user3 = $this->getDataGenerator()->create_user();
242
243 $now = time();
244
245 // Send messages from user 1 to user 2.
246 $m1 = $this->create_message($user1->id, $user2->id, $now - (9 * DAYSECS), true);
247 $m2 = $this->create_message($user2->id, $user1->id, $now - (8 * DAYSECS));
248 $m3 = $this->create_message($user1->id, $user2->id, $now - (7 * DAYSECS));
249
250 // Send messages from user 3 to user 1.
251 $m4 = $this->create_message($user3->id, $user1->id, $now - (6 * DAYSECS), true);
252 $m5 = $this->create_message($user1->id, $user3->id, $now - (5 * DAYSECS));
253 $m6 = $this->create_message($user3->id, $user1->id, $now - (4 * DAYSECS));
254
255 // Send messages from user 3 to user 2 - these should not be included in the export.
256 $m7 = $this->create_message($user3->id, $user2->id, $now - (3 * DAYSECS), true);
257 $m8 = $this->create_message($user2->id, $user3->id, $now - (2 * DAYSECS));
258 $m9 = $this->create_message($user3->id, $user2->id, $now - (1 * DAYSECS));
259
260 // Mark message 2 and 5 as deleted.
261 \core_message\api::delete_message($user1->id, $m2);
262 \core_message\api::delete_message($user1->id, $m5);
263
264 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
265
266 $writer = writer::with_context(\context_system::instance());
267
268 $this->assertTrue($writer->has_any_data());
269
270 // Confirm the messages with user 2 are correct.
271 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user2)]);
272 $this->assertCount(3, $messages);
273
274 $dbm1 = $DB->get_record('messages', ['id' => $m1]);
275 $dbm2 = $DB->get_record('messages', ['id' => $m2]);
276 $dbm3 = $DB->get_record('messages', ['id' => $m3]);
277
823e08bf 278 usort($messages, ['static', 'sort_messages']);
04cd1b3f
MN
279 $m1 = array_shift($messages);
280 $m2 = array_shift($messages);
281 $m3 = array_shift($messages);
282
283 $this->assertEquals(get_string('yes'), $m1->sender);
284 $this->assertEquals(message_format_message_text($dbm1), $m1->message);
285 $this->assertEquals(transform::datetime($now - (9 * DAYSECS)), $m1->timecreated);
286 $this->assertNotEquals('-', $m1->timeread);
287 $this->assertArrayNotHasKey('timedeleted', (array) $m1);
288
289 $this->assertEquals(get_string('no'), $m2->sender);
290 $this->assertEquals(message_format_message_text($dbm2), $m2->message);
291 $this->assertEquals(transform::datetime($now - (8 * DAYSECS)), $m2->timecreated);
292 $this->assertEquals('-', $m2->timeread);
293 $this->assertArrayHasKey('timedeleted', (array) $m2);
294
295 $this->assertEquals(get_string('yes'), $m3->sender);
296 $this->assertEquals(message_format_message_text($dbm3), $m3->message);
297 $this->assertEquals(transform::datetime($now - (7 * DAYSECS)), $m3->timecreated);
298 $this->assertEquals('-', $m3->timeread);
299
300 // Confirm the messages with user 3 are correct.
301 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user3)]);
302 $this->assertCount(3, $messages);
303
304 $dbm4 = $DB->get_record('messages', ['id' => $m4]);
305 $dbm5 = $DB->get_record('messages', ['id' => $m5]);
306 $dbm6 = $DB->get_record('messages', ['id' => $m6]);
307
823e08bf 308 usort($messages, ['static', 'sort_messages']);
04cd1b3f
MN
309 $m4 = array_shift($messages);
310 $m5 = array_shift($messages);
311 $m6 = array_shift($messages);
312
313 $this->assertEquals(get_string('no'), $m4->sender);
314 $this->assertEquals(message_format_message_text($dbm4), $m4->message);
315 $this->assertEquals(transform::datetime($now - (6 * DAYSECS)), $m4->timecreated);
316 $this->assertNotEquals('-', $m4->timeread);
317 $this->assertArrayNotHasKey('timedeleted', (array) $m4);
318
319 $this->assertEquals(get_string('yes'), $m5->sender);
320 $this->assertEquals(message_format_message_text($dbm5), $m5->message);
321 $this->assertEquals(transform::datetime($now - (5 * DAYSECS)), $m5->timecreated);
322 $this->assertEquals('-', $m5->timeread);
323 $this->assertArrayHasKey('timedeleted', (array) $m5);
324
325 $this->assertEquals(get_string('no'), $m6->sender);
326 $this->assertEquals(message_format_message_text($dbm6), $m6->message);
327 $this->assertEquals(transform::datetime($now - (4 * DAYSECS)), $m6->timecreated);
328 $this->assertEquals('-', $m6->timeread);
329 }
330
331 /**
332 * Test for provider::export_user_data().
333 */
334 public function test_export_for_context_with_notifications() {
335 $this->resetAfterTest();
336
337 // Create users to test with.
338 $user1 = $this->getDataGenerator()->create_user();
339 $user2 = $this->getDataGenerator()->create_user();
340 $user3 = $this->getDataGenerator()->create_user();
341
342 $now = time();
343 $timeread = $now - DAYSECS;
344
345 // Send notifications from user 1 to user 2.
346 $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
347 $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
348 $this->create_notification($user1->id, $user2->id, $now + (7 * DAYSECS));
349
350 // Send notifications from user 3 to user 1.
351 $this->create_notification($user3->id, $user1->id, $now + (6 * DAYSECS), $timeread);
352 $this->create_notification($user1->id, $user3->id, $now + (5 * DAYSECS));
353 $this->create_notification($user3->id, $user1->id, $now + (4 * DAYSECS));
354
355 // Send notifications from user 3 to user 2 - should not be part of the export.
356 $this->create_notification($user3->id, $user2->id, $now + (3 * DAYSECS), $timeread);
357 $this->create_notification($user2->id, $user3->id, $now + (2 * DAYSECS));
358 $this->create_notification($user3->id, $user2->id, $now + (1 * DAYSECS));
359
360 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
361
362 $writer = writer::with_context(\context_system::instance());
363
364 $this->assertTrue($writer->has_any_data());
365
366 // Confirm the notifications.
367 $notifications = (array) $writer->get_data([get_string('notifications', 'core_message')]);
368
369 $this->assertCount(6, $notifications);
370 }
371
372 /**
373 * Test for provider::delete_data_for_all_users_in_context().
374 */
375 public function test_delete_data_for_all_users_in_context() {
376 global $DB;
377
378 $this->resetAfterTest();
379
380 // Create users to test with.
381 $user1 = $this->getDataGenerator()->create_user();
382 $user2 = $this->getDataGenerator()->create_user();
383
384 $now = time();
385 $timeread = $now - DAYSECS;
386
387 $systemcontext = \context_system::instance();
388
389 // Create contacts.
390 message_add_contact($user1->id, 0, $user2->id);
391 message_add_contact($user2->id, 0, $user1->id);
392
393 // Create messages.
394 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
395 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
396
397 // Create notifications.
398 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
399 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
400
401 // Delete one of the messages.
402 \core_message\api::delete_message($user1->id, $m2);
403
404 // There should be 2 contacts.
405 $this->assertEquals(2, $DB->count_records('message_contacts'));
406
407 // There should be two messages.
408 $this->assertEquals(2, $DB->count_records('messages'));
409
410 // There should be two user actions - one for reading the message, one for deleting.
411 $this->assertEquals(2, $DB->count_records('message_user_actions'));
412
413 // There should be two conversation members.
414 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
415
416 // There should be two notifications.
417 $this->assertEquals(2, $DB->count_records('notifications'));
418
419 provider::delete_data_for_all_users_in_context($systemcontext);
420
421 // Confirm all has been deleted.
422 $this->assertEquals(0, $DB->count_records('message_contacts'));
423 $this->assertEquals(0, $DB->count_records('messages'));
424 $this->assertEquals(0, $DB->count_records('message_user_actions'));
425 $this->assertEquals(0, $DB->count_records('message_conversation_members'));
426 $this->assertEquals(0, $DB->count_records('notifications'));
427 }
428
429 /**
430 * Test for provider::delete_data_for_user().
431 */
432 public function test_delete_data_for_user() {
433 global $DB;
434
435 $this->resetAfterTest();
436
437 // Create users to test with.
438 $user1 = $this->getDataGenerator()->create_user();
439 $user2 = $this->getDataGenerator()->create_user();
440 $user3 = $this->getDataGenerator()->create_user();
441
442 $now = time();
443 $timeread = $now - DAYSECS;
444
445 // Create contacts.
446 message_add_contact($user1->id, 0, $user2->id);
447 message_add_contact($user2->id, 0, $user1->id);
448 message_add_contact($user2->id, 0, $user3->id);
449
450 // Create messages.
451 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
452 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
453
454 // Create notifications.
455 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
456 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
457 $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
458
459 // Delete one of the messages.
460 \core_message\api::delete_message($user1->id, $m2);
461
462 // There should be 3 contacts.
463 $this->assertEquals(3, $DB->count_records('message_contacts'));
464
465 // There should be two messages.
466 $this->assertEquals(2, $DB->count_records('messages'));
467
468 // There should be two user actions - one for reading the message, one for deleting.
469 $this->assertEquals(2, $DB->count_records('message_user_actions'));
470
471 // There should be two conversation members.
472 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
473
474 // There should be three notifications.
475 $this->assertEquals(3, $DB->count_records('notifications'));
476
477 $systemcontext = \context_system::instance();
478 $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_message',
479 [$systemcontext->id]);
480 provider::delete_data_for_user($contextlist);
481
482 // Confirm the user 2 data still exists.
483 $contacts = $DB->get_records('message_contacts');
484 $messages = $DB->get_records('messages');
485 $muas = $DB->get_records('message_user_actions');
486 $mcms = $DB->get_records('message_conversation_members');
487 $notifications = $DB->get_records('notifications');
488
489 $this->assertCount(1, $contacts);
490 $contact = reset($contacts);
491 $this->assertEquals($user3->id, $contact->userid);
492 $this->assertEquals($user2->id, $contact->contactid);
493
494 $this->assertCount(1, $messages);
495 $message = reset($messages);
496 $this->assertEquals($m2, $message->id);
497
498 $this->assertCount(1, $muas);
499 $mua = reset($muas);
500 $this->assertEquals($user2->id, $mua->userid);
501 $this->assertEquals($m1, $mua->messageid);
502 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua->action);
503
504 $this->assertCount(1, $mcms);
505 $mcm = reset($mcms);
506 $this->assertEquals($user2->id, $mcm->userid);
507
508 $this->assertCount(1, $notifications);
509 $notification = reset($notifications);
510 $this->assertEquals($user2->id, $notification->useridfrom);
511 $this->assertEquals($user3->id, $notification->useridto);
512 }
513
514 /**
515 * Creates a message to be used for testing.
516 *
517 * @param int $useridfrom The user id from
518 * @param int $useridto The user id to
519 * @param int $timecreated
520 * @param bool $read Do we want to mark the message as read?
521 * @return int The id of the message
522 * @throws dml_exception
523 */
524 private function create_message(int $useridfrom, int $useridto, int $timecreated = null, bool $read = false) {
525 global $DB;
526
527 static $i = 1;
528
529 if (is_null($timecreated)) {
530 $timecreated = time();
531 }
532
533 if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
534 $conversationid = \core_message\api::create_conversation_between_users([$useridfrom,
535 $useridto]);
536 }
537
538 // Ok, send the message.
539 $record = new stdClass();
540 $record->useridfrom = $useridfrom;
541 $record->conversationid = $conversationid;
542 $record->subject = 'No subject';
543 $record->fullmessage = 'A rad message ' . $i;
544 $record->smallmessage = 'A rad message ' . $i;
545 $record->timecreated = $timecreated;
546
547 $i++;
548
549 $record->id = $DB->insert_record('messages', $record);
550
551 if ($read) {
552 \core_message\api::mark_message_as_read($useridto, $record);
553 }
554
555 return $record->id;
556 }
557
558 /**
559 * Creates a notification to be used for testing.
560 *
561 * @param int $useridfrom The user id from
562 * @param int $useridto The user id to
563 * @param int|null $timecreated The time the notification was created
564 * @param int|null $timeread The time the notification was read, null if it hasn't been.
565 * @return int The id of the notification
566 * @throws dml_exception
567 */
568 private function create_notification(int $useridfrom, int $useridto, int $timecreated = null, int $timeread = null) {
569 global $DB;
570
571 static $i = 1;
572
573 if (is_null($timecreated)) {
574 $timecreated = time();
575 }
576
577 $record = new stdClass();
578 $record->useridfrom = $useridfrom;
579 $record->useridto = $useridto;
580 $record->subject = 'No subject';
581 $record->fullmessage = 'Some rad notification ' . $i;
582 $record->smallmessage = 'Yo homie, you got some stuff to do, yolo. ' . $i;
583 $record->timeread = $timeread;
584 $record->timecreated = $timecreated;
585
586 $i++;
587
588 return $DB->insert_record('notifications', $record);
589 }
823e08bf
AN
590
591 /**
592 * Comparison function for sorting messages.
593 *
594 * @param \stdClass $a
595 * @param \stdClass $b
596 * @return bool
597 */
598 protected static function sort_messages($a, $b) {
599 return $a->message > $b->message;
600 }
601
602 /**
603 * Comparison function for sorting contacts.
604 *
605 * @param \stdClass $a
606 * @param \stdClass $b
607 * @return bool
608 */
609 protected static function sort_contacts($a, $b) {
610 return $a->contact > $b->contact;
611 }
04cd1b3f 612}