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