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