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 | define ('MESSAGE_SHORTLENGTH', 300); |
d3875524 | 29 | |
94363029 AD |
30 | //$PAGE isnt set if we're being loaded by cron which doesnt display popups anyway |
31 | if (isset($PAGE)) { | |
32 | $PAGE->set_popup_notification_allowed(false); // We are in a message window (so don't pop up a new one) | |
33 | } | |
fd7006f6 | 34 | |
c8621a02 AD |
35 | define ('MESSAGE_DISCUSSION_WIDTH',600); |
36 | define ('MESSAGE_DISCUSSION_HEIGHT',500); | |
37 | ||
38 | define ('MESSAGE_SHORTVIEW_LIMIT', 8);//the maximum number of messages to show on the short message history | |
39 | ||
c8621a02 AD |
40 | define('MESSAGE_HISTORY_SHORT',0); |
41 | define('MESSAGE_HISTORY_ALL',1); | |
42 | ||
25bd5c75 | 43 | define('MESSAGE_VIEW_UNREAD_MESSAGES','unread'); |
44 | define('MESSAGE_VIEW_RECENT_CONVERSATIONS','recentconversations'); | |
45 | define('MESSAGE_VIEW_RECENT_NOTIFICATIONS','recentnotifications'); | |
46 | define('MESSAGE_VIEW_CONTACTS','contacts'); | |
47 | define('MESSAGE_VIEW_BLOCKED','blockedusers'); | |
48 | define('MESSAGE_VIEW_COURSE','course_'); | |
49 | define('MESSAGE_VIEW_SEARCH','search'); | |
c8621a02 | 50 | |
c3931654 AD |
51 | define('MESSAGE_SEARCH_MAX_RESULTS', 200); |
52 | ||
32ef43e1 | 53 | define('MESSAGE_CONTACTS_PER_PAGE',10); |
b2bce32f | 54 | define('MESSAGE_MAX_COURSE_NAME_LENGTH', 30); |
32ef43e1 | 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 | |
bcab42da | 66 | /** |
67 | * Print the selector that allows the user to view their contacts, course participants, their recent | |
68 | * conversations etc | |
69 | * @param int $countunreadtotal how many unread messages does the user have? | |
25bd5c75 | 70 | * @param int $viewing What is the user viewing? ie MESSAGE_VIEW_UNREAD_MESSAGES, MESSAGE_VIEW_SEARCH etc |
bcab42da | 71 | * @param object $user1 the user whose messages are being viewed |
72 | * @param object $user2 the user $user1 is talking to | |
73 | * @param array $blockedusers an array of users blocked by $user1 | |
74 | * @param array $onlinecontacts an array of $user1's online contacts | |
75 | * @param array $offlinecontacts an array of $user1's offline contacts | |
76 | * @param array $strangers an array of users who have messaged $user1 who aren't contacts | |
77 | * @param bool $showcontactactionlinks show action links (add/remove contact etc) next to the users in the contact selector | |
78 | * @param int $page if there are so many users listed that they have to be split into pages what page are we viewing | |
79 | * @return void | |
80 | */ | |
81 | function message_print_contact_selector($countunreadtotal, $viewing, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showcontactactionlinks, $page=0) { | |
c8621a02 | 82 | global $PAGE; |
3a11c09f | 83 | |
bcab42da | 84 | echo html_writer::start_tag('div', array('class' => 'contactselector mdl-align')); |
c8621a02 | 85 | |
bcab42da | 86 | //if 0 unread messages and they've requested unread messages then show contacts |
25bd5c75 | 87 | if ($countunreadtotal == 0 && $viewing == MESSAGE_VIEW_UNREAD_MESSAGES) { |
88 | $viewing = MESSAGE_VIEW_CONTACTS; | |
bcab42da | 89 | } |
65c7853e | 90 | |
bcab42da | 91 | //if they have no blocked users and they've requested blocked users switch them over to contacts |
25bd5c75 | 92 | if (count($blockedusers) == 0 && $viewing == MESSAGE_VIEW_BLOCKED) { |
93 | $viewing = MESSAGE_VIEW_CONTACTS; | |
bcab42da | 94 | } |
c8621a02 | 95 | |
bcab42da | 96 | $onlyactivecourses = true; |
97 | $courses = enrol_get_users_courses($user1->id, $onlyactivecourses); | |
98 | $coursecontexts = message_get_course_contexts($courses);//we need one of these again so holding on to them | |
43240ea1 | 99 | |
bcab42da | 100 | $strunreadmessages = null; |
101 | if ($countunreadtotal>0) { //if there are unread messages | |
102 | $strunreadmessages = get_string('unreadmessages','message', $countunreadtotal); | |
103 | } | |
c8621a02 | 104 | |
bcab42da | 105 | message_print_usergroup_selector($viewing, $courses, $coursecontexts, $countunreadtotal, count($blockedusers), $strunreadmessages); |
c8621a02 | 106 | |
25bd5c75 | 107 | if ($viewing == MESSAGE_VIEW_UNREAD_MESSAGES) { |
bcab42da | 108 | message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $PAGE->url, 1, $showcontactactionlinks,$strunreadmessages, $user2); |
25bd5c75 | 109 | } else if ($viewing == MESSAGE_VIEW_CONTACTS || $viewing == MESSAGE_VIEW_SEARCH || $viewing == MESSAGE_VIEW_RECENT_CONVERSATIONS || $viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) { |
bcab42da | 110 | message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $PAGE->url, 0, $showcontactactionlinks, $strunreadmessages, $user2); |
25bd5c75 | 111 | } else if ($viewing == MESSAGE_VIEW_BLOCKED) { |
bcab42da | 112 | message_print_blocked_users($blockedusers, $PAGE->url, $showcontactactionlinks, null, $user2); |
25bd5c75 | 113 | } else if (substr($viewing, 0, 7) == MESSAGE_VIEW_COURSE) { |
bcab42da | 114 | $courseidtoshow = intval(substr($viewing, 7)); |
c8621a02 | 115 | |
bcab42da | 116 | if (!empty($courseidtoshow) |
117 | && array_key_exists($courseidtoshow, $coursecontexts) | |
118 | && has_capability('moodle/course:viewparticipants', $coursecontexts[$courseidtoshow])) { | |
c8621a02 | 119 | |
bcab42da | 120 | message_print_participants($coursecontexts[$courseidtoshow], $courseidtoshow, $PAGE->url, $showcontactactionlinks, null, $page, $user2); |
121 | } else { | |
122 | //shouldn't get here. User trying to access a course they're not in perhaps. | |
123 | add_to_log(SITEID, 'message', 'view', 'index.php', $viewing); | |
c8621a02 | 124 | } |
bcab42da | 125 | } |
c8621a02 | 126 | |
bcab42da | 127 | echo html_writer::start_tag('form', array('action' => 'index.php','method' => 'GET')); |
128 | echo html_writer::start_tag('fieldset'); | |
129 | $managebuttonclass = 'visible'; | |
25bd5c75 | 130 | if ($viewing == MESSAGE_VIEW_SEARCH) { |
bcab42da | 131 | $managebuttonclass = 'hiddenelement'; |
132 | } | |
133 | $strmanagecontacts = get_string('search','message'); | |
25bd5c75 | 134 | echo html_writer::empty_tag('input', array('type' => 'hidden','name' => 'viewing','value' => MESSAGE_VIEW_SEARCH)); |
bcab42da | 135 | echo html_writer::empty_tag('input', array('type' => 'submit','value' => $strmanagecontacts,'class' => $managebuttonclass)); |
136 | echo html_writer::end_tag('fieldset'); | |
137 | echo html_writer::end_tag('form'); | |
08cd70cf | 138 | |
c8621a02 AD |
139 | echo html_writer::end_tag('div'); |
140 | } | |
141 | ||
bcab42da | 142 | /** |
143 | * Print course participants. Called by message_print_contact_selector() | |
144 | * @param object $context the course context | |
145 | * @param int $courseid the course ID | |
146 | * @param string $contactselecturl the url to send the user to when a contact's name is clicked | |
147 | * @param bool $showactionlinks show action links (add/remove contact etc) next to the users | |
148 | * @param string $titletodisplay Optionally specify a title to display above the participants | |
149 | * @param int $page if there are so many users listed that they have to be split into pages what page are we viewing | |
150 | * @param object $user2 the user $user1 is talking to. They will be highlighted if they appear in the list of participants | |
151 | * @return void | |
152 | */ | |
08cd70cf | 153 | function message_print_participants($context, $courseid, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null, $page=0, $user2=null) { |
32ef43e1 | 154 | global $DB, $USER, $PAGE, $OUTPUT; |
c8621a02 | 155 | |
bcab42da | 156 | if (empty($titletodisplay)) { |
157 | $titletodisplay = get_string('participants'); | |
158 | } | |
159 | ||
32ef43e1 AD |
160 | $countparticipants = count_enrolled_users($context); |
161 | $participants = get_enrolled_users($context, '', 0, 'u.*', '', $page*MESSAGE_CONTACTS_PER_PAGE, MESSAGE_CONTACTS_PER_PAGE); | |
31da70cc | 162 | |
32ef43e1 AD |
163 | $pagingbar = new paging_bar($countparticipants, $page, MESSAGE_CONTACTS_PER_PAGE, $PAGE->url, 'page'); |
164 | echo $OUTPUT->render($pagingbar); | |
3a11c09f | 165 | |
bcab42da | 166 | echo html_writer::start_tag('table', array('id' => 'message_participants', 'class' => 'boxaligncenter', 'cellspacing' => '2', 'cellpadding' => '0', 'border' => '0')); |
c8621a02 | 167 | |
bcab42da | 168 | echo html_writer::start_tag('tr'); |
169 | echo html_writer::tag('td', $titletodisplay, array('colspan' => 3, 'class' => 'heading')); | |
170 | echo html_writer::end_tag('tr'); | |
c8621a02 AD |
171 | |
172 | //todo these need to come from somewhere if the course participants list is to show users with unread messages | |
173 | $iscontact = true; | |
174 | $isblocked = false; | |
175 | foreach ($participants as $participant) { | |
176 | if ($participant->id != $USER->id) { | |
177 | $participant->messagecount = 0;//todo it would be nice if the course participant could report new messages | |
08cd70cf | 178 | message_print_contactlist_user($participant, $iscontact, $isblocked, $contactselecturl, $showactionlinks, $user2); |
c8621a02 AD |
179 | } |
180 | } | |
c8621a02 | 181 | |
bcab42da | 182 | echo html_writer::end_tag('table'); |
c8621a02 AD |
183 | } |
184 | ||
bcab42da | 185 | /** |
186 | * Retrieve users blocked by $user1 | |
187 | * @param object $user1 the user whose messages are being viewed | |
188 | * @param object $user2 the user $user1 is talking to. If they are being blocked | |
189 | * they will have a variable called 'isblocked' added to their user object | |
190 | * @return array the users blocked by $user1 | |
191 | */ | |
192 | function message_get_blocked_users($user1=null, $user2=null) { | |
c8621a02 AD |
193 | global $DB, $USER; |
194 | ||
195 | if (empty($user1)) { | |
196 | $user1 = $USER; | |
197 | } | |
198 | ||
199 | if (!empty($user2)) { | |
200 | $user2->isblocked = false; | |
201 | } | |
202 | ||
19c5532b AD |
203 | $blockedusers = array(); |
204 | ||
3a11c09f PS |
205 | $userfields = user_picture::fields('u', array('lastaccess')); |
206 | $blockeduserssql = "SELECT $userfields, COUNT(m.id) AS messagecount | |
207 | FROM {message_contacts} mc | |
208 | JOIN {user} u ON u.id = mc.contactid | |
209 | LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = :user1id1 | |
210 | WHERE mc.userid = :user1id2 AND mc.blocked = 1 | |
211 | GROUP BY $userfields | |
212 | ORDER BY u.firstname ASC"; | |
bcab42da | 213 | $rs = $DB->get_recordset_sql($blockeduserssql, array('user1id1' => $user1->id, 'user1id2' => $user1->id)); |
c8621a02 | 214 | |
19c5532b AD |
215 | foreach($rs as $rd) { |
216 | $blockedusers[] = $rd; | |
c8621a02 | 217 | |
19c5532b AD |
218 | if (!empty($user2) && $user2->id == $rd->id) { |
219 | $user2->isblocked = true; | |
c8621a02 | 220 | } |
c8621a02 | 221 | } |
19c5532b | 222 | $rs->close(); |
c8621a02 AD |
223 | |
224 | return $blockedusers; | |
225 | } | |
226 | ||
bcab42da | 227 | /** |
228 | * Print users blocked by $user1. Called by message_print_contact_selector() | |
229 | * @param array $blockedusers the users blocked by $user1 | |
230 | * @param string $contactselecturl the url to send the user to when a contact's name is clicked | |
231 | * @param bool $showactionlinks show action links (add/remove contact etc) next to the users | |
232 | * @param string $titletodisplay Optionally specify a title to display above the participants | |
233 | * @param object $user2 the user $user1 is talking to. They will be highlighted if they appear in the list of blocked users | |
234 | * @return void | |
235 | */ | |
236 | function message_print_blocked_users($blockedusers, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null, $user2=null) { | |
c8621a02 AD |
237 | global $DB, $USER; |
238 | ||
239 | $countblocked = count($blockedusers); | |
240 | ||
bcab42da | 241 | echo html_writer::start_tag('table', array('id' => 'message_contacts', 'class' => 'boxaligncenter')); |
c8621a02 AD |
242 | |
243 | if (!empty($titletodisplay)) { | |
bcab42da | 244 | echo html_writer::start_tag('tr'); |
245 | echo html_writer::tag('td', $titletodisplay, array('colspan' => 3, 'class' => 'heading')); | |
246 | echo html_writer::end_tag('tr'); | |
c8621a02 AD |
247 | } |
248 | ||
249 | if ($countblocked) { | |
bcab42da | 250 | echo html_writer::start_tag('tr'); |
251 | echo html_writer::tag('td', get_string('blockedusers', 'message', $countblocked), array('colspan' => 3, 'class' => 'heading')); | |
252 | echo html_writer::end_tag('tr'); | |
c8621a02 | 253 | |
25bd5c75 | 254 | $isuserblocked = true; |
255 | $isusercontact = false; | |
c8621a02 | 256 | foreach ($blockedusers as $blockeduser) { |
25bd5c75 | 257 | message_print_contactlist_user($blockeduser, $isusercontact, $isuserblocked, $contactselecturl, $showactionlinks, $user2); |
c8621a02 AD |
258 | } |
259 | } | |
260 | ||
bcab42da | 261 | echo html_writer::end_tag('table'); |
c8621a02 AD |
262 | } |
263 | ||
bcab42da | 264 | /** |
265 | * Retrieve $user1's contacts (online, offline and strangers) | |
266 | * @param object $user1 the user whose messages are being viewed | |
267 | * @param object $user2 the user $user1 is talking to. If they are a contact | |
268 | * they will have a variable called 'iscontact' added to their user object | |
269 | * @return array containing 3 arrays. array($onlinecontacts, $offlinecontacts, $strangers) | |
270 | */ | |
271 | function message_get_contacts($user1=null, $user2=null) { | |
c8621a02 | 272 | global $DB, $CFG, $USER; |
172186b8 | 273 | |
c8621a02 AD |
274 | if (empty($user1)) { |
275 | $user1 = $USER; | |
276 | } | |
277 | ||
278 | if (!empty($user2)) { | |
279 | $user2->iscontact = false; | |
280 | } | |
e8e2d7f1 | 281 | |
282 | $timetoshowusers = 300; //Seconds default | |
283 | if (isset($CFG->block_online_users_timetosee)) { | |
284 | $timetoshowusers = $CFG->block_online_users_timetosee * 60; | |
285 | } | |
e8e2d7f1 | 286 | |
f46b6587 | 287 | // time which a user is counting as being active since |
288 | $timefrom = time()-$timetoshowusers; | |
531e58f1 | 289 | |
f46b6587 | 290 | // people in our contactlist who are online |
291 | $onlinecontacts = array(); | |
292 | // people in our contactlist who are offline | |
293 | $offlinecontacts = array(); | |
294 | // people who are not in our contactlist but have sent us a message | |
627bb9ec AD |
295 | $strangers = array(); |
296 | ||
3a11c09f | 297 | $userfields = user_picture::fields('u', array('lastaccess')); |
f46b6587 | 298 | |
1d422980 | 299 | // get all in our contactlist who are not blocked in our contact list |
f46b6587 | 300 | // and count messages we have waiting from each of them |
3a11c09f | 301 | $contactsql = "SELECT $userfields, COUNT(m.id) AS messagecount |
fd1cb1e8 | 302 | FROM {message_contacts} mc |
303 | JOIN {user} u ON u.id = mc.contactid | |
304 | LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = ? | |
1d422980 | 305 | WHERE mc.userid = ? AND mc.blocked = 0 |
3a11c09f | 306 | GROUP BY $userfields |
bbe973da | 307 | ORDER BY u.firstname ASC"; |
fd1cb1e8 | 308 | |
20222a3d EL |
309 | $rs = $DB->get_recordset_sql($contactsql, array($user1->id, $user1->id)); |
310 | foreach ($rs as $rd) { | |
311 | if ($rd->lastaccess >= $timefrom) { | |
312 | // they have been active recently, so are counted online | |
313 | $onlinecontacts[] = $rd; | |
314 | ||
315 | } else { | |
316 | $offlinecontacts[] = $rd; | |
317 | } | |
c8621a02 | 318 | |
bcab42da | 319 | if (!empty($user2) && $user2->id == $rd->id) { |
20222a3d | 320 | $user2->iscontact = true; |
e8e2d7f1 | 321 | } |
f46b6587 | 322 | } |
20222a3d | 323 | $rs->close(); |
f46b6587 | 324 | |
f46b6587 | 325 | // get messages from anyone who isn't in our contact list and count the number |
326 | // of messages we have from each of them | |
3a11c09f | 327 | $strangersql = "SELECT $userfields, count(m.id) as messagecount |
fd1cb1e8 | 328 | FROM {message} m |
329 | JOIN {user} u ON u.id = m.useridfrom | |
330 | LEFT OUTER JOIN {message_contacts} mc ON mc.contactid = m.useridfrom AND mc.userid = m.useridto | |
1d422980 | 331 | WHERE mc.id IS NULL AND m.useridto = ? |
3a11c09f | 332 | GROUP BY $userfields |
bbe973da | 333 | ORDER BY u.firstname ASC"; |
fd1cb1e8 | 334 | |
20222a3d EL |
335 | $rs = $DB->get_recordset_sql($strangersql, array($USER->id)); |
336 | foreach ($rs as $rd) { | |
337 | $strangers[] = $rd; | |
e8e2d7f1 | 338 | } |
20222a3d | 339 | $rs->close(); |
e8e2d7f1 | 340 | |
c8621a02 AD |
341 | return array($onlinecontacts, $offlinecontacts, $strangers); |
342 | } | |
343 | ||
bcab42da | 344 | /** |
345 | * Print $user1's contacts. Called by message_print_contact_selector() | |
346 | * @param array $onlinecontacts $user1's contacts which are online | |
347 | * @param array $offlinecontacts $user1's contacts which are offline | |
348 | * @param array $strangers users which are not contacts but who have messaged $user1 | |
349 | * @param string $contactselecturl the url to send the user to when a contact's name is clicked | |
350 | * @param int $minmessages The minimum number of unread messages required from a user for them to be displayed | |
351 | * Typically 0 (show all contacts) or 1 (only show contacts from whom we have a new message) | |
352 | * @param bool $showactionlinks show action links (add/remove contact etc) next to the users | |
353 | * @param string $titletodisplay Optionally specify a title to display above the participants | |
354 | * @param object $user2 the user $user1 is talking to. They will be highlighted if they appear in the list of contacts | |
355 | * @return void | |
356 | */ | |
357 | function message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $contactselecturl=null, $minmessages=0, $showactionlinks=true, $titletodisplay=null, $user2=null) { | |
c8621a02 AD |
358 | global $CFG, $PAGE, $OUTPUT; |
359 | ||
f46b6587 | 360 | $countonlinecontacts = count($onlinecontacts); |
361 | $countofflinecontacts = count($offlinecontacts); | |
362 | $countstrangers = count($strangers); | |
25bd5c75 | 363 | $isuserblocked = null; |
f46b6587 | 364 | |
a1157dda | 365 | if ($countonlinecontacts + $countofflinecontacts == 0) { |
bcab42da | 366 | echo html_writer::tag('div', get_string('contactlistempty', 'message'), array('class' => 'heading')); |
65ef518b | 367 | } |
368 | ||
bcab42da | 369 | echo html_writer::start_tag('table', array('id' => 'message_contacts', 'class' => 'boxaligncenter')); |
531e58f1 | 370 | |
c8621a02 | 371 | if (!empty($titletodisplay)) { |
bcab42da | 372 | message_print_heading($titletodisplay); |
c8621a02 AD |
373 | } |
374 | ||
f46b6587 | 375 | if($countonlinecontacts) { |
376 | /// print out list of online contacts | |
531e58f1 | 377 | |
c8621a02 | 378 | if (empty($titletodisplay)) { |
bcab42da | 379 | message_print_heading(get_string('onlinecontacts', 'message', $countonlinecontacts)); |
c8621a02 | 380 | } |
531e58f1 | 381 | |
25bd5c75 | 382 | $isuserblocked = false; |
383 | $isusercontact = true; | |
f46b6587 | 384 | foreach ($onlinecontacts as $contact) { |
bcab42da | 385 | if ($minmessages == 0 || $contact->messagecount >= $minmessages) { |
25bd5c75 | 386 | message_print_contactlist_user($contact, $isusercontact, $isuserblocked, $contactselecturl, $showactionlinks, $user2); |
c8621a02 | 387 | } |
65ef518b | 388 | } |
f46b6587 | 389 | } |
531e58f1 | 390 | |
f46b6587 | 391 | if ($countofflinecontacts) { |
392 | /// print out list of offline contacts | |
531e58f1 | 393 | |
c8621a02 | 394 | if (empty($titletodisplay)) { |
bcab42da | 395 | message_print_heading(get_string('offlinecontacts', 'message', $countofflinecontacts)); |
c8621a02 | 396 | } |
576ad290 | 397 | |
25bd5c75 | 398 | $isuserblocked = false; |
399 | $isusercontact = true; | |
f46b6587 | 400 | foreach ($offlinecontacts as $contact) { |
bcab42da | 401 | if ($minmessages == 0 || $contact->messagecount >= $minmessages) { |
25bd5c75 | 402 | message_print_contactlist_user($contact, $isusercontact, $isuserblocked, $contactselecturl, $showactionlinks, $user2); |
c8621a02 | 403 | } |
65ef518b | 404 | } |
bcab42da | 405 | |
f46b6587 | 406 | } |
65ef518b | 407 | |
f46b6587 | 408 | /// print out list of incoming contacts |
409 | if ($countstrangers) { | |
bcab42da | 410 | message_print_heading(get_string('incomingcontacts', 'message', $countstrangers)); |
531e58f1 | 411 | |
25bd5c75 | 412 | $isuserblocked = false; |
413 | $isusercontact = false; | |
f46b6587 | 414 | foreach ($strangers as $stranger) { |
bcab42da | 415 | if ($minmessages == 0 || $stranger->messagecount >= $minmessages) { |
25bd5c75 | 416 | message_print_contactlist_user($stranger, $isusercontact, $isuserblocked, $contactselecturl, $showactionlinks, $user2); |
c8621a02 | 417 | } |
669be60c | 418 | } |
f46b6587 | 419 | } |
531e58f1 | 420 | |
bcab42da | 421 | echo html_writer::end_tag('table'); |
65ef518b | 422 | |
f46b6587 | 423 | if ($countstrangers && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help |
bcab42da | 424 | echo html_writer::tag('div','('.get_string('addsomecontactsincoming', 'message').')',array('class' => 'note')); |
65ef518b | 425 | } |
e8e2d7f1 | 426 | } |
427 | ||
bcab42da | 428 | /** |
429 | * Print a select box allowing the user to choose to view new messages, course participants etc. | |
430 | * Called by message_print_contact_selector() | |
25bd5c75 | 431 | * @param int $viewing What page is the user viewing ie MESSAGE_VIEW_UNREAD_MESSAGES, MESSAGE_VIEW_RECENT_CONVERSATIONS etc |
bcab42da | 432 | * @param array $courses array of course objects. The courses the user is enrolled in. |
433 | * @param array $coursecontexts array of course contexts. Keyed on course id. | |
434 | * @param int $countunreadtotal how many unread messages does the user have? | |
435 | * @param int $countblocked how many users has the current user blocked? | |
436 | * @param string $strunreadmessages a preconstructed message about the number of unread messages the user has | |
437 | * @return void | |
438 | */ | |
439 | function message_print_usergroup_selector($viewing, $courses, $coursecontexts, $countunreadtotal, $countblocked, $strunreadmessages) { | |
65c7853e | 440 | $options = array(); |
a8ccca53 | 441 | $textlib = textlib_get_instance(); // going to use textlib services |
c8621a02 AD |
442 | |
443 | if ($countunreadtotal>0) { //if there are unread messages | |
25bd5c75 | 444 | $options[MESSAGE_VIEW_UNREAD_MESSAGES] = $strunreadmessages; |
c8621a02 AD |
445 | } |
446 | ||
bcab42da | 447 | $str = get_string('mycontacts', 'message'); |
25bd5c75 | 448 | $options[MESSAGE_VIEW_CONTACTS] = $str; |
bcab42da | 449 | |
25bd5c75 | 450 | $options[MESSAGE_VIEW_RECENT_CONVERSATIONS] = get_string('mostrecentconversations', 'message'); |
451 | $options[MESSAGE_VIEW_RECENT_NOTIFICATIONS] = get_string('mostrecentnotifications', 'message'); | |
c8621a02 AD |
452 | |
453 | if (!empty($courses)) { | |
454 | $courses_options = array(); | |
455 | ||
456 | foreach($courses as $course) { | |
457 | if (has_capability('moodle/course:viewparticipants', $coursecontexts[$course->id])) { | |
b2bce32f | 458 | //Not using short_text() as we want the end of the course name. Not the beginning. |
b2bce32f | 459 | if ($textlib->strlen($course->shortname) > MESSAGE_MAX_COURSE_NAME_LENGTH) { |
25bd5c75 | 460 | $courses_options[MESSAGE_VIEW_COURSE.$course->id] = '...'.$textlib->substr($course->shortname, -MESSAGE_MAX_COURSE_NAME_LENGTH); |
b2bce32f | 461 | } else { |
25bd5c75 | 462 | $courses_options[MESSAGE_VIEW_COURSE.$course->id] = $course->shortname; |
b2bce32f | 463 | } |
c8621a02 AD |
464 | } |
465 | } | |
466 | ||
467 | if (!empty($courses_options)) { | |
bcab42da | 468 | $options[] = array(get_string('courses') => $courses_options); |
c8621a02 AD |
469 | } |
470 | } | |
471 | ||
31c532d0 | 472 | if ($countblocked>0) { |
bcab42da | 473 | $str = get_string('blockedusers','message', $countblocked); |
25bd5c75 | 474 | $options[MESSAGE_VIEW_BLOCKED] = $str; |
31c532d0 AD |
475 | } |
476 | ||
bcab42da | 477 | echo html_writer::start_tag('form', array('id' => 'usergroupform','method' => 'get','action' => '')); |
229f4e92 | 478 | echo html_writer::start_tag('fieldset'); |
25bd5c75 | 479 | echo html_writer::select($options, 'viewing', $viewing, false, array('id' => 'viewing','onchange' => 'this.form.submit()')); |
229f4e92 | 480 | echo html_writer::end_tag('fieldset'); |
c8621a02 AD |
481 | echo html_writer::end_tag('form'); |
482 | } | |
483 | ||
bcab42da | 484 | /** |
485 | * Load the course contexts for all of the users courses | |
486 | * @param array $courses array of course objects. The courses the user is enrolled in. | |
487 | * @return array of course contexts | |
488 | */ | |
489 | function message_get_course_contexts($courses) { | |
c8621a02 AD |
490 | $coursecontexts = array(); |
491 | ||
492 | foreach($courses as $course) { | |
493 | $coursecontexts[$course->id] = get_context_instance(CONTEXT_COURSE, $course->id); | |
494 | } | |
495 | ||
496 | return $coursecontexts; | |
497 | } | |
498 | ||
cee92282 AD |
499 | /** |
500 | * strip off action parameters like 'removecontact' | |
bcab42da | 501 | * @param moodle_url/string $moodleurl a URL. Typically the current page URL. |
502 | * @return string the URL minus parameters that perform actions (like adding/removing/blocking a contact). | |
cee92282 | 503 | */ |
c8621a02 | 504 | function message_remove_url_params($moodleurl) { |
c8621a02 AD |
505 | $newurl = new moodle_url($moodleurl); |
506 | $newurl->remove_params('addcontact','removecontact','blockcontact','unblockcontact'); | |
507 | return $newurl->out(); | |
508 | } | |
509 | ||
bcab42da | 510 | /** |
511 | * Count the number of messages with a field having a specified value. | |
512 | * if $field is empty then return count of the whole array | |
513 | * if $field is non-existent then return 0 | |
514 | * @param array $messagearray array of message objects | |
515 | * @param string $field the field to inspect on the message objects | |
516 | * @param string $value the value to test the field against | |
517 | */ | |
e8e2d7f1 | 518 | function message_count_messages($messagearray, $field='', $value='') { |
519 | if (!is_array($messagearray)) return 0; | |
520 | if ($field == '' or empty($messagearray)) return count($messagearray); | |
531e58f1 | 521 | |
e8e2d7f1 | 522 | $count = 0; |
523 | foreach ($messagearray as $message) { | |
524 | $count += ($message->$field == $value) ? 1 : 0; | |
525 | } | |
526 | return $count; | |
172186b8 | 527 | } |
528 | ||
c8621a02 AD |
529 | /** |
530 | * Returns the count of unread messages for user. Either from a specific user or from all users. | |
c8621a02 AD |
531 | * @param object $user1 the first user. Defaults to $USER |
532 | * @param object $user2 the second user. If null this function will count all of user 1's unread messages. | |
533 | * @return int the count of $user1's unread messages | |
534 | */ | |
535 | function message_count_unread_messages($user1=null, $user2=null) { | |
536 | global $USER, $DB; | |
e8e2d7f1 | 537 | |
c8621a02 AD |
538 | if (empty($user1)) { |
539 | $user1 = $USER; | |
540 | } | |
541 | ||
542 | if (!empty($user2)) { | |
543 | return $DB->count_records_select('message', "useridto = ? AND useridfrom = ?", | |
544 | array($user1->id, $user2->id), "COUNT('id')"); | |
545 | } else { | |
546 | return $DB->count_records_select('message', "useridto = ?", | |
547 | array($user1->id), "COUNT('id')"); | |
548 | } | |
549 | } | |
550 | ||
bcab42da | 551 | /** |
552 | * Count the number of users blocked by $user1 | |
553 | * @param $user1 | |
554 | */ | |
c8621a02 AD |
555 | function message_count_blocked_users($user1=null) { |
556 | global $USER, $DB; | |
557 | ||
558 | if (empty($user1)) { | |
559 | $user1 = $USER; | |
560 | } | |
561 | ||
562 | $sql = "SELECT count(mc.id) | |
563 | FROM {message_contacts} mc | |
564 | WHERE mc.userid = :userid AND mc.blocked = 1"; | |
bcab42da | 565 | $params = array('userid' => $user1->id); |
c8621a02 AD |
566 | |
567 | return $DB->count_records_sql($sql, $params); | |
568 | } | |
569 | ||
570 | /** | |
bcab42da | 571 | * Print the search form and search results if a search has been performed |
572 | * @param boolean $advancedsearch show basic or advanced search form | |
573 | * @param object $user1 the current user | |
574 | * @return boolean true if a search was performed | |
c8621a02 AD |
575 | */ |
576 | function message_print_search($advancedsearch = false, $user1=null) { | |
c8621a02 | 577 | $frm = data_submitted(); |
3a11c09f | 578 | |
c8621a02 AD |
579 | $doingsearch = false; |
580 | if ($frm) { | |
31da70cc PS |
581 | if (confirm_sesskey()) { |
582 | $doingsearch = !empty($frm->combinedsubmit) || !empty($frm->keywords) || (!empty($frm->personsubmit) and !empty($frm->name)); | |
583 | } else { | |
584 | $frm = false; | |
585 | } | |
c8621a02 | 586 | } |
531e58f1 | 587 | |
c8621a02 AD |
588 | if (!empty($frm->combinedsearch)) { |
589 | $combinedsearchstring = $frm->combinedsearch; | |
590 | } else { | |
a813a748 AD |
591 | //$combinedsearchstring = get_string('searchcombined','message').'...'; |
592 | $combinedsearchstring = ''; | |
c8621a02 | 593 | } |
531e58f1 | 594 | |
c8621a02 AD |
595 | if ($doingsearch) { |
596 | if ($advancedsearch) { | |
3a11c09f | 597 | |
c8621a02 AD |
598 | $messagesearch = ''; |
599 | if (!empty($frm->keywords)) { | |
600 | $messagesearch = $frm->keywords; | |
601 | } | |
602 | $personsearch = ''; | |
603 | if (!empty($frm->name)) { | |
604 | $personsearch = $frm->name; | |
605 | } | |
606 | include('search_advanced.html'); | |
607 | } else { | |
608 | include('search.html'); | |
609 | } | |
610 | ||
611 | $showicontext = false; | |
612 | message_print_search_results($frm, $showicontext, $user1); | |
613 | ||
614 | return true; | |
172186b8 | 615 | } else { |
c8621a02 AD |
616 | |
617 | if ($advancedsearch) { | |
618 | $personsearch = $messagesearch = ''; | |
619 | include('search_advanced.html'); | |
620 | } else { | |
621 | include('search.html'); | |
622 | } | |
623 | return false; | |
172186b8 | 624 | } |
625 | } | |
626 | ||
bcab42da | 627 | /** |
628 | * Get the users recent conversations meaning all the people they've recently | |
629 | * sent or received a message from plus the most recent message sent to or received from each other user | |
630 | * @param object $user the current user | |
631 | * @param int $limitfrom can be used for paging | |
632 | * @param int $limitto can be used for paging | |
633 | * @return array | |
634 | */ | |
635 | function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) { | |
636 | global $DB; | |
637 | ||
638 | $userfields = user_picture::fields('u', array('lastaccess')); | |
639 | //This query retrieves the last message received from and sent to each user | |
640 | //It unions that data then, within that set, it finds the most recent message you've exchanged with each user over all | |
641 | //It then joins with some other tables to get some additional data we need | |
642 | ||
643 | //message ID is used instead of timecreated as it should sort the same and will be much faster | |
644 | ||
645 | //There is a separate query for read and unread queries as they are stored in different tables | |
646 | //They were originally retrieved in one query but it was so large that it was difficult to be confident in its correctness | |
647 | $sql = "SELECT $userfields, mr.id as mid, mr.smallmessage, mr.fullmessage, mr.timecreated, mc.id as contactlistid, mc.blocked | |
648 | FROM {message_read} mr | |
649 | JOIN ( | |
650 | SELECT messages.userid AS userid, MAX(messages.mid) AS mid | |
651 | FROM ( | |
652 | SELECT mr1.useridto AS userid, MAX(mr1.id) AS mid | |
653 | FROM {message_read} mr1 | |
654 | WHERE mr1.useridfrom = :userid1 | |
655 | AND mr1.notification = 0 | |
656 | GROUP BY mr1.useridto | |
657 | UNION | |
658 | SELECT mr2.useridfrom AS userid, MAX(mr2.id) AS mid | |
659 | FROM {message_read} mr2 | |
660 | WHERE mr2.useridto = :userid2 | |
661 | AND mr2.notification = 0 | |
662 | GROUP BY mr2.useridfrom | |
663 | ) messages | |
664 | GROUP BY messages.userid | |
665 | ) messages2 ON mr.id = messages2.mid AND (mr.useridto = messages2.userid OR mr.useridfrom = messages2.userid) | |
666 | JOIN {user} u ON u.id = messages2.userid | |
667 | LEFT JOIN {message_contacts} mc ON mc.userid = :userid3 AND mc.contactid = u.id | |
668 | WHERE u.deleted = '0' | |
669 | ORDER BY mr.id DESC"; | |
670 | $params = array('userid1' => $user->id, 'userid2' => $user->id, 'userid3' => $user->id); | |
671 | $read = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); | |
672 | ||
673 | $sql = "SELECT $userfields, m.id as mid, m.smallmessage, m.fullmessage, m.timecreated, mc.id as contactlistid, mc.blocked | |
674 | FROM {message} m | |
675 | JOIN ( | |
676 | SELECT messages.userid AS userid, MAX(messages.mid) AS mid | |
677 | FROM ( | |
678 | SELECT m1.useridto AS userid, MAX(m1.id) AS mid | |
679 | FROM {message} m1 | |
680 | WHERE m1.useridfrom = :userid1 | |
681 | AND m1.notification = 0 | |
682 | GROUP BY m1.useridto | |
683 | UNION | |
684 | SELECT m2.useridfrom AS userid, MAX(m2.id) AS mid | |
685 | FROM {message} m2 | |
686 | WHERE m2.useridto = :userid2 | |
687 | AND m2.notification = 0 | |
688 | GROUP BY m2.useridfrom | |
689 | ) messages | |
690 | GROUP BY messages.userid | |
691 | ) messages2 ON m.id = messages2.mid AND (m.useridto = messages2.userid OR m.useridfrom = messages2.userid) | |
692 | JOIN {user} u ON u.id = messages2.userid | |
693 | LEFT JOIN {message_contacts} mc ON mc.userid = :userid3 AND mc.contactid = u.id | |
694 | WHERE u.deleted = '0' | |
695 | ORDER BY m.id DESC"; | |
696 | $unread = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); | |
697 | ||
698 | $conversations = array(); | |
699 | ||
700 | //Union the 2 result sets together looking for the message with the most recent timecreated for each other user | |
701 | //$conversation->id (the array key) is the other user's ID | |
702 | $conversation_arrays = array($unread, $read); | |
703 | foreach ($conversation_arrays as $conversation_array) { | |
704 | foreach ($conversation_array as $conversation) { | |
705 | if (empty($conversations[$conversation->id]) || $conversations[$conversation->id]->timecreated < $conversation->timecreated ) { | |
706 | $conversations[$conversation->id] = $conversation; | |
707 | } | |
708 | } | |
709 | } | |
710 | ||
711 | //Sort the conversations. This is a bit complicated as we need to sort by $conversation->timecreated | |
712 | //and there may be multiple conversations with the same timecreated value. | |
713 | //The conversations array contains both read and unread messages (different tables) so sorting by ID won't work | |
714 | usort($conversations, "conversationsort"); | |
715 | ||
716 | return $conversations; | |
717 | } | |
718 | ||
719 | /** | |
720 | * Sort function used to order conversations | |
721 | * @param object $a A conversation object | |
722 | * @param object $b A conversation object | |
723 | * @return integer | |
724 | */ | |
725 | function conversationsort($a, $b) | |
726 | { | |
727 | if ($a->timecreated == $b->timecreated) { | |
728 | return 0; | |
729 | } | |
730 | return ($a->timecreated > $b->timecreated) ? -1 : 1; | |
731 | } | |
732 | ||
733 | /** | |
734 | * Get the users recent event notifications | |
735 | * @param object $user the current user | |
736 | * @param int $limitfrom can be used for paging | |
737 | * @param int $limitto can be used for paging | |
738 | * @return array | |
739 | */ | |
740 | function message_get_recent_notifications($user, $limitfrom=0, $limitto=100) { | |
741 | global $DB; | |
742 | ||
743 | $userfields = user_picture::fields('u', array('lastaccess')); | |
744 | $sql = "SELECT mr.id AS message_read_id, $userfields, mr.smallmessage, mr.fullmessage, mr.timecreated as timecreated, mr.contexturl, mr.contexturlname | |
745 | FROM {message_read} mr | |
746 | JOIN {user} u ON u.id=mr.useridfrom | |
747 | WHERE mr.useridto = :userid1 AND u.deleted = '0' AND mr.notification = :notification | |
748 | ORDER BY mr.id DESC";//ordering by id should give the same result as ordering by timecreated but will be faster | |
749 | $params = array('userid1' => $user->id, 'notification' => 1); | |
750 | ||
751 | $notifications = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); | |
752 | return $notifications; | |
753 | } | |
754 | ||
755 | /** | |
756 | * Print the user's recent conversations | |
757 | * @param object $user1 the current user | |
758 | * @param bool $showicontext flag indicating whether or not to show text next to the action icons | |
759 | */ | |
760 | function message_print_recent_conversations($user=null, $showicontext=false) { | |
761 | global $USER; | |
762 | ||
763 | echo html_writer::start_tag('p', array('class' => 'heading')); | |
764 | echo get_string('mostrecentconversations', 'message'); | |
765 | echo html_writer::end_tag('p'); | |
766 | ||
767 | if (empty($user)) { | |
768 | $user = $USER; | |
769 | } | |
770 | ||
771 | $conversations = message_get_recent_conversations($user); | |
772 | ||
773 | $showotheruser = true; | |
774 | message_print_recent_messages_table($conversations, $user, $showotheruser, $showicontext); | |
775 | } | |
776 | ||
777 | /** | |
778 | * Print the user's recent notifications | |
779 | * @param object $user1 the current user | |
780 | */ | |
781 | function message_print_recent_notifications($user=null) { | |
782 | global $USER; | |
783 | ||
784 | echo html_writer::start_tag('p', array('class' => 'heading')); | |
785 | echo get_string('mostrecentnotifications', 'message'); | |
786 | echo html_writer::end_tag('p'); | |
787 | ||
788 | if (empty($user)) { | |
789 | $user = $USER; | |
790 | } | |
791 | ||
792 | $notifications = message_get_recent_notifications($user); | |
793 | ||
794 | $showicontext = false; | |
795 | $showotheruser = false; | |
796 | message_print_recent_messages_table($notifications, $user, $showotheruser, $showicontext); | |
e8e2d7f1 | 797 | } |
798 | ||
bcab42da | 799 | /** |
800 | * Print a list of recent messages | |
801 | * @staticvar type $dateformat | |
802 | * @param array $messages the messages to display | |
803 | * @param object $user the current user | |
804 | * @param bool $showotheruser display information on the other user? | |
805 | * @param bool $showicontext show text next to the action icons? | |
806 | */ | |
807 | function message_print_recent_messages_table($messages, $user=null, $showotheruser=true, $showicontext=false) { | |
808 | global $OUTPUT; | |
809 | static $dateformat; | |
e8e2d7f1 | 810 | |
bcab42da | 811 | if (empty($dateformat)) { |
812 | $dateformat = get_string('strftimedatetimeshort'); | |
813 | } | |
e8e2d7f1 | 814 | |
bcab42da | 815 | echo html_writer::start_tag('div', array('class' => 'messagerecent')); |
816 | foreach ($messages as $message) { | |
817 | echo html_writer::start_tag('div', array('class' => 'singlemessage')); | |
818 | ||
819 | if ($showotheruser) { | |
820 | if ( $message->contactlistid ) { | |
821 | if ($message->blocked == 0) { /// not blocked | |
822 | $strcontact = message_contact_link($message->id, 'remove', true, null, $showicontext); | |
823 | $strblock = message_contact_link($message->id, 'block', true, null, $showicontext); | |
824 | } else { // blocked | |
825 | $strcontact = message_contact_link($message->id, 'add', true, null, $showicontext); | |
826 | $strblock = message_contact_link($message->id, 'unblock', true, null, $showicontext); | |
827 | } | |
828 | } else { | |
829 | $strcontact = message_contact_link($message->id, 'add', true, null, $showicontext); | |
830 | $strblock = message_contact_link($message->id, 'block', true, null, $showicontext); | |
831 | } | |
832 | ||
833 | //should we show just the icon or icon and text? | |
834 | $histicontext = 'icon'; | |
835 | if ($showicontext) { | |
836 | $histicontext = 'both'; | |
837 | } | |
838 | $strhistory = message_history_link($user->id, $message->id, true, '', '', $histicontext); | |
839 | ||
840 | echo html_writer::start_tag('span', array('class' => 'otheruser')); | |
841 | ||
842 | echo html_writer::start_tag('span', array('class' => 'pix')); | |
843 | echo $OUTPUT->user_picture($message, array('size' => 20, 'courseid' => SITEID)); | |
844 | echo html_writer::end_tag('span'); | |
845 | ||
846 | echo html_writer::start_tag('span', array('class' => 'contact')); | |
847 | ||
848 | $link = new moodle_url("/message/index.php?id=$message->id"); | |
849 | $action = null; | |
850 | echo $OUTPUT->action_link($link, fullname($message), $action, array('title' => get_string('sendmessageto', 'message', fullname($message)))); | |
851 | ||
852 | echo html_writer::end_tag('span');//end contact | |
853 | ||
854 | echo $strcontact.$strblock.$strhistory; | |
855 | echo html_writer::end_tag('span');//end otheruser | |
856 | } | |
857 | $messagetoprint = null; | |
858 | if (!empty($message->smallmessage)) { | |
859 | $messagetoprint = $message->smallmessage; | |
860 | } else { | |
861 | $messagetoprint = $message->fullmessage; | |
862 | } | |
863 | ||
864 | echo html_writer::tag('span', userdate($message->timecreated, $dateformat), array('class' => 'messagedate')); | |
865 | echo html_writer::tag('span', format_text($messagetoprint, FORMAT_HTML), array('class' => 'themessage')); | |
866 | echo message_format_contexturl($message); | |
867 | echo html_writer::end_tag('div');//end singlemessage | |
868 | } | |
869 | echo html_writer::end_tag('div');//end messagerecent | |
870 | } | |
871 | ||
872 | /** | |
873 | * Add the selected user as a contact for the current user | |
874 | * @param int $contactid the ID of the user to add as a contact | |
875 | * @param int $blocked 1 if you wish to block the contact | |
876 | * @return bool/int false if the $contactid isnt a valid user id. True if no changes made. | |
877 | * Otherwise returns the result of update_record() or insert_record() | |
878 | */ | |
e8e2d7f1 | 879 | function message_add_contact($contactid, $blocked=0) { |
fd1cb1e8 | 880 | global $USER, $DB; |
531e58f1 | 881 | |
bcab42da | 882 | if (!$DB->record_exists('user', array('id' => $contactid))) { // invalid userid |
e8e2d7f1 | 883 | return false; |
884 | } | |
531e58f1 | 885 | |
bcab42da | 886 | if (($contact = $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid))) !== false) { |
e8e2d7f1 | 887 | /// record already exists - we may be changing blocking status |
531e58f1 | 888 | |
e8e2d7f1 | 889 | if ($contact->blocked !== $blocked) { |
890 | /// change to blocking status | |
891 | $contact->blocked = $blocked; | |
fd1cb1e8 | 892 | return $DB->update_record('message_contacts', $contact); |
e8e2d7f1 | 893 | } else { |
894 | /// no changes to blocking status | |
895 | return true; | |
896 | } | |
531e58f1 | 897 | |
172186b8 | 898 | } else { |
e8e2d7f1 | 899 | /// new contact record |
900 | unset($contact); | |
901 | $contact->userid = $USER->id; | |
902 | $contact->contactid = $contactid; | |
903 | $contact->blocked = $blocked; | |
fd1cb1e8 | 904 | return $DB->insert_record('message_contacts', $contact, false); |
172186b8 | 905 | } |
906 | } | |
907 | ||
bcab42da | 908 | /** |
909 | * remove a contact | |
910 | * @param type $contactid the user ID of the contact to remove | |
911 | * @return bool returns the result of delete_records() | |
912 | */ | |
e8e2d7f1 | 913 | function message_remove_contact($contactid) { |
fd1cb1e8 | 914 | global $USER, $DB; |
bcab42da | 915 | return $DB->delete_records('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid)); |
e8e2d7f1 | 916 | } |
917 | ||
bcab42da | 918 | /** |
919 | * Unblock a contact. Note that this reverts the previously blocked user back to a non-contact. | |
920 | * @param int $contactid the user ID of the contact to unblock | |
921 | * @return bool returns the result of delete_records() | |
922 | */ | |
e8e2d7f1 | 923 | function message_unblock_contact($contactid) { |
fd1cb1e8 | 924 | global $USER, $DB; |
bcab42da | 925 | return $DB->delete_records('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid)); |
e8e2d7f1 | 926 | } |
927 | ||
bcab42da | 928 | /** |
929 | * block a user | |
930 | * @param int $contactid the user ID of the user to block | |
931 | */ | |
e8e2d7f1 | 932 | function message_block_contact($contactid) { |
933 | return message_add_contact($contactid, 1); | |
934 | } | |
935 | ||
bcab42da | 936 | /** |
937 | * Load a user's contact record | |
938 | * @param int $contactid the user ID of the user whose contact record you want | |
939 | */ | |
e8e2d7f1 | 940 | function message_get_contact($contactid) { |
fd1cb1e8 | 941 | global $USER, $DB; |
bcab42da | 942 | return $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid)); |
e8e2d7f1 | 943 | } |
531e58f1 | 944 | |
bcab42da | 945 | /** |
946 | * Print the results of a message search | |
947 | * @param mixed $frm submitted form data | |
948 | * @param bool $showicontext show text next to action icons? | |
949 | * @param object $user1 the current user | |
950 | */ | |
c8621a02 | 951 | function message_print_search_results($frm, $showicontext=false, $user1=null) { |
bcab42da | 952 | global $USER, $DB, $OUTPUT; |
c8621a02 AD |
953 | |
954 | if (empty($user1)) { | |
955 | $user1 = $USER; | |
956 | } | |
e8e2d7f1 | 957 | |
bcab42da | 958 | echo html_writer::start_tag('div', array('class' => 'mdl-left')); |
e8e2d7f1 | 959 | |
c8621a02 AD |
960 | $personsearch = false; |
961 | $personsearchstring = null; | |
e8e2d7f1 | 962 | if (!empty($frm->personsubmit) and !empty($frm->name)) { |
c8621a02 AD |
963 | $personsearch = true; |
964 | $personsearchstring = $frm->name; | |
965 | } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) { | |
966 | $personsearch = true; | |
967 | $personsearchstring = $frm->combinedsearch; | |
968 | } | |
531e58f1 | 969 | |
c8621a02 AD |
970 | /// search for person |
971 | if ($personsearch) { | |
b71d4dd2 | 972 | if (optional_param('mycourses', 0, PARAM_BOOL)) { |
e8e2d7f1 | 973 | $users = array(); |
df997f84 | 974 | $mycourses = enrol_get_my_courses(); |
e8e2d7f1 | 975 | foreach ($mycourses as $mycourse) { |
c8621a02 | 976 | if (is_array($susers = message_search_users($mycourse->id, $personsearchstring))) { |
e8e2d7f1 | 977 | foreach ($susers as $suser) $users[$suser->id] = $suser; |
978 | } | |
979 | } | |
980 | } else { | |
c8621a02 | 981 | $users = message_search_users(SITEID, $personsearchstring); |
e8e2d7f1 | 982 | } |
983 | ||
984 | if (!empty($users)) { | |
bcab42da | 985 | echo html_writer::start_tag('p', array('class' => 'heading searchresultcount')); |
c3931654 AD |
986 | echo get_string('userssearchresults', 'message', count($users)); |
987 | echo html_writer::end_tag('p'); | |
988 | ||
bcab42da | 989 | echo html_writer::start_tag('table', array('class' => 'messagesearchresults')); |
e8e2d7f1 | 990 | foreach ($users as $user) { |
531e58f1 | 991 | |
7390832c | 992 | if ( $user->contactlistid ) { |
993 | if ($user->blocked == 0) { /// not blocked | |
c8621a02 AD |
994 | $strcontact = message_contact_link($user->id, 'remove', true, null, $showicontext); |
995 | $strblock = message_contact_link($user->id, 'block', true, null, $showicontext); | |
e8e2d7f1 | 996 | } else { // blocked |
c8621a02 AD |
997 | $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext); |
998 | $strblock = message_contact_link($user->id, 'unblock', true, null, $showicontext); | |
e8e2d7f1 | 999 | } |
e8e2d7f1 | 1000 | } else { |
c8621a02 AD |
1001 | $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext); |
1002 | $strblock = message_contact_link($user->id, 'block', true, null, $showicontext); | |
1003 | } | |
1004 | ||
1005 | //should we show just the icon or icon and text? | |
1006 | $histicontext = 'icon'; | |
1007 | if ($showicontext) { | |
1008 | $histicontext = 'both'; | |
e8e2d7f1 | 1009 | } |
c8621a02 | 1010 | $strhistory = message_history_link($USER->id, $user->id, true, '', '', $histicontext); |
531e58f1 | 1011 | |
bcab42da | 1012 | echo html_writer::start_tag('tr'); |
40a26286 | 1013 | |
bcab42da | 1014 | echo html_writer::start_tag('td', array('class' => 'pix')); |
1015 | echo $OUTPUT->user_picture($user, array('size' => 20, 'courseid' => SITEID)); | |
1016 | echo html_writer::end_tag('td'); | |
1017 | ||
1018 | echo html_writer::start_tag('td',array('class' => 'contact')); | |
c8621a02 | 1019 | $action = null; |
bcab42da | 1020 | $link = new moodle_url("/message/index.php?id=$user->id"); |
1021 | echo $OUTPUT->action_link($link, fullname($user), $action, array('title' => get_string('sendmessageto', 'message', fullname($user)))); | |
1022 | echo html_writer::end_tag('td'); | |
576ad290 | 1023 | |
bcab42da | 1024 | echo html_writer::tag('td', $strcontact, array('class' => 'link')); |
1025 | echo html_writer::tag('td', $strblock, array('class' => 'link')); | |
1026 | echo html_writer::tag('td', $strhistory, array('class' => 'link')); | |
531e58f1 | 1027 | |
bcab42da | 1028 | echo html_writer::end_tag('tr'); |
e8e2d7f1 | 1029 | } |
bcab42da | 1030 | echo html_writer::end_tag('table'); |
531e58f1 | 1031 | |
e8e2d7f1 | 1032 | } else { |
bcab42da | 1033 | echo html_writer::start_tag('p', array('class' => 'heading searchresultcount')); |
c8621a02 | 1034 | echo get_string('userssearchresults', 'message', 0).'<br /><br />'; |
c3931654 | 1035 | echo html_writer::end_tag('p'); |
e8e2d7f1 | 1036 | } |
c8621a02 | 1037 | } |
531e58f1 | 1038 | |
c8621a02 AD |
1039 | // search messages for keywords |
1040 | $messagesearch = false; | |
1041 | $messagesearchstring = null; | |
1042 | if (!empty($frm->keywords)) { | |
1043 | $messagesearch = true; | |
1044 | $messagesearchstring = clean_text(trim($frm->keywords)); | |
1045 | } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) { | |
1046 | $messagesearch = true; | |
1047 | $messagesearchstring = clean_text(trim($frm->combinedsearch)); | |
1048 | } | |
531e58f1 | 1049 | |
c8621a02 AD |
1050 | if ($messagesearch) { |
1051 | if ($messagesearchstring) { | |
1052 | $keywords = explode(' ', $messagesearchstring); | |
ff49c389 | 1053 | } else { |
1054 | $keywords = array(); | |
1055 | } | |
e8e2d7f1 | 1056 | $tome = false; |
1057 | $fromme = false; | |
1058 | $courseid = 'none'; | |
1059 | ||
c8621a02 AD |
1060 | if (empty($frm->keywordsoption)) { |
1061 | $frm->keywordsoption = 'allmine'; | |
1062 | } | |
1063 | ||
e8e2d7f1 | 1064 | switch ($frm->keywordsoption) { |
1065 | case 'tome': | |
1066 | $tome = true; | |
1067 | break; | |
1068 | case 'fromme': | |
1069 | $fromme = true; | |
1070 | break; | |
1071 | case 'allmine': | |
1072 | $tome = true; | |
1073 | $fromme = true; | |
1074 | break; | |
1075 | case 'allusers': | |
1076 | $courseid = SITEID; | |
1077 | break; | |
1078 | case 'courseusers': | |
1079 | $courseid = $frm->courseid; | |
1080 | break; | |
1081 | default: | |
1082 | $tome = true; | |
1083 | $fromme = true; | |
1084 | } | |
1085 | ||
1086 | if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) { | |
531e58f1 | 1087 | |
e8e2d7f1 | 1088 | /// get a list of contacts |
bcab42da | 1089 | if (($contacts = $DB->get_records('message_contacts', array('userid' => $USER->id), '', 'contactid, blocked') ) === false) { |
082864f9 | 1090 | $contacts = array(); |
1091 | } | |
e8e2d7f1 | 1092 | |
082864f9 | 1093 | /// print heading with number of results |
bcab42da | 1094 | echo html_writer::start_tag('p', array('class' => 'heading searchresultcount')); |
c3931654 | 1095 | $countresults = count($messages); |
bcab42da | 1096 | if ($countresults == MESSAGE_SEARCH_MAX_RESULTS) { |
c3931654 AD |
1097 | echo get_string('keywordssearchresultstoomany', 'message', $countresults).' ("'.s($messagesearchstring).'")'; |
1098 | } else { | |
a813a748 | 1099 | echo get_string('keywordssearchresults', 'message', $countresults); |
c3931654 AD |
1100 | } |
1101 | echo html_writer::end_tag('p'); | |
082864f9 | 1102 | |
1103 | /// print table headings | |
bcab42da | 1104 | echo html_writer::start_tag('table', array('class' => 'messagesearchresults', 'cellspacing' => '0')); |
c3931654 | 1105 | |
bcab42da | 1106 | $headertdstart = html_writer::start_tag('td', array('class' => 'messagesearchresultscol')); |
c3931654 | 1107 | $headertdend = html_writer::end_tag('td'); |
bcab42da | 1108 | echo html_writer::start_tag('tr'); |
c3931654 AD |
1109 | echo $headertdstart.get_string('from').$headertdend; |
1110 | echo $headertdstart.get_string('to').$headertdend; | |
1111 | echo $headertdstart.get_string('message', 'message').$headertdend; | |
1112 | echo $headertdstart.get_string('timesent', 'message').$headertdend; | |
bcab42da | 1113 | echo html_writer::end_tag('tr'); |
082864f9 | 1114 | |
1115 | $blockedcount = 0; | |
54d8f804 | 1116 | $dateformat = get_string('strftimedatetimeshort'); |
38c6a928 | 1117 | $strcontext = get_string('context', 'message'); |
e8e2d7f1 | 1118 | foreach ($messages as $message) { |
082864f9 | 1119 | |
1120 | /// ignore messages to and from blocked users unless $frm->includeblocked is set | |
b71d4dd2 | 1121 | if (!optional_param('includeblocked', 0, PARAM_BOOL) and ( |
082864f9 | 1122 | ( isset($contacts[$message->useridfrom]) and ($contacts[$message->useridfrom]->blocked == 1)) or |
1123 | ( isset($contacts[$message->useridto] ) and ($contacts[$message->useridto]->blocked == 1)) | |
1124 | ) | |
1125 | ) { | |
1126 | $blockedcount ++; | |
e8e2d7f1 | 1127 | continue; |
1128 | } | |
531e58f1 | 1129 | |
082864f9 | 1130 | /// load up user to record |
1131 | if ($message->useridto !== $USER->id) { | |
bcab42da | 1132 | $userto = $DB->get_record('user', array('id' => $message->useridto)); |
531e58f1 | 1133 | $tocontact = (array_key_exists($message->useridto, $contacts) and |
082864f9 | 1134 | ($contacts[$message->useridto]->blocked == 0) ); |
531e58f1 | 1135 | $toblocked = (array_key_exists($message->useridto, $contacts) and |
082864f9 | 1136 | ($contacts[$message->useridto]->blocked == 1) ); |
1137 | } else { | |
1138 | $userto = false; | |
1139 | $tocontact = false; | |
1140 | $toblocked = false; | |
1141 | } | |
531e58f1 | 1142 | |
082864f9 | 1143 | /// load up user from record |
1144 | if ($message->useridfrom !== $USER->id) { | |
bcab42da | 1145 | $userfrom = $DB->get_record('user', array('id' => $message->useridfrom)); |
531e58f1 | 1146 | $fromcontact = (array_key_exists($message->useridfrom, $contacts) and |
082864f9 | 1147 | ($contacts[$message->useridfrom]->blocked == 0) ); |
531e58f1 | 1148 | $fromblocked = (array_key_exists($message->useridfrom, $contacts) and |
082864f9 | 1149 | ($contacts[$message->useridfrom]->blocked == 1) ); |
1150 | } else { | |
1151 | $userfrom = false; | |
1152 | $fromcontact = false; | |
1153 | $fromblocked = false; | |
1154 | } | |
1155 | ||
e520509a | 1156 | /// find date string for this message |
1157 | $date = usergetdate($message->timecreated); | |
1158 | $datestring = $date['year'].$date['mon'].$date['mday']; | |
1159 | ||
082864f9 | 1160 | /// print out message row |
bcab42da | 1161 | echo html_writer::start_tag('tr', array('valign' => 'top')); |
1162 | ||
1163 | echo html_writer::start_tag('td', array('class' => 'contact')); | |
c8621a02 | 1164 | message_print_user($userfrom, $fromcontact, $fromblocked, $showicontext); |
bcab42da | 1165 | echo html_writer::end_tag('td'); |
1166 | ||
1167 | echo html_writer::start_tag('td', array('class' => 'contact')); | |
c8621a02 | 1168 | message_print_user($userto, $tocontact, $toblocked, $showicontext); |
bcab42da | 1169 | echo html_writer::end_tag('td'); |
1170 | ||
1171 | echo html_writer::start_tag('td', array('class' => 'summary')); | |
1172 | echo message_get_fragment($message->fullmessage, $keywords); | |
1173 | echo html_writer::start_tag('div', array('class' => 'link')); | |
c8621a02 | 1174 | |
71666cf3 | 1175 | //find the user involved that isn't the current user |
c8621a02 AD |
1176 | $user2id = null; |
1177 | if ($user1->id == $message->useridto) { | |
1178 | $user2id = $message->useridfrom; | |
1179 | } else { | |
1180 | $user2id = $message->useridto; | |
1181 | } | |
1182 | message_history_link($user1->id, $user2id, false, | |
1183 | $messagesearchstring, 'm'.$message->id, $strcontext); | |
bcab42da | 1184 | echo html_writer::end_tag('div'); |
1185 | echo html_writer::end_tag('td'); | |
1186 | ||
1187 | echo html_writer::tag('td', userdate($message->timecreated, $dateformat), array('class' => 'date')); | |
1188 | ||
1189 | echo html_writer::end_tag('tr'); | |
082864f9 | 1190 | } |
531e58f1 | 1191 | |
e8e2d7f1 | 1192 | |
e520509a | 1193 | if ($blockedcount > 0) { |
bcab42da | 1194 | echo html_writer::start_tag('tr'); |
1195 | echo html_writer::tag('td', get_string('blockedmessages', 'message', $blockedcount), array('colspan' => 4, 'align' => 'center')); | |
1196 | echo html_writer::end_tag('tr'); | |
e520509a | 1197 | } |
bcab42da | 1198 | echo html_writer::end_tag('table'); |
531e58f1 | 1199 | |
e8e2d7f1 | 1200 | } else { |
bcab42da | 1201 | echo html_writer::tag('p', get_string('keywordssearchresults', 'message', 0), array('class' => 'heading')); |
e8e2d7f1 | 1202 | } |
c8621a02 | 1203 | } |
e8e2d7f1 | 1204 | |
c8621a02 | 1205 | if (!$personsearch && !$messagesearch) { |
71666cf3 | 1206 | //they didn't enter any search terms |
aa9a6867 | 1207 | echo $OUTPUT->notification(get_string('emptysearchstring', 'message')); |
e8e2d7f1 | 1208 | } |
1209 | ||
bcab42da | 1210 | echo html_writer::end_tag('div'); |
e8e2d7f1 | 1211 | } |
1212 | ||
bcab42da | 1213 | /** |
1214 | * Print information on a user. Used when printing search results. | |
1215 | * @param object/bool $user the user to display or false if you just want $USER | |
1216 | * @param bool $iscontact is the user being displayed a contact? | |
1217 | * @param bool $isblocked is the user being displayed blocked? | |
1218 | * @param bool $includeicontext include text next to the action icons? | |
1219 | */ | |
c8621a02 | 1220 | function message_print_user ($user=false, $iscontact=false, $isblocked=false, $includeicontext=false) { |
7cb1a1ad | 1221 | global $USER, $OUTPUT; |
1d422980 | 1222 | |
082864f9 | 1223 | if ($user === false) { |
bcab42da | 1224 | echo $OUTPUT->user_picture($USER, array('size' => 20, 'courseid' => SITEID)); |
082864f9 | 1225 | } else { |
bcab42da | 1226 | echo $OUTPUT->user_picture($user, array('size' => 20, 'courseid' => SITEID)); |
e520509a | 1227 | echo ' '; |
c8621a02 AD |
1228 | |
1229 | $return = false; | |
1230 | $script = null; | |
082864f9 | 1231 | if ($iscontact) { |
c8621a02 | 1232 | message_contact_link($user->id, 'remove', $return, $script, $includeicontext); |
082864f9 | 1233 | } else { |
c8621a02 | 1234 | message_contact_link($user->id, 'add', $return, $script, $includeicontext); |
082864f9 | 1235 | } |
1236 | echo ' '; | |
1237 | if ($isblocked) { | |
c8621a02 | 1238 | message_contact_link($user->id, 'unblock', $return, $script, $includeicontext); |
082864f9 | 1239 | } else { |
c8621a02 | 1240 | message_contact_link($user->id, 'block', $return, $script, $includeicontext); |
082864f9 | 1241 | } |
576ad290 | 1242 | |
40a26286 | 1243 | $popupoptions = array( |
c8621a02 AD |
1244 | 'height' => MESSAGE_DISCUSSION_HEIGHT, |
1245 | 'width' => MESSAGE_DISCUSSION_WIDTH, | |
40a26286 | 1246 | 'menubar' => false, |
1247 | 'location' => false, | |
1248 | 'status' => true, | |
1249 | 'scrollbars' => true, | |
1250 | 'resizable' => true); | |
1251 | ||
02f2c7bd | 1252 | $link = new moodle_url("/message/index.php?id=$user->id"); |
c8621a02 AD |
1253 | //$action = new popup_action('click', $link, "message_$user->id", $popupoptions); |
1254 | $action = null; | |
bcab42da | 1255 | echo $OUTPUT->action_link($link, fullname($user), $action, array('title' => get_string('sendmessageto', 'message', fullname($user)))); |
40a26286 | 1256 | |
082864f9 | 1257 | } |
1258 | } | |
1259 | ||
bcab42da | 1260 | /** |
1261 | * Print a message contact link | |
1262 | * @staticvar type $str | |
1263 | * @param int $userid the ID of the user to apply to action to | |
1264 | * @param string $linktype can be add, remove, block or unblock | |
1265 | * @param bool $return if true return the link as a string. If false echo the link. | |
1266 | * @param string $script the URL to send the user to when the link is clicked. If null, the current page. | |
1267 | * @param bool $text include text next to the icons? | |
1268 | * @param bool $icon include a graphical icon? | |
1269 | * @return string if $return is true otherwise bool | |
1270 | */ | |
c8621a02 | 1271 | function message_contact_link($userid, $linktype='add', $return=false, $script=null, $text=false, $icon=true) { |
bcab42da | 1272 | global $OUTPUT, $PAGE; |
e520509a | 1273 | |
6898e848 | 1274 | //hold onto the strings as we're probably creating a bunch of links |
e520509a | 1275 | static $str; |
1276 | ||
c8621a02 | 1277 | if (empty($script)) { |
cee92282 | 1278 | //strip off previous action params like 'removecontact' |
4090a30f | 1279 | $script = message_remove_url_params($PAGE->url); |
c8621a02 AD |
1280 | } |
1281 | ||
e520509a | 1282 | if (empty($str->blockcontact)) { |
1283 | $str->blockcontact = get_string('blockcontact', 'message'); | |
1284 | $str->unblockcontact = get_string('unblockcontact', 'message'); | |
1285 | $str->removecontact = get_string('removecontact', 'message'); | |
1286 | $str->addcontact = get_string('addcontact', 'message'); | |
1287 | } | |
1288 | ||
5d6b319b | 1289 | $command = $linktype.'contact'; |
1290 | $string = $str->{$command}; | |
6898e848 AD |
1291 | |
1292 | $safealttext = s($string); | |
1293 | ||
1294 | $safestring = ''; | |
3a11c09f | 1295 | if (!empty($text)) { |
6898e848 | 1296 | $safestring = $safealttext; |
c8621a02 AD |
1297 | } |
1298 | ||
1299 | $img = ''; | |
1300 | if ($icon) { | |
6898e848 AD |
1301 | $iconpath = null; |
1302 | switch ($linktype) { | |
1303 | case 'block': | |
1304 | $iconpath = 't/block'; | |
1305 | break; | |
1306 | case 'unblock': | |
1307 | $iconpath = 't/userblue'; | |
1308 | break; | |
1309 | case 'remove': | |
1310 | $iconpath = 'i/cross_red_big'; | |
1311 | break; | |
1312 | case 'add': | |
1313 | default: | |
1314 | $iconpath = 't/addgreen'; | |
1315 | } | |
1316 | ||
1317 | $img = '<img src="'.$OUTPUT->pix_url($iconpath).'" class="iconsmall" alt="'.$safealttext.'" />'; | |
082864f9 | 1318 | } |
5d6b319b | 1319 | |
c8621a02 | 1320 | $output = '<span class="'.$linktype.'contact">'. |
5d6b319b | 1321 | '<a href="'.$script.'&'.$command.'='.$userid. |
01e85326 | 1322 | '&sesskey='.sesskey().'" title="'.$safealttext.'">'. |
c8621a02 | 1323 | $img. |
6898e848 | 1324 | $safestring.'</a></span>'; |
5d6b319b | 1325 | |
e520509a | 1326 | if ($return) { |
1327 | return $output; | |
082864f9 | 1328 | } else { |
e520509a | 1329 | echo $output; |
082864f9 | 1330 | return true; |
1331 | } | |
1332 | } | |
1333 | ||
bcab42da | 1334 | /** |
1335 | * echo or return a link to take the user to the full message history between themselves and another user | |
1336 | * @staticvar type $strmessagehistory | |
1337 | * @param int $userid1 the ID of the current user | |
1338 | * @param int $userid2 the ID of the other user | |
1339 | * @param bool $return true to return the link as a string. False to echo the link. | |
1340 | * @param string $keywords any keywords to highlight in the message history | |
1341 | * @param string $position anchor name to jump to within the message history | |
1342 | * @param string $linktext optionally specify the link text | |
1343 | * @return string|bool. Returns a string if $return is true. Otherwise returns a boolean. | |
1344 | */ | |
1345 | function message_history_link($userid1, $userid2, $return=false, $keywords='', $position='', $linktext='') { | |
1346 | global $OUTPUT; | |
62119d65 | 1347 | |
830d0af6 | 1348 | static $strmessagehistory; |
1349 | ||
1350 | if (empty($strmessagehistory)) { | |
1351 | $strmessagehistory = get_string('messagehistory', 'message'); | |
1352 | } | |
2514081c | 1353 | |
e520509a | 1354 | if ($position) { |
1355 | $position = "#$position"; | |
1356 | } | |
38c6a928 | 1357 | if ($keywords) { |
1358 | $keywords = "&search=".urlencode($keywords); | |
1359 | } | |
62119d65 | 1360 | |
830d0af6 | 1361 | if ($linktext == 'icon') { // Icon only |
b5d0cafc | 1362 | $fulllink = '<img src="'.$OUTPUT->pix_url('t/log') . '" class="iconsmall" alt="'.$strmessagehistory.'" />'; |
830d0af6 | 1363 | } else if ($linktext == 'both') { // Icon and standard name |
b5d0cafc | 1364 | $fulllink = '<img src="'.$OUTPUT->pix_url('t/log') . '" class="iconsmall" alt="" />'; |
830d0af6 | 1365 | $fulllink .= ' '.$strmessagehistory; |
1366 | } else if ($linktext) { // Custom name | |
1367 | $fulllink = $linktext; | |
1368 | } else { // Standard name only | |
1369 | $fulllink = $strmessagehistory; | |
fd7006f6 | 1370 | } |
1371 | ||
40a26286 | 1372 | $popupoptions = array( |
1373 | 'height' => 500, | |
1374 | 'width' => 500, | |
1375 | 'menubar' => false, | |
1376 | 'location' => false, | |
1377 | 'status' => true, | |
1378 | 'scrollbars' => true, | |
1379 | 'resizable' => true); | |
1380 | ||
02f2c7bd | 1381 | $link = new moodle_url('/message/index.php?history='.MESSAGE_HISTORY_ALL."&user=$userid1&id=$userid2$keywords$position"); |
c8621a02 | 1382 | $action = null; |
bcab42da | 1383 | $str = $OUTPUT->action_link($link, $fulllink, $action, array('title' => $strmessagehistory)); |
62119d65 | 1384 | |
5d6b319b | 1385 | $str = '<span class="history">'.$str.'</span>'; |
1386 | ||
bcab42da | 1387 | if ($return) { |
62119d65 | 1388 | return $str; |
1389 | } else { | |
1390 | echo $str; | |
1391 | return true; | |
1392 | } | |
1393 | } | |
e8e2d7f1 | 1394 | |
1395 | ||
1396 | /** | |
1397 | * Search through course users | |
1398 | * | |
531e58f1 | 1399 | * If $coursid specifies the site course then this function searches |
e8e2d7f1 | 1400 | * through all undeleted and confirmed users |
e8e2d7f1 | 1401 | * @param int $courseid The course in question. |
bcab42da | 1402 | * @param string $searchtext the text to search for |
1403 | * @param string $sort the column name to order by | |
1404 | * @param string $exceptions comma separated list of user IDs to exclude | |
e8e2d7f1 | 1405 | * @return array An array of {@link $USER} records. |
e8e2d7f1 | 1406 | */ |
1407 | function message_search_users($courseid, $searchtext, $sort='', $exceptions='') { | |
fd1cb1e8 | 1408 | global $CFG, $USER, $DB; |
e8e2d7f1 | 1409 | |
245ac557 | 1410 | $fullname = $DB->sql_fullname(); |
e8e2d7f1 | 1411 | |
1412 | if (!empty($exceptions)) { | |
1413 | $except = ' AND u.id NOT IN ('. $exceptions .') '; | |
1414 | } else { | |
1415 | $except = ''; | |
1416 | } | |
1417 | ||
1418 | if (!empty($sort)) { | |
1419 | $order = ' ORDER BY '. $sort; | |
1420 | } else { | |
1421 | $order = ''; | |
1422 | } | |
1423 | ||
3a11c09f | 1424 | $ufields = user_picture::fields('u'); |
e8e2d7f1 | 1425 | if (!$courseid or $courseid == SITEID) { |
fd1cb1e8 | 1426 | $params = array($USER->id, "%$searchtext%"); |
3a11c09f | 1427 | return $DB->get_records_sql("SELECT $ufields, mc.id as contactlistid, mc.blocked |
d9eef08a | 1428 | FROM {user} u |
fd1cb1e8 | 1429 | LEFT JOIN {message_contacts} mc |
1d422980 | 1430 | ON mc.contactid = u.id AND mc.userid = ? |
3a11c09f | 1431 | WHERE u.deleted = '0' AND u.confirmed = '1' |
dd54dc31 | 1432 | AND (".$DB->sql_like($fullname, '?', false).") |
fd1cb1e8 | 1433 | $except |
1434 | $order", $params); | |
e8e2d7f1 | 1435 | } else { |
3a11c09f | 1436 | //TODO: add enabled enrolment join here (skodak) |
d76a5a7f | 1437 | $context = get_context_instance(CONTEXT_COURSE, $courseid); |
1438 | $contextlists = get_related_contexts_string($context); | |
531e58f1 | 1439 | |
91421f3e | 1440 | // everyone who has a role assignment in this course or higher |
fd1cb1e8 | 1441 | $params = array($USER->id, "%$searchtext%"); |
b6e52658 AD |
1442 | $users = $DB->get_records_sql("SELECT $ufields, mc.id as contactlistid, mc.blocked |
1443 | FROM {user} u | |
fd1cb1e8 | 1444 | JOIN {role_assignments} ra ON ra.userid = u.id |
1445 | LEFT JOIN {message_contacts} mc | |
1d422980 | 1446 | ON mc.contactid = u.id AND mc.userid = ? |
3a11c09f | 1447 | WHERE u.deleted = '0' AND u.confirmed = '1' |
fd1cb1e8 | 1448 | AND ra.contextid $contextlists |
dd54dc31 | 1449 | AND (".$DB->sql_like($fullname, '?', false).") |
fd1cb1e8 | 1450 | $except |
1451 | $order", $params); | |
d76a5a7f | 1452 | |
1453 | return $users; | |
e8e2d7f1 | 1454 | } |
1455 | } | |
1456 | ||
bcab42da | 1457 | /** |
1458 | * search a user's messages | |
1459 | * @param array $searchterms an array of search terms (strings) | |
1460 | * @param bool $fromme include messages from the user? | |
1461 | * @param bool $tome include messages to the user? | |
1462 | * @param mixed $courseid SITEID for admins searching all messages. Other behaviour not yet implemented | |
1463 | * @param int $userid the user ID of the current user | |
1464 | * @return mixed An array of messages or false if no matching messages were found | |
1465 | */ | |
082864f9 | 1466 | function message_search($searchterms, $fromme=true, $tome=true, $courseid='none', $userid=0) { |
e8e2d7f1 | 1467 | /// Returns a list of posts found using an array of search terms |
1468 | /// eg word +word -word | |
1469 | /// | |
fd1cb1e8 | 1470 | global $CFG, $USER, $DB; |
e8e2d7f1 | 1471 | |
082864f9 | 1472 | /// If no userid sent then assume current user |
531e58f1 | 1473 | if ($userid == 0) $userid = $USER->id; |
082864f9 | 1474 | |
6eb7722f | 1475 | /// Some differences in SQL syntax |
d9eef08a | 1476 | if ($DB->sql_regex_supported()) { |
1477 | $REGEXP = $DB->sql_regex(true); | |
1478 | $NOTREGEXP = $DB->sql_regex(false); | |
e8e2d7f1 | 1479 | } |
1480 | ||
d9eef08a | 1481 | $searchcond = array(); |
1482 | $params = array(); | |
1483 | $i = 0; | |
e8e2d7f1 | 1484 | |
c3931654 AD |
1485 | //preprocess search terms to check whether we have at least 1 eligible search term |
1486 | //if we do we can drop words around it like 'a' | |
1487 | $dropshortwords = false; | |
1488 | foreach ($searchterms as $searchterm) { | |
1489 | if (strlen($searchterm) >= 2) { | |
1490 | $dropshortwords = true; | |
1491 | } | |
1492 | } | |
1493 | ||
e8e2d7f1 | 1494 | foreach ($searchterms as $searchterm) { |
d9eef08a | 1495 | $i++; |
1496 | ||
47586394 | 1497 | $NOT = false; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle |
d9eef08a | 1498 | |
c3931654 | 1499 | if ($dropshortwords && strlen($searchterm) < 2) { |
e8e2d7f1 | 1500 | continue; |
1501 | } | |
6bb4875f | 1502 | /// Under Oracle and MSSQL, trim the + and - operators and perform |
1503 | /// simpler LIKE search | |
d9eef08a | 1504 | if (!$DB->sql_regex_supported()) { |
1505 | if (substr($searchterm, 0, 1) == '-') { | |
47586394 | 1506 | $NOT = true; |
d9eef08a | 1507 | } |
6bb4875f | 1508 | $searchterm = trim($searchterm, '+-'); |
1509 | } | |
1510 | ||
e8e2d7f1 | 1511 | if (substr($searchterm,0,1) == "+") { |
1512 | $searchterm = substr($searchterm,1); | |
d9eef08a | 1513 | $searchterm = preg_quote($searchterm, '|'); |
3d143595 | 1514 | $searchcond[] = "m.fullmessage $REGEXP :ss$i"; |
d9eef08a | 1515 | $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; |
1516 | ||
e8e2d7f1 | 1517 | } else if (substr($searchterm,0,1) == "-") { |
1518 | $searchterm = substr($searchterm,1); | |
d9eef08a | 1519 | $searchterm = preg_quote($searchterm, '|'); |
3d143595 | 1520 | $searchcond[] = "m.fullmessage $NOTREGEXP :ss$i"; |
d9eef08a | 1521 | $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; |
1522 | ||
e8e2d7f1 | 1523 | } else { |
47586394 | 1524 | $searchcond[] = $DB->sql_like("m.fullmessage", ":ss$i", false, true, $NOT); |
d9eef08a | 1525 | $params['ss'.$i] = "%$searchterm%"; |
e8e2d7f1 | 1526 | } |
172186b8 | 1527 | } |
1528 | ||
d9eef08a | 1529 | if (empty($searchcond)) { |
dd54dc31 | 1530 | $searchcond = " ".$DB->sql_like('m.fullmessage', ':ss1', false); |
ff49c389 | 1531 | $params['ss1'] = "%"; |
1532 | } else { | |
1533 | $searchcond = implode(" AND ", $searchcond); | |
8a51efe9 | 1534 | } |
e8e2d7f1 | 1535 | |
082864f9 | 1536 | /// There are several possibilities |
1537 | /// 1. courseid = SITEID : The admin is searching messages by all users | |
1538 | /// 2. courseid = ?? : A teacher is searching messages by users in | |
1539 | /// one of their courses - currently disabled | |
1540 | /// 3. courseid = none : User is searching their own messages; | |
1541 | /// a. Messages from user | |
1542 | /// b. Messages to user | |
1543 | /// c. Messages to and from user | |
1544 | ||
1545 | if ($courseid == SITEID) { /// admin is searching all messages | |
3d143595 | 1546 | $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated |
d9eef08a | 1547 | FROM {message_read} m |
c3931654 | 1548 | WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); |
3d143595 | 1549 | $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated |
d9eef08a | 1550 | FROM {message} m |
c3931654 | 1551 | WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); |
d9eef08a | 1552 | |
1553 | } else if ($courseid !== 'none') { | |
082864f9 | 1554 | /// This has not been implemented due to security concerns |
d9eef08a | 1555 | $m_read = array(); |
1556 | $m_unread = array(); | |
e8e2d7f1 | 1557 | |
082864f9 | 1558 | } else { |
531e58f1 | 1559 | |
d9eef08a | 1560 | if ($fromme and $tome) { |
1561 | $searchcond .= " AND (m.useridfrom=:userid1 OR m.useridto=:userid2)"; | |
1562 | $params['userid1'] = $userid; | |
1563 | $params['userid2'] = $userid; | |
1564 | ||
1565 | } else if ($fromme) { | |
1566 | $searchcond .= " AND m.useridfrom=:userid"; | |
1567 | $params['userid'] = $userid; | |
531e58f1 | 1568 | |
d9eef08a | 1569 | } else if ($tome) { |
1570 | $searchcond .= " AND m.useridto=:userid"; | |
1571 | $params['userid'] = $userid; | |
1572 | } | |
531e58f1 | 1573 | |
3d143595 | 1574 | $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated |
d9eef08a | 1575 | FROM {message_read} m |
c3931654 | 1576 | WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); |
3d143595 | 1577 | $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated |
d9eef08a | 1578 | FROM {message} m |
c3931654 | 1579 | WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); |
531e58f1 | 1580 | |
e8e2d7f1 | 1581 | } |
1582 | ||
082864f9 | 1583 | /// The keys may be duplicated in $m_read and $m_unread so we can't |
1584 | /// do a simple concatenation | |
1585 | $message = array(); | |
d9eef08a | 1586 | foreach ($m_read as $m) { |
1587 | $messages[] = $m; | |
1588 | } | |
1589 | foreach ($m_unread as $m) { | |
1590 | $messages[] = $m; | |
1591 | } | |
e8e2d7f1 | 1592 | |
1593 | return (empty($messages)) ? false : $messages; | |
172186b8 | 1594 | } |
1595 | ||
bcab42da | 1596 | /** |
1597 | * Given a message object that we already know has a long message | |
1598 | * this function truncates the message nicely to the first | |
1599 | * sane place between $CFG->forum_longpost and $CFG->forum_shortpost | |
1600 | * @param string $message the message | |
1601 | * @param int $minlength the minimum length to trim the message to | |
1602 | * @return string the shortened message | |
1603 | */ | |
1604 | function message_shorten_message($message, $minlength = 0) { | |
e8e2d7f1 | 1605 | $i = 0; |
1606 | $tag = false; | |
1607 | $length = strlen($message); | |
1608 | $count = 0; | |
1609 | $stopzone = false; | |
1610 | $truncate = 0; | |
1611 | if ($minlength == 0) $minlength = MESSAGE_SHORTLENGTH; | |
531e58f1 | 1612 | |
e8e2d7f1 | 1613 | |
1614 | for ($i=0; $i<$length; $i++) { | |
1615 | $char = $message[$i]; | |
1616 | ||
1617 | switch ($char) { | |
1618 | case "<": | |
1619 | $tag = true; | |
1620 | break; | |
1621 | case ">": | |
1622 | $tag = false; | |
1623 | break; | |
1624 | default: | |
1625 | if (!$tag) { | |
1626 | if ($stopzone) { | |
1627 | if ($char == '.' or $char == ' ') { | |
1628 | $truncate = $i+1; | |
1629 | break 2; | |
1630 | } | |
1631 | } | |
1632 | $count++; | |
1633 | } | |
1634 | break; | |
1635 | } | |
1636 | if (!$stopzone) { | |
1637 | if ($count > $minlength) { | |
1638 | $stopzone = true; | |
1639 | } | |
1640 | } | |
1641 | } | |
1642 | ||
1643 | if (!$truncate) { | |
1644 | $truncate = $i; | |
1645 | } | |
1646 | ||
1647 | return substr($message, 0, $truncate); | |
1648 | } | |
1649 | ||
38c6a928 | 1650 | |
d9eef08a | 1651 | /** |
38c6a928 | 1652 | * Given a string and an array of keywords, this function looks |
1653 | * for the first keyword in the string, and then chops out a | |
1654 | * small section from the text that shows that word in context. | |
bcab42da | 1655 | * @param string $message the text to search |
1656 | * @param array $keywords array of keywords to find | |
38c6a928 | 1657 | */ |
1658 | function message_get_fragment($message, $keywords) { | |
1659 | ||
f3832596 | 1660 | $fullsize = 160; |
38c6a928 | 1661 | $halfsize = (int)($fullsize/2); |
1662 | ||
1663 | $message = strip_tags($message); | |
1664 | ||
1665 | foreach ($keywords as $keyword) { // Just get the first one | |
1666 | if ($keyword !== '') { | |
1667 | break; | |
1668 | } | |
1669 | } | |
1670 | if (empty($keyword)) { // None found, so just return start of message | |
1671 | return message_shorten_message($message, 30); | |
1672 | } | |
1673 | ||
1674 | $leadin = $leadout = ''; | |
1675 | ||
1676 | /// Find the start of the fragment | |
1677 | $start = 0; | |
1678 | $length = strlen($message); | |
1679 | ||
1680 | $pos = strpos($message, $keyword); | |
1681 | if ($pos > $halfsize) { | |
1682 | $start = $pos - $halfsize; | |
1683 | $leadin = '...'; | |
1684 | } | |
1685 | /// Find the end of the fragment | |
1686 | $end = $start + $fullsize; | |
1687 | if ($end > $length) { | |
1688 | $end = $length; | |
1689 | } else { | |
1690 | $leadout = '...'; | |
1691 | } | |
1692 | ||
1693 | /// Pull out the fragment and format it | |
1694 | ||
1695 | $fragment = substr($message, $start, $end - $start); | |
1696 | $fragment = $leadin.highlight(implode(' ',$keywords), $fragment).$leadout; | |
1697 | return $fragment; | |
1698 | } | |
1699 | ||
bcab42da | 1700 | /** |
1701 | * Retrieve the messages between two users | |
1702 | * @param object $user1 the current user | |
1703 | * @param object $user2 the other user | |
1704 | * @param int $limitnum the maximum number of messages to retrieve | |
1705 | * @param bool $viewingnewmessages are we currently viewing new messages? | |
1706 | */ | |
a813a748 AD |
1707 | function message_get_history($user1, $user2, $limitnum=0, $viewingnewmessages=false) { |
1708 | global $DB, $CFG; | |
fd1cb1e8 | 1709 | |
c8621a02 AD |
1710 | $messages = array(); |
1711 | ||
1712 | //we want messages sorted oldest to newest but if getting a subset of messages we need to sort | |
1713 | //desc to get the last $limitnum messages then flip the order in php | |
1714 | $sort = 'asc'; | |
1715 | if ($limitnum>0) { | |
1716 | $sort = 'desc'; | |
1717 | } | |
1718 | ||
a813a748 AD |
1719 | $notificationswhere = null; |
1720 | //we have just moved new messages to read. If theyre here to see new messages dont hide notifications | |
1721 | if (!$viewingnewmessages && $CFG->messaginghidereadnotifications) { | |
1722 | $notificationswhere = 'AND notification=0'; | |
1723 | } | |
1724 | ||
33bd52f3 AD |
1725 | //prevent notifications of your own actions appearing in your own message history |
1726 | $ownnotificationwhere = ' AND NOT (useridfrom=? AND notification=1)'; | |
1727 | ||
a813a748 | 1728 | if ($messages_read = $DB->get_records_select('message_read', "((useridto = ? AND useridfrom = ?) OR |
33bd52f3 AD |
1729 | (useridto = ? AND useridfrom = ?)) $notificationswhere $ownnotificationwhere", |
1730 | array($user1->id, $user2->id, $user2->id, $user1->id, $user1->id), | |
c8621a02 AD |
1731 | "timecreated $sort", '*', 0, $limitnum)) { |
1732 | foreach ($messages_read as $message) { | |
1733 | $messages[$message->timecreated] = $message; | |
1734 | } | |
1735 | } | |
33bd52f3 AD |
1736 | if ($messages_new = $DB->get_records_select('message', "((useridto = ? AND useridfrom = ?) OR |
1737 | (useridto = ? AND useridfrom = ?)) $ownnotificationwhere", | |
1738 | array($user1->id, $user2->id, $user2->id, $user1->id, $user1->id), | |
c8621a02 | 1739 | "timecreated $sort", '*', 0, $limitnum)) { |
b9ee0638 | 1740 | foreach ($messages_new as $message) { |
c8621a02 | 1741 | $messages[$message->timecreated] = $message; |
b9ee0638 | 1742 | } |
1743 | } | |
c8621a02 AD |
1744 | |
1745 | //if we only want the last $limitnum messages | |
2846b9a6 AD |
1746 | ksort($messages); |
1747 | $messagecount = count($messages); | |
1748 | if ($limitnum>0 && $messagecount>$limitnum) { | |
1749 | $messages = array_slice($messages, $messagecount-$limitnum, $limitnum, true); | |
c8621a02 | 1750 | } |
3a11c09f | 1751 | |
b9ee0638 | 1752 | return $messages; |
1753 | } | |
1754 | ||
bcab42da | 1755 | /** |
1756 | * Print the message history between two users | |
1757 | * @param object $user1 the current user | |
1758 | * @param object $user2 the other user | |
1759 | * @param string $search search terms to highlight | |
1760 | * @param int $messagelimit maximum number of messages to return | |
1761 | * @param string $messagehistorylink the html for the message history link or false | |
1762 | * @param bool $viewingnewmessages are we currently viewing new messages? | |
1763 | */ | |
a813a748 | 1764 | function message_print_message_history($user1,$user2,$search='',$messagelimit=0, $messagehistorylink=false, $viewingnewmessages=false) { |
c8621a02 AD |
1765 | global $CFG, $OUTPUT; |
1766 | ||
1767 | echo $OUTPUT->box_start('center'); | |
bcab42da | 1768 | echo html_writer::start_tag('table', array('cellpadding' => '10', 'class' => 'message_user_pictures')); |
1769 | echo html_writer::start_tag('tr'); | |
1770 | ||
1771 | echo html_writer::start_tag('td', array('align' => 'center', 'id' => 'user1')); | |
1772 | echo $OUTPUT->user_picture($user1, array('size' => 100, 'courseid' => SITEID)); | |
1773 | echo html_writer::tag('div', fullname($user1), array('class' => 'heading')); | |
1774 | echo html_writer::end_tag('td'); | |
1775 | ||
1776 | echo html_writer::start_tag('td', array('align' => 'center')); | |
1777 | echo html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/left'), 'alt' => get_string('from'))); | |
1778 | echo html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/right'), 'alt' => get_string('to'))); | |
1779 | echo html_writer::end_tag('td'); | |
1780 | ||
1781 | echo html_writer::start_tag('td', array('align' => 'center', 'id' => 'user2')); | |
1782 | echo $OUTPUT->user_picture($user2, array('size' => 100, 'courseid' => SITEID)); | |
1783 | echo html_writer::tag('div', fullname($user2), array('class' => 'heading')); | |
c8621a02 AD |
1784 | |
1785 | if (isset($user2->iscontact) && isset($user2->isblocked)) { | |
1786 | $incontactlist = $user2->iscontact; | |
1787 | $isblocked = $user2->isblocked; | |
1788 | ||
1789 | $script = null; | |
1790 | $text = true; | |
1791 | $icon = false; | |
1792 | ||
1793 | $strcontact = message_get_contact_add_remove_link($incontactlist, $isblocked, $user2, $script, $text, $icon); | |
1794 | $strblock = message_get_contact_block_link($incontactlist, $isblocked, $user2, $script, $text, $icon); | |
41117b3e AD |
1795 | $useractionlinks = $strcontact.' |'.$strblock; |
1796 | ||
bcab42da | 1797 | echo html_writer::tag('div', $useractionlinks, array('class' => 'useractionlinks')); |
c8621a02 AD |
1798 | } |
1799 | ||
bcab42da | 1800 | echo html_writer::end_tag('td'); |
1801 | echo html_writer::end_tag('tr'); | |
1802 | echo html_writer::end_tag('table'); | |
c8621a02 AD |
1803 | echo $OUTPUT->box_end(); |
1804 | ||
1805 | if (!empty($messagehistorylink)) { | |
1806 | echo $messagehistorylink; | |
1807 | } | |
1808 | ||
1809 | /// Get all the messages and print them | |
a813a748 | 1810 | if ($messages = message_get_history($user1, $user2, $messagelimit, $viewingnewmessages)) { |
c8621a02 AD |
1811 | $tablecontents = ''; |
1812 | ||
8e803c3f | 1813 | $current = new stdClass(); |
c8621a02 AD |
1814 | $current->mday = ''; |
1815 | $current->month = ''; | |
1816 | $current->year = ''; | |
1817 | $messagedate = get_string('strftimetime'); | |
1818 | $blockdate = get_string('strftimedaydate'); | |
1819 | foreach ($messages as $message) { | |
a813a748 AD |
1820 | if ($message->notification) { |
1821 | $notificationclass = ' notification'; | |
1822 | } else { | |
1823 | $notificationclass = null; | |
1824 | } | |
c8621a02 AD |
1825 | $date = usergetdate($message->timecreated); |
1826 | if ($current->mday != $date['mday'] | $current->month != $date['month'] | $current->year != $date['year']) { | |
1827 | $current->mday = $date['mday']; | |
1828 | $current->month = $date['month']; | |
1829 | $current->year = $date['year']; | |
1830 | ||
bcab42da | 1831 | $datestring = html_writer::empty_tag('a', array('name' => $date['year'].$date['mon'].$date['mday'])); |
1832 | $tablecontents .= html_writer::tag('div', $datestring, array('class' => 'mdl-align heading')); | |
1833 | ||
1834 | $tablecontents .= $OUTPUT->heading(userdate($message->timecreated, $blockdate), 4, 'mdl-align'); | |
c8621a02 | 1835 | } |
bcab42da | 1836 | |
1837 | $formatted_message = $side = null; | |
c8621a02 | 1838 | if ($message->useridfrom == $user1->id) { |
bcab42da | 1839 | $formatted_message = message_format_message($message, $messagedate, $search, 'me'); |
1840 | $side = 'left'; | |
c8621a02 | 1841 | } else { |
bcab42da | 1842 | $formatted_message = message_format_message($message, $messagedate, $search, 'other'); |
1843 | $side = 'right'; | |
c8621a02 | 1844 | } |
bcab42da | 1845 | $tablecontents .= html_writer::tag('div', $formatted_message, array('class' => "mdl-left $side $notificationclass")); |
c8621a02 AD |
1846 | } |
1847 | ||
bcab42da | 1848 | echo html_writer::nonempty_tag('div', $tablecontents, array('class' => 'mdl-left messagehistory')); |
c8621a02 | 1849 | } else { |
bcab42da | 1850 | echo html_writer::nonempty_tag('div', '('.get_string('nomessagesfound', 'message').')', array('class' => 'mdl-align messagehistory')); |
c8621a02 AD |
1851 | } |
1852 | } | |
1853 | ||
bcab42da | 1854 | /** |
1855 | * Format a message for display in the message history | |
1856 | * @param object $message the message object | |
1857 | * @param string $format optional date format | |
1858 | * @param string $keywords keywords to highlight | |
1859 | * @param string $class CSS class to apply to the div around the message | |
1860 | * @return string the formatted message | |
1861 | */ | |
1862 | function message_format_message($message, $format='', $keywords='', $class='other') { | |
acd21a2d | 1863 | |
1864 | static $dateformat; | |
1865 | ||
bcab42da | 1866 | //if we haven't previously set the date format or they've supplied a new one |
1867 | if ( empty($dateformat) || (!empty($format) && $dateformat != $format) ) { | |
acd21a2d | 1868 | if ($format) { |
1869 | $dateformat = $format; | |
1870 | } else { | |
bcab42da | 1871 | $dateformat = get_string('strftimedatetimeshort'); |
acd21a2d | 1872 | } |
b9ee0638 | 1873 | } |
acd21a2d | 1874 | $time = userdate($message->timecreated, $dateformat); |
8e803c3f | 1875 | $options = new stdClass(); |
ff6048dd | 1876 | $options->para = false; |
6ee2611c AD |
1877 | |
1878 | //if supplied display small messages as fullmessage may contain boilerplate text that shouldnt appear in the messaging UI | |
1879 | if (!empty($message->smallmessage)) { | |
7e98f60b | 1880 | $messagetext = format_text(s($message->smallmessage), FORMAT_MOODLE, $options); |
6ee2611c | 1881 | } else { |
7e98f60b | 1882 | $messagetext = format_text(s($message->fullmessage), $message->fullmessageformat, $options); |
6ee2611c AD |
1883 | } |
1884 | ||
bcab42da | 1885 | $messagetext .= message_format_contexturl($message); |
1886 | ||
1887 | if ($keywords) { | |
1888 | $messagetext = highlight($keywords, $messagetext); | |
1889 | } | |
1890 | ||
1891 | return '<div class="message '.$class.'"><a name="m'.$message->id.'"></a> <span class="time">'.$time.'</span>: <span class="content">'.$messagetext.'</span></div>'; | |
1892 | } | |
1893 | ||
1894 | /** | |
1895 | * Format a the context url and context url name of a message for display | |
1896 | * @param object $message the message object | |
1897 | * @return string the formatted string | |
1898 | */ | |
1899 | function message_format_contexturl($message) { | |
1900 | $s = null; | |
1901 | ||
14a0e7dd AD |
1902 | if (!empty($message->contexturl)) { |
1903 | $displaytext = null; | |
1904 | if (!empty($message->contexturlname)) { | |
1905 | $displaytext= $message->contexturlname; | |
1906 | } else { | |
1907 | $displaytext= $message->contexturl; | |
1908 | } | |
bcab42da | 1909 | $s .= html_writer::start_tag('div',array('class' => 'messagecontext')); |
1910 | $s .= get_string('view').': '.html_writer::tag('a', $displaytext, array('href' => $message->contexturl)); | |
1911 | $s .= html_writer::end_tag('div'); | |
38c6a928 | 1912 | } |
14a0e7dd | 1913 | |
bcab42da | 1914 | return $s; |
b9ee0638 | 1915 | } |
e8e2d7f1 | 1916 | |
fd1cb1e8 | 1917 | /** |
bcab42da | 1918 | * Send a message from one user to another. Will be delivered according to the message recipients messaging preferences |
1919 | * @param object $userfrom the message sender | |
1920 | * @param object $userto the message recipient | |
1921 | * @param string $message the message | |
1922 | * @param int $format message format such as FORMAT_PLAIN or FORMAT_HTML | |
3a00a167 | 1923 | * @return int|false the ID of the new message or false |
405f01ee | 1924 | */ |
bcab42da | 1925 | function message_post_message($userfrom, $userto, $message, $format) { |
27a39763 | 1926 | global $SITE, $CFG, $USER; |
4ffa1463 | 1927 | |
8e803c3f | 1928 | $eventdata = new stdClass(); |
1560760f | 1929 | $eventdata->component = 'moodle'; |
1c50df9f | 1930 | $eventdata->name = 'instantmessage'; |
3b120e46 | 1931 | $eventdata->userfrom = $userfrom; |
1932 | $eventdata->userto = $userto; | |
4ffa1463 AD |
1933 | |
1934 | //using string manager directly so that strings in the message will be in the message recipients language rather than the senders | |
1935 | $eventdata->subject = get_string_manager()->get_string('unreadnewmessage', 'message', fullname($userfrom), $userto->lang); | |
1936 | ||
bcab42da | 1937 | if ($format == FORMAT_HTML) { |
156205fc AD |
1938 | $eventdata->fullmessage = ''; |
1939 | $eventdata->fullmessagehtml = $message; | |
1940 | } else { | |
1941 | $eventdata->fullmessage = $message; | |
1942 | $eventdata->fullmessagehtml = ''; | |
1943 | } | |
31da70cc | 1944 | |
c8621a02 | 1945 | $eventdata->fullmessageformat = $format; |
7e98f60b | 1946 | $eventdata->smallmessage = $message;//store the message unfiltered. Clean up on output. |
27a39763 AD |
1947 | |
1948 | $s = new stdClass(); | |
1949 | $s->sitename = $SITE->shortname; | |
536a56e7 | 1950 | $s->url = $CFG->wwwroot.'/message/index.php?user='.$userto->id.'&id='.$userfrom->id; |
27a39763 | 1951 | |
4ffa1463 | 1952 | $emailtagline = get_string_manager()->get_string('emailtagline', 'message', $s, $userto->lang); |
6ee2611c AD |
1953 | if (!empty($eventdata->fullmessage)) { |
1954 | $eventdata->fullmessage .= "\n\n---------------------------------------------------------------------\n".$emailtagline; | |
1955 | } | |
6ee2611c AD |
1956 | if (!empty($eventdata->fullmessagehtml)) { |
1957 | $eventdata->fullmessagehtml .= "<br /><br />---------------------------------------------------------------------<br />".$emailtagline; | |
1958 | } | |
31da70cc | 1959 | |
a1b53dcf | 1960 | $eventdata->timecreated = time(); |
7c7d3afa | 1961 | return message_send($eventdata); |
405f01ee | 1962 | } |
e8e2d7f1 | 1963 | |
c135a425 | 1964 | |
fd1cb1e8 | 1965 | /** |
c135a425 | 1966 | * Returns a list of all user ids who have used messaging in the site |
1967 | * This was the simple way to code the SQL ... is it going to blow up | |
1968 | * on large datasets? | |
b47dcdf0 EL |
1969 | * |
1970 | * @todo: deprecated - to be deleted in 2.2 | |
c135a425 | 1971 | */ |
1972 | function message_get_participants() { | |
fd1cb1e8 | 1973 | global $CFG, $DB; |
1974 | ||
76b0191c | 1975 | return $DB->get_records_sql("SELECT useridfrom as id,1 FROM {message} |
1976 | UNION SELECT useridto as id,1 FROM {message} | |
1977 | UNION SELECT useridfrom as id,1 FROM {message_read} | |
1978 | UNION SELECT useridto as id,1 FROM {message_read} | |
1979 | UNION SELECT userid as id,1 FROM {message_contacts} | |
1980 | UNION SELECT contactid as id,1 from {message_contacts}"); | |
c135a425 | 1981 | } |
1982 | ||
f46b6587 | 1983 | /** |
1d422980 | 1984 | * Print a row of contactlist displaying user picture, messages waiting and |
f46b6587 | 1985 | * block links etc |
bcab42da | 1986 | * @param object $contact contact object containing all fields required for $OUTPUT->user_picture() |
1987 | * @param bool $incontactlist is the user a contact of ours? | |
1988 | * @param bool $isblocked is the user blocked? | |
1989 | * @param string $selectcontacturl the url to send the user to when a contact's name is clicked | |
1990 | * @param bool $showactionlinks display action links next to the other users (add contact, block user etc) | |
1991 | * @param object $selecteduser the user the current user is viewing (if any). They will be highlighted. | |
f46b6587 | 1992 | */ |
a402c309 | 1993 | function message_print_contactlist_user($contact, $incontactlist = true, $isblocked = false, $selectcontacturl = null, $showactionlinks = true, $selecteduser=null) { |
c8621a02 | 1994 | global $OUTPUT, $USER; |
f46b6587 | 1995 | $fullname = fullname($contact); |
1996 | $fullnamelink = $fullname; | |
1997 | ||
a402c309 | 1998 | $linkclass = ''; |
bcab42da | 1999 | if (!empty($selecteduser) && $contact->id == $selecteduser->id) { |
a402c309 AD |
2000 | $linkclass = 'messageselecteduser'; |
2001 | } | |
2002 | ||
f46b6587 | 2003 | /// are there any unread messages for this contact? |
2004 | if ($contact->messagecount > 0 ){ | |
2005 | $fullnamelink = '<strong>'.$fullnamelink.' ('.$contact->messagecount.')</strong>'; | |
2006 | } | |
2007 | ||
c8621a02 | 2008 | $strcontact = $strblock = $strhistory = null; |
f46b6587 | 2009 | |
c8621a02 AD |
2010 | if ($showactionlinks) { |
2011 | $strcontact = message_get_contact_add_remove_link($incontactlist, $isblocked, $contact); | |
2012 | $strblock = message_get_contact_block_link($incontactlist, $isblocked, $contact); | |
2013 | $strhistory = message_history_link($USER->id, $contact->id, true, '', '', 'icon'); | |
f46b6587 | 2014 | } |
2015 | ||
bcab42da | 2016 | echo html_writer::start_tag('tr'); |
2017 | echo html_writer::start_tag('td', array('class' => 'pix')); | |
2018 | echo $OUTPUT->user_picture($contact, array('size' => 20, 'courseid' => SITEID)); | |
2019 | echo html_writer::end_tag('td'); | |
2020 | ||
2021 | echo html_writer::start_tag('td', array('class' => 'contact')); | |
1d422980 | 2022 | |
40a26286 | 2023 | $popupoptions = array( |
c8621a02 AD |
2024 | 'height' => MESSAGE_DISCUSSION_HEIGHT, |
2025 | 'width' => MESSAGE_DISCUSSION_WIDTH, | |
40a26286 | 2026 | 'menubar' => false, |
2027 | 'location' => false, | |
2028 | 'status' => true, | |
2029 | 'scrollbars' => true, | |
2030 | 'resizable' => true); | |
2031 | ||
c8621a02 AD |
2032 | $link = $action = null; |
2033 | if (!empty($selectcontacturl)) { | |
25bd5c75 | 2034 | $link = new moodle_url($selectcontacturl.'&user2='.$contact->id); |
c8621a02 | 2035 | } else { |
06d30c43 AD |
2036 | //can $selectcontacturl be removed and maybe the be removed and hardcoded? |
2037 | $link = new moodle_url("/message/index.php?id=$contact->id"); | |
c8621a02 AD |
2038 | $action = new popup_action('click', $link, "message_$contact->id", $popupoptions); |
2039 | } | |
bcab42da | 2040 | echo $OUTPUT->action_link($link, $fullnamelink, $action, array('class' => $linkclass,'title' => get_string('sendmessageto', 'message', $fullname))); |
2041 | ||
2042 | echo html_writer::end_tag('td'); | |
f46b6587 | 2043 | |
bcab42da | 2044 | echo html_writer::tag('td', ' '.$strcontact.$strblock.' '.$strhistory, array('class' => 'link')); |
2045 | ||
2046 | echo html_writer::end_tag('tr'); | |
f46b6587 | 2047 | } |
2048 | ||
bcab42da | 2049 | /** |
2050 | * Constructs the add/remove contact link to display next to other users | |
2051 | * @param bool $incontactlist is the user a contact | |
2052 | * @param bool $isblocked is the user blocked | |
2053 | * @param type $contact contact object | |
2054 | * @param string $script the URL to send the user to when the link is clicked. If null, the current page. | |
2055 | * @param bool $text include text next to the icons? | |
2056 | * @param bool $icon include a graphical icon? | |
2057 | * @return string | |
2058 | */ | |
c8621a02 AD |
2059 | function message_get_contact_add_remove_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { |
2060 | $strcontact = ''; | |
2061 | ||
2062 | if($incontactlist){ | |
2063 | $strcontact = message_contact_link($contact->id, 'remove', true, $script, $text, $icon); | |
2064 | } else if ($isblocked) { | |
2065 | $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); | |
2066 | } else{ | |
2067 | $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); | |
2068 | } | |
2069 | ||
2070 | return $strcontact; | |
2071 | } | |
2072 | ||
bcab42da | 2073 | /** |
2074 | * Constructs the block contact link to display next to other users | |
2075 | * @param bool $incontactlist is the user a contact | |
2076 | * @param bool $isblocked is the user blocked | |
2077 | * @param type $contact contact object | |
2078 | * @param string $script the URL to send the user to when the link is clicked. If null, the current page. | |
2079 | * @param bool $text include text next to the icons? | |
2080 | * @param bool $icon include a graphical icon? | |
2081 | * @return string | |
2082 | */ | |
c8621a02 AD |
2083 | function message_get_contact_block_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { |
2084 | $strblock = ''; | |
2085 | ||
2086 | //commented out to allow the user to block a contact without having to remove them first | |
2087 | /*if ($incontactlist) { | |
2088 | //$strblock = ''; | |
2089 | } else*/ | |
2090 | if ($isblocked) { | |
2091 | $strblock = ' '.message_contact_link($contact->id, 'unblock', true, $script, $text, $icon); | |
2092 | } else{ | |
2093 | $strblock = ' '.message_contact_link($contact->id, 'block', true, $script, $text, $icon); | |
2094 | } | |
2095 | ||
2096 | return $strblock; | |
2097 | } | |
2098 | ||
6bdf4c99 | 2099 | /** |
bcab42da | 2100 | * Moves messages from a particular user from the message table (unread messages) to message_read |
2101 | * This is typically only used when a user is deleted | |
2102 | * @param object $userid User id | |
6bdf4c99 | 2103 | * @return boolean success |
2104 | */ | |
2105 | function message_move_userfrom_unread2read($userid) { | |
6bdf4c99 | 2106 | global $DB; |
2107 | ||
71666cf3 | 2108 | // move all unread messages from message table to message_read |
6bdf4c99 | 2109 | if ($messages = $DB->get_records_select('message', 'useridfrom = ?', array($userid), 'timecreated')) { |
2110 | foreach ($messages as $message) { | |
ee7cd81a | 2111 | message_mark_message_read($message, 0); //set timeread to 0 as the message was never read |
6bdf4c99 | 2112 | } |
2113 | } | |
2114 | return true; | |
2115 | } | |
2116 | ||
ee7cd81a AD |
2117 | /** |
2118 | * marks ALL messages being sent from $fromuserid to $touserid as read | |
2119 | * @param int $touserid the id of the message recipient | |
2120 | * @param int $fromuserid the id of the message sender | |
2121 | * @return void | |
2122 | */ | |
1560760f | 2123 | function message_mark_messages_read($touserid, $fromuserid){ |
c8621a02 AD |
2124 | global $DB; |
2125 | ||
ee7cd81a | 2126 | $sql = 'SELECT m.* FROM {message} m WHERE m.useridto=:useridto AND m.useridfrom=:useridfrom'; |
bcab42da | 2127 | $messages = $DB->get_recordset_sql($sql, array('useridto' => $touserid,'useridfrom' => $fromuserid)); |
1560760f | 2128 | |
c8621a02 | 2129 | foreach ($messages as $message) { |
ee7cd81a AD |
2130 | message_mark_message_read($message, time()); |
2131 | } | |
19c5532b AD |
2132 | |
2133 | $messages->close(); | |
ee7cd81a AD |
2134 | } |
2135 | ||
2136 | /** | |
2137 | * Mark a single message as read | |
2138 | * @param message an object with an object property ie $message->id which is an id in the message table | |
2139 | * @param int $timeread the timestamp for when the message should be marked read. Usually time(). | |
2140 | * @param bool $messageworkingempty Is the message_working table already confirmed empty for this message? | |
3a00a167 | 2141 | * @return int the ID of the message in the message_read table |
ee7cd81a AD |
2142 | */ |
2143 | function message_mark_message_read($message, $timeread, $messageworkingempty=false) { | |
2144 | global $DB; | |
31da70cc | 2145 | |
ee7cd81a | 2146 | $message->timeread = $timeread; |
c8621a02 | 2147 | |
ee7cd81a AD |
2148 | $messageid = $message->id; |
2149 | unset($message->id);//unset because it will get a new id on insert into message_read | |
1560760f | 2150 | |
ee7cd81a AD |
2151 | //If any processors have pending actions abort them |
2152 | if (!$messageworkingempty) { | |
2153 | $DB->delete_records('message_working', array('unreadmessageid' => $messageid)); | |
c8621a02 | 2154 | } |
3a00a167 | 2155 | $messagereadid = $DB->insert_record('message_read', $message); |
ee7cd81a | 2156 | $DB->delete_records('message', array('id' => $messageid)); |
3a00a167 | 2157 | return $messagereadid; |
65fbace7 | 2158 | } |
bcab42da | 2159 | |
2160 | /** | |
2161 | * A helper function that prints a formatted heading | |
2162 | * @param string $title the heading to display | |
2163 | * @param int $colspan | |
2164 | */ | |
2165 | function message_print_heading($title, $colspan=3) { | |
2166 | echo html_writer::start_tag('tr'); | |
2167 | echo html_writer::tag('td', $title, array('colspan' => $colspan, 'class' => 'heading')); | |
2168 | echo html_writer::end_tag('tr'); | |
2169 | } |