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