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