172186b8 |
1 | <?php |
2 | /// library functions for messaging |
3 | |
e8e2d7f1 |
4 | |
5 | define ('MESSAGE_SHORTLENGTH', 300); |
172186b8 |
6 | |
7 | |
8 | function message_print_contacts() { |
e8e2d7f1 |
9 | global $USER, $CFG; |
10 | |
11 | $timetoshowusers = 300; //Seconds default |
12 | if (isset($CFG->block_online_users_timetosee)) { |
13 | $timetoshowusers = $CFG->block_online_users_timetosee * 60; |
14 | } |
15 | $timefrom = time()-$timetoshowusers; |
16 | |
17 | |
18 | /// get lists of contacts and unread messages |
19 | $onlinecontacts = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture |
20 | FROM {$CFG->prefix}user u, {$CFG->prefix}message_contacts mc |
21 | WHERE mc.userid='$USER->id' AND u.id=mc.contactid AND u.lastaccess>=$timefrom |
22 | AND mc.blocked='0' |
23 | ORDER BY u.lastaccess DESC"); |
24 | |
25 | $offlinecontacts = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture |
26 | FROM {$CFG->prefix}user u, {$CFG->prefix}message_contacts mc |
27 | WHERE mc.userid='$USER->id' AND u.id=mc.contactid AND u.lastaccess<$timefrom |
28 | AND mc.blocked='0' |
29 | ORDER BY u.lastaccess DESC"); |
30 | |
31 | $unreadmessages = get_records_sql("SELECT m.id, m.useridfrom, u.firstname, u.lastname, u.picture |
32 | FROM {$CFG->prefix}user u, {$CFG->prefix}message m |
33 | WHERE m.useridto='$USER->id' AND u.id=m.useridfrom"); |
34 | |
35 | $blockedcontacts = get_records_select('message_contacts', "userid='$USER->id' AND blocked='1'", '', 'contactid, id'); |
36 | |
37 | |
38 | echo '<table id="message_contacts" align="center" cellspacing="2" cellpadding="0">'; |
39 | echo '<tr><td colspan="2"><strong>'.get_string('mycontacts', 'message').'</strong></td></tr>'; |
40 | |
41 | |
42 | /// print out list of online contacts |
43 | echo '<tr><td width="20"> </td>'; |
44 | echo '<td id="message_onlinecontacts">'; |
45 | |
46 | $countcontacts = (is_array($onlinecontacts)) ? count($onlinecontacts) : 0; |
47 | |
48 | echo '<table class="message_contacts">'; |
49 | echo '<tr><td colspan="3">'; |
50 | echo '<strong>'.get_string('onlinecontacts', 'message', $countcontacts).'</strong>'; |
51 | echo '</td></tr>'; |
52 | |
53 | if (!empty($onlinecontacts)) { |
54 | foreach ($onlinecontacts as $contact) { |
55 | if ($contact->blocked == 1) continue; |
56 | $strcontact = ''; |
57 | /// are there any unread messages for this contact? |
58 | if (($unread = message_count_messages($unreadmessages, 'useridfrom', $contact->id)) > 0) { |
59 | $strcontact .= '<strong>( '.get_string('unreadmessages', 'message', $unread).')</strong>'; |
60 | } |
61 | /// link to remove from contact list |
62 | $strcontact .= ' [<a href="index.php?tab=contacts&removecontact='.$contact->id. |
63 | '&sesskey='.$USER->sesskey.'" title="'. |
64 | get_string('removecontact', 'message').'">'. |
65 | get_string('removecontact', 'message').'</a>]</td>'; |
66 | |
67 | echo '<tr><td class="message_pic">'; |
68 | print_user_picture($contact->id, SITEID, $contact->picture, 20, false, false); |
69 | echo '</td>'; |
70 | echo '<td class="message_contact">'; |
71 | link_to_popup_window("/message/user.php?id=$contact->id", "message_$contact->id", fullname($contact), 400, 400, get_string('sendmessageto', 'message', fullname($contact))); |
72 | echo '</td>'; |
73 | echo '<td class="message_link">'.$strcontact.'</td>'; |
74 | echo '</tr>'; |
75 | } |
76 | } |
77 | echo '<tr><td colspan="3"> </td></tr>'; |
78 | echo '</table>'; |
79 | |
80 | echo '</td></tr>'; |
81 | |
82 | |
83 | /// print out list of offline contacts |
84 | echo '<tr><td width="20"> </td>'; |
85 | echo '<td id="message_offlinecontacts">'; |
86 | |
87 | $countcontacts = (is_array($offlinecontacts)) ? count($offlinecontacts) : 0; |
88 | |
89 | echo '<table class="message_contacts">'; |
90 | echo '<tr><td colspan="3">'; |
91 | echo '<strong>'.get_string('offlinecontacts', 'message', $countcontacts).'</strong>'; |
92 | echo '</td></tr>'; |
93 | |
94 | if (!empty($offlinecontacts)) { |
95 | foreach ($offlinecontacts as $contact) { |
96 | if ($contact->blocked == 1) continue; |
97 | $strcontact = ''; |
98 | /// are there any unread messages for this contact? |
99 | if (($unread = message_count_messages($unreadmessages, 'useridfrom', $contact->id)) > 0) { |
100 | $strcontact .= '<strong>( '.get_string('unreadmessages', 'message', $unread).')</strong>'; |
101 | } |
102 | /// link to remove from contact list |
103 | $strcontact .= ' [<a href="index.php?tab=contacts&removecontact='.$contact->id. |
104 | '&sesskey='.$USER->sesskey.'" title="'. |
105 | get_string('removecontact', 'message').'">'. |
106 | get_string('removecontact', 'message').'</a>]</td>'; |
107 | |
108 | echo '<tr><td class="message_pic">'; |
109 | print_user_picture($contact->id, SITEID, $contact->picture, 20, false, false); |
110 | echo '</td>'; |
111 | echo '<td class="message_contact">'; |
112 | link_to_popup_window("/message/user.php?id=$contact->id", "message_$contact->id", fullname($contact), 400, 400, get_string('sendmessageto', 'message', fullname($contact))); |
113 | echo '</td>'; |
114 | echo '<td class="message_link">'.$strcontact.'</td>'; |
115 | echo '</tr>'; |
116 | } |
117 | } |
118 | echo '<tr><td colspan="3"> </td></tr>'; |
119 | echo '</table>'; |
120 | |
121 | echo '</td></tr>'; |
122 | |
123 | |
124 | /// Cycle through messages and extract those that are from unknown contacts |
125 | /// We can take advantage of the keys for $onlinecontacts and $offlinecontacts |
126 | /// which are set to the userid and therefore we just need to see if the key |
127 | /// exists in either of those arrays |
128 | /// We can also discard any messages from users in our blocked contact list |
129 | $unknownmessages = array(); |
130 | if (!empty($unreadmessages)) { |
131 | /// make sure we have valid arrays to test against - they may be boolean false |
132 | if (empty($onlinecontacts)) $onlinecontacts = array(); |
133 | if (empty($offlinecontacts)) $offlinecontacts = array(); |
134 | if (empty($blockedcontacts)) $blockedcontacts = array(); |
135 | foreach ($unreadmessages as $unreadmessage) { |
136 | if (array_key_exists($unreadmessage->useridfrom, $onlinecontacts) or |
137 | array_key_exists($unreadmessage->useridfrom, $offlinecontacts) or |
138 | array_key_exists($unreadmessage->useridfrom, $blockedcontacts) ) { |
139 | continue; |
140 | } |
141 | if (!isset($unknownmessages[$unreadmessage->useridfrom])) { |
142 | $message = $unreadmessage; |
143 | $message->count = 1; |
144 | $unknownmessages[$unreadmessage->useridfrom] = $message; |
145 | } else { |
146 | $unknownmessages[$unreadmessage->useridfrom]->count++; |
147 | } |
148 | } |
149 | } |
150 | |
151 | /// print out list of incoming contacts |
152 | if (!empty($unknownmessages)) { |
153 | echo '<tr><td colspan="2">'; |
154 | echo '<strong>'.get_string('incomingcontacts', 'message', count($unknownmessages)).'</strong>'; |
155 | echo '</td></tr>'; |
156 | echo '<tr><td width="20"> </td>'; |
157 | echo '<td id="message_unknowncontacts">'; |
158 | |
159 | echo '<table class="message_contacts">'; |
160 | foreach ($unknownmessages as $messageuser) { |
161 | $strcontact = '<strong>( '.get_string('unreadmessages', 'message', $messageuser->count).')</strong>'; |
162 | /// link to add to contact list |
163 | $strcontact .= ' [<a href="index.php?tab=contacts&addcontact='.$messageuser->useridfrom. |
164 | '&sesskey='.$USER->sesskey.'" title="'. |
165 | get_string('addcontact', 'message').'">'. |
166 | get_string('addcontact', 'message').'</a>]</td>'; |
167 | $strblock = '[<a href="index.php?tab=contacts&blockcontact='.$messageuser->useridfrom. |
168 | '&sesskey='.$USER->sesskey.'" title="'. |
169 | get_string('blockcontact', 'message').'">'. |
170 | get_string('blockcontact', 'message').'</a>]</td>'; |
171 | |
172 | echo '<tr><td class="message_pic">'; |
173 | print_user_picture($messageuser->useridfrom, SITEID, $messageuser->picture, 20, false, false); |
174 | echo '</td>'; |
175 | echo '<td class="message_contact">'; |
176 | link_to_popup_window("/message/user.php?id=$messageuser->useridfrom", "message_$messageuser->useridfrom", fullname($messageuser), 400, 400, get_string('sendmessageto', 'message', fullname($messageuser))); |
177 | echo '</td>'; |
178 | echo '<td class="message_link">'.$strcontact.'</td>'; |
179 | echo '<td class="message_link">'.$strblock.'</td>'; |
180 | echo '</tr>'; |
181 | } |
182 | echo '</table>'; |
183 | echo '</td></tr>'; |
184 | } |
185 | |
186 | echo '</table>'; |
187 | } |
188 | |
189 | |
190 | |
191 | |
192 | /// $messagearray is an array of objects |
193 | /// $field is a valid property of object |
194 | /// $value is the value $field should equal to be counted |
195 | /// if $field is empty then return count of the whole array |
196 | /// if $field is non-existent then return 0; |
197 | function message_count_messages($messagearray, $field='', $value='') { |
198 | if (!is_array($messagearray)) return 0; |
199 | if ($field == '' or empty($messagearray)) return count($messagearray); |
200 | |
201 | $count = 0; |
202 | foreach ($messagearray as $message) { |
203 | $count += ($message->$field == $value) ? 1 : 0; |
204 | } |
205 | return $count; |
172186b8 |
206 | } |
207 | |
e8e2d7f1 |
208 | |
172186b8 |
209 | function message_print_search() { |
210 | global $USER; |
211 | |
212 | if ($frm = data_submitted()) { |
213 | |
214 | message_print_search_results($frm); |
215 | |
216 | } else { |
172186b8 |
217 | if ($teachers = get_records('user_teachers', 'userid', $USER->id, '', 'id, course')) { |
218 | |
219 | $courses = get_courses('all', 'c.sortorder ASC', 'c.id, c.shortname'); |
220 | $cs = '<select name="courseselect">'; |
221 | foreach ($teachers as $tcourse) { |
222 | $cs .= "<option value=\"$tcourse->course\">".$courses[$tcourse->course]->shortname."</option>\n"; |
223 | } |
224 | $cs .= '</select>'; |
225 | } |
226 | |
227 | include('search.html'); |
228 | } |
229 | } |
230 | |
231 | function message_print_settings() { |
e8e2d7f1 |
232 | global $ME, $USER; |
233 | |
172186b8 |
234 | if ($frm = data_submitted()) { |
e8e2d7f1 |
235 | $pref = array(); |
236 | $pref['message_showmessagewindow'] = (isset($frm->showmessagewindow)) ? '1' : '0'; |
237 | $pref['message_beepnewmessage'] = (isset($frm->beepnewmessage)) ? '1' : '0'; |
238 | $pref['message_maxmessages'] = ((int)$frm->maxmessages > 0) ? (int)$frm->maxmessages : '20'; |
239 | $pref['message_deletemessagesdays'] = ((int)($frm->deletemessagesdays) > 0) ? (int)$frm->deletemessagesdays : '30'; |
240 | $pref['message_emailmessages'] = (isset($frm->emailmessages)) ? '1' : '0'; |
241 | $pref['message_emailaddress'] = (!empty($frm->emailaddress)) ? $frm->emailaddress : $USER->email; |
242 | $pref['message_emailformat'] = (isset($frm->emailformat)) ? $frm->emailformat : FORMAT_PLAIN; |
243 | $pref['message_emailtimenosee'] = ((int)$frm->emailtimenosee > 0) ? (int)$frm->emailtimenosee : '10'; |
244 | |
245 | set_user_preferences($pref); |
246 | |
247 | redirect($ME, get_string('settingssaved', 'message'), 3); |
248 | } |
249 | |
250 | $cbshowmessagewindow = (get_user_preferences('message_showmessagewindow', 1) == '1') ? 'checked="checked"' : ''; |
251 | $cbbeepnewmessage = (get_user_preferences('message_beepnewmessage', 1) == '1') ? 'checked="checked"' : ''; |
252 | $txmaxmessages = get_user_preferences('message_maxmessages', 20); |
253 | $txdeletemessagesdays = get_user_preferences('message_deletemessagesdays', 30); |
254 | $cbemailmessages = (get_user_preferences('message_emailmessages', 1) == '1') ? 'checked="checked"' : ''; |
255 | $txemailaddress = get_user_preferences('message_emailaddress', $USER->email); |
256 | $txemailtimenosee = get_user_preferences('message_emailtimenosee', 10); |
257 | $format_select = choose_from_menu( array(FORMAT_PLAIN => get_string('formatplain'), |
258 | FORMAT_HTML => get_string('formathtml')), |
259 | 'emailformat', |
260 | get_user_preferences('message_emailformat', FORMAT_PLAIN), |
261 | false, '', '0', true ); |
262 | |
263 | include('settings.html'); |
264 | } |
265 | |
266 | |
267 | |
268 | function message_add_contact($contactid, $blocked=0) { |
269 | global $USER; |
270 | |
271 | if (!record_exists('user', 'id', $contactid)) { // invalid userid |
272 | return false; |
273 | } |
274 | |
275 | if (($contact = get_record('message_contacts', 'userid', $USER->id, 'contactid', $contactid)) !== false) { |
276 | /// record already exists - we may be changing blocking status |
277 | |
278 | if ($contact->blocked !== $blocked) { |
279 | /// change to blocking status |
280 | $contact->blocked = $blocked; |
281 | return update_record('message_contacts', $contact); |
282 | } else { |
283 | /// no changes to blocking status |
284 | return true; |
285 | } |
286 | |
172186b8 |
287 | } else { |
e8e2d7f1 |
288 | /// new contact record |
289 | unset($contact); |
290 | $contact->userid = $USER->id; |
291 | $contact->contactid = $contactid; |
292 | $contact->blocked = $blocked; |
293 | return insert_record('message_contacts', $contact, false); |
172186b8 |
294 | } |
295 | } |
296 | |
e8e2d7f1 |
297 | function message_remove_contact($contactid) { |
298 | global $USER; |
299 | |
300 | return delete_records('message_contacts', 'userid', $USER->id, 'contactid', $contactid); |
301 | } |
302 | |
303 | function message_unblock_contact($contactid) { |
304 | return message_add_contact($contactid, 0); |
305 | } |
306 | |
307 | function message_block_contact($contactid) { |
308 | return message_add_contact($contactid, 1); |
309 | } |
310 | |
311 | function message_get_contact($contactid) { |
312 | global $USER; |
313 | return get_record('message_contacts', 'userid', $USER->id, 'contactid', $contactid); |
314 | } |
315 | |
316 | |
317 | |
172186b8 |
318 | function message_print_search_results($frm) { |
e8e2d7f1 |
319 | global $USER; |
320 | |
321 | echo '<div align="center">'; |
322 | |
323 | /// search for person |
324 | if (!empty($frm->personsubmit) and !empty($frm->name)) { |
325 | |
326 | if ($frm->mycourses) { |
327 | $users = array(); |
328 | $mycourses = get_my_courses($USER->id); |
329 | foreach ($mycourses as $mycourse) { |
330 | if (is_array($susers = message_search_users($mycourse->id, $frm->name))) { |
331 | foreach ($susers as $suser) $users[$suser->id] = $suser; |
332 | } |
333 | } |
334 | } else { |
335 | $users = message_search_users(SITEID, $frm->name); |
336 | } |
337 | |
338 | if (!empty($users)) { |
339 | echo '<strong>'.get_string('userssearchresults', 'message', count($users)).'</strong>'; |
340 | echo '<table class="message_users">'; |
341 | foreach ($users as $user) { |
342 | if (($contact = message_get_contact($user->id)) !== false) { |
343 | if ($contact->blocked == 0) { /// not blocked |
344 | $strcontact = '[<a href="index.php?tab=contacts&removecontact='.$user->id. |
345 | '&sesskey='.$USER->sesskey.'" title="'. |
346 | get_string('removecontact', 'message').'">'. |
347 | get_string('removecontact', 'message').'</a>]</td>'; |
348 | $strblock = '[<a href="index.php?tab=contacts&blockcontact='.$user->id. |
349 | '&sesskey='.$USER->sesskey.'" title="'. |
350 | get_string('blockcontact', 'message').'">'. |
351 | get_string('blockcontact', 'message').'</a>]</td>'; |
352 | } else { // blocked |
353 | $strcontact = '[<a href="index.php?tab=contacts&unblockcontact='.$user->id. |
354 | '&sesskey='.$USER->sesskey.'" title="'. |
355 | get_string('addcontact', 'message').'">'. |
356 | get_string('addcontact', 'message').'</a>]</td>'; |
357 | $strblock = '[<a href="index.php?tab=contacts&removecontact='.$user->id. |
358 | '&sesskey='.$USER->sesskey.'" title="'. |
359 | get_string('unblockcontact', 'message').'">'. |
360 | get_string('unblockcontact', 'message').'</a>]</td>'; |
361 | |
362 | } |
363 | |
364 | } else { |
365 | $strcontact = '[<a href="index.php?tab=contacts&addcontact='.$user->id. |
366 | '&sesskey='.$USER->sesskey.'" title="'. |
367 | get_string('addcontact', 'message').'">'. |
368 | get_string('addcontact', 'message').'</a>]</td>'; |
369 | $strblock = '[<a href="index.php?tab=contacts&blockcontact='.$user->id. |
370 | '&sesskey='.$USER->sesskey.'" title="'. |
371 | get_string('blockcontact', 'message').'">'. |
372 | get_string('blockcontact', 'message').'</a>]</td>'; |
373 | |
374 | } |
375 | echo '<tr><td class="message_pic">'; |
376 | print_user_picture($user->id, SITEID, $user->picture, 20, false, false); |
377 | echo '</td>'; |
378 | echo '<td class="message_contact">'; |
379 | link_to_popup_window("/message/user.php?id=$user->id", "message_$user->id", fullname($user), 400, 400, get_string('sendmessageto', 'message', fullname($user))); |
380 | echo '</td>'; |
381 | |
382 | echo '<td class="message_link">'.$strcontact.'</td>'; |
383 | echo '<td class="message_link">'.$strblock.'</td>'; |
384 | echo '</tr>'; |
385 | } |
386 | echo '</table>'; |
387 | |
388 | } else { |
389 | notify(get_string('nosearchresults', 'message')); |
390 | } |
391 | |
392 | |
393 | /// search messages for keywords |
394 | } else if (!empty($frm->keywordssubmit) and !empty($frm->keywords)) { |
395 | $keywords = explode(' ', $frm->keywords); |
396 | $tome = false; |
397 | $fromme = false; |
398 | $courseid = 'none'; |
399 | |
400 | switch ($frm->keywordsoption) { |
401 | case 'tome': |
402 | $tome = true; |
403 | break; |
404 | case 'fromme': |
405 | $fromme = true; |
406 | break; |
407 | case 'allmine': |
408 | $tome = true; |
409 | $fromme = true; |
410 | break; |
411 | case 'allusers': |
412 | $courseid = SITEID; |
413 | break; |
414 | case 'courseusers': |
415 | $courseid = $frm->courseid; |
416 | break; |
417 | default: |
418 | $tome = true; |
419 | $fromme = true; |
420 | } |
421 | |
422 | if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) { |
423 | |
424 | /// get a list of contacts |
425 | $contacts = get_records_sql("SELECT contactid, blocked FROM {$CFG->prefix}message_contacts |
426 | WHERE userid='$USER->id'"); |
427 | |
428 | echo '<strong>'.get_string('keywordssearchresults', 'message', count($messages)).'</strong>'; |
429 | echo '<table class="message_users">'; |
430 | |
431 | foreach ($messages as $message) { |
432 | /// ignore messages from blocked users |
433 | if ((!$frm->includeblocked) and isset($contacts[$message->useridfrom]) and ($contacts[$message->useridfrom]->blocked == 1)) { |
434 | continue; |
435 | } |
436 | echo '<tr><td class="message_pic">'; |
437 | print_user_picture($message->useridfrom, SITEID, $message->picture, 20, false, false); |
438 | echo '</td>'; |
439 | echo '<td class="message_contact">'; |
440 | link_to_popup_window("/message/user.php?id=$message->useridfrom", "message_$message->useridfrom", fullname($message), 400, 400, get_string('sendmessageto', 'message', fullname($message))); |
441 | echo '</td>'; |
442 | echo '<td>'.message_shorten_message($message->message, 20).'</td>'; |
443 | echo '</tr>'; |
444 | } |
445 | |
446 | echo '</table>'; |
447 | |
448 | } else { |
449 | notify(get_string('nosearchresults', 'message')); |
450 | } |
451 | |
452 | |
453 | /// what the ????, probably an empty search string, duh! |
454 | } else { |
455 | notify(get_string('emptysearchstring', 'message')); |
456 | } |
457 | |
458 | print_single_button($ME, array( 'tab' => 'search'), get_string('newsearch', 'message') ); |
459 | |
460 | echo '</div>'; |
461 | } |
462 | |
463 | |
464 | |
465 | |
466 | /** |
467 | * Search through course users |
468 | * |
469 | * If $coursid specifies the site course then this function searches |
470 | * through all undeleted and confirmed users |
471 | * |
472 | * @uses $CFG |
473 | * @uses SITEID |
474 | * @param int $courseid The course in question. |
475 | * @param string $searchtext ? |
476 | * @param string $sort ? |
477 | * @param string $exceptions ? |
478 | * @return array An array of {@link $USER} records. |
479 | * @todo Finish documenting this function |
480 | */ |
481 | function message_search_users($courseid, $searchtext, $sort='', $exceptions='') { |
482 | global $CFG; |
483 | |
484 | switch ($CFG->dbtype) { |
485 | case 'mysql': |
486 | $fullname = ' CONCAT(u.firstname," ",u.lastname) '; |
487 | $LIKE = 'LIKE'; |
488 | break; |
489 | case 'postgres7': |
490 | $fullname = " u.firstname||' '||u.lastname "; |
491 | $LIKE = 'ILIKE'; |
492 | break; |
493 | default: |
494 | $fullname = ' u.firstname||" "||u.lastname '; |
495 | $LIKE = 'ILIKE'; |
496 | } |
497 | |
498 | if (!empty($exceptions)) { |
499 | $except = ' AND u.id NOT IN ('. $exceptions .') '; |
500 | } else { |
501 | $except = ''; |
502 | } |
503 | |
504 | if (!empty($sort)) { |
505 | $order = ' ORDER BY '. $sort; |
506 | } else { |
507 | $order = ''; |
508 | } |
509 | |
510 | $select = 'u.deleted = \'0\' AND u.confirmed = \'1\''; |
511 | |
512 | if (!$courseid or $courseid == SITEID) { |
513 | return get_records_sql("SELECT u.id, u.firstname, u.lastname |
514 | FROM {$CFG->prefix}user u |
515 | WHERE $select |
516 | AND ($fullname $LIKE '%$searchtext%') |
517 | $except $order"); |
518 | } else { |
519 | |
520 | |
521 | if (!$teachers = get_records_sql("SELECT u.id, u.firstname, u.lastname |
522 | FROM {$CFG->prefix}user u, |
523 | {$CFG->prefix}user_teachers s |
524 | WHERE $select AND s.course = '$courseid' AND s.userid = u.id |
525 | AND ($fullname $LIKE '%$searchtext%') |
526 | $except $order")) { |
527 | $teachers = array(); |
528 | } |
529 | if (!$students = get_records_sql("SELECT u.id, u.firstname, u.lastname |
530 | FROM {$CFG->prefix}user u, |
531 | {$CFG->prefix}user_students s |
532 | WHERE $select AND s.course = '$courseid' AND s.userid = u.id |
533 | AND ($fullname $LIKE '%$searchtext%') |
534 | $except $order")) { |
535 | $students = array(); |
536 | } |
537 | return $teachers + $students; |
538 | } |
539 | } |
540 | |
541 | |
542 | |
543 | |
544 | function message_search($searchterms, $fromme=true, $tome=true, $courseid='none') { |
545 | /// Returns a list of posts found using an array of search terms |
546 | /// eg word +word -word |
547 | /// |
548 | |
549 | global $CFG, $USER; |
550 | |
551 | /// Some differences in syntax for PostgreSQL |
552 | if ($CFG->dbtype == "postgres7") { |
553 | $LIKE = "ILIKE"; // case-insensitive |
554 | $NOTLIKE = "NOT ILIKE"; // case-insensitive |
555 | $REGEXP = "~*"; |
556 | $NOTREGEXP = "!~*"; |
557 | } else { |
558 | $LIKE = "LIKE"; |
559 | $NOTLIKE = "NOT LIKE"; |
560 | $REGEXP = "REGEXP"; |
561 | $NOTREGEXP = "NOT REGEXP"; |
562 | } |
563 | |
564 | $messagesearch = ""; |
565 | |
566 | foreach ($searchterms as $searchterm) { |
567 | if (strlen($searchterm) < 2) { |
568 | continue; |
569 | } |
570 | if ($messagesearch) { |
571 | $messagesearch .= " AND "; |
572 | } |
573 | |
574 | if (substr($searchterm,0,1) == "+") { |
575 | $searchterm = substr($searchterm,1); |
576 | $messagesearch .= " m.message $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; |
577 | } else if (substr($searchterm,0,1) == "-") { |
578 | $searchterm = substr($searchterm,1); |
579 | $messagesearch .= " m.message $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; |
580 | } else { |
581 | $messagesearch .= " m.message $LIKE '%$searchterm%' "; |
582 | } |
172186b8 |
583 | } |
584 | |
e8e2d7f1 |
585 | |
586 | $messagesearch = "($messagesearch) "; |
587 | |
588 | |
589 | ///////////////////////////////////// |
590 | |
591 | if ($courseid !== SITEID) { |
592 | if ($courseid == 'none') { |
593 | if ($fromme and $tome) $messagesearch .= "AND (m.useridfrom='$USER->id' OR m.useridto='$USER->id') "; |
594 | elseif ($fromme) $messagesearch .= "AND m.useridfrom='$USER->id' "; |
595 | elseif ($tome) $messagesearch .= "AND m.useridto='$USER->id' "; |
596 | } else { |
597 | /// Code in here for searching for messages between users in a particular course |
598 | /// This is not implemented at present because of potential abuse issues |
599 | /// Temporary measure, in case we ever get here is all the users messages |
600 | $messagesearch .= "AND (m.useridto='$USER->id' OR m.useridfrom='$USER->id') "; |
601 | } |
602 | } |
603 | |
604 | $messages = array(); |
605 | |
606 | if (($m_unread = get_records_sql("SELECT m.id, m.useridfrom, m.message, u.firstname, u.lastname, u.picture |
607 | FROM {$CFG->prefix}user u, {$CFG->prefix}message m |
608 | WHERE $messagesearch AND u.id=m.useridfrom ") ) !== false) { |
609 | foreach ($m_unread as $m) $messages[] = $m; |
610 | } |
611 | |
612 | if (($m_read = get_records_sql("SELECT m.id, m.useridfrom, m.message_read, u.firstname, u.lastname, u.picture |
613 | FROM {$CFG->prefix}user u, {$CFG->prefix}message m |
614 | WHERE $messagesearch AND u.id=m.useridfrom ") ) !== false) { |
615 | foreach ($m_read as $m) $messages[] = $m; |
616 | } |
617 | |
618 | /////////////////////////////////////// |
619 | |
620 | return (empty($messages)) ? false : $messages; |
172186b8 |
621 | } |
622 | |
e8e2d7f1 |
623 | |
624 | |
625 | /// Borrowed with changes from mod/forum/lib.php |
626 | function message_shorten_message($message, $minlength=0) { |
627 | // Given a post object that we already know has a long message |
628 | // this function truncates the message nicely to the first |
629 | // sane place between $CFG->forum_longpost and $CFG->forum_shortpost |
630 | |
631 | $i = 0; |
632 | $tag = false; |
633 | $length = strlen($message); |
634 | $count = 0; |
635 | $stopzone = false; |
636 | $truncate = 0; |
637 | if ($minlength == 0) $minlength = MESSAGE_SHORTLENGTH; |
638 | |
639 | |
640 | for ($i=0; $i<$length; $i++) { |
641 | $char = $message[$i]; |
642 | |
643 | switch ($char) { |
644 | case "<": |
645 | $tag = true; |
646 | break; |
647 | case ">": |
648 | $tag = false; |
649 | break; |
650 | default: |
651 | if (!$tag) { |
652 | if ($stopzone) { |
653 | if ($char == '.' or $char == ' ') { |
654 | $truncate = $i+1; |
655 | break 2; |
656 | } |
657 | } |
658 | $count++; |
659 | } |
660 | break; |
661 | } |
662 | if (!$stopzone) { |
663 | if ($count > $minlength) { |
664 | $stopzone = true; |
665 | } |
666 | } |
667 | } |
668 | |
669 | if (!$truncate) { |
670 | $truncate = $i; |
671 | } |
672 | |
673 | return substr($message, 0, $truncate); |
674 | } |
675 | |
676 | |
677 | |
172186b8 |
678 | ?> |