forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externallib.php
2689 lines (2362 loc) · 99.7 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 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);
$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, SQL_PARAMS_NAMED, 'userid_');
$tousers = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
$blocklist = array();
$contactlist = array();
$sqlparams['contactid'] = $USER->id;
$rs = $DB->get_recordset_sql("SELECT *
FROM {message_contacts}
WHERE userid $sqluserids
AND contactid = :contactid", $sqlparams);
foreach ($rs as $record) {
if ($record->blocked) {
// $record->userid is blocking current user
$blocklist[$record->userid] = true;
} else {
// $record->userid have current user as contact
$contactlist[$record->userid] = true;
}
}
$rs->close();
$canreadallmessages = has_capability('moodle/site:readallmessages', $context);
$resultmessages = 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']);
}
//check that the touser is not blocking the current user
if ($success and !empty($blocklist[$message['touserid']]) and !$canreadallmessages) {
$success = false;
$errormessage = get_string('userisblockingyou', 'message');
}
// Check if the user is a contact
//TODO MDL-31118 performance improvement - edit the function so we can pass an array instead userid
$blocknoncontacts = get_user_preferences('message_blocknoncontacts', NULL, $message['touserid']);
// message_blocknoncontacts option is on and current user is not in contact list
if ($success && empty($contactlist[$message['touserid']]) && !empty($blocknoncontacts)) {
// The user isn't a contact and they have selected to block non contacts so this message won't be sent.
$success = false;
$errormessage = get_string('userisblockingyounoncontact', '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;
} 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;
$resultmsg['errormessage'] = $errormessage;
}
$resultmessages[] = $resultmsg;
}
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)
)
)
);
}
/**
* Create contacts parameters description.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function create_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 creating the contacts for, 0 for the
current user', VALUE_DEFAULT, 0)
)
);
}
/**
* Create contacts.
*
* @param array $userids array of user IDs.
* @param int $userid The id of the user we are creating the contacts for
* @return external_description
* @since Moodle 2.5
*/
public static function create_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);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $userid) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
$params = array('userids' => $userids, 'userid' => $userid);
$params = self::validate_parameters(self::create_contacts_parameters(), $params);
$warnings = array();
foreach ($params['userids'] as $id) {
if (!message_add_contact($id, 0, $userid)) {
$warnings[] = array(
'item' => 'user',
'itemid' => $id,
'warningcode' => 'contactnotcreated',
'message' => 'The contact could not be created'
);
}
}
return $warnings;
}
/**
* Create contacts return description.
*
* @return external_description
* @since Moodle 2.5
*/
public static function create_contacts_returns() {
return new external_warnings();
}
/**
* 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);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $userid) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
$params = array('userids' => $userids, 'userid' => $userid);
$params = self::validate_parameters(self::delete_contacts_parameters(), $params);
foreach ($params['userids'] as $id) {
message_remove_contact($id, $userid);
}
return null;
}
/**
* Delete contacts return description.
*
* @return external_description
* @since Moodle 2.5
*/
public static function delete_contacts_returns() {
return null;
}
/**
* Block contacts parameters description.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function block_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 blocking the contacts for, 0 for the
current user', VALUE_DEFAULT, 0)
)
);
}
/**
* Block contacts.
*
* @param array $userids array of user IDs.
* @param int $userid The id of the user we are blocking the contacts for
* @return external_description
* @since Moodle 2.5
*/
public static function block_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);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $userid) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
$params = array('userids' => $userids, 'userid' => $userid);
$params = self::validate_parameters(self::block_contacts_parameters(), $params);
$warnings = array();
foreach ($params['userids'] as $id) {
if (!message_block_contact($id, $userid)) {
$warnings[] = array(
'item' => 'user',
'itemid' => $id,
'warningcode' => 'contactnotblocked',
'message' => 'The contact could not be blocked'
);
}
}
return $warnings;
}
/**
* Block contacts return description.
*
* @return external_description
* @since Moodle 2.5
*/
public static function block_contacts_returns() {
return new external_warnings();
}
/**
* Unblock contacts parameters description.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function unblock_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 unblocking the contacts for, 0 for the
current user', VALUE_DEFAULT, 0)
)
);
}
/**
* Unblock contacts.
*
* @param array $userids array of user IDs.
* @param int $userid The id of the user we are unblocking the contacts for
* @return null
* @since Moodle 2.5
*/
public static function unblock_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);
$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $userid) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
$params = array('userids' => $userids, 'userid' => $userid);
$params = self::validate_parameters(self::unblock_contacts_parameters(), $params);
foreach ($params['userids'] as $id) {
message_unblock_contact($id, $userid);
}
return null;
}
/**
* Unblock contacts return description.
*
* @return external_description
* @since Moodle 2.5
*/
public static function unblock_contacts_returns() {
return null;
}
/**
* 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'),
'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),
)
);
}
/**
* Return the structure of a message area message.
*
* @return external_single_structure
* @since Moodle 3.2
*/
private static function get_messagearea_message_structure() {
return new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'The id of the message'),
'useridfrom' => new external_value(PARAM_INT, 'The id of the user who sent the message'),
'useridto' => new external_value(PARAM_INT, 'The id of the user who received the message'),
'text' => new external_value(PARAM_RAW, 'The text of the message'),
'displayblocktime' => new external_value(PARAM_BOOL, 'Should we display the block time?'),
'blocktime' => new external_value(PARAM_NOTAGS, 'The time to display above the message'),
'position' => new external_value(PARAM_ALPHA, 'The position of the text'),
'timesent' => new external_value(PARAM_NOTAGS, 'The time the message was sent'),
'timecreated' => new external_value(PARAM_INT, 'The timecreated timestamp for the message'),
'isread' => new external_value(PARAM_INT, 'Determines if the message was read or not'),
)
);
}
/**
* Get messagearea search users in course parameters.
*
* @return external_function_parameters
* @since 3.2
*/
public static function data_for_messagearea_search_users_in_course_parameters() {
return new external_function_parameters(
array(
'userid' => new external_value(PARAM_INT, 'The id of the user who is performing the search'),
'courseid' => new external_value(PARAM_INT, 'The id of the course'),
'search' => new external_value(PARAM_RAW, 'The string being searched'),
'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0),
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0)
)
);
}
/**
* Get messagearea search users in course results.
*
* @param int $userid The id of the user who is performing the search
* @param int $courseid The id of the course
* @param string $search The string being searched
* @param int $limitfrom
* @param int $limitnum
* @return stdClass
* @throws moodle_exception
* @since 3.2
*/
public static function data_for_messagearea_search_users_in_course($userid, $courseid, $search, $limitfrom = 0,
$limitnum = 0) {
global $CFG, $PAGE, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
$systemcontext = context_system::instance();
$params = array(
'userid' => $userid,
'courseid' => $courseid,
'search' => $search,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum
);
self::validate_parameters(self::data_for_messagearea_search_users_in_course_parameters(), $params);
self::validate_context($systemcontext);
if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
throw new moodle_exception('You do not have permission to perform this action.');
}
$users = \core_message\api::search_users_in_course($userid, $courseid, $search, $limitfrom, $limitnum);
$results = new \core_message\output\messagearea\user_search_results($users);
$renderer = $PAGE->get_renderer('core_message');
return $results->export_for_template($renderer);
}
/**
* Get messagearea search users in course returns.
*
* @return external_single_structure
* @since 3.2
*/
public static function data_for_messagearea_search_users_in_course_returns() {
return new external_single_structure(
array(
'contacts' => new external_multiple_structure(
self::get_messagearea_contact_structure()
),
)
);
}
/**
* Get messagearea search users parameters.
*
* @return external_function_parameters
* @since 3.2
*/
public static function data_for_messagearea_search_users_parameters() {
return new external_function_parameters(
array(
'userid' => new external_value(PARAM_INT, 'The id of the user who is performing the search'),
'search' => new external_value(PARAM_RAW, 'The string being searched'),
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0)
)
);
}
/**
* Get messagearea search users results.
*
* @param int $userid The id of the user who is performing the search
* @param string $search The string being searched
* @param int $limitnum
* @return stdClass
* @throws moodle_exception
* @since 3.2
*/
public static function data_for_messagearea_search_users($userid, $search, $limitnum = 0) {
global $CFG, $PAGE, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
$systemcontext = context_system::instance();
$params = array(
'userid' => $userid,
'search' => $search,
'limitnum' => $limitnum
);
self::validate_parameters(self::data_for_messagearea_search_users_parameters(), $params);
self::validate_context($systemcontext);
if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
throw new moodle_exception('You do not have permission to perform this action.');
}
list($contacts, $courses, $noncontacts) = \core_message\api::search_users($userid, $search, $limitnum);
$search = new \core_message\output\messagearea\user_search_results($contacts, $courses, $noncontacts);
$renderer = $PAGE->get_renderer('core_message');
return $search->export_for_template($renderer);
}
/**
* Get messagearea search users returns.
*
* @return external_single_structure
* @since 3.2
*/
public static function data_for_messagearea_search_users_returns() {
return new external_single_structure(
array(
'contacts' => new external_multiple_structure(
self::get_messagearea_contact_structure()
),
'courses' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'The course id'),
'shortname' => new external_value(PARAM_TEXT, 'The course shortname'),
'fullname' => new external_value(PARAM_TEXT, 'The course fullname'),
)
)
),
'noncontacts' => new external_multiple_structure(
self::get_messagearea_contact_structure()
)
)
);
}
/**
* Get messagearea search messages parameters.
*
* @return external_function_parameters
* @since 3.2
*/
public static function data_for_messagearea_search_messages_parameters() {
return new external_function_parameters(
array(
'userid' => new external_value(PARAM_INT, 'The id of the user who is performing the search'),
'search' => new external_value(PARAM_RAW, 'The string being searched'),
'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0),
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0)
)
);
}
/**
* Get messagearea search messages results.
*
* @param int $userid The id of the user who is performing the search
* @param string $search The string being searched
* @param int $limitfrom
* @param int $limitnum
* @return stdClass
* @throws moodle_exception
* @since 3.2
*/
public static function data_for_messagearea_search_messages($userid, $search, $limitfrom = 0, $limitnum = 0) {
global $CFG, $PAGE, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
$systemcontext = context_system::instance();
$params = array(
'userid' => $userid,
'search' => $search,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum
);
self::validate_parameters(self::data_for_messagearea_search_messages_parameters(), $params);
self::validate_context($systemcontext);
if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
throw new moodle_exception('You do not have permission to perform this action.');
}
$messages = \core_message\api::search_messages($userid, $search, $limitfrom, $limitnum);
$results = new \core_message\output\messagearea\message_search_results($messages);
$renderer = $PAGE->get_renderer('core_message');
return $results->export_for_template($renderer);
}
/**
* Get messagearea search messages returns.
*
* @return external_single_structure
* @since 3.2
*/
public static function data_for_messagearea_search_messages_returns() {
return new external_single_structure(
array(
'contacts' => new external_multiple_structure(
self::get_messagearea_contact_structure()
)
)
);
}
/**
* The messagearea conversations parameters.
*
* @return external_function_parameters
* @since 3.2
*/
public static function data_for_messagearea_conversations_parameters() {
return new external_function_parameters(
array(
'userid' => new external_value(PARAM_INT, 'The id of the user who we are viewing conversations for'),
'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0),
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0)
)
);
}
/**
* Get messagearea conversations.
*
* @param int $userid The id of the user who we are viewing conversations for
* @param int $limitfrom
* @param int $limitnum
* @return stdClass
* @throws moodle_exception
* @since 3.2
*/
public static function data_for_messagearea_conversations($userid, $limitfrom = 0, $limitnum = 0) {
global $CFG, $PAGE, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
$systemcontext = context_system::instance();
$params = array(
'userid' => $userid,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum
);
self::validate_parameters(self::data_for_messagearea_conversations_parameters(), $params);
self::validate_context($systemcontext);
if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
throw new moodle_exception('You do not have permission to perform this action.');
}
$conversations = \core_message\api::get_conversations($userid, $limitfrom, $limitnum);
$conversations = new \core_message\output\messagearea\contacts(null, $conversations);
$renderer = $PAGE->get_renderer('core_message');
return $conversations->export_for_template($renderer);
}
/**
* The messagearea conversations return structure.
*
* @return external_single_structure
* @since 3.2
*/
public static function data_for_messagearea_conversations_returns() {
return new external_single_structure(
array(
'contacts' => new external_multiple_structure(
self::get_messagearea_contact_structure()
)
)
);
}
/**
* The messagearea contacts return parameters.
*
* @return external_function_parameters
* @since 3.2
*/
public static function data_for_messagearea_contacts_parameters() {
return self::data_for_messagearea_conversations_parameters();
}
/**
* Get messagearea contacts parameters.
*
* @param int $userid The id of the user who we are viewing conversations for
* @param int $limitfrom
* @param int $limitnum
* @return stdClass
* @throws moodle_exception
* @since 3.2
*/
public static function data_for_messagearea_contacts($userid, $limitfrom = 0, $limitnum = 0) {
global $CFG, $PAGE, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
$systemcontext = context_system::instance();
$params = array(
'userid' => $userid,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum
);
self::validate_parameters(self::data_for_messagearea_contacts_parameters(), $params);
self::validate_context($systemcontext);
if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
throw new moodle_exception('You do not have permission to perform this action.');
}
$contacts = \core_message\api::get_contacts($userid, $limitfrom, $limitnum);
$contacts = new \core_message\output\messagearea\contacts(null, $contacts);
$renderer = $PAGE->get_renderer('core_message');
return $contacts->export_for_template($renderer);
}
/**
* The messagearea contacts return structure.
*
* @return external_single_structure
* @since 3.2
*/
public static function data_for_messagearea_contacts_returns() {
return self::data_for_messagearea_conversations_returns();
}
/**
* The messagearea messages parameters.
*
* @return external_function_parameters
* @since 3.2
*/
public static function data_for_messagearea_messages_parameters() {
return new external_function_parameters(
array(
'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'),
'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'),
'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0),
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0),
'newest' => new external_value(PARAM_BOOL, 'Newest first?', VALUE_DEFAULT, false),
'timefrom' => new external_value(PARAM_INT,
'The timestamp from which the messages were created', VALUE_DEFAULT, 0),
)
);
}
/**
* Get messagearea messages.
*
* @param int $currentuserid The current user's id
* @param int $otheruserid The other user's id
* @param int $limitfrom
* @param int $limitnum
* @param boolean $newest
* @return stdClass
* @throws moodle_exception
* @since 3.2
*/
public static function data_for_messagearea_messages($currentuserid, $otheruserid, $limitfrom = 0, $limitnum = 0,
$newest = false, $timefrom = 0) {
global $CFG, $PAGE, $USER;
// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
$systemcontext = context_system::instance();
$params = array(
'currentuserid' => $currentuserid,
'otheruserid' => $otheruserid,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum,
'newest' => $newest,
'timefrom' => $timefrom,
);
self::validate_parameters(self::data_for_messagearea_messages_parameters(), $params);
self::validate_context($systemcontext);
if (($USER->id != $currentuserid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
throw new moodle_exception('You do not have permission to perform this action.');
}
if ($newest) {
$sort = 'timecreated DESC';
} else {
$sort = 'timecreated ASC';
}
// We need to enforce a one second delay on messages to avoid race conditions of current
// messages still being sent.
//
// There is a chance that we could request messages before the current time's
// second has elapsed and while other messages are being sent in that same second. In which
// case those messages will be lost.
//
// Instead we ignore the current time in the result set to ensure that second is allowed to finish.
if (!empty($timefrom)) {
$timeto = time() - 1;
} else {
$timeto = 0;
}
// No requesting messages from the current time, as stated above.
if ($timefrom == time()) {
$messages = [];
} else {
$messages = \core_message\api::get_messages($currentuserid, $otheruserid, $limitfrom,
$limitnum, $sort, $timefrom, $timeto);
}
$messages = new \core_message\output\messagearea\messages($currentuserid, $otheruserid, $messages);
$renderer = $PAGE->get_renderer('core_message');
return $messages->export_for_template($renderer);
}
/**
* The messagearea messages return structure.
*
* @return external_single_structure
* @since 3.2
*/
public static function data_for_messagearea_messages_returns() {
return new external_single_structure(
array(
'iscurrentuser' => new external_value(PARAM_BOOL, 'Is the currently logged in user the user we are viewing
the messages on behalf of?'),
'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'),
'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'),
'otheruserfullname' => new external_value(PARAM_NOTAGS, 'The other user\'s fullname'),
'showonlinestatus' => new external_value(PARAM_BOOL, 'Show the user\'s online status?'),
'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'),