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