| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php 2 /// library functions for messaging 3 4 5 define ('MESSAGE_SHORTLENGTH', 300); 6 define ('MESSAGE_WINDOW', true); // We are in a message window (so don't pop up a new one!) 7 8 if (!isset($CFG->message_contacts_refresh)) { // Refresh the contacts list every 60 seconds 9 $CFG->message_contacts_refresh = 60; 10 } 11 if (!isset($CFG->message_chat_refresh)) { // Look for new comments every 5 seconds 12 $CFG->message_chat_refresh = 5; 13 } 14 if (!isset($CFG->message_offline_time)) { 15 $CFG->message_offline_time = 300; 16 } 17 18 19 function message_print_contacts() { 20 global $USER, $CFG; 21 22 $timetoshowusers = 300; //Seconds default 23 if (isset($CFG->block_online_users_timetosee)) { 24 $timetoshowusers = $CFG->block_online_users_timetosee * 60; 25 } 26 27 // time which a user is counting as being active since 28 $timefrom = time()-$timetoshowusers; 29 30 // people in our contactlist who are online 31 $onlinecontacts = array(); 32 // people in our contactlist who are offline 33 $offlinecontacts = array(); 34 // people who are not in our contactlist but have sent us a message 35 $strangers = array(); 36 37 38 // get all in our contactlist who are not blocked in our contact list 39 // and count messages we have waiting from each of them 40 $contactsql = "SELECT u.id, u.firstname, u.lastname, u.picture, 41 u.imagealt, u.lastaccess, count(m.id) as messagecount 42 FROM {$CFG->prefix}message_contacts mc 43 JOIN {$CFG->prefix}user u 44 ON u.id = mc.contactid 45 LEFT OUTER JOIN {$CFG->prefix}message m 46 ON m.useridfrom = mc.contactid 47 AND m.useridto = {$USER->id} 48 WHERE mc.userid = {$USER->id} 49 AND mc.blocked = 0 50 GROUP BY u.id, u.firstname, u.lastname, u.picture, 51 u.imagealt, u.lastaccess 52 ORDER BY u.firstname ASC;"; 53 54 if($rs = get_recordset_sql($contactsql)){ 55 while($rd = rs_fetch_next_record($rs)){ 56 57 if($rd->lastaccess >= $timefrom){ 58 // they have been active recently, so are counted online 59 $onlinecontacts[] = $rd; 60 }else{ 61 $offlinecontacts[] = $rd; 62 } 63 } 64 unset($rd); 65 rs_close($rs); 66 } 67 68 69 // get messages from anyone who isn't in our contact list and count the number 70 // of messages we have from each of them 71 $strangersql = "SELECT u.id, u.firstname, u.lastname, u.picture, 72 u.imagealt, u.lastaccess, count(m.id) as messagecount 73 FROM {$CFG->prefix}message m 74 JOIN {$CFG->prefix}user u 75 ON u.id = m.useridfrom 76 LEFT OUTER JOIN {$CFG->prefix}message_contacts mc 77 ON mc.contactid = m.useridfrom AND 78 mc.userid = m.useridto 79 WHERE mc.id IS NULL AND m.useridto = {$USER->id} 80 GROUP BY u.id, u.firstname, u.lastname, u.picture, 81 u.imagealt, u.lastaccess 82 ORDER BY u.firstname ASC;"; 83 84 if($rs = get_recordset_sql($strangersql)){ 85 while($rd= rs_fetch_next_record($rs)){ 86 $strangers[] = $rd; 87 } 88 unset($rd); 89 rs_close($rs); 90 } 91 92 $countonlinecontacts = count($onlinecontacts); 93 $countofflinecontacts = count($offlinecontacts); 94 $countstrangers = count($strangers); 95 96 if ($countonlinecontacts + $countofflinecontacts == 0) { 97 echo '<div class="heading">'; 98 print_string('contactlistempty', 'message'); 99 echo '</div>'; 100 echo '<div class="note">'; 101 print_string('addsomecontacts', 'message', $CFG->wwwroot.'/message/index.php?tab=search'); 102 echo '</div>'; 103 } 104 105 echo '<table id="message_contacts" class="boxaligncenter" cellspacing="2" cellpadding="0" border="0">'; 106 107 if($countonlinecontacts) { 108 /// print out list of online contacts 109 110 echo '<tr><td colspan="3" class="heading">'; 111 echo get_string('onlinecontacts', 'message', $countonlinecontacts); 112 echo '</td></tr>'; 113 114 foreach ($onlinecontacts as $contact) { 115 message_print_contactlist_user($contact); 116 } 117 } 118 echo '<tr><td colspan="3"> </td></tr>'; 119 120 if ($countofflinecontacts) { 121 /// print out list of offline contacts 122 123 echo '<tr><td colspan="3" class="heading">'; 124 echo get_string('offlinecontacts', 'message', $countofflinecontacts); 125 echo '</td></tr>'; 126 127 foreach ($offlinecontacts as $contact) { 128 message_print_contactlist_user($contact); 129 } 130 echo '<tr><td colspan="3"> </td></tr>'; 131 } 132 133 /// print out list of incoming contacts 134 if ($countstrangers) { 135 echo '<tr><td colspan="3" class="heading">'; 136 echo get_string('incomingcontacts', 'message', $countstrangers); 137 echo '</td></tr>'; 138 139 foreach ($strangers as $stranger) { 140 message_print_contactlist_user($stranger, false); 141 } 142 } 143 144 echo '</table>'; 145 146 if ($countstrangers && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help 147 echo '<div class="note">('; 148 print_string('addsomecontactsincoming', 'message'); 149 echo ')</div>'; 150 } 151 152 echo '<br />'; 153 154 $autorefresh = '<p align="center" class="note">'.get_string('pagerefreshes', 'message', $CFG->message_contacts_refresh).'</p>'; 155 $autorefresh = addslashes_js($autorefresh); // js escaping 156 157 // gracefully degrade JS autorefresh 158 echo '<script type="text/javascript"> 159 //<![CDATA[ 160 document.write("'.$autorefresh.'") 161 //]]> 162 </script>'; 163 echo '<noscript><div class="button aligncenter">'; 164 echo print_single_button('index.php', false, get_string('refresh')); 165 echo '</div></noscript>'; 166 } 167 168 169 /// $messagearray is an array of objects 170 /// $field is a valid property of object 171 /// $value is the value $field should equal to be counted 172 /// if $field is empty then return count of the whole array 173 /// if $field is non-existent then return 0; 174 function message_count_messages($messagearray, $field='', $value='') { 175 if (!is_array($messagearray)) return 0; 176 if ($field == '' or empty($messagearray)) return count($messagearray); 177 178 $count = 0; 179 foreach ($messagearray as $message) { 180 $count += ($message->$field == $value) ? 1 : 0; 181 } 182 return $count; 183 } 184 185 186 function message_print_search() { 187 global $USER; 188 189 if ($frm = data_submitted()) { 190 191 message_print_search_results($frm); 192 193 } else { 194 /* 195 /// unfinished buggy code disabled in search.html anyway 196 // find all courses this use has readallmessages capabilities in 197 if ($teachers = get_user_capability_course('moodle/site:readallmessages')) { 198 $courses = get_courses('all', 'c.sortorder ASC', 'c.id, c.shortname'); 199 $cs = '<select name="courseselect">'; 200 foreach ($teachers as $tcourse) { 201 $cs .= "<option value=\"$tcourse->course\">".$courses[$tcourse->id]->shortname."</option>\n"; 202 } 203 $cs .= '</select>'; 204 } 205 */ 206 include ('search.html'); 207 } 208 } 209 210 function message_print_settings() { 211 global $USER; 212 213 if ($frm = data_submitted() and confirm_sesskey()) { 214 215 $pref = array(); 216 $pref['message_showmessagewindow'] = (isset($frm->showmessagewindow)) ? '1' : '0'; 217 $pref['message_beepnewmessage'] = (isset($frm->beepnewmessage)) ? '1' : '0'; 218 $pref['message_blocknoncontacts'] = (isset($frm->blocknoncontacts)) ? '1' : '0'; 219 $pref['message_usehtmleditor'] = (isset($frm->usehtmleditor)) ? '1' : '0'; 220 $pref['message_noframesjs'] = (isset($frm->noframesjs)) ? '1' : '0'; 221 $pref['message_emailmessages'] = (isset($frm->emailmessages)) ? '1' : '0'; 222 $pref['message_emailtimenosee'] = ((int)$frm->emailtimenosee > 0) ? (int)$frm->emailtimenosee : '10'; 223 $pref['message_emailaddress'] = (!empty($frm->emailaddress)) ? $frm->emailaddress : $USER->email; 224 $pref['message_emailformat'] = (isset($frm->emailformat)) ? $frm->emailformat : FORMAT_PLAIN; 225 226 set_user_preferences($pref); 227 228 redirect('index.php', get_string('settingssaved', 'message'), 1); 229 } 230 231 $cbshowmessagewindow = (get_user_preferences('message_showmessagewindow', 1) == '1') ? 'checked="checked"' : ''; 232 $cbbeepnewmessage = (get_user_preferences('message_beepnewmessage', 0) == '1') ? 'checked="checked"' : ''; 233 $cbblocknoncontacts = (get_user_preferences('message_blocknoncontacts', 0) == '1') ? 'checked="checked"' : ''; 234 $cbusehtmleditor = (get_user_preferences('message_usehtmleditor', 0) == '1') ? 'checked="checked"' : ''; 235 $cbnoframesjs = (get_user_preferences('message_noframesjs', 0) == '1') ? 'checked="checked"' : ''; 236 $cbemailmessages = (get_user_preferences('message_emailmessages', 1) == '1') ? 'checked="checked"' : ''; 237 $txemailaddress = get_user_preferences('message_emailaddress', $USER->email); 238 $txemailtimenosee = get_user_preferences('message_emailtimenosee', 10); 239 $format_select = choose_from_menu( array(FORMAT_PLAIN => get_string('formatplain'), 240 FORMAT_HTML => get_string('formathtml')), 241 'emailformat', 242 get_user_preferences('message_emailformat', FORMAT_PLAIN), 243 false, '', '0', true ); 244 245 include ('settings.html'); 246 } 247 248 249 250 function message_add_contact($contactid, $blocked=0) { 251 global $USER; 252 253 if (!record_exists('user', 'id', $contactid)) { // invalid userid 254 return false; 255 } 256 257 if (($contact = get_record('message_contacts', 'userid', $USER->id, 'contactid', $contactid)) !== false) { 258 /// record already exists - we may be changing blocking status 259 260 if ($contact->blocked !== $blocked) { 261 /// change to blocking status 262 $contact->blocked = $blocked; 263 return update_record('message_contacts', $contact); 264 } else { 265 /// no changes to blocking status 266 return true; 267 } 268 269 } else { 270 /// new contact record 271 unset($contact); 272 $contact->userid = $USER->id; 273 $contact->contactid = $contactid; 274 $contact->blocked = $blocked; 275 return insert_record('message_contacts', $contact, false); 276 } 277 } 278 279 function message_remove_contact($contactid) { 280 global $USER; 281 return delete_records('message_contacts', 'userid', $USER->id, 'contactid', $contactid); 282 } 283 284 function message_unblock_contact($contactid) { 285 global $USER; 286 return delete_records('message_contacts', 'userid', $USER->id, 'contactid', $contactid); 287 } 288 289 function message_block_contact($contactid) { 290 return message_add_contact($contactid, 1); 291 } 292 293 function message_get_contact($contactid) { 294 global $USER; 295 return get_record('message_contacts', 'userid', $USER->id, 'contactid', $contactid); 296 } 297 298 299 300 function message_print_search_results($frm) { 301 global $USER, $CFG; 302 303 echo '<div align="center">'; 304 305 /// search for person 306 if (!empty($frm->personsubmit) and !empty($frm->name)) { 307 308 if (optional_param('mycourses', 0, PARAM_BOOL)) { 309 $users = array(); 310 $mycourses = get_my_courses($USER->id); 311 foreach ($mycourses as $mycourse) { 312 if (is_array($susers = message_search_users($mycourse->id, $frm->name))) { 313 foreach ($susers as $suser) $users[$suser->id] = $suser; 314 } 315 } 316 } else { 317 $users = message_search_users(SITEID, $frm->name); 318 } 319 320 if (!empty($users)) { 321 echo '<strong>'.get_string('userssearchresults', 'message', count($users)).'</strong>'; 322 echo '<table class="message_users">'; 323 foreach ($users as $user) { 324 325 if ( $user->contactlistid ) { 326 if ($user->blocked == 0) { /// not blocked 327 $strcontact = message_contact_link($user->id, 'remove', true); 328 $strblock = message_contact_link($user->id, 'block', true); 329 } else { // blocked 330 $strcontact = message_contact_link($user->id, 'add', true); 331 $strblock = message_contact_link($user->id, 'unblock', true); 332 } 333 } else { 334 $strcontact = message_contact_link($user->id, 'add', true); 335 $strblock = message_contact_link($user->id, 'block', true); 336 } 337 $strhistory = message_history_link($user->id, 0, true, '', '', 'icon'); 338 339 echo '<tr><td class="pix">'; 340 print_user_picture($user, SITEID, $user->picture, 20, false, true, 'userwindow'); 341 echo '</td>'; 342 echo '<td class="contact">'; 343 link_to_popup_window("/message/discussion.php?id=$user->id", "message_$user->id", fullname($user), 344 500, 500, get_string('sendmessageto', 'message', fullname($user)), 345 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500'); 346 347 echo '</td>'; 348 349 echo '<td class="link">'.$strcontact.'</td>'; 350 echo '<td class="link">'.$strblock.'</td>'; 351 echo '<td class="link">'.$strhistory.'</td>'; 352 echo '</tr>'; 353 } 354 echo '</table>'; 355 356 } else { 357 notify(get_string('nosearchresults', 'message')); 358 } 359 360 361 /// search messages for keywords 362 } else if (!empty($frm->keywordssubmit) and !empty($frm->keywords)) { 363 $keywordstring = clean_text(trim($frm->keywords)); 364 $keywords = explode(' ', $keywordstring); 365 $tome = false; 366 $fromme = false; 367 $courseid = 'none'; 368 369 switch ($frm->keywordsoption) { 370 case 'tome': 371 $tome = true; 372 break; 373 case 'fromme': 374 $fromme = true; 375 break; 376 case 'allmine': 377 $tome = true; 378 $fromme = true; 379 break; 380 case 'allusers': 381 $courseid = SITEID; 382 break; 383 case 'courseusers': 384 $courseid = $frm->courseid; 385 break; 386 default: 387 $tome = true; 388 $fromme = true; 389 } 390 391 if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) { 392 393 /// get a list of contacts 394 if (($contacts = get_records('message_contacts', 'userid', $USER->id, '', 'contactid, blocked') ) === false) { 395 $contacts = array(); 396 } 397 398 /// print heading with number of results 399 echo '<p class="heading">'.get_string('keywordssearchresults', 'message', count($messages)).' ("'.s($keywordstring).'")</p>'; 400 401 /// print table headings 402 echo '<table class="searchresults" cellspacing="0">'; 403 echo '<tr>'; 404 echo '<td><strong>'.get_string('from').'</strong></td>'; 405 echo '<td><strong>'.get_string('to').'</strong></td>'; 406 echo '<td><strong>'.get_string('message', 'message').'</strong></td>'; 407 echo '<td><strong>'.get_string('timesent', 'message').'</strong></td>'; 408 echo "</tr>\n"; 409 410 $blockedcount = 0; 411 $dateformat = get_string('strftimedatetimeshort'); 412 $strcontext = get_string('context', 'message'); 413 foreach ($messages as $message) { 414 415 /// ignore messages to and from blocked users unless $frm->includeblocked is set 416 if (!optional_param('includeblocked', 0, PARAM_BOOL) and ( 417 ( isset($contacts[$message->useridfrom]) and ($contacts[$message->useridfrom]->blocked == 1)) or 418 ( isset($contacts[$message->useridto] ) and ($contacts[$message->useridto]->blocked == 1)) 419 ) 420 ) { 421 $blockedcount ++; 422 continue; 423 } 424 425 /// load up user to record 426 if ($message->useridto !== $USER->id) { 427 $userto = get_record('user', 'id', $message->useridto); 428 $tocontact = (array_key_exists($message->useridto, $contacts) and 429 ($contacts[$message->useridto]->blocked == 0) ); 430 $toblocked = (array_key_exists($message->useridto, $contacts) and 431 ($contacts[$message->useridto]->blocked == 1) ); 432 } else { 433 $userto = false; 434 $tocontact = false; 435 $toblocked = false; 436 } 437 438 /// load up user from record 439 if ($message->useridfrom !== $USER->id) { 440 $userfrom = get_record('user', 'id', $message->useridfrom); 441 $fromcontact = (array_key_exists($message->useridfrom, $contacts) and 442 ($contacts[$message->useridfrom]->blocked == 0) ); 443 $fromblocked = (array_key_exists($message->useridfrom, $contacts) and 444 ($contacts[$message->useridfrom]->blocked == 1) ); 445 } else { 446 $userfrom = false; 447 $fromcontact = false; 448 $fromblocked = false; 449 } 450 451 /// find date string for this message 452 $date = usergetdate($message->timecreated); 453 $datestring = $date['year'].$date['mon'].$date['mday']; 454 455 /// print out message row 456 echo '<tr valign="top">'; 457 echo '<td class="contact">'; 458 message_print_user($userfrom, $fromcontact, $fromblocked); 459 echo '</td>'; 460 echo '<td class="contact">'; 461 message_print_user($userto, $tocontact, $toblocked); 462 echo '</td>'; 463 echo '<td class="summary">'.message_get_fragment($message->message, $keywords); 464 echo '<br /><div class="link">'; 465 message_history_link($message->useridto, $message->useridfrom, false, 466 $keywordstring, 'm'.$message->id, $strcontext); 467 echo '</div>'; 468 echo '</td>'; 469 echo '<td class="date">'.userdate($message->timecreated, $dateformat).'</td>'; 470 echo "</tr>\n"; 471 } 472 473 474 if ($blockedcount > 0) { 475 echo '<tr><td colspan="4" align="center">'.get_string('blockedmessages', 'message', $blockedcount).'</td></tr>'; 476 } 477 echo '</table>'; 478 479 } else { 480 notify(get_string('nosearchresults', 'message')); 481 } 482 483 484 /// what the ????, probably an empty search string, duh! 485 } else { 486 notify(get_string('emptysearchstring', 'message')); 487 } 488 489 echo '<br />'; 490 print_single_button('index.php', array( 'tab' => 'search'), get_string('newsearch', 'message') ); 491 492 echo '</div>'; 493 } 494 495 496 function message_print_user ($user=false, $iscontact=false, $isblocked=false) { 497 global $USER; 498 if ($user === false) { 499 print_user_picture($USER, SITEID, $USER->picture, 20, false, true, 'userwindow'); 500 } else { 501 print_user_picture($user, SITEID, $user->picture, 20, false, true, 'userwindow'); 502 echo ' '; 503 if ($iscontact) { 504 message_contact_link($user->id, 'remove'); 505 } else { 506 message_contact_link($user->id, 'add'); 507 } 508 echo ' '; 509 if ($isblocked) { 510 message_contact_link($user->id, 'unblock'); 511 } else { 512 message_contact_link($user->id, 'block'); 513 } 514 echo '<br />'; 515 516 link_to_popup_window("/message/discussion.php?id=$user->id", "message_$user->id", 517 fullname($user), 400, 400, get_string('sendmessageto', 'message', fullname($user)), 518 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500'); 519 } 520 } 521 522 523 /// linktype can be: add, remove, block, unblock 524 function message_contact_link($userid, $linktype='add', $return=false, $script="index.php?tab=contacts", $text=false) { 525 global $USER, $CFG; 526 527 static $str; 528 529 if (empty($str->blockcontact)) { 530 $str->blockcontact = get_string('blockcontact', 'message'); 531 $str->unblockcontact = get_string('unblockcontact', 'message'); 532 $str->removecontact = get_string('removecontact', 'message'); 533 $str->addcontact = get_string('addcontact', 'message'); 534 } 535 536 $command = $linktype.'contact'; 537 $string = $str->{$command}; 538 $alttext = $text ? '' : $string; 539 $text = $text ? ' '.$string : ''; 540 541 switch ($linktype) { 542 case 'block': 543 $icon = '/t/go.gif'; 544 break; 545 case 'unblock': 546 $icon = '/t/stop.gif'; 547 break; 548 case 'remove': 549 $icon = '/t/user.gif'; 550 break; 551 case 'add': 552 default: 553 $icon = '/t/usernot.gif'; 554 } 555 556 $output = '<span class="'.$linktype.'">'. 557 '<a href="'.$script.'&'.$command.'='.$userid. 558 '&sesskey='.sesskey().'" title="'.s($string).'">'. 559 '<img src="'.$CFG->pixpath.$icon.'" class="iconsmall" alt="'.s($alttext).'" />'. 560 $text.'</a></span>'; 561 562 if ($return) { 563 return $output; 564 } else { 565 echo $output; 566 return true; 567 } 568 } 569 570 function message_history_link($userid1, $userid2=0, $returnstr=false, $keywords='', $position='', $linktext='') { 571 global $USER, $CFG; 572 573 static $strmessagehistory; 574 575 if (empty($strmessagehistory)) { 576 $strmessagehistory = get_string('messagehistory', 'message'); 577 } 578 579 if (!$userid2) { 580 $userid2 = $USER->id; 581 } 582 if ($position) { 583 $position = "#$position"; 584 } 585 if ($keywords) { 586 $keywords = "&search=".urlencode($keywords); 587 } 588 589 if ($linktext == 'icon') { // Icon only 590 $fulllink = '<img src="'.$CFG->pixpath.'/t/log.gif" class="iconsmall" alt="'.$strmessagehistory.'" />'; 591 } else if ($linktext == 'both') { // Icon and standard name 592 $fulllink = '<img src="'.$CFG->pixpath.'/t/log.gif" class="iconsmall" alt="" />'; 593 $fulllink .= ' '.$strmessagehistory; 594 } else if ($linktext) { // Custom name 595 $fulllink = $linktext; 596 } else { // Standard name only 597 $fulllink = $strmessagehistory; 598 } 599 600 $str = link_to_popup_window("/message/history.php?user1=$userid1&user2=$userid2$keywords$position", 601 "message_history_$userid1", $fulllink, 500, 500, $strmessagehistory, 602 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500', true); 603 604 $str = '<span class="history">'.$str.'</span>'; 605 606 if ($returnstr) { 607 return $str; 608 } else { 609 echo $str; 610 return true; 611 } 612 } 613 614 615 /** 616 * Search through course users 617 * 618 * If $coursid specifies the site course then this function searches 619 * through all undeleted and confirmed users 620 * 621 * @uses $CFG, $USER 622 * @uses SITEID 623 * @param int $courseid The course in question. 624 * @param string $searchtext ? 625 * @param string $sort ? 626 * @param string $exceptions ? 627 * @return array An array of {@link $USER} records. 628 * @todo Finish documenting this function 629 */ 630 function message_search_users($courseid, $searchtext, $sort='', $exceptions='') { 631 global $CFG, $USER; 632 633 $fullname = sql_fullname(); 634 $LIKE = sql_ilike(); 635 636 if (!empty($exceptions)) { 637 $except = ' AND u.id NOT IN ('. $exceptions .') '; 638 } else { 639 $except = ''; 640 } 641 642 if (!empty($sort)) { 643 $order = ' ORDER BY '. $sort; 644 } else { 645 $order = ''; 646 } 647 648 $select = 'u.deleted = \'0\' AND u.confirmed = \'1\''; 649 $fields = 'u.id, u.firstname, u.lastname, u.picture, u.imagealt, mc.id as contactlistid, mc.blocked'; 650 651 if (!$courseid or $courseid == SITEID) { 652 return get_records_sql("SELECT $fields 653 FROM {$CFG->prefix}user u 654 LEFT OUTER JOIN {$CFG->prefix}message_contacts mc 655 ON mc.contactid = u.id AND mc.userid = {$USER->id} 656 WHERE $select 657 AND ($fullname $LIKE '%$searchtext%') 658 $except $order"); 659 } else { 660 661 $context = get_context_instance(CONTEXT_COURSE, $courseid); 662 $contextlists = get_related_contexts_string($context); 663 664 // everyone who has a role assignement in this course or higher 665 $users = get_records_sql("SELECT $fields 666 FROM {$CFG->prefix}user u 667 JOIN {$CFG->prefix}role_assignments ra 668 ON ra.userid = u.id 669 LEFT OUTER JOIN {$CFG->prefix}message_contacts mc 670 ON mc.contactid = u.id AND mc.userid = {$USER->id} 671 WHERE $select 672 AND ra.contextid $contextlists 673 AND ($fullname $LIKE '%$searchtext%') 674 $except $order"); 675 676 return $users; 677 } 678 } 679 680 681 682 683 function message_search($searchterms, $fromme=true, $tome=true, $courseid='none', $userid=0) { 684 /// Returns a list of posts found using an array of search terms 685 /// eg word +word -word 686 /// 687 688 global $CFG, $USER; 689 690 /// If no userid sent then assume current user 691 if ($userid == 0) $userid = $USER->id; 692 693 /// Some differences in SQL syntax 694 $LIKE = sql_ilike(); 695 $NOTLIKE = 'NOT ' . $LIKE; 696 if ($CFG->dbfamily == "postgres") { 697 $REGEXP = "~*"; 698 $NOTREGEXP = "!~*"; 699 } else { 700 $REGEXP = "REGEXP"; 701 $NOTREGEXP = "NOT REGEXP"; 702 } 703 704 $messagesearch = ""; 705 706 foreach ($searchterms as $searchterm) { 707 if (strlen($searchterm) < 2) { 708 continue; 709 } 710 /// Under Oracle and MSSQL, trim the + and - operators and perform 711 /// simpler LIKE search 712 if ($CFG->dbfamily == 'oracle' || $CFG->dbfamily == 'mssql') { 713 $searchterm = trim($searchterm, '+-'); 714 } 715 716 if ($messagesearch) { 717 $messagesearch .= " AND "; 718 } 719 720 if (substr($searchterm,0,1) == "+") { 721 $searchterm = substr($searchterm,1); 722 $messagesearch .= " m.message $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; 723 } else if (substr($searchterm,0,1) == "-") { 724 $searchterm = substr($searchterm,1); 725 $messagesearch .= " m.message $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; 726 } else { 727 $messagesearch .= " m.message $LIKE '%$searchterm%' "; 728 } 729 } 730 731 if ($messagesearch == '') { // if only 1 letter words searched 732 return false; 733 } 734 735 $messagesearch = "($messagesearch) "; 736 737 738 /// There are several possibilities 739 /// 1. courseid = SITEID : The admin is searching messages by all users 740 /// 2. courseid = ?? : A teacher is searching messages by users in 741 /// one of their courses - currently disabled 742 /// 3. courseid = none : User is searching their own messages; 743 /// a. Messages from user 744 /// b. Messages to user 745 /// c. Messages to and from user 746 747 if ($courseid == SITEID) { /// admin is searching all messages 748 $m_read = get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.message, m.timecreated 749 FROM {$CFG->prefix}message_read m 750 WHERE $messagesearch"); 751 $m_unread = get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.message, m.timecreated 752 FROM {$CFG->prefix}message m 753 WHERE $messagesearch"); 754 755 if ($m_read === false) $m_read = array(); 756 if ($m_unread === false) $m_unread = array(); 757 758 } elseif ($courseid !== 'none') { 759 /// This has not been implemented due to security concerns 760 761 } else { 762 763 if ($fromme and $tome) $messagesearch .= "AND (m.useridfrom='$userid' OR m.useridto='$userid') "; 764 elseif ($fromme) $messagesearch .= "AND m.useridfrom='$userid' "; 765 elseif ($tome) $messagesearch .= "AND m.useridto='$userid' "; 766 767 $m_read = get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.message, m.timecreated 768 FROM {$CFG->prefix}message_read m 769 WHERE $messagesearch"); 770 $m_unread = get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.message, m.timecreated 771 FROM {$CFG->prefix}message m 772 WHERE $messagesearch"); 773 774 if ($m_read === false) $m_read = array(); 775 if ($m_unread === false) $m_unread = array(); 776 777 } 778 779 /// The keys may be duplicated in $m_read and $m_unread so we can't 780 /// do a simple concatenation 781 $message = array(); 782 foreach ($m_read as $m) $messages[] = $m; 783 foreach ($m_unread as $m) $messages[] = $m; 784 785 786 return (empty($messages)) ? false : $messages; 787 } 788 789 790 791 /// Borrowed with changes from mod/forum/lib.php 792 function message_shorten_message($message, $minlength=0) { 793 // Given a post object that we already know has a long message 794 // this function truncates the message nicely to the first 795 // sane place between $CFG->forum_longpost and $CFG->forum_shortpost 796 797 $i = 0; 798 $tag = false; 799 $length = strlen($message); 800 $count = 0; 801 $stopzone = false; 802 $truncate = 0; 803 if ($minlength == 0) $minlength = MESSAGE_SHORTLENGTH; 804 805 806 for ($i=0; $i<$length; $i++) { 807 $char = $message[$i]; 808 809 switch ($char) { 810 case "<": 811 $tag = true; 812 break; 813 case ">": 814 $tag = false; 815 break; 816 default: 817 if (!$tag) { 818 if ($stopzone) { 819 if ($char == '.' or $char == ' ') { 820 $truncate = $i+1; 821 break 2; 822 } 823 } 824 $count++; 825 } 826 break; 827 } 828 if (!$stopzone) { 829 if ($count > $minlength) { 830 $stopzone = true; 831 } 832 } 833 } 834 835 if (!$truncate) { 836 $truncate = $i; 837 } 838 839 return substr($message, 0, $truncate); 840 } 841 842 843 /* 844 * Given a string and an array of keywords, this function looks 845 * for the first keyword in the string, and then chops out a 846 * small section from the text that shows that word in context. 847 */ 848 function message_get_fragment($message, $keywords) { 849 850 $fullsize = 120; 851 $halfsize = (int)($fullsize/2); 852 853 $message = strip_tags($message); 854 855 foreach ($keywords as $keyword) { // Just get the first one 856 if ($keyword !== '') { 857 break; 858 } 859 } 860 if (empty($keyword)) { // None found, so just return start of message 861 return message_shorten_message($message, 30); 862 } 863 864 $leadin = $leadout = ''; 865 866 /// Find the start of the fragment 867 $start = 0; 868 $length = strlen($message); 869 870 $pos = strpos($message, $keyword); 871 if ($pos > $halfsize) { 872 $start = $pos - $halfsize; 873 $leadin = '...'; 874 } 875 /// Find the end of the fragment 876 $end = $start + $fullsize; 877 if ($end > $length) { 878 $end = $length; 879 } else { 880 $leadout = '...'; 881 } 882 883 /// Pull out the fragment and format it 884 885 $fragment = substr($message, $start, $end - $start); 886 $fragment = $leadin.highlight(implode(' ',$keywords), $fragment).$leadout; 887 return $fragment; 888 } 889 890 891 function message_get_history($user1, $user2) { 892 $messages = get_records_select('message_read', "(useridto = '$user1->id' AND useridfrom = '$user2->id') OR 893 (useridto = '$user2->id' AND useridfrom = '$user1->id')", 894 'timecreated'); 895 if ($messages_new = get_records_select('message', "(useridto = '$user1->id' AND useridfrom = '$user2->id') OR 896 (useridto = '$user2->id' AND useridfrom = '$user1->id')", 897 'timecreated')) { 898 foreach ($messages_new as $message) { 899 $messages[] = $message; 900 } 901 } 902 return $messages; 903 } 904 905 function message_format_message(&$message, &$user, $format='', $keywords='', $class='other') { 906 907 static $dateformat; 908 909 if (empty($dateformat)) { 910 if ($format) { 911 $dateformat = $format; 912 } else { 913 $format = get_string('strftimedatetimeshort'); 914 } 915 } 916 $time = userdate($message->timecreated, $dateformat); 917 $options->para = false; 918 $messagetext = format_text($message->message, $message->format, $options); 919 if ($keywords) { 920 $messagetext = highlight($keywords, $messagetext); 921 } 922 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>'; 923 } 924 925 /* 926 * Inserts a message into the database, but also forwards it 927 * via other means if appropriate. 928 */ 929 function message_post_message($userfrom, $userto, $message, $format, $messagetype) { 930 931 global $CFG, $SITE, $USER; 932 933 /// Set up current language to suit the receiver of the message 934 $savelang = $USER->lang; 935 936 if (!empty($userto->lang)) { 937 $USER->lang = $userto->lang; 938 } 939 940 /// Save the new message in the database 941 942 $savemessage = NULL; 943 $savemessage->useridfrom = $userfrom->id; 944 $savemessage->useridto = $userto->id; 945 $savemessage->message = $message; 946 $savemessage->format = $format; 947 $savemessage->timecreated = time(); 948 $savemessage->messagetype = 'direct'; 949 950 if ($CFG->messaging) { 951 if (!$savemessage->id = insert_record('message', $savemessage)) { 952 return false; 953 } 954 $emailforced = false; 955 } else { // $CFG->messaging is not on, we need to force sending of emails 956 $emailforced = true; 957 $savemessage->id = true; 958 } 959 960 /// Check to see if anything else needs to be done with it 961 962 $preference = (object)get_user_preferences(NULL, NULL, $userto->id); 963 964 if ($emailforced || (!isset($preference->message_emailmessages) || $preference->message_emailmessages)) { // Receiver wants mail forwarding 965 if (!isset($preference->message_emailtimenosee)) { 966 $preference->message_emailtimenosee = 10; 967 } 968 if (!isset($preference->message_emailformat)) { 969 $preference->message_emailformat = FORMAT_HTML; 970 } 971 if ($emailforced || (time() - $userto->lastaccess) > ((int)$preference->message_emailtimenosee * 60)) { // Long enough 972 973 $message = stripslashes_safe($message); 974 $tagline = get_string('emailtagline', 'message', $SITE->shortname); 975 976 $messagesubject = preg_replace('/\s+/', ' ', strip_tags($message)); // make sure it's all on one line 977 $messagesubject = message_shorten_message($messagesubject, 30).'...'; 978 979 $messagetext = format_text_email($message, $format). 980 "\n\n--\n".$tagline."\n"."$CFG->wwwroot/message/index.php?popup=1"; 981 982 if (isset($preference->message_emailformat) and $preference->message_emailformat == FORMAT_HTML) { 983 $messagehtml = format_text($message, $format); 984 // MDL-10294, do not print link if messaging is disabled 985 if ($CFG->messaging) { 986 $messagehtml .= '<hr /><p><a href="'.$CFG->wwwroot.'/message/index.php?popup=1">'.$tagline.'</a></p>'; 987 } 988 } else { 989 $messagehtml = NULL; 990 } 991 992 if (!empty($preference->message_emailaddress)) { 993 $userto->email = $preference->message_emailaddress; // Use custom messaging address 994 } 995 996 if (email_to_user($userto, $userfrom, $messagesubject, $messagetext, $messagehtml)) { 997 $CFG->messagewasjustemailed = true; 998 } 999 1000 sleep(3); 1001 } 1002 } 1003 1004 $USER->lang = $savelang; // restore original language 1005 1006 return $savemessage->id; 1007 } 1008 1009 1010 /* 1011 * Returns a list of all user ids who have used messaging in the site 1012 * This was the simple way to code the SQL ... is it going to blow up 1013 * on large datasets? 1014 */ 1015 function message_get_participants() { 1016 1017 global $CFG; 1018 1019 return get_records_sql("SELECT useridfrom as id,1 FROM {$CFG->prefix}message 1020 UNION SELECT useridto as id,1 FROM {$CFG->prefix}message 1021 UNION SELECT useridfrom as id,1 FROM {$CFG->prefix}message_read 1022 UNION SELECT useridto as id,1 FROM {$CFG->prefix}message_read 1023 UNION SELECT userid as id,1 FROM {$CFG->prefix}message_contacts 1024 UNION SELECT contactid as id,1 from {$CFG->prefix}message_contacts"); 1025 } 1026 1027 /** 1028 * Print a row of contactlist displaying user picture, messages waiting and 1029 * block links etc 1030 * @param $contact contact object containing all fields required for print_user_picture() 1031 * @param $incontactlist is the user a contact of ours? 1032 */ 1033 function message_print_contactlist_user($contact, $incontactlist = true){ 1034 $fullname = fullname($contact); 1035 $fullnamelink = $fullname; 1036 1037 /// are there any unread messages for this contact? 1038 if ($contact->messagecount > 0 ){ 1039 $fullnamelink = '<strong>'.$fullnamelink.' ('.$contact->messagecount.')</strong>'; 1040 } 1041 1042 1043 if($incontactlist){ 1044 $strcontact = message_contact_link($contact->id, 'remove', true); 1045 $strblock = ''; 1046 }else{ 1047 $strcontact = message_contact_link($contact->id, 'add', true); 1048 $strblock = ' '. message_contact_link($contact->id, 'block', true); 1049 } 1050 1051 $strhistory = message_history_link($contact->id, 0, true, '', '', 'icon'); 1052 1053 echo '<tr><td class="pix">'; 1054 print_user_picture($contact, SITEID, $contact->picture, 20, false, true, 'userwindow'); 1055 echo '</td>'; 1056 echo '<td class="contact">'; 1057 1058 link_to_popup_window("/message/discussion.php?id=$contact->id", "message_$contact->id", 1059 $fullnamelink, 500, 500, get_string('sendmessageto', 'message', $fullname), 1060 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500'); 1061 1062 echo '</td>'; 1063 echo '<td class="link"> '.$strcontact.$strblock.' '.$strhistory.'</td>'; 1064 echo '</tr>'; 1065 } 1066 1067 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |