Commit | Line | Data |
---|---|---|
eb5334ff | 1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | /** | |
19 | * Library functions for messaging | |
20 | * | |
21 | * @copyright Luis Rodrigues | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | * @package message | |
24 | */ | |
172186b8 | 25 | |
3b120e46 | 26 | require_once($CFG->libdir.'/eventslib.php'); |
27 | ||
e8e2d7f1 | 28 | |
29 | define ('MESSAGE_SHORTLENGTH', 300); | |
fd7006f6 | 30 | define ('MESSAGE_WINDOW', true); // We are in a message window (so don't pop up a new one!) |
31 | ||
c8621a02 AD |
32 | define ('MESSAGE_DISCUSSION_WIDTH',600); |
33 | define ('MESSAGE_DISCUSSION_HEIGHT',500); | |
34 | ||
35 | define ('MESSAGE_SHORTVIEW_LIMIT', 8);//the maximum number of messages to show on the short message history | |
36 | ||
37 | define ('CONTACT_ID','id'); | |
38 | ||
39 | define('MESSAGE_HISTORY_SHORT',0); | |
40 | define('MESSAGE_HISTORY_ALL',1); | |
41 | ||
42 | //some constants used as function arguments. Just to make function calls a bit more understandable | |
43 | define('IS_CONTACT',true); | |
44 | define('IS_NOT_CONTACT',false); | |
45 | ||
46 | define('IS_BLOCKED',true); | |
47 | define('IS_NOT_BLOCKED',false); | |
48 | ||
49 | define('VIEW_UNREAD_MESSAGES','unread'); | |
50 | define('VIEW_CONTACTS','contacts'); | |
51 | define('VIEW_BLOCKED','blockedusers'); | |
52 | define('VIEW_COURSE','course_'); | |
53 | ||
54 | define('SHOW_ACTION_LINKS_IN_CONTACT_LIST', false); | |
55 | ||
05753d2b | 56 | if (!isset($CFG->message_contacts_refresh)) { // Refresh the contacts list every 60 seconds |
2a38a6be | 57 | $CFG->message_contacts_refresh = 60; |
fd7006f6 | 58 | } |
5d6b319b | 59 | if (!isset($CFG->message_chat_refresh)) { // Look for new comments every 5 seconds |
60 | $CFG->message_chat_refresh = 5; | |
fd7006f6 | 61 | } |
f520438c | 62 | if (!isset($CFG->message_offline_time)) { |
63 | $CFG->message_offline_time = 300; | |
64 | } | |
172186b8 | 65 | |
c8621a02 AD |
66 | function message_print_contact_selector($countunreadtotal, $usergroup, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showcontactactionlinks) { |
67 | global $PAGE; | |
68 | ||
69 | echo html_writer::start_tag('div', array('class'=>'contactselector mdl-align')); | |
70 | ||
71 | //if 0 unread messages and they've requested unread messages then show contacts | |
72 | if ($countunreadtotal==0 && $usergroup==VIEW_UNREAD_MESSAGES) { | |
73 | $usergroup = VIEW_CONTACTS; | |
74 | } | |
75 | ||
65c7853e AD |
76 | //if they have no blocked users and they've requested blocked users switch them over to contacts |
77 | if (count($blockedusers)==0 && $usergroup==VIEW_BLOCKED) { | |
78 | $usergroup = VIEW_CONTACTS; | |
79 | } | |
80 | ||
c8621a02 AD |
81 | $onlyactivecourses = true; |
82 | $courses = enrol_get_users_courses($user1->id, $onlyactivecourses); | |
83 | $coursecontexts = message_get_course_contexts($courses);//we need one of these again so holding on to them | |
84 | ||
43240ea1 AD |
85 | $strunreadmessages = null; |
86 | if ($countunreadtotal>0) { //if there are unread messages | |
87 | $strunreadmessages = get_string('unreadmessages','message', $countunreadtotal); | |
88 | } | |
89 | ||
90 | message_print_usergroup_selector($usergroup, $courses, $coursecontexts, $countunreadtotal, count($blockedusers), $strunreadmessages); | |
c8621a02 AD |
91 | |
92 | $refreshpage = false; | |
93 | ||
94 | if ($usergroup==VIEW_UNREAD_MESSAGES) { | |
95 | message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $refreshpage, $PAGE->url, 1, $showcontactactionlinks,$strunreadmessages, $user2); | |
96 | } else if ($usergroup==VIEW_CONTACTS) { | |
b32778c4 | 97 | message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $refreshpage, $PAGE->url, 0, $showcontactactionlinks, $strunreadmessages, $user2); |
c8621a02 AD |
98 | } else if ($usergroup==VIEW_BLOCKED) { |
99 | message_print_blocked_users($blockedusers, $PAGE->url, $showcontactactionlinks, null, $user2); | |
100 | } else if (substr($usergroup, 0, 7)==VIEW_COURSE) { | |
101 | $courseidtoshow = intval(substr($usergroup, 7)); | |
102 | ||
103 | if (!empty($courseidtoshow) | |
104 | && array_key_exists($courseidtoshow, $coursecontexts) | |
105 | && has_capability('moodle/course:viewparticipants', $coursecontexts[$courseidtoshow])) { | |
106 | ||
107 | message_print_participants($coursecontexts[$courseidtoshow], $courseidtoshow, $PAGE->url, $showcontactactionlinks); | |
108 | } else { | |
109 | //shouldnt get here. User trying to access a course theyre not in perhaps. | |
02f2c7bd | 110 | add_to_log(SITEID, 'message', 'view', 'index.php', $usergroup); |
c8621a02 AD |
111 | } |
112 | } | |
113 | ||
114 | echo html_writer::start_tag('form', array('action'=>'contacts.php?advanced=1')); | |
115 | echo html_writer::start_tag('fieldset'); | |
116 | $managebuttonclass = 'hiddenelement'; | |
117 | if ($usergroup == VIEW_CONTACTS) { | |
118 | $managebuttonclass = 'visible'; | |
119 | } | |
120 | $strmanagecontacts = get_string('managecontacts','message'); | |
121 | echo html_writer::empty_tag('input', array('type'=>'submit','value'=>$strmanagecontacts,'class'=>$managebuttonclass)); | |
122 | echo html_writer::end_tag('fieldset'); | |
123 | echo html_writer::end_tag('form'); | |
124 | echo html_writer::end_tag('div'); | |
125 | } | |
126 | ||
127 | function message_print_participants($context, $courseid, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null) { | |
128 | global $DB, $USER; | |
129 | ||
130 | $participants = get_enrolled_users($context); | |
131 | ||
132 | echo '<table id="message_participants" class="boxaligncenter" cellspacing="2" cellpadding="0" border="0">'; | |
133 | ||
134 | if (!empty($titletodisplay)) { | |
135 | echo "<tr><td colspan='3' class='heading'>$titletodisplay</td></tr>"; | |
136 | } | |
137 | ||
138 | if (empty($titletodisplay)) { | |
139 | echo '<tr><td colspan="3" class="heading">'; | |
140 | echo get_string('participants'); | |
141 | echo '</td></tr>'; | |
142 | } | |
143 | ||
144 | //todo these need to come from somewhere if the course participants list is to show users with unread messages | |
145 | $iscontact = true; | |
146 | $isblocked = false; | |
147 | foreach ($participants as $participant) { | |
148 | if ($participant->id != $USER->id) { | |
149 | $participant->messagecount = 0;//todo it would be nice if the course participant could report new messages | |
150 | message_print_contactlist_user($participant, $iscontact, $isblocked, $contactselecturl, $showactionlinks); | |
151 | } | |
152 | } | |
153 | //$participants->close(); | |
154 | ||
155 | echo '</table>'; | |
156 | } | |
157 | ||
158 | function message_get_blocked_users($user1=null, &$user2=null) { | |
159 | global $DB, $USER; | |
160 | ||
161 | if (empty($user1)) { | |
162 | $user1 = $USER; | |
163 | } | |
164 | ||
165 | if (!empty($user2)) { | |
166 | $user2->isblocked = false; | |
167 | } | |
168 | ||
627bb9ec AD |
169 | $userfields = user_picture::fields('u'); |
170 | $blockeduserssql = "SELECT $userfields, u.lastaccess, count(m.id) as messagecount | |
c8621a02 AD |
171 | FROM {message_contacts} mc |
172 | JOIN {user} u ON u.id = mc.contactid | |
173 | LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = :user1id1 | |
174 | WHERE mc.userid = :user1id2 AND mc.blocked = 1 | |
175 | GROUP BY u.id, u.firstname, u.lastname, u.picture, | |
176 | u.imagealt, u.lastaccess | |
177 | ORDER BY u.firstname ASC"; | |
178 | $rs = $DB->get_recordset_sql($blockeduserssql, array('user1id1'=>$user1->id, 'user1id2'=>$user1->id)); | |
179 | ||
180 | $blockedusers = array(); | |
181 | if (!empty($rs)) { | |
182 | foreach($rs as $rd) { | |
183 | $blockedusers[] = $rd; | |
184 | ||
185 | if (!empty($user2) && $user2->id==$rd->id) { | |
186 | $user2->isblocked = true; | |
187 | } | |
188 | } | |
189 | unset($rd); | |
190 | $rs->close(); | |
191 | } | |
192 | ||
193 | return $blockedusers; | |
194 | } | |
195 | ||
196 | function message_print_blocked_users(&$blockedusers, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null) { | |
197 | global $DB, $USER; | |
198 | ||
199 | $countblocked = count($blockedusers); | |
200 | ||
201 | echo '<table id="message_contacts" class="boxaligncenter" cellspacing="2" cellpadding="0" border="0">'; | |
202 | ||
203 | if (!empty($titletodisplay)) { | |
204 | echo "<tr><td colspan='3' class='heading'>$titletodisplay</td></tr>"; | |
205 | } | |
206 | ||
207 | if ($countblocked) { | |
208 | echo '<tr><td colspan="3" class="heading">'; | |
209 | echo get_string('blockedusers', 'message', $countblocked); | |
210 | echo '</td></tr>'; | |
211 | ||
212 | foreach ($blockedusers as $blockeduser) { | |
213 | message_print_contactlist_user($blockeduser, IS_NOT_CONTACT, IS_BLOCKED, $contactselecturl, $showactionlinks); | |
214 | } | |
215 | } | |
216 | ||
217 | echo '</table>'; | |
218 | } | |
219 | ||
220 | function message_get_contacts($user1=null, &$user2=null) { | |
221 | global $DB, $CFG, $USER; | |
172186b8 | 222 | |
c8621a02 AD |
223 | if (empty($user1)) { |
224 | $user1 = $USER; | |
225 | } | |
226 | ||
227 | if (!empty($user2)) { | |
228 | $user2->iscontact = false; | |
229 | } | |
e8e2d7f1 | 230 | |
231 | $timetoshowusers = 300; //Seconds default | |
232 | if (isset($CFG->block_online_users_timetosee)) { | |
233 | $timetoshowusers = $CFG->block_online_users_timetosee * 60; | |
234 | } | |
e8e2d7f1 | 235 | |
f46b6587 | 236 | // time which a user is counting as being active since |
237 | $timefrom = time()-$timetoshowusers; | |
531e58f1 | 238 | |
f46b6587 | 239 | // people in our contactlist who are online |
240 | $onlinecontacts = array(); | |
241 | // people in our contactlist who are offline | |
242 | $offlinecontacts = array(); | |
243 | // people who are not in our contactlist but have sent us a message | |
627bb9ec AD |
244 | $strangers = array(); |
245 | ||
246 | $userfields = user_picture::fields('u'); | |
f46b6587 | 247 | |
1d422980 | 248 | // get all in our contactlist who are not blocked in our contact list |
f46b6587 | 249 | // and count messages we have waiting from each of them |
627bb9ec | 250 | $contactsql = "SELECT $userfields, u.lastaccess, count(m.id) as messagecount |
fd1cb1e8 | 251 | FROM {message_contacts} mc |
252 | JOIN {user} u ON u.id = mc.contactid | |
253 | LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = ? | |
1d422980 | 254 | WHERE mc.userid = ? AND mc.blocked = 0 |
fd1cb1e8 | 255 | GROUP BY u.id, u.firstname, u.lastname, u.picture, |
256 | u.imagealt, u.lastaccess | |
bbe973da | 257 | ORDER BY u.firstname ASC"; |
fd1cb1e8 | 258 | |
c8621a02 | 259 | if ($rs = $DB->get_recordset_sql($contactsql, array($user1->id, $user1->id))){ |
fd1cb1e8 | 260 | foreach($rs as $rd){ |
f46b6587 | 261 | |
262 | if($rd->lastaccess >= $timefrom){ | |
263 | // they have been active recently, so are counted online | |
264 | $onlinecontacts[] = $rd; | |
c8621a02 | 265 | |
f46b6587 | 266 | }else{ |
267 | $offlinecontacts[] = $rd; | |
e8e2d7f1 | 268 | } |
c8621a02 AD |
269 | |
270 | if (!empty($user2) && $user2->id==$rd->id) { | |
271 | $user2->iscontact = true; | |
272 | } | |
e8e2d7f1 | 273 | } |
f46b6587 | 274 | unset($rd); |
fd1cb1e8 | 275 | $rs->close(); |
f46b6587 | 276 | } |
277 | ||
f46b6587 | 278 | // get messages from anyone who isn't in our contact list and count the number |
279 | // of messages we have from each of them | |
627bb9ec | 280 | $strangersql = "SELECT $userfields, u.lastaccess, count(m.id) as messagecount |
fd1cb1e8 | 281 | FROM {message} m |
282 | JOIN {user} u ON u.id = m.useridfrom | |
283 | LEFT OUTER JOIN {message_contacts} mc ON mc.contactid = m.useridfrom AND mc.userid = m.useridto | |
1d422980 | 284 | WHERE mc.id IS NULL AND m.useridto = ? |
fd1cb1e8 | 285 | GROUP BY u.id, u.firstname, u.lastname, u.picture, |
286 | u.imagealt, u.lastaccess | |
bbe973da | 287 | ORDER BY u.firstname ASC"; |
fd1cb1e8 | 288 | |
289 | if($rs = $DB->get_recordset_sql($strangersql, array($USER->id))){ | |
290 | foreach($rs as $rd){ | |
f46b6587 | 291 | $strangers[] = $rd; |
292 | } | |
293 | unset($rd); | |
fd1cb1e8 | 294 | $rs->close(); |
e8e2d7f1 | 295 | } |
296 | ||
c8621a02 AD |
297 | return array($onlinecontacts, $offlinecontacts, $strangers); |
298 | } | |
299 | ||
300 | function message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $refresh=true, $contactselecturl=null, $minmessages=0, $showactionlinks=true, $titletodisplay=null) { | |
301 | global $CFG, $PAGE, $OUTPUT; | |
302 | ||
f46b6587 | 303 | $countonlinecontacts = count($onlinecontacts); |
304 | $countofflinecontacts = count($offlinecontacts); | |
305 | $countstrangers = count($strangers); | |
306 | ||
a1157dda | 307 | if ($countonlinecontacts + $countofflinecontacts == 0) { |
669be60c | 308 | echo '<div class="heading">'; |
65ef518b | 309 | print_string('contactlistempty', 'message'); |
a1157dda | 310 | echo '</div>'; |
65c7853e AD |
311 | //echo '<div class="note">'; |
312 | //print_string('addsomecontacts', 'message', message_remove_url_params($PAGE->url).'?tab=search'); | |
313 | //echo '</div>'; | |
65ef518b | 314 | } |
315 | ||
04b687e8 | 316 | echo '<table id="message_contacts" class="boxaligncenter" cellspacing="2" cellpadding="0" border="0">'; |
531e58f1 | 317 | |
c8621a02 AD |
318 | if (!empty($titletodisplay)) { |
319 | echo "<tr><td colspan='3' class='heading'>$titletodisplay</td></tr>"; | |
320 | } | |
321 | ||
f46b6587 | 322 | if($countonlinecontacts) { |
323 | /// print out list of online contacts | |
531e58f1 | 324 | |
c8621a02 AD |
325 | if (empty($titletodisplay)) { |
326 | echo '<tr><td colspan="3" class="heading">'; | |
327 | echo get_string('onlinecontacts', 'message', $countonlinecontacts); | |
328 | echo '</td></tr>'; | |
329 | } | |
531e58f1 | 330 | |
f46b6587 | 331 | foreach ($onlinecontacts as $contact) { |
c8621a02 AD |
332 | if ($minmessages==0 || $contact->messagecount>=$minmessages) { |
333 | message_print_contactlist_user($contact, IS_CONTACT, IS_NOT_BLOCKED, $contactselecturl, $showactionlinks); | |
334 | } | |
65ef518b | 335 | } |
f46b6587 | 336 | } |
531e58f1 | 337 | |
f46b6587 | 338 | if ($countofflinecontacts) { |
339 | /// print out list of offline contacts | |
531e58f1 | 340 | |
c8621a02 AD |
341 | if (empty($titletodisplay)) { |
342 | echo '<tr><td colspan="3" class="heading">'; | |
343 | echo get_string('offlinecontacts', 'message', $countofflinecontacts); | |
344 | echo '</td></tr>'; | |
345 | } | |
576ad290 | 346 | |
f46b6587 | 347 | foreach ($offlinecontacts as $contact) { |
c8621a02 AD |
348 | if ($minmessages==0 || $contact->messagecount>=$minmessages) { |
349 | message_print_contactlist_user($contact, IS_CONTACT, IS_NOT_BLOCKED, $contactselecturl, $showactionlinks); | |
350 | } | |
65ef518b | 351 | } |
f46b6587 | 352 | echo '<tr><td colspan="3"> </td></tr>'; |
353 | } | |
65ef518b | 354 | |
f46b6587 | 355 | /// print out list of incoming contacts |
356 | if ($countstrangers) { | |
357 | echo '<tr><td colspan="3" class="heading">'; | |
358 | echo get_string('incomingcontacts', 'message', $countstrangers); | |
359 | echo '</td></tr>'; | |
531e58f1 | 360 | |
f46b6587 | 361 | foreach ($strangers as $stranger) { |
65fbace7 | 362 | if ($minmessages==0 || $stranger->messagecount>=$minmessages) { |
c8621a02 AD |
363 | message_print_contactlist_user($stranger, IS_NOT_CONTACT, IS_NOT_BLOCKED, $contactselecturl, $showactionlinks); |
364 | } | |
669be60c | 365 | } |
f46b6587 | 366 | } |
531e58f1 | 367 | |
f46b6587 | 368 | echo '</table>'; |
65ef518b | 369 | |
f46b6587 | 370 | if ($countstrangers && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help |
371 | echo '<div class="note">('; | |
372 | print_string('addsomecontactsincoming', 'message'); | |
373 | echo ')</div>'; | |
65ef518b | 374 | } |
b9ee0638 | 375 | |
531e58f1 | 376 | echo '<br />'; |
a9d9b46a | 377 | |
c8621a02 AD |
378 | if ($refresh) { |
379 | $PAGE->requires->js_init_call('M.core_message.init_refresh_page', array(60*1000, $PAGE->url->out(false))); | |
a1b53dcf | 380 | |
c8621a02 AD |
381 | echo $OUTPUT->container_start('messagejsautorefresh note center'); |
382 | echo get_string('pagerefreshes', 'message', $CFG->message_contacts_refresh); | |
383 | echo $OUTPUT->container_end(); | |
384 | } | |
a1b53dcf | 385 | |
7cb1a1ad | 386 | echo $OUTPUT->container_start('messagejsmanualrefresh aligncenter'); |
c8621a02 | 387 | echo $OUTPUT->single_button(message_remove_url_params($PAGE->url), get_string('refresh')); |
7cb1a1ad | 388 | echo $OUTPUT->container_end(); |
e8e2d7f1 | 389 | } |
390 | ||
43240ea1 AD |
391 | function message_print_usergroup_selector($usergroup, &$courses, &$coursecontexts, $countunreadtotal, $countblocked, $strunreadmessages) { |
392 | $strblockedusers = null; | |
65c7853e | 393 | $options = array(); |
c8621a02 AD |
394 | |
395 | if ($countunreadtotal>0) { //if there are unread messages | |
65c7853e | 396 | $options[VIEW_UNREAD_MESSAGES] = $strunreadmessages; |
c8621a02 AD |
397 | } |
398 | ||
399 | if ($countblocked>0) { | |
400 | $strblockedusers = get_string('blockedusers','message', $countblocked); | |
65c7853e | 401 | $options[VIEW_BLOCKED] = $strblockedusers; |
c8621a02 AD |
402 | } |
403 | ||
404 | $strcontacts = get_string('mycontacts', 'message'); | |
405 | $options[VIEW_CONTACTS] = $strcontacts; | |
406 | ||
407 | if (!empty($courses)) { | |
408 | $courses_options = array(); | |
409 | ||
410 | foreach($courses as $course) { | |
411 | if (has_capability('moodle/course:viewparticipants', $coursecontexts[$course->id])) { | |
412 | $courses_options[VIEW_COURSE.$course->id] = $course->fullname; | |
413 | } | |
414 | } | |
415 | ||
416 | if (!empty($courses_options)) { | |
417 | $options[] = array(get_string('courses')=>$courses_options); | |
418 | } | |
419 | } | |
420 | ||
421 | echo html_writer::start_tag('form', array('id'=>'usergroupform','method'=>'get','action'=>'')); | |
229f4e92 AD |
422 | echo html_writer::start_tag('fieldset'); |
423 | echo html_writer::select($options, 'usergroup', $usergroup, false, array('id'=>'usergroup','onchange'=>'this.form.submit()')); | |
424 | echo html_writer::end_tag('fieldset'); | |
c8621a02 AD |
425 | echo html_writer::end_tag('form'); |
426 | } | |
427 | ||
428 | function message_get_course_contexts(&$courses) { | |
429 | $coursecontexts = array(); | |
430 | ||
431 | foreach($courses as $course) { | |
432 | $coursecontexts[$course->id] = get_context_instance(CONTEXT_COURSE, $course->id); | |
433 | } | |
434 | ||
435 | return $coursecontexts; | |
436 | } | |
437 | ||
cee92282 AD |
438 | /** |
439 | * strip off action parameters like 'removecontact' | |
440 | */ | |
c8621a02 | 441 | function message_remove_url_params($moodleurl) { |
c8621a02 AD |
442 | $newurl = new moodle_url($moodleurl); |
443 | $newurl->remove_params('addcontact','removecontact','blockcontact','unblockcontact'); | |
444 | return $newurl->out(); | |
445 | } | |
446 | ||
e8e2d7f1 | 447 | |
e8e2d7f1 | 448 | /// $messagearray is an array of objects |
449 | /// $field is a valid property of object | |
450 | /// $value is the value $field should equal to be counted | |
451 | /// if $field is empty then return count of the whole array | |
452 | /// if $field is non-existent then return 0; | |
453 | function message_count_messages($messagearray, $field='', $value='') { | |
454 | if (!is_array($messagearray)) return 0; | |
455 | if ($field == '' or empty($messagearray)) return count($messagearray); | |
531e58f1 | 456 | |
e8e2d7f1 | 457 | $count = 0; |
458 | foreach ($messagearray as $message) { | |
459 | $count += ($message->$field == $value) ? 1 : 0; | |
460 | } | |
461 | return $count; | |
172186b8 | 462 | } |
463 | ||
c8621a02 AD |
464 | /** |
465 | * Returns the count of unread messages for user. Either from a specific user or from all users. | |
466 | * @global <type> $USER | |
467 | * @global <type> $DB | |
468 | * @param object $user1 the first user. Defaults to $USER | |
469 | * @param object $user2 the second user. If null this function will count all of user 1's unread messages. | |
470 | * @return int the count of $user1's unread messages | |
471 | */ | |
472 | function message_count_unread_messages($user1=null, $user2=null) { | |
473 | global $USER, $DB; | |
e8e2d7f1 | 474 | |
c8621a02 AD |
475 | if (empty($user1)) { |
476 | $user1 = $USER; | |
477 | } | |
478 | ||
479 | if (!empty($user2)) { | |
480 | return $DB->count_records_select('message', "useridto = ? AND useridfrom = ?", | |
481 | array($user1->id, $user2->id), "COUNT('id')"); | |
482 | } else { | |
483 | return $DB->count_records_select('message', "useridto = ?", | |
484 | array($user1->id), "COUNT('id')"); | |
485 | } | |
486 | } | |
487 | ||
488 | function message_count_blocked_users($user1=null) { | |
489 | global $USER, $DB; | |
490 | ||
491 | if (empty($user1)) { | |
492 | $user1 = $USER; | |
493 | } | |
494 | ||
495 | $sql = "SELECT count(mc.id) | |
496 | FROM {message_contacts} mc | |
497 | WHERE mc.userid = :userid AND mc.blocked = 1"; | |
498 | $params = array('userid'=>$user1->id); | |
499 | ||
500 | return $DB->count_records_sql($sql, $params); | |
501 | } | |
502 | ||
503 | /** | |
504 | * | |
505 | * @global <type> $USER | |
506 | * @global <type> $PAGE | |
507 | * @global <type> $OUTPUT | |
508 | * @param boolean advancedsearch show basic or advanced search form | |
509 | * @return boolean was a search performed? | |
510 | */ | |
511 | function message_print_search($advancedsearch = false, $user1=null) { | |
512 | global $USER, $PAGE, $OUTPUT; | |
513 | ||
514 | $frm = data_submitted(); | |
515 | ||
516 | $doingsearch = false; | |
517 | if ($frm) { | |
518 | $doingsearch = !empty($frm->combinedsubmit) || !empty($frm->keywords) || (!empty($frm->personsubmit) and !empty($frm->name)); | |
519 | } | |
531e58f1 | 520 | |
c8621a02 AD |
521 | if (!empty($frm->combinedsearch)) { |
522 | $combinedsearchstring = $frm->combinedsearch; | |
523 | } else { | |
524 | $combinedsearchstring = get_string('searchcombined','message').'...'; | |
525 | } | |
531e58f1 | 526 | |
c8621a02 | 527 | $PAGE->requires->js_init_call('M.core_message.init_search_page', array($combinedsearchstring)); |
531e58f1 | 528 | |
c8621a02 AD |
529 | if ($doingsearch) { |
530 | if ($advancedsearch) { | |
531 | ||
532 | $messagesearch = ''; | |
533 | if (!empty($frm->keywords)) { | |
534 | $messagesearch = $frm->keywords; | |
535 | } | |
536 | $personsearch = ''; | |
537 | if (!empty($frm->name)) { | |
538 | $personsearch = $frm->name; | |
539 | } | |
540 | include('search_advanced.html'); | |
541 | } else { | |
542 | include('search.html'); | |
543 | } | |
544 | ||
545 | $showicontext = false; | |
546 | message_print_search_results($frm, $showicontext, $user1); | |
547 | ||
548 | return true; | |
172186b8 | 549 | } else { |
1ac41751 | 550 | /* |
531e58f1 | 551 | /// unfinished buggy code disabled in search.html anyway |
d76a5a7f | 552 | // find all courses this use has readallmessages capabilities in |
553 | if ($teachers = get_user_capability_course('moodle/site:readallmessages')) { | |
172186b8 | 554 | $courses = get_courses('all', 'c.sortorder ASC', 'c.id, c.shortname'); |
555 | $cs = '<select name="courseselect">'; | |
556 | foreach ($teachers as $tcourse) { | |
d76a5a7f | 557 | $cs .= "<option value=\"$tcourse->course\">".$courses[$tcourse->id]->shortname."</option>\n"; |
172186b8 | 558 | } |
559 | $cs .= '</select>'; | |
560 | } | |
1ac41751 | 561 | */ |
c8621a02 AD |
562 | |
563 | if ($advancedsearch) { | |
564 | $personsearch = $messagesearch = ''; | |
565 | include('search_advanced.html'); | |
566 | } else { | |
567 | include('search.html'); | |
568 | } | |
569 | return false; | |
172186b8 | 570 | } |
571 | } | |
572 | ||
573 | function message_print_settings() { | |
54b16692 | 574 | global $USER, $OUTPUT; |
531e58f1 | 575 | |
d6b4fed0 | 576 | if ($frm = data_submitted() and confirm_sesskey()) { |
acef58f4 | 577 | |
e8e2d7f1 | 578 | $pref = array(); |
e8e2d7f1 | 579 | $pref['message_beepnewmessage'] = (isset($frm->beepnewmessage)) ? '1' : '0'; |
3f85157b | 580 | $pref['message_blocknoncontacts'] = (isset($frm->blocknoncontacts)) ? '1' : '0'; |
e8e2d7f1 | 581 | |
582 | set_user_preferences($pref); | |
531e58f1 | 583 | |
c8621a02 | 584 | redirect(message_remove_url_params($PAGE->url), get_string('settingssaved', 'message'), 1); |
e8e2d7f1 | 585 | } |
586 | ||
fd7006f6 | 587 | $cbbeepnewmessage = (get_user_preferences('message_beepnewmessage', 0) == '1') ? 'checked="checked"' : ''; |
3f85157b | 588 | $cbblocknoncontacts = (get_user_preferences('message_blocknoncontacts', 0) == '1') ? 'checked="checked"' : ''; |
531e58f1 | 589 | |
e8e2d7f1 | 590 | include('settings.html'); |
591 | } | |
592 | ||
593 | ||
594 | ||
595 | function message_add_contact($contactid, $blocked=0) { | |
fd1cb1e8 | 596 | global $USER, $DB; |
531e58f1 | 597 | |
fd1cb1e8 | 598 | if (!$DB->record_exists('user', array('id'=>$contactid))) { // invalid userid |
e8e2d7f1 | 599 | return false; |
600 | } | |
531e58f1 | 601 | |
fd1cb1e8 | 602 | if (($contact = $DB->get_record('message_contacts', array('userid'=>$USER->id, 'contactid'=>$contactid))) !== false) { |
e8e2d7f1 | 603 | /// record already exists - we may be changing blocking status |
531e58f1 | 604 | |
e8e2d7f1 | 605 | if ($contact->blocked !== $blocked) { |
606 | /// change to blocking status | |
607 | $contact->blocked = $blocked; | |
fd1cb1e8 | 608 | return $DB->update_record('message_contacts', $contact); |
e8e2d7f1 | 609 | } else { |
610 | /// no changes to blocking status | |
611 | return true; | |
612 | } | |
531e58f1 | 613 | |
172186b8 | 614 | } else { |
e8e2d7f1 | 615 | /// new contact record |
616 | unset($contact); | |
617 | $contact->userid = $USER->id; | |
618 | $contact->contactid = $contactid; | |
619 | $contact->blocked = $blocked; | |
fd1cb1e8 | 620 | return $DB->insert_record('message_contacts', $contact, false); |
172186b8 | 621 | } |
622 | } | |
623 | ||
e8e2d7f1 | 624 | function message_remove_contact($contactid) { |
fd1cb1e8 | 625 | global $USER, $DB; |
626 | return $DB->delete_records('message_contacts', array('userid'=>$USER->id, 'contactid'=>$contactid)); | |
e8e2d7f1 | 627 | } |
628 | ||
629 | function message_unblock_contact($contactid) { | |
fd1cb1e8 | 630 | global $USER, $DB; |
631 | return $DB->delete_records('message_contacts', array('userid'=>$USER->id, 'contactid'=>$contactid)); | |
e8e2d7f1 | 632 | } |
633 | ||
634 | function message_block_contact($contactid) { | |
635 | return message_add_contact($contactid, 1); | |
636 | } | |
637 | ||
638 | function message_get_contact($contactid) { | |
fd1cb1e8 | 639 | global $USER, $DB; |
640 | return $DB->get_record('message_contacts', array('userid'=>$USER->id, 'contactid'=>$contactid)); | |
e8e2d7f1 | 641 | } |
531e58f1 | 642 | |
e8e2d7f1 | 643 | |
644 | ||
c8621a02 AD |
645 | function message_print_search_results($frm, $showicontext=false, $user1=null) { |
646 | global $USER, $CFG, $DB, $OUTPUT, $PAGE; | |
647 | ||
648 | if (empty($user1)) { | |
649 | $user1 = $USER; | |
650 | } | |
e8e2d7f1 | 651 | |
c8621a02 | 652 | echo '<div class="mdl-left">'; |
e8e2d7f1 | 653 | |
c8621a02 AD |
654 | $personsearch = false; |
655 | $personsearchstring = null; | |
e8e2d7f1 | 656 | if (!empty($frm->personsubmit) and !empty($frm->name)) { |
c8621a02 AD |
657 | $personsearch = true; |
658 | $personsearchstring = $frm->name; | |
659 | } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) { | |
660 | $personsearch = true; | |
661 | $personsearchstring = $frm->combinedsearch; | |
662 | } | |
531e58f1 | 663 | |
c8621a02 AD |
664 | /// search for person |
665 | if ($personsearch) { | |
b71d4dd2 | 666 | if (optional_param('mycourses', 0, PARAM_BOOL)) { |
e8e2d7f1 | 667 | $users = array(); |
df997f84 | 668 | $mycourses = enrol_get_my_courses(); |
e8e2d7f1 | 669 | foreach ($mycourses as $mycourse) { |
c8621a02 | 670 | if (is_array($susers = message_search_users($mycourse->id, $personsearchstring))) { |
e8e2d7f1 | 671 | foreach ($susers as $suser) $users[$suser->id] = $suser; |
672 | } | |
673 | } | |
674 | } else { | |
c8621a02 | 675 | $users = message_search_users(SITEID, $personsearchstring); |
e8e2d7f1 | 676 | } |
677 | ||
678 | if (!empty($users)) { | |
679 | echo '<strong>'.get_string('userssearchresults', 'message', count($users)).'</strong>'; | |
c8621a02 | 680 | echo '<table class="messagesearchresults">'; |
e8e2d7f1 | 681 | foreach ($users as $user) { |
531e58f1 | 682 | |
7390832c | 683 | if ( $user->contactlistid ) { |
684 | if ($user->blocked == 0) { /// not blocked | |
c8621a02 AD |
685 | $strcontact = message_contact_link($user->id, 'remove', true, null, $showicontext); |
686 | $strblock = message_contact_link($user->id, 'block', true, null, $showicontext); | |
e8e2d7f1 | 687 | } else { // blocked |
c8621a02 AD |
688 | $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext); |
689 | $strblock = message_contact_link($user->id, 'unblock', true, null, $showicontext); | |
e8e2d7f1 | 690 | } |
e8e2d7f1 | 691 | } else { |
c8621a02 AD |
692 | $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext); |
693 | $strblock = message_contact_link($user->id, 'block', true, null, $showicontext); | |
694 | } | |
695 | ||
696 | //should we show just the icon or icon and text? | |
697 | $histicontext = 'icon'; | |
698 | if ($showicontext) { | |
699 | $histicontext = 'both'; | |
e8e2d7f1 | 700 | } |
c8621a02 | 701 | $strhistory = message_history_link($USER->id, $user->id, true, '', '', $histicontext); |
531e58f1 | 702 | |
669be60c | 703 | echo '<tr><td class="pix">'; |
812dbaf7 | 704 | echo $OUTPUT->user_picture($user, array('size'=>20, 'courseid'=>SITEID)); |
e8e2d7f1 | 705 | echo '</td>'; |
669be60c | 706 | echo '<td class="contact">'; |
40a26286 | 707 | $popupoptions = array( |
c8621a02 AD |
708 | 'height' => MESSAGE_DISCUSSION_HEIGHT, |
709 | 'width' => MESSAGE_DISCUSSION_WIDTH, | |
40a26286 | 710 | 'menubar' => false, |
711 | 'location' => false, | |
712 | 'status' => true, | |
713 | 'scrollbars' => true, | |
714 | 'resizable' => true); | |
715 | ||
c8621a02 | 716 | //$link = new moodle_url("/message/discussion.php?id=$user->id"); |
02f2c7bd | 717 | $link = new moodle_url("/message/index.php?id=$user->id"); |
c8621a02 AD |
718 | //$action = new popup_action('click', $link, "message_$user->id", $popupoptions); |
719 | $action = null; | |
9bf16314 | 720 | echo $OUTPUT->action_link($link, fullname($user), $action, array('title'=>get_string('sendmessageto', 'message', fullname($user)))); |
576ad290 | 721 | |
e8e2d7f1 | 722 | echo '</td>'; |
531e58f1 | 723 | |
669be60c | 724 | echo '<td class="link">'.$strcontact.'</td>'; |
725 | echo '<td class="link">'.$strblock.'</td>'; | |
726 | echo '<td class="link">'.$strhistory.'</td>'; | |
e8e2d7f1 | 727 | echo '</tr>'; |
728 | } | |
729 | echo '</table>'; | |
531e58f1 | 730 | |
e8e2d7f1 | 731 | } else { |
c8621a02 | 732 | echo get_string('userssearchresults', 'message', 0).'<br /><br />'; |
e8e2d7f1 | 733 | } |
c8621a02 | 734 | } |
531e58f1 | 735 | |
c8621a02 AD |
736 | // search messages for keywords |
737 | $messagesearch = false; | |
738 | $messagesearchstring = null; | |
739 | if (!empty($frm->keywords)) { | |
740 | $messagesearch = true; | |
741 | $messagesearchstring = clean_text(trim($frm->keywords)); | |
742 | } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) { | |
743 | $messagesearch = true; | |
744 | $messagesearchstring = clean_text(trim($frm->combinedsearch)); | |
745 | } | |
531e58f1 | 746 | |
c8621a02 AD |
747 | if ($messagesearch) { |
748 | if ($messagesearchstring) { | |
749 | $keywords = explode(' ', $messagesearchstring); | |
ff49c389 | 750 | } else { |
751 | $keywords = array(); | |
752 | } | |
e8e2d7f1 | 753 | $tome = false; |
754 | $fromme = false; | |
755 | $courseid = 'none'; | |
756 | ||
c8621a02 AD |
757 | if (empty($frm->keywordsoption)) { |
758 | $frm->keywordsoption = 'allmine'; | |
759 | } | |
760 | ||
e8e2d7f1 | 761 | switch ($frm->keywordsoption) { |
762 | case 'tome': | |
763 | $tome = true; | |
764 | break; | |
765 | case 'fromme': | |
766 | $fromme = true; | |
767 | break; | |
768 | case 'allmine': | |
769 | $tome = true; | |
770 | $fromme = true; | |
771 | break; | |
772 | case 'allusers': | |
773 | $courseid = SITEID; | |
774 | break; | |
775 | case 'courseusers': | |
776 | $courseid = $frm->courseid; | |
777 | break; | |
778 | default: | |
779 | $tome = true; | |
780 | $fromme = true; | |
781 | } | |
782 | ||
783 | if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) { | |
531e58f1 | 784 | |
e8e2d7f1 | 785 | /// get a list of contacts |
fd1cb1e8 | 786 | if (($contacts = $DB->get_records('message_contacts', array('userid'=>$USER->id), '', 'contactid, blocked') ) === false) { |
082864f9 | 787 | $contacts = array(); |
788 | } | |
e8e2d7f1 | 789 | |
082864f9 | 790 | /// print heading with number of results |
c8621a02 | 791 | echo '<p class="heading">'.get_string('keywordssearchresults', 'message', count($messages)).' ("'.s($messagesearchstring).'")</p>'; |
082864f9 | 792 | |
793 | /// print table headings | |
c8621a02 | 794 | echo '<table class="messagesearchresults" cellspacing="0">'; |
082864f9 | 795 | echo '<tr>'; |
fd7006f6 | 796 | echo '<td><strong>'.get_string('from').'</strong></td>'; |
797 | echo '<td><strong>'.get_string('to').'</strong></td>'; | |
798 | echo '<td><strong>'.get_string('message', 'message').'</strong></td>'; | |
799 | echo '<td><strong>'.get_string('timesent', 'message').'</strong></td>'; | |
082864f9 | 800 | echo "</tr>\n"; |
801 | ||
802 | $blockedcount = 0; | |
54d8f804 | 803 | $dateformat = get_string('strftimedatetimeshort'); |
38c6a928 | 804 | $strcontext = get_string('context', 'message'); |
e8e2d7f1 | 805 | foreach ($messages as $message) { |
082864f9 | 806 | |
807 | /// ignore messages to and from blocked users unless $frm->includeblocked is set | |
b71d4dd2 | 808 | if (!optional_param('includeblocked', 0, PARAM_BOOL) and ( |
082864f9 | 809 | ( isset($contacts[$message->useridfrom]) and ($contacts[$message->useridfrom]->blocked == 1)) or |
810 | ( isset($contacts[$message->useridto] ) and ($contacts[$message->useridto]->blocked == 1)) | |
811 | ) | |
812 | ) { | |
813 | $blockedcount ++; | |
e8e2d7f1 | 814 | continue; |
815 | } | |
531e58f1 | 816 | |
082864f9 | 817 | /// load up user to record |
818 | if ($message->useridto !== $USER->id) { | |
fd1cb1e8 | 819 | $userto = $DB->get_record('user', array('id'=>$message->useridto)); |
531e58f1 | 820 | $tocontact = (array_key_exists($message->useridto, $contacts) and |
082864f9 | 821 | ($contacts[$message->useridto]->blocked == 0) ); |
531e58f1 | 822 | $toblocked = (array_key_exists($message->useridto, $contacts) and |
082864f9 | 823 | ($contacts[$message->useridto]->blocked == 1) ); |
824 | } else { | |
825 | $userto = false; | |
826 | $tocontact = false; | |
827 | $toblocked = false; | |
828 | } | |
531e58f1 | 829 | |
082864f9 | 830 | /// load up user from record |
831 | if ($message->useridfrom !== $USER->id) { | |
fd1cb1e8 | 832 | $userfrom = $DB->get_record('user', array('id'=>$message->useridfrom)); |
531e58f1 | 833 | $fromcontact = (array_key_exists($message->useridfrom, $contacts) and |
082864f9 | 834 | ($contacts[$message->useridfrom]->blocked == 0) ); |
531e58f1 | 835 | $fromblocked = (array_key_exists($message->useridfrom, $contacts) and |
082864f9 | 836 | ($contacts[$message->useridfrom]->blocked == 1) ); |
837 | } else { | |
838 | $userfrom = false; | |
839 | $fromcontact = false; | |
840 | $fromblocked = false; | |
841 | } | |
842 | ||
e520509a | 843 | /// find date string for this message |
844 | $date = usergetdate($message->timecreated); | |
845 | $datestring = $date['year'].$date['mon'].$date['mday']; | |
846 | ||
082864f9 | 847 | /// print out message row |
848 | echo '<tr valign="top">'; | |
669be60c | 849 | echo '<td class="contact">'; |
c8621a02 | 850 | message_print_user($userfrom, $fromcontact, $fromblocked, $showicontext); |
e8e2d7f1 | 851 | echo '</td>'; |
669be60c | 852 | echo '<td class="contact">'; |
c8621a02 | 853 | message_print_user($userto, $tocontact, $toblocked, $showicontext); |
e8e2d7f1 | 854 | echo '</td>'; |
3d143595 | 855 | echo '<td class="summary">'.message_get_fragment($message->fullmessage, $keywords); |
669be60c | 856 | echo '<br /><div class="link">'; |
c8621a02 AD |
857 | |
858 | //find the user involved that isnt the current user | |
859 | $user2id = null; | |
860 | if ($user1->id == $message->useridto) { | |
861 | $user2id = $message->useridfrom; | |
862 | } else { | |
863 | $user2id = $message->useridto; | |
864 | } | |
865 | message_history_link($user1->id, $user2id, false, | |
866 | $messagesearchstring, 'm'.$message->id, $strcontext); | |
f520438c | 867 | echo '</div>'; |
e520509a | 868 | echo '</td>'; |
669be60c | 869 | echo '<td class="date">'.userdate($message->timecreated, $dateformat).'</td>'; |
082864f9 | 870 | echo "</tr>\n"; |
871 | } | |
531e58f1 | 872 | |
e8e2d7f1 | 873 | |
e520509a | 874 | if ($blockedcount > 0) { |
875 | echo '<tr><td colspan="4" align="center">'.get_string('blockedmessages', 'message', $blockedcount).'</td></tr>'; | |
876 | } | |
e8e2d7f1 | 877 | echo '</table>'; |
531e58f1 | 878 | |
e8e2d7f1 | 879 | } else { |
c8621a02 | 880 | echo '<p class="heading">'.get_string('keywordssearchresults', 'message', 0).' ("'.s($messagesearchstring).'")</p>'; |
e8e2d7f1 | 881 | } |
c8621a02 | 882 | } |
e8e2d7f1 | 883 | |
c8621a02 AD |
884 | if (!$personsearch && !$messagesearch) { |
885 | //they didnt enter any search terms | |
aa9a6867 | 886 | echo $OUTPUT->notification(get_string('emptysearchstring', 'message')); |
e8e2d7f1 | 887 | } |
888 | ||
082864f9 | 889 | echo '<br />'; |
c8621a02 | 890 | echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('tab' => 'search')), get_string('newsearch', 'message')); |
e8e2d7f1 | 891 | |
892 | echo '</div>'; | |
893 | } | |
894 | ||
895 | ||
c8621a02 | 896 | function message_print_user ($user=false, $iscontact=false, $isblocked=false, $includeicontext=false) { |
7cb1a1ad | 897 | global $USER, $OUTPUT; |
1d422980 | 898 | |
082864f9 | 899 | if ($user === false) { |
812dbaf7 | 900 | echo $OUTPUT->user_picture($USER, array('size'=>20, 'courseid'=>SITEID)); |
082864f9 | 901 | } else { |
c8621a02 | 902 | echo $OUTPUT->user_picture($USER, array('size'=>20, 'courseid'=>SITEID)); |
e520509a | 903 | echo ' '; |
c8621a02 AD |
904 | |
905 | $return = false; | |
906 | $script = null; | |
082864f9 | 907 | if ($iscontact) { |
c8621a02 | 908 | message_contact_link($user->id, 'remove', $return, $script, $includeicontext); |
082864f9 | 909 | } else { |
c8621a02 | 910 | message_contact_link($user->id, 'add', $return, $script, $includeicontext); |
082864f9 | 911 | } |
912 | echo ' '; | |
913 | if ($isblocked) { | |
c8621a02 | 914 | message_contact_link($user->id, 'unblock', $return, $script, $includeicontext); |
082864f9 | 915 | } else { |
c8621a02 | 916 | message_contact_link($user->id, 'block', $return, $script, $includeicontext); |
082864f9 | 917 | } |
e520509a | 918 | echo '<br />'; |
576ad290 | 919 | |
40a26286 | 920 | $popupoptions = array( |
c8621a02 AD |
921 | 'height' => MESSAGE_DISCUSSION_HEIGHT, |
922 | 'width' => MESSAGE_DISCUSSION_WIDTH, | |
40a26286 | 923 | 'menubar' => false, |
924 | 'location' => false, | |
925 | 'status' => true, | |
926 | 'scrollbars' => true, | |
927 | 'resizable' => true); | |
928 | ||
c8621a02 | 929 | //$link = new moodle_url("/message/discussion.php?id=$user->id"); |
02f2c7bd | 930 | $link = new moodle_url("/message/index.php?id=$user->id"); |
c8621a02 AD |
931 | //$action = new popup_action('click', $link, "message_$user->id", $popupoptions); |
932 | $action = null; | |
9bf16314 | 933 | echo $OUTPUT->action_link($link, fullname($user), $action, array('title'=>get_string('sendmessageto', 'message', fullname($user)))); |
40a26286 | 934 | |
082864f9 | 935 | } |
936 | } | |
937 | ||
938 | ||
939 | /// linktype can be: add, remove, block, unblock | |
c8621a02 AD |
940 | function message_contact_link($userid, $linktype='add', $return=false, $script=null, $text=false, $icon=true) { |
941 | global $USER, $CFG, $OUTPUT, $PAGE; | |
e520509a | 942 | |
6898e848 | 943 | //hold onto the strings as we're probably creating a bunch of links |
e520509a | 944 | static $str; |
945 | ||
c8621a02 | 946 | if (empty($script)) { |
cee92282 AD |
947 | //strip off previous action params like 'removecontact' |
948 | $theurl = message_remove_url_params($PAGE->url); | |
949 | ||
950 | if (strpos($theurl, '?')===false) { | |
951 | $script = $theurl.'?tab=contacts'; | |
952 | } else { | |
953 | $script = $theurl.'&tab=contacts'; | |
954 | } | |
c8621a02 AD |
955 | } |
956 | ||
e520509a | 957 | if (empty($str->blockcontact)) { |
958 | $str->blockcontact = get_string('blockcontact', 'message'); | |
959 | $str->unblockcontact = get_string('unblockcontact', 'message'); | |
960 | $str->removecontact = get_string('removecontact', 'message'); | |
961 | $str->addcontact = get_string('addcontact', 'message'); | |
962 | } | |
963 | ||
5d6b319b | 964 | $command = $linktype.'contact'; |
965 | $string = $str->{$command}; | |
6898e848 AD |
966 | |
967 | $safealttext = s($string); | |
968 | ||
969 | $safestring = ''; | |
970 | if (!empty($text)) { | |
971 | $safestring = $safealttext; | |
c8621a02 AD |
972 | } |
973 | ||
974 | $img = ''; | |
975 | if ($icon) { | |
6898e848 AD |
976 | $iconpath = null; |
977 | switch ($linktype) { | |
978 | case 'block': | |
979 | $iconpath = 't/block'; | |
980 | break; | |
981 | case 'unblock': | |
982 | $iconpath = 't/userblue'; | |
983 | break; | |
984 | case 'remove': | |
985 | $iconpath = 'i/cross_red_big'; | |
986 | break; | |
987 | case 'add': | |
988 | default: | |
989 | $iconpath = 't/addgreen'; | |
990 | } | |
991 | ||
992 | $img = '<img src="'.$OUTPUT->pix_url($iconpath).'" class="iconsmall" alt="'.$safealttext.'" />'; | |
082864f9 | 993 | } |
5d6b319b | 994 | |
c8621a02 | 995 | $output = '<span class="'.$linktype.'contact">'. |
5d6b319b | 996 | '<a href="'.$script.'&'.$command.'='.$userid. |
6898e848 | 997 | '&sesskey='.sesskey().'" title="'.$safestring.'">'. |
c8621a02 | 998 | $img. |
6898e848 | 999 | $safestring.'</a></span>'; |
5d6b319b | 1000 | |
e520509a | 1001 | if ($return) { |
1002 | return $output; | |
082864f9 | 1003 | } else { |
e520509a | 1004 | echo $output; |
082864f9 | 1005 | return true; |
1006 | } | |
1007 | } | |
1008 | ||
c8621a02 | 1009 | function message_history_link($userid1, $userid2, $returnstr=false, $keywords='', $position='', $linktext='') { |
f2a1963c | 1010 | global $USER, $CFG, $OUTPUT; |
62119d65 | 1011 | |
830d0af6 | 1012 | static $strmessagehistory; |
1013 | ||
1014 | if (empty($strmessagehistory)) { | |
1015 | $strmessagehistory = get_string('messagehistory', 'message'); | |
1016 | } | |
2514081c | 1017 | |
c8621a02 AD |
1018 | //todo andrew user1 is generally the current user |
1019 | //if (!$userid2) { | |
1020 | // $userid2 = $USER->id; | |
1021 | //} | |
e520509a | 1022 | if ($position) { |
1023 | $position = "#$position"; | |
1024 | } | |
38c6a928 | 1025 | if ($keywords) { |
1026 | $keywords = "&search=".urlencode($keywords); | |
1027 | } | |
62119d65 | 1028 | |
830d0af6 | 1029 | if ($linktext == 'icon') { // Icon only |
b5d0cafc | 1030 | $fulllink = '<img src="'.$OUTPUT->pix_url('t/log') . '" class="iconsmall" alt="'.$strmessagehistory.'" />'; |
830d0af6 | 1031 | } else if ($linktext == 'both') { // Icon and standard name |
b5d0cafc | 1032 | $fulllink = '<img src="'.$OUTPUT->pix_url('t/log') . '" class="iconsmall" alt="" />'; |
830d0af6 | 1033 | $fulllink .= ' '.$strmessagehistory; |
1034 | } else if ($linktext) { // Custom name | |
1035 | $fulllink = $linktext; | |
1036 | } else { // Standard name only | |
1037 | $fulllink = $strmessagehistory; | |
fd7006f6 | 1038 | } |
1039 | ||
40a26286 | 1040 | $popupoptions = array( |
1041 | 'height' => 500, | |
1042 | 'width' => 500, | |
1043 | 'menubar' => false, | |
1044 | 'location' => false, | |
1045 | 'status' => true, | |
1046 | 'scrollbars' => true, | |
1047 | 'resizable' => true); | |
1048 | ||
02f2c7bd | 1049 | $link = new moodle_url('/message/index.php?history='.MESSAGE_HISTORY_ALL."&user=$userid1&id=$userid2$keywords$position"); |
c8621a02 AD |
1050 | //$action = new popup_action('click', $link, "message_history_$userid1", $popupoptions); |
1051 | $action = null; | |
9bf16314 | 1052 | $str = $OUTPUT->action_link($link, $fulllink, $action, array('title'=>$strmessagehistory)); |
62119d65 | 1053 | |
5d6b319b | 1054 | $str = '<span class="history">'.$str.'</span>'; |
1055 | ||
62119d65 | 1056 | if ($returnstr) { |
1057 | return $str; | |
1058 | } else { | |
1059 | echo $str; | |
1060 | return true; | |
1061 | } | |
1062 | } | |
e8e2d7f1 | 1063 | |
1064 | ||
1065 | /** | |
1066 | * Search through course users | |
1067 | * | |
531e58f1 | 1068 | * If $coursid specifies the site course then this function searches |
e8e2d7f1 | 1069 | * through all undeleted and confirmed users |
1070 | * | |
7390832c | 1071 | * @uses $CFG, $USER |
e8e2d7f1 | 1072 | * @uses SITEID |
1073 | * @param int $courseid The course in question. | |
1074 | * @param string $searchtext ? | |
1075 | * @param string $sort ? | |
531e58f1 | 1076 | * @param string $exceptions ? |
e8e2d7f1 | 1077 | * @return array An array of {@link $USER} records. |
1078 | * @todo Finish documenting this function | |
1079 | */ | |
1080 | function message_search_users($courseid, $searchtext, $sort='', $exceptions='') { | |
fd1cb1e8 | 1081 | global $CFG, $USER, $DB; |
e8e2d7f1 | 1082 | |
245ac557 | 1083 | $fullname = $DB->sql_fullname(); |
1084 | $LIKE = $DB->sql_ilike(); | |
e8e2d7f1 | 1085 | |
1086 | if (!empty($exceptions)) { | |
1087 | $except = ' AND u.id NOT IN ('. $exceptions .') '; | |
1088 | } else { | |
1089 | $except = ''; | |
1090 | } | |
1091 | ||
1092 | if (!empty($sort)) { | |
1093 | $order = ' ORDER BY '. $sort; | |
1094 | } else { | |
1095 | $order = ''; | |
1096 | } | |
1097 | ||
1098 | $select = 'u.deleted = \'0\' AND u.confirmed = \'1\''; | |
7390832c | 1099 | $fields = 'u.id, u.firstname, u.lastname, u.picture, u.imagealt, mc.id as contactlistid, mc.blocked'; |
e8e2d7f1 | 1100 | |
1101 | if (!$courseid or $courseid == SITEID) { | |
fd1cb1e8 | 1102 | $params = array($USER->id, "%$searchtext%"); |
1103 | return $DB->get_records_sql("SELECT $fields | |
d9eef08a | 1104 | FROM {user} u |
fd1cb1e8 | 1105 | LEFT JOIN {message_contacts} mc |
1d422980 | 1106 | ON mc.contactid = u.id AND mc.userid = ? |
fd1cb1e8 | 1107 | WHERE $select |
1108 | AND ($fullname $LIKE ?) | |
1109 | $except | |
1110 | $order", $params); | |
e8e2d7f1 | 1111 | } else { |
1112 | ||
d76a5a7f | 1113 | $context = get_context_instance(CONTEXT_COURSE, $courseid); |
1114 | $contextlists = get_related_contexts_string($context); | |
531e58f1 | 1115 | |
d76a5a7f | 1116 | // everyone who has a role assignement in this course or higher |
fd1cb1e8 | 1117 | $params = array($USER->id, "%$searchtext%"); |
1118 | $users = $DB->get_records_sql("SELECT $fields | |
1119 | FROM {user} u | |
1120 | JOIN {role_assignments} ra ON ra.userid = u.id | |
1121 | LEFT JOIN {message_contacts} mc | |
1d422980 | 1122 | ON mc.contactid = u.id AND mc.userid = ? |
fd1cb1e8 | 1123 | WHERE $select |
1124 | AND ra.contextid $contextlists | |
1125 | AND ($fullname $LIKE ?) | |
1126 | $except | |
1127 | $order", $params); | |
d76a5a7f | 1128 | |
1129 | return $users; | |
e8e2d7f1 | 1130 | } |
1131 | } | |
1132 | ||
1133 | ||
1134 | ||
1135 | ||
082864f9 | 1136 | function message_search($searchterms, $fromme=true, $tome=true, $courseid='none', $userid=0) { |
e8e2d7f1 | 1137 | /// Returns a list of posts found using an array of search terms |
1138 | /// eg word +word -word | |
1139 | /// | |
fd1cb1e8 | 1140 | global $CFG, $USER, $DB; |
e8e2d7f1 | 1141 | |
082864f9 | 1142 | /// If no userid sent then assume current user |
531e58f1 | 1143 | if ($userid == 0) $userid = $USER->id; |
082864f9 | 1144 | |
6eb7722f | 1145 | /// Some differences in SQL syntax |
d9eef08a | 1146 | if ($DB->sql_regex_supported()) { |
1147 | $REGEXP = $DB->sql_regex(true); | |
1148 | $NOTREGEXP = $DB->sql_regex(false); | |
e8e2d7f1 | 1149 | } |
1150 | ||
d9eef08a | 1151 | $LIKE = $DB->sql_ilike(); |
1152 | ||
1153 | $searchcond = array(); | |
1154 | $params = array(); | |
1155 | $i = 0; | |
e8e2d7f1 | 1156 | |
1157 | foreach ($searchterms as $searchterm) { | |
d9eef08a | 1158 | $i++; |
1159 | ||
1160 | $NOT = ''; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle | |
1161 | ||
e8e2d7f1 | 1162 | if (strlen($searchterm) < 2) { |
1163 | continue; | |
1164 | } | |
6bb4875f | 1165 | /// Under Oracle and MSSQL, trim the + and - operators and perform |
1166 | /// simpler LIKE search | |
d9eef08a | 1167 | if (!$DB->sql_regex_supported()) { |
1168 | if (substr($searchterm, 0, 1) == '-') { | |
1169 | $NOT = ' NOT '; | |
1170 | } | |
6bb4875f | 1171 | $searchterm = trim($searchterm, '+-'); |
1172 | } | |
1173 | ||
e8e2d7f1 | 1174 | if (substr($searchterm,0,1) == "+") { |
1175 | $searchterm = substr($searchterm,1); | |
d9eef08a | 1176 | $searchterm = preg_quote($searchterm, '|'); |
3d143595 | 1177 | $searchcond[] = "m.fullmessage $REGEXP :ss$i"; |
d9eef08a | 1178 | $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; |
1179 | ||
e8e2d7f1 | 1180 | } else if (substr($searchterm,0,1) == "-") { |
1181 | $searchterm = substr($searchterm,1); | |
d9eef08a | 1182 | $searchterm = preg_quote($searchterm, '|'); |
3d143595 | 1183 | $searchcond[] = "m.fullmessage $NOTREGEXP :ss$i"; |
d9eef08a | 1184 | $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; |
1185 | ||
e8e2d7f1 | 1186 | } else { |
3d143595 | 1187 | $searchcond[] = "m.fullmessage $NOT $LIKE :ss$i"; |
d9eef08a | 1188 | $params['ss'.$i] = "%$searchterm%"; |
e8e2d7f1 | 1189 | } |
172186b8 | 1190 | } |
1191 | ||
d9eef08a | 1192 | if (empty($searchcond)) { |
ff49c389 | 1193 | $searchcond = " m.fullmessage $LIKE :ss1"; |
1194 | $params['ss1'] = "%"; | |
1195 | } else { | |
1196 | $searchcond = implode(" AND ", $searchcond); | |
8a51efe9 | 1197 | } |
e8e2d7f1 | 1198 | |
082864f9 | 1199 | /// There are several possibilities |
1200 | /// 1. courseid = SITEID : The admin is searching messages by all users | |
1201 | /// 2. courseid = ?? : A teacher is searching messages by users in | |
1202 | /// one of their courses - currently disabled | |
1203 | /// 3. courseid = none : User is searching their own messages; | |
1204 | /// a. Messages from user | |
1205 | /// b. Messages to user | |
1206 | /// c. Messages to and from user | |
1207 | ||
1208 | if ($courseid == SITEID) { /// admin is searching all messages | |
3d143595 | 1209 | $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated |
d9eef08a | 1210 | FROM {message_read} m |
1211 | WHERE $searchcond", $params); | |
3d143595 | 1212 | $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated |
d9eef08a | 1213 | FROM {message} m |
1214 | WHERE $searchcond", $params); | |
1215 | ||
1216 | } else if ($courseid !== 'none') { | |
082864f9 | 1217 | /// This has not been implemented due to security concerns |
d9eef08a | 1218 | $m_read = array(); |
1219 | $m_unread = array(); | |
e8e2d7f1 | 1220 | |
082864f9 | 1221 | } else { |
531e58f1 | 1222 | |
d9eef08a | 1223 | if ($fromme and $tome) { |
1224 | $searchcond .= " AND (m.useridfrom=:userid1 OR m.useridto=:userid2)"; | |
1225 | $params['userid1'] = $userid; | |
1226 | $params['userid2'] = $userid; | |
1227 | ||
1228 | } else if ($fromme) { | |
1229 | $searchcond .= " AND m.useridfrom=:userid"; | |
1230 | $params['userid'] = $userid; | |
531e58f1 | 1231 | |
d9eef08a | 1232 | } else if ($tome) { |
1233 | $searchcond .= " AND m.useridto=:userid"; | |
1234 | $params['userid'] = $userid; | |
1235 | } | |
531e58f1 | 1236 | |
3d143595 | 1237 | $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated |
d9eef08a | 1238 | FROM {message_read} m |
1239 | WHERE $searchcond", $params); | |
3d143595 | 1240 | $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated |
d9eef08a | 1241 | FROM {message} m |
1242 | WHERE $searchcond", $params); | |
531e58f1 | 1243 | |
e8e2d7f1 | 1244 | } |
1245 | ||
082864f9 | 1246 | /// The keys may be duplicated in $m_read and $m_unread so we can't |
1247 | /// do a simple concatenation | |
1248 | $message = array(); | |
d9eef08a | 1249 | foreach ($m_read as $m) { |
1250 | $messages[] = $m; | |
1251 | } | |
1252 | foreach ($m_unread as $m) { | |
1253 | $messages[] = $m; | |
1254 | } | |
e8e2d7f1 | 1255 | |
1256 | return (empty($messages)) ? false : $messages; | |
172186b8 | 1257 | } |
1258 | ||
e8e2d7f1 | 1259 | |
1260 | ||
1261 | /// Borrowed with changes from mod/forum/lib.php | |
1262 | function message_shorten_message($message, $minlength=0) { | |
1263 | // Given a post object that we already know has a long message | |
1264 | // this function truncates the message nicely to the first | |
1265 | // sane place between $CFG->forum_longpost and $CFG->forum_shortpost | |
1266 | ||
1267 | $i = 0; | |
1268 | $tag = false; | |
1269 | $length = strlen($message); | |
1270 | $count = 0; | |
1271 | $stopzone = false; | |
1272 | $truncate = 0; | |
1273 | if ($minlength == 0) $minlength = MESSAGE_SHORTLENGTH; | |
531e58f1 | 1274 | |
e8e2d7f1 | 1275 | |
1276 | for ($i=0; $i<$length; $i++) { | |
1277 | $char = $message[$i]; | |
1278 | ||
1279 | switch ($char) { | |
1280 | case "<": | |
1281 | $tag = true; | |
1282 | break; | |
1283 | case ">": | |
1284 | $tag = false; | |
1285 | break; | |
1286 | default: | |
1287 | if (!$tag) { | |
1288 | if ($stopzone) { | |
1289 | if ($char == '.' or $char == ' ') { | |
1290 | $truncate = $i+1; | |
1291 | break 2; | |
1292 | } | |
1293 | } | |
1294 | $count++; | |
1295 | } | |
1296 | break; | |
1297 | } | |
1298 | if (!$stopzone) { | |
1299 | if ($count > $minlength) { | |
1300 | $stopzone = true; | |
1301 | } | |
1302 | } | |
1303 | } | |
1304 | ||
1305 | if (!$truncate) { | |
1306 | $truncate = $i; | |
1307 | } | |
1308 | ||
1309 | return substr($message, 0, $truncate); | |
1310 | } | |
1311 | ||
38c6a928 | 1312 | |
d9eef08a | 1313 | /** |
38c6a928 | 1314 | * Given a string and an array of keywords, this function looks |
1315 | * for the first keyword in the string, and then chops out a | |
1316 | * small section from the text that shows that word in context. | |
1317 | */ | |
1318 | function message_get_fragment($message, $keywords) { | |
1319 | ||
1320 | $fullsize = 120; | |
1321 | $halfsize = (int)($fullsize/2); | |
1322 | ||
1323 | $message = strip_tags($message); | |
1324 | ||
1325 | foreach ($keywords as $keyword) { // Just get the first one | |
1326 | if ($keyword !== '') { | |
1327 | break; | |
1328 | } | |
1329 | } | |
1330 | if (empty($keyword)) { // None found, so just return start of message | |
1331 | return message_shorten_message($message, 30); | |
1332 | } | |
1333 | ||
1334 | $leadin = $leadout = ''; | |
1335 | ||
1336 | /// Find the start of the fragment | |
1337 | $start = 0; | |
1338 | $length = strlen($message); | |
1339 | ||
1340 | $pos = strpos($message, $keyword); | |
1341 | if ($pos > $halfsize) { | |
1342 | $start = $pos - $halfsize; | |
1343 | $leadin = '...'; | |
1344 | } | |
1345 | /// Find the end of the fragment | |
1346 | $end = $start + $fullsize; | |
1347 | if ($end > $length) { | |
1348 | $end = $length; | |
1349 | } else { | |
1350 | $leadout = '...'; | |
1351 | } | |
1352 | ||
1353 | /// Pull out the fragment and format it | |
1354 | ||
1355 | $fragment = substr($message, $start, $end - $start); | |
1356 | $fragment = $leadin.highlight(implode(' ',$keywords), $fragment).$leadout; | |
1357 | return $fragment; | |
1358 | } | |
1359 | ||
c8621a02 AD |
1360 | //retrieve the messages between two users |
1361 | function message_get_history($user1, $user2, $limitnum=0) { | |
fd1cb1e8 | 1362 | global $DB; |
1363 | ||
c8621a02 AD |
1364 | $messages = array(); |
1365 | ||
1366 | //we want messages sorted oldest to newest but if getting a subset of messages we need to sort | |
1367 | //desc to get the last $limitnum messages then flip the order in php | |
1368 | $sort = 'asc'; | |
1369 | if ($limitnum>0) { | |
1370 | $sort = 'desc'; | |
1371 | } | |
1372 | ||
1373 | if ($messages_read = $DB->get_records_select('message_read', "(useridto = ? AND useridfrom = ?) OR | |
fd1cb1e8 | 1374 | (useridto = ? AND useridfrom = ?)", array($user1->id, $user2->id, $user2->id, $user1->id), |
c8621a02 AD |
1375 | "timecreated $sort", '*', 0, $limitnum)) { |
1376 | foreach ($messages_read as $message) { | |
1377 | $messages[$message->timecreated] = $message; | |
1378 | } | |
1379 | } | |
fd1cb1e8 | 1380 | if ($messages_new = $DB->get_records_select('message', "(useridto = ? AND useridfrom = ?) OR |
1381 | (useridto = ? AND useridfrom = ?)", array($user1->id, $user2->id, $user2->id, $user1->id), | |
c8621a02 | 1382 | "timecreated $sort", '*', 0, $limitnum)) { |
b9ee0638 | 1383 | foreach ($messages_new as $message) { |
c8621a02 | 1384 | $messages[$message->timecreated] = $message; |
b9ee0638 | 1385 | } |
1386 | } | |
c8621a02 AD |
1387 | |
1388 | //if we only want the last $limitnum messages | |
1389 | if ($limitnum>0) { | |
1390 | ksort($messages); | |
1391 | $messages = array_slice($messages, count($messages)-$limitnum, $limitnum, true); | |
1392 | } | |
1393 | ||
b9ee0638 | 1394 | return $messages; |
1395 | } | |
1396 | ||
c8621a02 AD |
1397 | function message_print_message_history($user1,$user2,$search='',$messagelimit=0, $messagehistorylink=false) { |
1398 | global $CFG, $OUTPUT; | |
1399 | ||
1400 | echo $OUTPUT->box_start('center'); | |
1401 | echo '<table cellpadding="10" class="message_user_pictures"><tr>'; | |
1402 | echo '<td align="center" id="user1">'; | |
1403 | echo $OUTPUT->user_picture($user1, array('size'=>100, 'courseid'=>SITEID)); | |
1404 | echo '<div class="heading">'.fullname($user1).'</div>'; | |
1405 | echo '</td>'; | |
1406 | echo '<td align="center">'; | |
1407 | echo '<img src="'.$CFG->wwwroot.'/pix/t/left.gif" alt="'.get_string('from').'" />'; | |
1408 | echo '<img src="'.$CFG->wwwroot.'/pix/t/right.gif" alt="'.get_string('to').'" />'; | |
1409 | echo '</td>'; | |
1410 | echo '<td align="center" id="user2">'; | |
1411 | echo $OUTPUT->user_picture($user2, array('size'=>100, 'courseid'=>SITEID)); | |
1412 | echo '<div class="heading">'.fullname($user2).'</div>'; | |
1413 | ||
1414 | if (isset($user2->iscontact) && isset($user2->isblocked)) { | |
1415 | $incontactlist = $user2->iscontact; | |
1416 | $isblocked = $user2->isblocked; | |
1417 | ||
1418 | $script = null; | |
1419 | $text = true; | |
1420 | $icon = false; | |
1421 | ||
1422 | $strcontact = message_get_contact_add_remove_link($incontactlist, $isblocked, $user2, $script, $text, $icon); | |
1423 | $strblock = message_get_contact_block_link($incontactlist, $isblocked, $user2, $script, $text, $icon); | |
41117b3e AD |
1424 | $useractionlinks = $strcontact.' |'.$strblock; |
1425 | ||
1426 | echo html_writer::tag('div', $useractionlinks, array('class'=>'useractionlinks')); | |
c8621a02 AD |
1427 | } |
1428 | ||
1429 | echo '</td>'; | |
1430 | echo '</tr></table>'; | |
1431 | echo $OUTPUT->box_end(); | |
1432 | ||
1433 | if (!empty($messagehistorylink)) { | |
1434 | echo $messagehistorylink; | |
1435 | } | |
1436 | ||
1437 | /// Get all the messages and print them | |
1438 | if ($messages = message_get_history($user1, $user2, $messagelimit)) { | |
1439 | $tablecontents = ''; | |
1440 | ||
1441 | $current->mday = ''; | |
1442 | $current->month = ''; | |
1443 | $current->year = ''; | |
1444 | $messagedate = get_string('strftimetime'); | |
1445 | $blockdate = get_string('strftimedaydate'); | |
1446 | foreach ($messages as $message) { | |
1447 | $date = usergetdate($message->timecreated); | |
1448 | if ($current->mday != $date['mday'] | $current->month != $date['month'] | $current->year != $date['year']) { | |
1449 | $current->mday = $date['mday']; | |
1450 | $current->month = $date['month']; | |
1451 | $current->year = $date['year']; | |
1452 | ||
1453 | $tablecontents .= '<div class="mdl-align heading"><a name="'.$date['year'].$date['mon'].$date['mday'].'"></a>'; | |
1454 | $tablecontents .= $OUTPUT->heading(userdate($message->timecreated, $blockdate), 4, 'center').'</div>'; | |
1455 | } | |
1456 | if ($message->useridfrom == $user1->id) { | |
1457 | $tablecontents .= '<div class="mdl-left left">'.message_format_message($message, $user1, $messagedate, $search, 'me').'</div><br />'; | |
1458 | } else { | |
1459 | $tablecontents .= '<div class="mdl-left right">'.message_format_message($message, $user2, $messagedate, $search, 'other').'</div><br />'; | |
1460 | } | |
1461 | } | |
1462 | ||
1463 | echo html_writer::nonempty_tag('div', $tablecontents, array('class'=>'mdl-left messagehistory')); | |
1464 | } else { | |
1465 | echo $OUTPUT->heading(get_string('nomessagesfound', 'message'), 1); | |
1466 | } | |
1467 | } | |
1468 | ||
030e3e03 | 1469 | function message_format_message(&$message, &$user, $format='', $keywords='', $class='other') { |
acd21a2d | 1470 | |
1471 | static $dateformat; | |
1472 | ||
1473 | if (empty($dateformat)) { | |
1474 | if ($format) { | |
1475 | $dateformat = $format; | |
1476 | } else { | |
54d8f804 | 1477 | $format = get_string('strftimedatetimeshort'); |
acd21a2d | 1478 | } |
b9ee0638 | 1479 | } |
acd21a2d | 1480 | $time = userdate($message->timecreated, $dateformat); |
fd1cb1e8 | 1481 | $options = new object(); |
ff6048dd | 1482 | $options->para = false; |
3d143595 | 1483 | $messagetext = format_text($message->fullmessage, $message->fullmessageformat, $options); |
38c6a928 | 1484 | if ($keywords) { |
1485 | $messagetext = highlight($keywords, $messagetext); | |
1486 | } | |
c8621a02 | 1487 | //return '<div class="message '.$class.'"><a name="m'.$message->id.'"></a><span class="author">'.s(fullname($user)).'</span> <span class="time">['.$time.']</span>: <span class="content">'.$messagetext.'</span></div>'; |
229f4e92 | 1488 | return '<div class="message '.$class.'"><a name="m'.$message->id.'"></a> <span class="time">'.$time.'</span>: <span class="content">'.$messagetext.'</span></div>'; |
b9ee0638 | 1489 | } |
e8e2d7f1 | 1490 | |
fd1cb1e8 | 1491 | /** |
405f01ee | 1492 | * Inserts a message into the database, but also forwards it |
1493 | * via other means if appropriate. | |
1494 | */ | |
1495 | function message_post_message($userfrom, $userto, $message, $format, $messagetype) { | |
fd1cb1e8 | 1496 | global $CFG, $SITE, $USER, $DB; |
1d422980 | 1497 | |
3b120e46 | 1498 | $eventdata = new object(); |
1c50df9f | 1499 | $eventdata->component = 'message'; |
1500 | $eventdata->name = 'instantmessage'; | |
3b120e46 | 1501 | $eventdata->userfrom = $userfrom; |
1502 | $eventdata->userto = $userto; | |
1c50df9f | 1503 | $eventdata->subject = "IM"; |
3b120e46 | 1504 | $eventdata->fullmessage = $message; |
c8621a02 | 1505 | $eventdata->fullmessageformat = $format; |
3b120e46 | 1506 | $eventdata->fullmessagehtml = ''; |
a1b53dcf | 1507 | $eventdata->smallmessage = ''; |
1508 | $eventdata->timecreated = time(); | |
7c7d3afa | 1509 | return message_send($eventdata); |
f49edc81 | 1510 | |
405f01ee | 1511 | } |
e8e2d7f1 | 1512 | |
c135a425 | 1513 | |
fd1cb1e8 | 1514 | /** |
c135a425 | 1515 | * Returns a list of all user ids who have used messaging in the site |
1516 | * This was the simple way to code the SQL ... is it going to blow up | |
1517 | * on large datasets? | |
1518 | */ | |
1519 | function message_get_participants() { | |
fd1cb1e8 | 1520 | global $CFG, $DB; |
1521 | ||
76b0191c | 1522 | return $DB->get_records_sql("SELECT useridfrom as id,1 FROM {message} |
1523 | UNION SELECT useridto as id,1 FROM {message} | |
1524 | UNION SELECT useridfrom as id,1 FROM {message_read} | |
1525 | UNION SELECT useridto as id,1 FROM {message_read} | |
1526 | UNION SELECT userid as id,1 FROM {message_contacts} | |
1527 | UNION SELECT contactid as id,1 from {message_contacts}"); | |
c135a425 | 1528 | } |
1529 | ||
f46b6587 | 1530 | /** |
1d422980 | 1531 | * Print a row of contactlist displaying user picture, messages waiting and |
f46b6587 | 1532 | * block links etc |
7cb1a1ad | 1533 | * @param $contact contact object containing all fields required for $OUTPUT->user_picture() |
f46b6587 | 1534 | * @param $incontactlist is the user a contact of ours? |
c8621a02 | 1535 | * @param $selectcontacturl string the url to send the user to when a contact's name is clicked |
f46b6587 | 1536 | */ |
c8621a02 AD |
1537 | function message_print_contactlist_user($contact, $incontactlist = true, $isblocked = false, $selectcontacturl = null, $showactionlinks = true) { |
1538 | global $OUTPUT, $USER; | |
f46b6587 | 1539 | $fullname = fullname($contact); |
1540 | $fullnamelink = $fullname; | |
1541 | ||
1542 | /// are there any unread messages for this contact? | |
1543 | if ($contact->messagecount > 0 ){ | |
1544 | $fullnamelink = '<strong>'.$fullnamelink.' ('.$contact->messagecount.')</strong>'; | |
1545 | } | |
1546 | ||
c8621a02 | 1547 | $strcontact = $strblock = $strhistory = null; |
f46b6587 | 1548 | |
c8621a02 AD |
1549 | if ($showactionlinks) { |
1550 | $strcontact = message_get_contact_add_remove_link($incontactlist, $isblocked, $contact); | |
1551 | $strblock = message_get_contact_block_link($incontactlist, $isblocked, $contact); | |
1552 | $strhistory = message_history_link($USER->id, $contact->id, true, '', '', 'icon'); | |
f46b6587 | 1553 | } |
1554 | ||
f46b6587 | 1555 | echo '<tr><td class="pix">'; |
812dbaf7 | 1556 | echo $OUTPUT->user_picture($contact, array('size'=>20, 'courseid'=>SITEID)); |
f46b6587 | 1557 | echo '</td>'; |
1558 | echo '<td class="contact">'; | |
1d422980 | 1559 | |
40a26286 | 1560 | $popupoptions = array( |
c8621a02 AD |
1561 | 'height' => MESSAGE_DISCUSSION_HEIGHT, |
1562 | 'width' => MESSAGE_DISCUSSION_WIDTH, | |
40a26286 | 1563 | 'menubar' => false, |
1564 | 'location' => false, | |
1565 | 'status' => true, | |
1566 | 'scrollbars' => true, | |
1567 | 'resizable' => true); | |
1568 | ||
c8621a02 AD |
1569 | $link = $action = null; |
1570 | if (!empty($selectcontacturl)) { | |
1571 | $link = new moodle_url($selectcontacturl.'&'.CONTACT_ID.'='.$contact->id); | |
1572 | } else { | |
1573 | //I suspect this else clause can be removed and maybe the $selectcontacturl arg can be removed and hardcoded | |
1574 | $link = new moodle_url("/message/discussion.php?id=$contact->id"); | |
1575 | $action = new popup_action('click', $link, "message_$contact->id", $popupoptions); | |
1576 | } | |
9bf16314 | 1577 | echo $OUTPUT->action_link($link, $fullnamelink, $action, array('title'=>get_string('sendmessageto', 'message', $fullname))); |
f46b6587 | 1578 | |
1579 | echo '</td>'; | |
1580 | echo '<td class="link"> '.$strcontact.$strblock.' '.$strhistory.'</td>'; | |
1581 | echo '</tr>'; | |
1582 | } | |
1583 | ||
c8621a02 AD |
1584 | function message_get_contact_add_remove_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { |
1585 | $strcontact = ''; | |
1586 | ||
1587 | if($incontactlist){ | |
1588 | $strcontact = message_contact_link($contact->id, 'remove', true, $script, $text, $icon); | |
1589 | } else if ($isblocked) { | |
1590 | $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); | |
1591 | } else{ | |
1592 | $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); | |
1593 | } | |
1594 | ||
1595 | return $strcontact; | |
1596 | } | |
1597 | ||
1598 | function message_get_contact_block_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { | |
1599 | $strblock = ''; | |
1600 | ||
1601 | //commented out to allow the user to block a contact without having to remove them first | |
1602 | /*if ($incontactlist) { | |
1603 | //$strblock = ''; | |
1604 | } else*/ | |
1605 | if ($isblocked) { | |
1606 | $strblock = ' '.message_contact_link($contact->id, 'unblock', true, $script, $text, $icon); | |
1607 | } else{ | |
1608 | $strblock = ' '.message_contact_link($contact->id, 'block', true, $script, $text, $icon); | |
1609 | } | |
1610 | ||
1611 | return $strblock; | |
1612 | } | |
1613 | ||
6bdf4c99 | 1614 | /** |
1615 | * Moves unread messages from message table to message_read for a given from user | |
1616 | * @param object $userid User id | |
1617 | * @return boolean success | |
1618 | */ | |
1619 | function message_move_userfrom_unread2read($userid) { | |
1620 | ||
1621 | global $DB; | |
1622 | ||
1623 | // move all unread messages from message table to messasge_read | |
1624 | if ($messages = $DB->get_records_select('message', 'useridfrom = ?', array($userid), 'timecreated')) { | |
1625 | foreach ($messages as $message) { | |
1626 | $message->timeread = 0; //the message was never read | |
1627 | $messageid = $message->id; | |
1628 | unset($message->id); | |
1629 | if ($DB->insert_record('message_read', $message)) { | |
1630 | $DB->delete_records('message', array('id' => $messageid)); | |
1631 | $DB->delete_records('message_working', array('unreadmessageid' => $messageid)); | |
1632 | } else { | |
1633 | return false; | |
1634 | } | |
1635 | } | |
1636 | } | |
1637 | return true; | |
1638 | } | |
1639 | ||
1c50df9f | 1640 | function message_get_popup_messages($destuserid, $fromuserid=NULL){ |
1641 | global $DB; | |
1d422980 | 1642 | |
1c50df9f | 1643 | $processor = $DB->get_record('message_processors', array('name' => 'popup')); |
1644 | ||
a1b53dcf | 1645 | $messagesproc = $DB->get_records('message_working', array('processorid'=>$processor->id), 'id ASC'); |
1c50df9f | 1646 | |
1647 | //for every message to process check if it's for current user and process | |
1648 | $messages = array(); | |
1649 | foreach ($messagesproc as $msgp){ | |
1650 | $query = array('id'=>$msgp->unreadmessageid, 'useridto'=>$destuserid); | |
1651 | if ($fromuserid){ | |
1652 | $query['useridfrom'] = $fromuserid; | |
1653 | } | |
1654 | if ($message = $DB->get_record('message', $query)){ | |
1655 | $messages[] = $message; | |
1656 | /// Move the entry to the other table | |
1657 | $message->timeread = time(); | |
1658 | $messageid = $message->id; | |
1659 | unset($message->id); | |
1d422980 | 1660 | |
1c50df9f | 1661 | //delete what we've processed and check if can move message |
1662 | $DB->delete_records('message_working', array('id' => $msgp->id)); | |
1663 | if ( $DB->count_records('message_working', array('unreadmessageid'=>$messageid)) == 0){ | |
1664 | if ($DB->insert_record('message_read', $message)) { | |
1665 | $DB->delete_records('message', array('id' => $messageid)); | |
1666 | } | |
1667 | } | |
1668 | } | |
1669 | } | |
1670 | return $messages; | |
c8621a02 AD |
1671 | } |
1672 | ||
1673 | //marks all messages being sent from $fromuserid to $destuserid as read | |
1674 | function message_mark_messages_read($destuserid, $fromuserid){ | |
1675 | global $DB; | |
1676 | ||
1677 | $sql = 'SELECT m.*, mw.id AS mwid FROM {message} m JOIN {message_working} mw ON m.id=mw.unreadmessageid WHERE m.useridto=:useridto AND m.useridfrom=:useridfrom'; | |
1678 | $messages = $DB->get_recordset_sql($sql, array('useridto'=>$destuserid,'useridfrom'=>$fromuserid)); | |
1679 | ||
1680 | foreach ($messages as $message) { | |
1681 | $message->timeread = time(); | |
1682 | $messageid = $message->id; | |
1683 | unset($message->id);//unset because it will get a new id on insert into message_read | |
1684 | ||
1685 | //delete what we've processed and check if can move message | |
1686 | $DB->delete_records('message_working', array('id' => $message->mwid)); | |
1687 | if ( $DB->count_records('message_working', array('unreadmessageid'=>$messageid)) == 0){ | |
1688 | if ($DB->insert_record('message_read', $message)) { | |
1689 | $DB->delete_records('message', array('id' => $messageid)); | |
1690 | } | |
1691 | } | |
1692 | } | |
65fbace7 | 1693 | } |