forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
1765 lines (1448 loc) · 64.4 KB
/
lib.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/>.
/**
* Library functions for messaging
*
* @copyright Luis Rodrigues
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package message
*/
require_once($CFG->libdir.'/eventslib.php');
define ('MESSAGE_SHORTLENGTH', 300);
define ('MESSAGE_WINDOW', true); // We are in a message window (so don't pop up a new one!)
define ('MESSAGE_DISCUSSION_WIDTH',600);
define ('MESSAGE_DISCUSSION_HEIGHT',500);
define ('MESSAGE_SHORTVIEW_LIMIT', 8);//the maximum number of messages to show on the short message history
define ('CONTACT_ID','id');
define('MESSAGE_HISTORY_SHORT',0);
define('MESSAGE_HISTORY_ALL',1);
//some constants used as function arguments. Just to make function calls a bit more understandable
define('IS_CONTACT',true);
define('IS_NOT_CONTACT',false);
define('IS_BLOCKED',true);
define('IS_NOT_BLOCKED',false);
define('VIEW_UNREAD_MESSAGES','unread');
define('VIEW_CONTACTS','contacts');
define('VIEW_BLOCKED','blockedusers');
define('VIEW_COURSE','course_');
define('SHOW_ACTION_LINKS_IN_CONTACT_LIST', true);
define('MESSAGE_SEARCH_MAX_RESULTS', 200);
define('MESSAGE_CONTACTS_PER_PAGE',10);
if (!isset($CFG->message_contacts_refresh)) { // Refresh the contacts list every 60 seconds
$CFG->message_contacts_refresh = 60;
}
if (!isset($CFG->message_chat_refresh)) { // Look for new comments every 5 seconds
$CFG->message_chat_refresh = 5;
}
if (!isset($CFG->message_offline_time)) {
$CFG->message_offline_time = 300;
}
function message_print_contact_selector($countunreadtotal, $usergroup, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showcontactactionlinks, $page=0) {
global $PAGE;
echo html_writer::start_tag('div', array('class'=>'contactselector mdl-align'));
//if 0 unread messages and they've requested unread messages then show contacts
if ($countunreadtotal==0 && $usergroup==VIEW_UNREAD_MESSAGES) {
$usergroup = VIEW_CONTACTS;
}
//if they have no blocked users and they've requested blocked users switch them over to contacts
if (count($blockedusers)==0 && $usergroup==VIEW_BLOCKED) {
$usergroup = VIEW_CONTACTS;
}
$onlyactivecourses = true;
$courses = enrol_get_users_courses($user1->id, $onlyactivecourses);
$coursecontexts = message_get_course_contexts($courses);//we need one of these again so holding on to them
$strunreadmessages = null;
if ($countunreadtotal>0) { //if there are unread messages
$strunreadmessages = get_string('unreadmessages','message', $countunreadtotal);
}
message_print_usergroup_selector($usergroup, $courses, $coursecontexts, $countunreadtotal, count($blockedusers), $strunreadmessages);
$refreshpage = false;
if ($usergroup==VIEW_UNREAD_MESSAGES) {
message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $refreshpage, $PAGE->url, 1, $showcontactactionlinks,$strunreadmessages, $user2);
} else if ($usergroup==VIEW_CONTACTS) {
message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $refreshpage, $PAGE->url, 0, $showcontactactionlinks, $strunreadmessages, $user2);
} else if ($usergroup==VIEW_BLOCKED) {
message_print_blocked_users($blockedusers, $PAGE->url, $showcontactactionlinks, null, $user2);
} else if (substr($usergroup, 0, 7)==VIEW_COURSE) {
$courseidtoshow = intval(substr($usergroup, 7));
if (!empty($courseidtoshow)
&& array_key_exists($courseidtoshow, $coursecontexts)
&& has_capability('moodle/course:viewparticipants', $coursecontexts[$courseidtoshow])) {
message_print_participants($coursecontexts[$courseidtoshow], $courseidtoshow, $PAGE->url, $showcontactactionlinks, null, $page);
} else {
//shouldn't get here. User trying to access a course they're not in perhaps.
add_to_log(SITEID, 'message', 'view', 'index.php', $usergroup);
}
}
echo html_writer::start_tag('form', array('action'=>'contacts.php?advanced=1'));
echo html_writer::start_tag('fieldset');
$managebuttonclass = 'hiddenelement';
if ($usergroup == VIEW_CONTACTS) {
$managebuttonclass = 'visible';
}
$strmanagecontacts = get_string('search','message');
echo html_writer::empty_tag('input', array('type'=>'submit','value'=>$strmanagecontacts,'class'=>$managebuttonclass));
echo html_writer::end_tag('fieldset');
echo html_writer::end_tag('form');
echo html_writer::end_tag('div');
}
function message_print_participants($context, $courseid, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null, $page=0) {
global $DB, $USER, $PAGE, $OUTPUT;
$countparticipants = count_enrolled_users($context);
$participants = get_enrolled_users($context, '', 0, 'u.*', '', $page*MESSAGE_CONTACTS_PER_PAGE, MESSAGE_CONTACTS_PER_PAGE);
$pagingbar = new paging_bar($countparticipants, $page, MESSAGE_CONTACTS_PER_PAGE, $PAGE->url, 'page');
echo $OUTPUT->render($pagingbar);
echo '<table id="message_participants" class="boxaligncenter" cellspacing="2" cellpadding="0" border="0">';
if (!empty($titletodisplay)) {
echo "<tr><td colspan='3' class='heading'>$titletodisplay</td></tr>";
}
if (empty($titletodisplay)) {
echo '<tr><td colspan="3" class="heading">';
echo get_string('participants');
echo '</td></tr>';
}
//todo these need to come from somewhere if the course participants list is to show users with unread messages
$iscontact = true;
$isblocked = false;
foreach ($participants as $participant) {
if ($participant->id != $USER->id) {
$participant->messagecount = 0;//todo it would be nice if the course participant could report new messages
message_print_contactlist_user($participant, $iscontact, $isblocked, $contactselecturl, $showactionlinks);
}
}
//$participants->close();
echo '</table>';
}
function message_get_blocked_users($user1=null, &$user2=null) {
global $DB, $USER;
if (empty($user1)) {
$user1 = $USER;
}
if (!empty($user2)) {
$user2->isblocked = false;
}
$userfields = user_picture::fields('u', array('lastaccess'));
$blockeduserssql = "SELECT $userfields, COUNT(m.id) AS messagecount
FROM {message_contacts} mc
JOIN {user} u ON u.id = mc.contactid
LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = :user1id1
WHERE mc.userid = :user1id2 AND mc.blocked = 1
GROUP BY $userfields
ORDER BY u.firstname ASC";
$rs = $DB->get_recordset_sql($blockeduserssql, array('user1id1'=>$user1->id, 'user1id2'=>$user1->id));
$blockedusers = array();
if (!empty($rs)) {
foreach($rs as $rd) {
$blockedusers[] = $rd;
if (!empty($user2) && $user2->id==$rd->id) {
$user2->isblocked = true;
}
}
unset($rd);
$rs->close();
}
return $blockedusers;
}
function message_print_blocked_users(&$blockedusers, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null) {
global $DB, $USER;
$countblocked = count($blockedusers);
echo '<table id="message_contacts" class="boxaligncenter" cellspacing="2" cellpadding="0" border="0">';
if (!empty($titletodisplay)) {
echo "<tr><td colspan='3' class='heading'>$titletodisplay</td></tr>";
}
if ($countblocked) {
echo '<tr><td colspan="3" class="heading">';
echo get_string('blockedusers', 'message', $countblocked);
echo '</td></tr>';
foreach ($blockedusers as $blockeduser) {
message_print_contactlist_user($blockeduser, IS_NOT_CONTACT, IS_BLOCKED, $contactselecturl, $showactionlinks);
}
}
echo '</table>';
}
function message_get_contacts($user1=null, &$user2=null) {
global $DB, $CFG, $USER;
if (empty($user1)) {
$user1 = $USER;
}
if (!empty($user2)) {
$user2->iscontact = false;
}
$timetoshowusers = 300; //Seconds default
if (isset($CFG->block_online_users_timetosee)) {
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
}
// time which a user is counting as being active since
$timefrom = time()-$timetoshowusers;
// people in our contactlist who are online
$onlinecontacts = array();
// people in our contactlist who are offline
$offlinecontacts = array();
// people who are not in our contactlist but have sent us a message
$strangers = array();
$userfields = user_picture::fields('u', array('lastaccess'));
// get all in our contactlist who are not blocked in our contact list
// and count messages we have waiting from each of them
$contactsql = "SELECT $userfields, COUNT(m.id) AS messagecount
FROM {message_contacts} mc
JOIN {user} u ON u.id = mc.contactid
LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = ?
WHERE mc.userid = ? AND mc.blocked = 0
GROUP BY $userfields
ORDER BY u.firstname ASC";
if ($rs = $DB->get_recordset_sql($contactsql, array($user1->id, $user1->id))){
foreach($rs as $rd){
if($rd->lastaccess >= $timefrom){
// they have been active recently, so are counted online
$onlinecontacts[] = $rd;
}else{
$offlinecontacts[] = $rd;
}
if (!empty($user2) && $user2->id==$rd->id) {
$user2->iscontact = true;
}
}
unset($rd);
$rs->close();
}
// get messages from anyone who isn't in our contact list and count the number
// of messages we have from each of them
$strangersql = "SELECT $userfields, count(m.id) as messagecount
FROM {message} m
JOIN {user} u ON u.id = m.useridfrom
LEFT OUTER JOIN {message_contacts} mc ON mc.contactid = m.useridfrom AND mc.userid = m.useridto
WHERE mc.id IS NULL AND m.useridto = ?
GROUP BY $userfields
ORDER BY u.firstname ASC";
if($rs = $DB->get_recordset_sql($strangersql, array($USER->id))){
foreach($rs as $rd){
$strangers[] = $rd;
}
unset($rd);
$rs->close();
}
return array($onlinecontacts, $offlinecontacts, $strangers);
}
function message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $refresh=true, $contactselecturl=null, $minmessages=0, $showactionlinks=true, $titletodisplay=null) {
global $CFG, $PAGE, $OUTPUT;
$countonlinecontacts = count($onlinecontacts);
$countofflinecontacts = count($offlinecontacts);
$countstrangers = count($strangers);
if ($countonlinecontacts + $countofflinecontacts == 0) {
echo '<div class="heading">';
print_string('contactlistempty', 'message');
echo '</div>';
//echo '<div class="note">';
//print_string('addsomecontacts', 'message', message_remove_url_params($PAGE->url).'?tab=search');
//echo '</div>';
}
echo '<table id="message_contacts" class="boxaligncenter" cellspacing="2" cellpadding="0" border="0">';
if (!empty($titletodisplay)) {
echo "<tr><td colspan='3' class='heading'>$titletodisplay</td></tr>";
}
if($countonlinecontacts) {
/// print out list of online contacts
if (empty($titletodisplay)) {
echo '<tr><td colspan="3" class="heading">';
echo get_string('onlinecontacts', 'message', $countonlinecontacts);
echo '</td></tr>';
}
foreach ($onlinecontacts as $contact) {
if ($minmessages==0 || $contact->messagecount>=$minmessages) {
message_print_contactlist_user($contact, IS_CONTACT, IS_NOT_BLOCKED, $contactselecturl, $showactionlinks);
}
}
}
if ($countofflinecontacts) {
/// print out list of offline contacts
if (empty($titletodisplay)) {
echo '<tr><td colspan="3" class="heading">';
echo get_string('offlinecontacts', 'message', $countofflinecontacts);
echo '</td></tr>';
}
foreach ($offlinecontacts as $contact) {
if ($minmessages==0 || $contact->messagecount>=$minmessages) {
message_print_contactlist_user($contact, IS_CONTACT, IS_NOT_BLOCKED, $contactselecturl, $showactionlinks);
}
}
echo '<tr><td colspan="3"> </td></tr>';
}
/// print out list of incoming contacts
if ($countstrangers) {
echo '<tr><td colspan="3" class="heading">';
echo get_string('incomingcontacts', 'message', $countstrangers);
echo '</td></tr>';
foreach ($strangers as $stranger) {
if ($minmessages==0 || $stranger->messagecount>=$minmessages) {
message_print_contactlist_user($stranger, IS_NOT_CONTACT, IS_NOT_BLOCKED, $contactselecturl, $showactionlinks);
}
}
}
echo '</table>';
if ($countstrangers && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help
echo '<div class="note">(';
print_string('addsomecontactsincoming', 'message');
echo ')</div>';
}
echo '<br />';
if ($refresh) {
$PAGE->requires->js_init_call('M.core_message.init_refresh_page', array(60*1000, $PAGE->url->out(false)));
echo $OUTPUT->container_start('messagejsautorefresh note center');
echo get_string('pagerefreshes', 'message', $CFG->message_contacts_refresh);
echo $OUTPUT->container_end();
echo $OUTPUT->container_start('messagejsmanualrefresh aligncenter');
echo $OUTPUT->single_button(message_remove_url_params($PAGE->url), get_string('refresh'));
echo $OUTPUT->container_end();
}
}
function message_print_usergroup_selector($usergroup, &$courses, &$coursecontexts, $countunreadtotal, $countblocked, $strunreadmessages) {
$strblockedusers = null;
$options = array();
if ($countunreadtotal>0) { //if there are unread messages
$options[VIEW_UNREAD_MESSAGES] = $strunreadmessages;
}
if ($countblocked>0) {
$strblockedusers = get_string('blockedusers','message', $countblocked);
$options[VIEW_BLOCKED] = $strblockedusers;
}
$strcontacts = get_string('mycontacts', 'message');
$options[VIEW_CONTACTS] = $strcontacts;
if (!empty($courses)) {
$courses_options = array();
foreach($courses as $course) {
if (has_capability('moodle/course:viewparticipants', $coursecontexts[$course->id])) {
$courses_options[VIEW_COURSE.$course->id] = $course->shortname;
}
}
if (!empty($courses_options)) {
$options[] = array(get_string('courses')=>$courses_options);
}
}
echo html_writer::start_tag('form', array('id'=>'usergroupform','method'=>'get','action'=>''));
echo html_writer::start_tag('fieldset');
echo html_writer::select($options, 'usergroup', $usergroup, false, array('id'=>'usergroup','onchange'=>'this.form.submit()'));
echo html_writer::end_tag('fieldset');
echo html_writer::end_tag('form');
}
function message_get_course_contexts(&$courses) {
$coursecontexts = array();
foreach($courses as $course) {
$coursecontexts[$course->id] = get_context_instance(CONTEXT_COURSE, $course->id);
}
return $coursecontexts;
}
/**
* strip off action parameters like 'removecontact'
*/
function message_remove_url_params($moodleurl) {
$newurl = new moodle_url($moodleurl);
$newurl->remove_params('addcontact','removecontact','blockcontact','unblockcontact');
return $newurl->out();
}
/// $messagearray is an array of objects
/// $field is a valid property of object
/// $value is the value $field should equal to be counted
/// if $field is empty then return count of the whole array
/// if $field is non-existent then return 0;
function message_count_messages($messagearray, $field='', $value='') {
if (!is_array($messagearray)) return 0;
if ($field == '' or empty($messagearray)) return count($messagearray);
$count = 0;
foreach ($messagearray as $message) {
$count += ($message->$field == $value) ? 1 : 0;
}
return $count;
}
/**
* Returns the count of unread messages for user. Either from a specific user or from all users.
* @global <type> $USER
* @global <type> $DB
* @param object $user1 the first user. Defaults to $USER
* @param object $user2 the second user. If null this function will count all of user 1's unread messages.
* @return int the count of $user1's unread messages
*/
function message_count_unread_messages($user1=null, $user2=null) {
global $USER, $DB;
if (empty($user1)) {
$user1 = $USER;
}
if (!empty($user2)) {
return $DB->count_records_select('message', "useridto = ? AND useridfrom = ?",
array($user1->id, $user2->id), "COUNT('id')");
} else {
return $DB->count_records_select('message', "useridto = ?",
array($user1->id), "COUNT('id')");
}
}
function message_count_blocked_users($user1=null) {
global $USER, $DB;
if (empty($user1)) {
$user1 = $USER;
}
$sql = "SELECT count(mc.id)
FROM {message_contacts} mc
WHERE mc.userid = :userid AND mc.blocked = 1";
$params = array('userid'=>$user1->id);
return $DB->count_records_sql($sql, $params);
}
/**
*
* @global <type> $USER
* @global <type> $PAGE
* @global <type> $OUTPUT
* @param boolean advancedsearch show basic or advanced search form
* @return boolean was a search performed?
*/
function message_print_search($advancedsearch = false, $user1=null) {
global $USER, $PAGE, $OUTPUT;
$frm = data_submitted();
$doingsearch = false;
if ($frm) {
$doingsearch = !empty($frm->combinedsubmit) || !empty($frm->keywords) || (!empty($frm->personsubmit) and !empty($frm->name));
}
if (!empty($frm->combinedsearch)) {
$combinedsearchstring = $frm->combinedsearch;
} else {
//$combinedsearchstring = get_string('searchcombined','message').'...';
$combinedsearchstring = '';
}
//$PAGE->requires->js_init_call('M.core_message.init_search_page', array($combinedsearchstring));
if ($doingsearch) {
if ($advancedsearch) {
$messagesearch = '';
if (!empty($frm->keywords)) {
$messagesearch = $frm->keywords;
}
$personsearch = '';
if (!empty($frm->name)) {
$personsearch = $frm->name;
}
include('search_advanced.html');
} else {
include('search.html');
}
$showicontext = false;
message_print_search_results($frm, $showicontext, $user1);
return true;
} else {
/*
/// unfinished buggy code disabled in search.html anyway
// find all courses this use has readallmessages capabilities in
if ($teachers = get_user_capability_course('moodle/site:readallmessages')) {
$courses = get_courses('all', 'c.sortorder ASC', 'c.id, c.shortname');
$cs = '<select name="courseselect">';
foreach ($teachers as $tcourse) {
$cs .= "<option value=\"$tcourse->course\">".$courses[$tcourse->id]->shortname."</option>\n";
}
$cs .= '</select>';
}
*/
if ($advancedsearch) {
$personsearch = $messagesearch = '';
include('search_advanced.html');
} else {
include('search.html');
}
return false;
}
}
function message_print_settings() {
global $USER, $OUTPUT, $PAGE;
if ($frm = data_submitted() and confirm_sesskey()) {
$pref = array();
$pref['message_beepnewmessage'] = (isset($frm->beepnewmessage)) ? '1' : '0';
$pref['message_blocknoncontacts'] = (isset($frm->blocknoncontacts)) ? '1' : '0';
set_user_preferences($pref);
redirect(message_remove_url_params($PAGE->url), get_string('settingssaved', 'message'), 1);
}
$cbbeepnewmessage = (get_user_preferences('message_beepnewmessage', 0) == '1') ? 'checked="checked"' : '';
$cbblocknoncontacts = (get_user_preferences('message_blocknoncontacts', 0) == '1') ? 'checked="checked"' : '';
include('settings.html');
}
function message_add_contact($contactid, $blocked=0) {
global $USER, $DB;
if (!$DB->record_exists('user', array('id'=>$contactid))) { // invalid userid
return false;
}
if (($contact = $DB->get_record('message_contacts', array('userid'=>$USER->id, 'contactid'=>$contactid))) !== false) {
/// record already exists - we may be changing blocking status
if ($contact->blocked !== $blocked) {
/// change to blocking status
$contact->blocked = $blocked;
return $DB->update_record('message_contacts', $contact);
} else {
/// no changes to blocking status
return true;
}
} else {
/// new contact record
unset($contact);
$contact->userid = $USER->id;
$contact->contactid = $contactid;
$contact->blocked = $blocked;
return $DB->insert_record('message_contacts', $contact, false);
}
}
function message_remove_contact($contactid) {
global $USER, $DB;
return $DB->delete_records('message_contacts', array('userid'=>$USER->id, 'contactid'=>$contactid));
}
function message_unblock_contact($contactid) {
global $USER, $DB;
return $DB->delete_records('message_contacts', array('userid'=>$USER->id, 'contactid'=>$contactid));
}
function message_block_contact($contactid) {
return message_add_contact($contactid, 1);
}
function message_get_contact($contactid) {
global $USER, $DB;
return $DB->get_record('message_contacts', array('userid'=>$USER->id, 'contactid'=>$contactid));
}
function message_print_search_results($frm, $showicontext=false, $user1=null) {
global $USER, $CFG, $DB, $OUTPUT, $PAGE;
if (empty($user1)) {
$user1 = $USER;
}
echo '<div class="mdl-left">';
$personsearch = false;
$personsearchstring = null;
if (!empty($frm->personsubmit) and !empty($frm->name)) {
$personsearch = true;
$personsearchstring = $frm->name;
} else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) {
$personsearch = true;
$personsearchstring = $frm->combinedsearch;
}
/// search for person
if ($personsearch) {
if (optional_param('mycourses', 0, PARAM_BOOL)) {
$users = array();
$mycourses = enrol_get_my_courses();
foreach ($mycourses as $mycourse) {
if (is_array($susers = message_search_users($mycourse->id, $personsearchstring))) {
foreach ($susers as $suser) $users[$suser->id] = $suser;
}
}
} else {
$users = message_search_users(SITEID, $personsearchstring);
}
if (!empty($users)) {
echo html_writer::start_tag('p', array('class'=>'heading searchresultcount'));
echo get_string('userssearchresults', 'message', count($users));
echo html_writer::end_tag('p');
echo '<table class="messagesearchresults">';
foreach ($users as $user) {
if ( $user->contactlistid ) {
if ($user->blocked == 0) { /// not blocked
$strcontact = message_contact_link($user->id, 'remove', true, null, $showicontext);
$strblock = message_contact_link($user->id, 'block', true, null, $showicontext);
} else { // blocked
$strcontact = message_contact_link($user->id, 'add', true, null, $showicontext);
$strblock = message_contact_link($user->id, 'unblock', true, null, $showicontext);
}
} else {
$strcontact = message_contact_link($user->id, 'add', true, null, $showicontext);
$strblock = message_contact_link($user->id, 'block', true, null, $showicontext);
}
//should we show just the icon or icon and text?
$histicontext = 'icon';
if ($showicontext) {
$histicontext = 'both';
}
$strhistory = message_history_link($USER->id, $user->id, true, '', '', $histicontext);
echo '<tr><td class="pix">';
echo $OUTPUT->user_picture($user, array('size'=>20, 'courseid'=>SITEID));
echo '</td>';
echo '<td class="contact">';
$popupoptions = array(
'height' => MESSAGE_DISCUSSION_HEIGHT,
'width' => MESSAGE_DISCUSSION_WIDTH,
'menubar' => false,
'location' => false,
'status' => true,
'scrollbars' => true,
'resizable' => true);
//$link = new moodle_url("/message/discussion.php?id=$user->id");
$link = new moodle_url("/message/index.php?id=$user->id");
//$action = new popup_action('click', $link, "message_$user->id", $popupoptions);
$action = null;
echo $OUTPUT->action_link($link, fullname($user), $action, array('title'=>get_string('sendmessageto', 'message', fullname($user))));
echo '</td>';
echo '<td class="link">'.$strcontact.'</td>';
echo '<td class="link">'.$strblock.'</td>';
echo '<td class="link">'.$strhistory.'</td>';
echo '</tr>';
}
echo '</table>';
} else {
echo html_writer::start_tag('p', array('class'=>'heading searchresultcount'));
echo get_string('userssearchresults', 'message', 0).'<br /><br />';
echo html_writer::end_tag('p');
}
}
// search messages for keywords
$messagesearch = false;
$messagesearchstring = null;
if (!empty($frm->keywords)) {
$messagesearch = true;
$messagesearchstring = clean_text(trim($frm->keywords));
} else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) {
$messagesearch = true;
$messagesearchstring = clean_text(trim($frm->combinedsearch));
}
if ($messagesearch) {
if ($messagesearchstring) {
$keywords = explode(' ', $messagesearchstring);
} else {
$keywords = array();
}
$tome = false;
$fromme = false;
$courseid = 'none';
if (empty($frm->keywordsoption)) {
$frm->keywordsoption = 'allmine';
}
switch ($frm->keywordsoption) {
case 'tome':
$tome = true;
break;
case 'fromme':
$fromme = true;
break;
case 'allmine':
$tome = true;
$fromme = true;
break;
case 'allusers':
$courseid = SITEID;
break;
case 'courseusers':
$courseid = $frm->courseid;
break;
default:
$tome = true;
$fromme = true;
}
if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) {
/// get a list of contacts
if (($contacts = $DB->get_records('message_contacts', array('userid'=>$USER->id), '', 'contactid, blocked') ) === false) {
$contacts = array();
}
/// print heading with number of results
echo html_writer::start_tag('p', array('class'=>'heading searchresultcount'));
$countresults = count($messages);
if ($countresults==MESSAGE_SEARCH_MAX_RESULTS) {
echo get_string('keywordssearchresultstoomany', 'message', $countresults).' ("'.s($messagesearchstring).'")';
} else {
echo get_string('keywordssearchresults', 'message', $countresults);
}
echo html_writer::end_tag('p');
/// print table headings
echo '<table class="messagesearchresults" cellspacing="0">';
$headertdstart = html_writer::start_tag('td', array('class'=>'messagesearchresultscol'));
$headertdend = html_writer::end_tag('td');
echo '<tr>';
echo $headertdstart.get_string('from').$headertdend;
echo $headertdstart.get_string('to').$headertdend;
echo $headertdstart.get_string('message', 'message').$headertdend;
echo $headertdstart.get_string('timesent', 'message').$headertdend;
echo "</tr>\n";
$blockedcount = 0;
$dateformat = get_string('strftimedatetimeshort');
$strcontext = get_string('context', 'message');
foreach ($messages as $message) {
/// ignore messages to and from blocked users unless $frm->includeblocked is set
if (!optional_param('includeblocked', 0, PARAM_BOOL) and (
( isset($contacts[$message->useridfrom]) and ($contacts[$message->useridfrom]->blocked == 1)) or
( isset($contacts[$message->useridto] ) and ($contacts[$message->useridto]->blocked == 1))
)
) {
$blockedcount ++;
continue;
}
/// load up user to record
if ($message->useridto !== $USER->id) {
$userto = $DB->get_record('user', array('id'=>$message->useridto));
$tocontact = (array_key_exists($message->useridto, $contacts) and
($contacts[$message->useridto]->blocked == 0) );
$toblocked = (array_key_exists($message->useridto, $contacts) and
($contacts[$message->useridto]->blocked == 1) );
} else {
$userto = false;
$tocontact = false;
$toblocked = false;
}
/// load up user from record
if ($message->useridfrom !== $USER->id) {
$userfrom = $DB->get_record('user', array('id'=>$message->useridfrom));
$fromcontact = (array_key_exists($message->useridfrom, $contacts) and
($contacts[$message->useridfrom]->blocked == 0) );
$fromblocked = (array_key_exists($message->useridfrom, $contacts) and
($contacts[$message->useridfrom]->blocked == 1) );
} else {
$userfrom = false;
$fromcontact = false;
$fromblocked = false;
}
/// find date string for this message
$date = usergetdate($message->timecreated);
$datestring = $date['year'].$date['mon'].$date['mday'];
/// print out message row
echo '<tr valign="top">';
echo '<td class="contact">';
message_print_user($userfrom, $fromcontact, $fromblocked, $showicontext);
echo '</td>';
echo '<td class="contact">';
message_print_user($userto, $tocontact, $toblocked, $showicontext);
echo '</td>';
echo '<td class="summary">'.message_get_fragment($message->fullmessage, $keywords);
echo '<br /><div class="link">';
//find the user involved that isn't the current user
$user2id = null;
if ($user1->id == $message->useridto) {
$user2id = $message->useridfrom;
} else {
$user2id = $message->useridto;
}
message_history_link($user1->id, $user2id, false,
$messagesearchstring, 'm'.$message->id, $strcontext);
echo '</div>';
echo '</td>';
echo '<td class="date">'.userdate($message->timecreated, $dateformat).'</td>';
echo "</tr>\n";
}
if ($blockedcount > 0) {
echo '<tr><td colspan="4" align="center">'.get_string('blockedmessages', 'message', $blockedcount).'</td></tr>';
}
echo '</table>';
} else {
echo '<p class="heading">'.get_string('keywordssearchresults', 'message', 0).' ("'.s($messagesearchstring).'")</p>';
}
}
if (!$personsearch && !$messagesearch) {
//they didn't enter any search terms
echo $OUTPUT->notification(get_string('emptysearchstring', 'message'));
}
echo '<br />';
echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('tab' => 'search')), get_string('newsearch', 'message'));
echo '</div>';
}
function message_print_user ($user=false, $iscontact=false, $isblocked=false, $includeicontext=false) {
global $USER, $OUTPUT;
if ($user === false) {
echo $OUTPUT->user_picture($USER, array('size'=>20, 'courseid'=>SITEID));
} else {
echo $OUTPUT->user_picture($user, array('size'=>20, 'courseid'=>SITEID));
echo ' ';
$return = false;
$script = null;
if ($iscontact) {
message_contact_link($user->id, 'remove', $return, $script, $includeicontext);
} else {
message_contact_link($user->id, 'add', $return, $script, $includeicontext);
}
echo ' ';
if ($isblocked) {
message_contact_link($user->id, 'unblock', $return, $script, $includeicontext);
} else {
message_contact_link($user->id, 'block', $return, $script, $includeicontext);
}
echo '<br />';
$popupoptions = array(
'height' => MESSAGE_DISCUSSION_HEIGHT,
'width' => MESSAGE_DISCUSSION_WIDTH,
'menubar' => false,
'location' => false,
'status' => true,
'scrollbars' => true,
'resizable' => true);
//$link = new moodle_url("/message/discussion.php?id=$user->id");
$link = new moodle_url("/message/index.php?id=$user->id");
//$action = new popup_action('click', $link, "message_$user->id", $popupoptions);
$action = null;
echo $OUTPUT->action_link($link, fullname($user), $action, array('title'=>get_string('sendmessageto', 'message', fullname($user))));
}
}
/// linktype can be: add, remove, block, unblock
function message_contact_link($userid, $linktype='add', $return=false, $script=null, $text=false, $icon=true) {
global $USER, $CFG, $OUTPUT, $PAGE;
//hold onto the strings as we're probably creating a bunch of links
static $str;
if (empty($script)) {
//strip off previous action params like 'removecontact'
$theurl = message_remove_url_params($PAGE->url);
if (strpos($theurl, '?')===false) {
$script = $theurl.'?tab=contacts';
} else {
$script = $theurl.'&tab=contacts';
}
}
if (empty($str->blockcontact)) {
$str->blockcontact = get_string('blockcontact', 'message');
$str->unblockcontact = get_string('unblockcontact', 'message');
$str->removecontact = get_string('removecontact', 'message');
$str->addcontact = get_string('addcontact', 'message');
}
$command = $linktype.'contact';
$string = $str->{$command};
$safealttext = s($string);
$safestring = '';
if (!empty($text)) {
$safestring = $safealttext;
}
$img = '';
if ($icon) {
$iconpath = null;
switch ($linktype) {
case 'block':
$iconpath = 't/block';