forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externallib.php
3611 lines (3189 loc) · 138 KB
/
externallib.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* External message API
*
* @package core_message
* @category external
* @copyright 2011 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/externallib.php");
require_once($CFG->dirroot . "/message/lib.php");
/**
* Message external functions
*
* @package core_message
* @category external
* @copyright 2011 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.2
*/
class core_message_external extends external_api {
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.6
*/
public static function send_messages_to_conversation_parameters() {
return new external_function_parameters(
array(
'conversationid' => new external_value(PARAM_INT, 'id of the conversation'),
'messages' => new external_multiple_structure(
new external_single_structure(
array(
'text' => new external_value(PARAM_RAW, 'the text of the message'),
'textformat' => new external_format_value('text', VALUE_DEFAULT, FORMAT_MOODLE),
)
)
)
)
);
}
/**
* Send messages from the current USER to a conversation.
*
* This conversation may be any type of conversation, individual or group.
*
* @param int $conversationid the id of the conversation to which the messages will be sent.
* @param array $messages An array of message to send.
* @return array the array of messages which were sent (created).
* @since Moodle 3.6
*/
public static function send_messages_to_conversation(int $conversationid, array $messages = []) {
global $CFG, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Ensure the current user is allowed to run this function.
$context = context_system::instance();
self::validate_context($context);
$params = self::validate_parameters(self::send_messages_to_conversation_parameters(), [
'conversationid' => $conversationid,
'messages' => $messages
]);
$messages = [];
foreach ($params['messages'] as $message) {
$createdmessage = \core_message\api::send_message_to_conversation($USER->id, $params['conversationid'], $message['text'],
$message['textformat']);
$createdmessage->text = message_format_message_text((object) [
'smallmessage' => $createdmessage->text,
'fullmessageformat' => external_validate_format($message['textformat']),
'fullmessagetrust' => $createdmessage->fullmessagetrust
]);
$messages[] = $createdmessage;
}
return $messages;
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 3.6
*/
public static function send_messages_to_conversation_returns() {
return new external_multiple_structure(
self::get_conversation_message_structure()
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.2
*/
public static function send_instant_messages_parameters() {
return new external_function_parameters(
array(
'messages' => new external_multiple_structure(
new external_single_structure(
array(
'touserid' => new external_value(PARAM_INT, 'id of the user to send the private message'),
'text' => new external_value(PARAM_RAW, 'the text of the message'),
'textformat' => new external_format_value('text', VALUE_DEFAULT, FORMAT_MOODLE),
'clientmsgid' => new external_value(PARAM_ALPHANUMEXT, 'your own client id for the message. If this id is provided, the fail message id will be returned to you', VALUE_OPTIONAL),
)
)
)
)
);
}
/**
* Send private messages from the current USER to other users
*
* @param array $messages An array of message to send.
* @return array
* @since Moodle 2.2
*/
public static function send_instant_messages($messages = array()) {
global $CFG, $USER, $DB;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Ensure the current user is allowed to run this function
$context = context_system::instance();
self::validate_context($context);
require_capability('moodle/site:sendmessage', $context);
// Ensure the current user is allowed to delete message for everyone.
$candeletemessagesforallusers = has_capability('moodle/site:deleteanymessage', $context);
$params = self::validate_parameters(self::send_instant_messages_parameters(), array('messages' => $messages));
//retrieve all tousers of the messages
$receivers = array();
foreach($params['messages'] as $message) {
$receivers[] = $message['touserid'];
}
list($sqluserids, $sqlparams) = $DB->get_in_or_equal($receivers);
$tousers = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
$resultmessages = array();
$messageids = array();
foreach ($params['messages'] as $message) {
$resultmsg = array(); //the infos about the success of the operation
// We are going to do some checking.
// Code should match /messages/index.php checks.
$success = true;
// Check the user exists.
if (empty($tousers[$message['touserid']])) {
$success = false;
$errormessage = get_string('touserdoesntexist', 'message', $message['touserid']);
}
// TODO MDL-31118 performance improvement - edit the function so we can pass an array instead userid
// Check if the recipient can be messaged by the sender.
if ($success && !\core_message\api::can_send_message($tousers[$message['touserid']]->id, $USER->id)) {
$success = false;
$errormessage = get_string('usercantbemessaged', 'message', fullname(\core_user::get_user($message['touserid'])));
}
// Now we can send the message (at least try).
if ($success) {
// TODO MDL-31118 performance improvement - edit the function so we can pass an array instead one touser object.
$success = message_post_message($USER, $tousers[$message['touserid']],
$message['text'], external_validate_format($message['textformat']));
}
// Build the resultmsg.
if (isset($message['clientmsgid'])) {
$resultmsg['clientmsgid'] = $message['clientmsgid'];
}
if ($success) {
$resultmsg['msgid'] = $success;
$resultmsg['timecreated'] = time();
$resultmsg['candeletemessagesforallusers'] = $candeletemessagesforallusers;
$messageids[] = $success;
} else {
// WARNINGS: for backward compatibility we return this errormessage.
// We should have thrown exceptions as these errors prevent results to be returned.
// See http://docs.moodle.org/dev/Errors_handling_in_web_services#When_to_send_a_warning_on_the_server_side .
$resultmsg['msgid'] = -1;
if (!isset($errormessage)) { // Nobody has set a message error or thrown an exception, let's set it.
$errormessage = get_string('messageundeliveredbynotificationsettings', 'error');
}
$resultmsg['errormessage'] = $errormessage;
}
$resultmessages[] = $resultmsg;
}
if (!empty($messageids)) {
$messagerecords = $DB->get_records_list(
'messages',
'id',
$messageids,
'',
'id, conversationid, smallmessage, fullmessageformat, fullmessagetrust');
$resultmessages = array_map(function($resultmessage) use ($messagerecords, $USER) {
$id = $resultmessage['msgid'];
$resultmessage['conversationid'] = isset($messagerecords[$id]) ? $messagerecords[$id]->conversationid : null;
$resultmessage['useridfrom'] = $USER->id;
$resultmessage['text'] = message_format_message_text((object) [
'smallmessage' => $messagerecords[$id]->smallmessage,
'fullmessageformat' => external_validate_format($messagerecords[$id]->fullmessageformat),
'fullmessagetrust' => $messagerecords[$id]->fullmessagetrust
]);
return $resultmessage;
}, $resultmessages);
}
return $resultmessages;
}
/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 2.2
*/
public static function send_instant_messages_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'msgid' => new external_value(PARAM_INT, 'test this to know if it succeeds: id of the created message if it succeeded, -1 when failed'),
'clientmsgid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the message', VALUE_OPTIONAL),
'errormessage' => new external_value(PARAM_TEXT, 'error message - if it failed', VALUE_OPTIONAL),
'text' => new external_value(PARAM_RAW, 'The text of the message', VALUE_OPTIONAL),
'timecreated' => new external_value(PARAM_INT, 'The timecreated timestamp for the message', VALUE_OPTIONAL),
'conversationid' => new external_value(PARAM_INT, 'The conversation id for this message', VALUE_OPTIONAL),
'useridfrom' => new external_value(PARAM_INT, 'The user id who sent the message', VALUE_OPTIONAL),
'candeletemessagesforallusers' => new external_value(PARAM_BOOL,
'If the user can delete messages in the conversation for all users', VALUE_DEFAULT, false),
)
)
);
}
/**
* Delete contacts parameters description.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function delete_contacts_parameters() {
return new external_function_parameters(
array(
'userids' => new external_multiple_structure(
new external_value(PARAM_INT, 'User ID'),
'List of user IDs'
),
'userid' => new external_value(PARAM_INT, 'The id of the user we are deleting the contacts for, 0 for the
current user', VALUE_DEFAULT, 0)
)
);
}
/**
* Delete contacts.
*
* @param array $userids array of user IDs.
* @param int $userid The id of the user we are deleting the contacts for
* @return null
* @since Moodle 2.5
*/
public static function delete_contacts($userids, $userid = 0) {
global $CFG, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
if (empty($userid)) {
$userid = $USER->id;
}
// Validate context.
$context = context_system::instance();
self::validate_context($context);
$params = array('userids' => $userids, 'userid' => $userid);
$params = self::validate_parameters(self::delete_contacts_parameters(), $params);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['userid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
foreach ($params['userids'] as $id) {
\core_message\api::remove_contact($params['userid'], $id);
}
return null;
}
/**
* Delete contacts return description.
*
* @return external_description
* @since Moodle 2.5
*/
public static function delete_contacts_returns() {
return null;
}
/**
* Mute conversations parameters description.
*
* @return external_function_parameters
*/
public static function mute_conversations_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'The id of the user who is blocking'),
'conversationids' => new external_multiple_structure(
new external_value(PARAM_INT, 'id of the conversation', VALUE_REQUIRED)
),
]
);
}
/**
* Mutes conversations.
*
* @param int $userid The id of the user who is blocking
* @param array $conversationids The list of conversations being muted
* @return external_description
*/
public static function mute_conversations(int $userid, array $conversationids) {
global $CFG, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Validate context.
$context = context_system::instance();
self::validate_context($context);
$params = ['userid' => $userid, 'conversationids' => $conversationids];
$params = self::validate_parameters(self::mute_conversations_parameters(), $params);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['userid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
foreach ($params['conversationids'] as $conversationid) {
if (!\core_message\api::is_conversation_muted($params['userid'], $conversationid)) {
\core_message\api::mute_conversation($params['userid'], $conversationid);
}
}
return [];
}
/**
* Mute conversations return description.
*
* @return external_description
*/
public static function mute_conversations_returns() {
return new external_warnings();
}
/**
* Unmute conversations parameters description.
*
* @return external_function_parameters
*/
public static function unmute_conversations_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'The id of the user who is unblocking'),
'conversationids' => new external_multiple_structure(
new external_value(PARAM_INT, 'id of the conversation', VALUE_REQUIRED)
),
]
);
}
/**
* Unmute conversations.
*
* @param int $userid The id of the user who is unblocking
* @param array $conversationids The list of conversations being muted
*/
public static function unmute_conversations(int $userid, array $conversationids) {
global $CFG, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Validate context.
$context = context_system::instance();
self::validate_context($context);
$params = ['userid' => $userid, 'conversationids' => $conversationids];
$params = self::validate_parameters(self::unmute_conversations_parameters(), $params);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['userid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
foreach ($params['conversationids'] as $conversationid) {
\core_message\api::unmute_conversation($params['userid'], $conversationid);
}
return [];
}
/**
* Unmute conversations return description.
*
* @return external_description
*/
public static function unmute_conversations_returns() {
return new external_warnings();
}
/**
* Block user parameters description.
*
* @return external_function_parameters
*/
public static function block_user_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'The id of the user who is blocking'),
'blockeduserid' => new external_value(PARAM_INT, 'The id of the user being blocked'),
]
);
}
/**
* Blocks a user.
*
* @param int $userid The id of the user who is blocking
* @param int $blockeduserid The id of the user being blocked
* @return external_description
*/
public static function block_user(int $userid, int $blockeduserid) {
global $CFG, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Validate context.
$context = context_system::instance();
self::validate_context($context);
$params = ['userid' => $userid, 'blockeduserid' => $blockeduserid];
$params = self::validate_parameters(self::block_user_parameters(), $params);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['userid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
// If the blocking is going to be useless then don't do it.
if (\core_message\api::can_send_message($userid, $blockeduserid, true)) {
return [];
}
if (!\core_message\api::is_blocked($params['userid'], $params['blockeduserid'])) {
\core_message\api::block_user($params['userid'], $params['blockeduserid']);
}
return [];
}
/**
* Block user return description.
*
* @return external_description
*/
public static function block_user_returns() {
return new external_warnings();
}
/**
* Unblock user parameters description.
*
* @return external_function_parameters
*/
public static function unblock_user_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'The id of the user who is unblocking'),
'unblockeduserid' => new external_value(PARAM_INT, 'The id of the user being unblocked'),
]
);
}
/**
* Unblock user.
*
* @param int $userid The id of the user who is unblocking
* @param int $unblockeduserid The id of the user being unblocked
*/
public static function unblock_user(int $userid, int $unblockeduserid) {
global $CFG, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Validate context.
$context = context_system::instance();
self::validate_context($context);
$params = ['userid' => $userid, 'unblockeduserid' => $unblockeduserid];
$params = self::validate_parameters(self::unblock_user_parameters(), $params);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['userid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
\core_message\api::unblock_user($params['userid'], $params['unblockeduserid']);
return [];
}
/**
* Unblock user return description.
*
* @return external_description
*/
public static function unblock_user_returns() {
return new external_warnings();
}
/**
* Returns contact requests parameters description.
*
* @return external_function_parameters
*/
public static function get_contact_requests_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'The id of the user we want the requests for'),
'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0),
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0)
]
);
}
/**
* Handles returning the contact requests for a user.
*
* This also includes the user data necessary to display information
* about the user.
*
* It will not include blocked users.
*
* @param int $userid The id of the user we want to get the contact requests for
* @param int $limitfrom
* @param int $limitnum
*/
public static function get_contact_requests(int $userid, int $limitfrom = 0, int $limitnum = 0) {
global $CFG, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Validate context.
$context = context_system::instance();
self::validate_context($context);
$params = [
'userid' => $userid,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum
];
$params = self::validate_parameters(self::get_contact_requests_parameters(), $params);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['userid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
return \core_message\api::get_contact_requests($params['userid'], $params['limitfrom'], $params['limitnum']);
}
/**
* Returns the contact requests return description.
*
* @return external_description
*/
public static function get_contact_requests_returns() {
return new external_multiple_structure(
self::get_conversation_member_structure()
);
}
/**
* Returns the number of contact requests the user has received parameters description.
*
* @return external_function_parameters
*/
public static function get_received_contact_requests_count_parameters() {
return new external_function_parameters(
array(
'userid' => new external_value(PARAM_INT, 'The id of the user we want to return the number of ' .
'received contact requests for', VALUE_REQUIRED),
)
);
}
/**
* Returns the number of contact requests the user has received.
*
* @param int $userid The ID of the user we want to return the number of received contact requests for
* @return external_value
*/
public static function get_received_contact_requests_count(int $userid) {
global $CFG, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Validate context.
$context = context_system::instance();
self::validate_context($context);
$params = [
'userid' => $userid,
];
$params = self::validate_parameters(self::get_received_contact_requests_count_parameters(), $params);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['userid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
return \core_message\api::get_received_contact_requests_count($params['userid']);
}
/**
* Returns the number of contact requests the user has received return description.
*
* @return external_value
*/
public static function get_received_contact_requests_count_returns() {
return new external_value(PARAM_INT, 'The number of received contact requests');
}
/**
* Returns get conversation members parameters description.
*
* @return external_function_parameters
*/
public static function get_conversation_members_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'The id of the user we are performing this action on behalf of'),
'conversationid' => new external_value(PARAM_INT, 'The id of the conversation'),
'includecontactrequests' => new external_value(PARAM_BOOL, 'Do we want to include contact requests?',
VALUE_DEFAULT, false),
'includeprivacyinfo' => new external_value(PARAM_BOOL, 'Do we want to include privacy info?',
VALUE_DEFAULT, false),
'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0),
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0)
]
);
}
/**
* Returns a list of conversation members.
*
* @param int $userid The user we are returning the conversation members for, used by helper::get_member_info.
* @param int $conversationid The id of the conversation
* @param bool $includecontactrequests Do we want to include contact requests with this data?
* @param bool $includeprivacyinfo Do we want to include privacy info?
* @param int $limitfrom
* @param int $limitnum
* @return array
*/
public static function get_conversation_members(int $userid, int $conversationid, bool $includecontactrequests = false,
bool $includeprivacyinfo = false, int $limitfrom = 0, int $limitnum = 0) {
global $CFG, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Validate context.
$context = context_system::instance();
self::validate_context($context);
$params = [
'userid' => $userid,
'conversationid' => $conversationid,
'includecontactrequests' => $includecontactrequests,
'includeprivacyinfo' => $includeprivacyinfo,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum
];
$params = self::validate_parameters(self::get_conversation_members_parameters(), $params);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['userid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
// The user needs to be a part of the conversation before querying who the members are.
if (!\core_message\api::is_user_in_conversation($params['userid'], $params['conversationid'])) {
throw new moodle_exception('You are not a member of this conversation.');
}
return \core_message\api::get_conversation_members($params['userid'], $params['conversationid'], $params['includecontactrequests'],
$params['includeprivacyinfo'], $params['limitfrom'], $params['limitnum']);
}
/**
* Returns the get conversation members return description.
*
* @return external_description
*/
public static function get_conversation_members_returns() {
return new external_multiple_structure(
self::get_conversation_member_structure()
);
}
/**
* Creates a contact request parameters description.
*
* @return external_function_parameters
*/
public static function create_contact_request_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'The id of the user making the request'),
'requesteduserid' => new external_value(PARAM_INT, 'The id of the user being requested')
]
);
}
/**
* Creates a contact request.
*
* @param int $userid The id of the user who is creating the contact request
* @param int $requesteduserid The id of the user being requested
*/
public static function create_contact_request(int $userid, int $requesteduserid) {
global $CFG, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Validate context.
$context = context_system::instance();
self::validate_context($context);
$params = ['userid' => $userid, 'requesteduserid' => $requesteduserid];
$params = self::validate_parameters(self::create_contact_request_parameters(), $params);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['userid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
$result = [
'warnings' => []
];
if (!\core_message\api::can_create_contact($params['userid'], $params['requesteduserid'])) {
$result['warnings'][] = [
'item' => 'user',
'itemid' => $params['requesteduserid'],
'warningcode' => 'cannotcreatecontactrequest',
'message' => 'You are unable to create a contact request for this user'
];
} else {
if ($requests = \core_message\api::get_contact_requests_between_users($params['userid'], $params['requesteduserid'])) {
// There should only ever be one but just in case there are multiple then we can return the first.
$result['request'] = array_shift($requests);
} else {
$result['request'] = \core_message\api::create_contact_request($params['userid'], $params['requesteduserid']);
}
}
return $result;
}
/**
* Creates a contact request return description.
*
* @return external_description
*/
public static function create_contact_request_returns() {
return new external_single_structure(
array(
'request' => new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Message id'),
'userid' => new external_value(PARAM_INT, 'User from id'),
'requesteduserid' => new external_value(PARAM_INT, 'User to id'),
'timecreated' => new external_value(PARAM_INT, 'Time created'),
),
'request record',
VALUE_OPTIONAL
),
'warnings' => new external_warnings()
)
);
}
/**
* Confirm a contact request parameters description.
*
* @return external_function_parameters
*/
public static function confirm_contact_request_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'The id of the user making the request'),
'requesteduserid' => new external_value(PARAM_INT, 'The id of the user being requested')
]
);
}
/**
* Confirm a contact request.
*
* @param int $userid The id of the user who is creating the contact request
* @param int $requesteduserid The id of the user being requested
*/
public static function confirm_contact_request(int $userid, int $requesteduserid) {
global $CFG, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Validate context.
$context = context_system::instance();
self::validate_context($context);
$params = ['userid' => $userid, 'requesteduserid' => $requesteduserid];
$params = self::validate_parameters(self::confirm_contact_request_parameters(), $params);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['requesteduserid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
\core_message\api::confirm_contact_request($params['userid'], $params['requesteduserid']);
return [];
}
/**
* Confirm a contact request return description.
*
* @return external_description
*/
public static function confirm_contact_request_returns() {
return new external_warnings();
}
/**
* Declines a contact request parameters description.
*
* @return external_function_parameters
*/
public static function decline_contact_request_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'The id of the user making the request'),
'requesteduserid' => new external_value(PARAM_INT, 'The id of the user being requested')
]
);
}
/**
* Declines a contact request.
*
* @param int $userid The id of the user who is creating the contact request
* @param int $requesteduserid The id of the user being requested
*/
public static function decline_contact_request(int $userid, int $requesteduserid) {
global $CFG, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Validate context.
$context = context_system::instance();
self::validate_context($context);
$params = ['userid' => $userid, 'requesteduserid' => $requesteduserid];
$params = self::validate_parameters(self::decline_contact_request_parameters(), $params);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['requesteduserid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
\core_message\api::decline_contact_request($params['userid'], $params['requesteduserid']);
return [];
}
/**
* Declines a contact request return description.
*
* @return external_description
*/
public static function decline_contact_request_returns() {
return new external_warnings();
}
/**
* Return the structure of a message area contact.
*
* @return external_single_structure
* @since Moodle 3.2
*/
private static function get_messagearea_contact_structure() {
return new external_single_structure(
array(
'userid' => new external_value(PARAM_INT, 'The user\'s id'),
'fullname' => new external_value(PARAM_NOTAGS, 'The user\'s name'),
'profileimageurl' => new external_value(PARAM_URL, 'User picture URL'),
'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL'),
'ismessaging' => new external_value(PARAM_BOOL, 'If we are messaging the user'),
'sentfromcurrentuser' => new external_value(PARAM_BOOL, 'Was the last message sent from the current user?'),
'lastmessage' => new external_value(PARAM_NOTAGS, 'The user\'s last message'),
'lastmessagedate' => new external_value(PARAM_INT, 'Timestamp for last message', VALUE_DEFAULT, null),
'messageid' => new external_value(PARAM_INT, 'The unique search message id', VALUE_DEFAULT, null),
'showonlinestatus' => new external_value(PARAM_BOOL, 'Show the user\'s online status?'),
'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'),
'isread' => new external_value(PARAM_BOOL, 'If the user has read the message'),
'isblocked' => new external_value(PARAM_BOOL, 'If the user has been blocked'),
'unreadcount' => new external_value(PARAM_INT, 'The number of unread messages in this conversation',
VALUE_DEFAULT, null),
'conversationid' => new external_value(PARAM_INT, 'The id of the conversation', VALUE_DEFAULT, null),
)
);
}
/**
* Return the structure of a conversation.
*