[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Library functions for messaging 19 * 20 * @package core_message 21 * @copyright 2008 Luis Rodrigues 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 require_once($CFG->libdir.'/eventslib.php'); 26 27 define ('MESSAGE_SHORTLENGTH', 300); 28 29 define ('MESSAGE_DISCUSSION_WIDTH',600); 30 define ('MESSAGE_DISCUSSION_HEIGHT',500); 31 32 define ('MESSAGE_SHORTVIEW_LIMIT', 8);//the maximum number of messages to show on the short message history 33 34 define('MESSAGE_HISTORY_SHORT',0); 35 define('MESSAGE_HISTORY_ALL',1); 36 37 define('MESSAGE_VIEW_UNREAD_MESSAGES','unread'); 38 define('MESSAGE_VIEW_RECENT_CONVERSATIONS','recentconversations'); 39 define('MESSAGE_VIEW_RECENT_NOTIFICATIONS','recentnotifications'); 40 define('MESSAGE_VIEW_CONTACTS','contacts'); 41 define('MESSAGE_VIEW_BLOCKED','blockedusers'); 42 define('MESSAGE_VIEW_COURSE','course_'); 43 define('MESSAGE_VIEW_SEARCH','search'); 44 45 define('MESSAGE_SEARCH_MAX_RESULTS', 200); 46 47 define('MESSAGE_CONTACTS_PER_PAGE',10); 48 define('MESSAGE_MAX_COURSE_NAME_LENGTH', 30); 49 50 /** 51 * Define contants for messaging default settings population. For unambiguity of 52 * plugin developer intentions we use 4-bit value (LSB numbering): 53 * bit 0 - whether to send message when user is loggedin (MESSAGE_DEFAULT_LOGGEDIN) 54 * bit 1 - whether to send message when user is loggedoff (MESSAGE_DEFAULT_LOGGEDOFF) 55 * bit 2..3 - messaging permission (MESSAGE_DISALLOWED|MESSAGE_PERMITTED|MESSAGE_FORCED) 56 * 57 * MESSAGE_PERMITTED_MASK contains the mask we use to distinguish permission setting 58 */ 59 60 define('MESSAGE_DEFAULT_LOGGEDIN', 0x01); // 0001 61 define('MESSAGE_DEFAULT_LOGGEDOFF', 0x02); // 0010 62 63 define('MESSAGE_DISALLOWED', 0x04); // 0100 64 define('MESSAGE_PERMITTED', 0x08); // 1000 65 define('MESSAGE_FORCED', 0x0c); // 1100 66 67 define('MESSAGE_PERMITTED_MASK', 0x0c); // 1100 68 69 /** 70 * Set default value for default outputs permitted setting 71 */ 72 define('MESSAGE_DEFAULT_PERMITTED', 'permitted'); 73 74 /** 75 * Print the selector that allows the user to view their contacts, course participants, their recent 76 * conversations etc 77 * 78 * @param int $countunreadtotal how many unread messages does the user have? 79 * @param int $viewing What is the user viewing? ie MESSAGE_VIEW_UNREAD_MESSAGES, MESSAGE_VIEW_SEARCH etc 80 * @param object $user1 the user whose messages are being viewed 81 * @param object $user2 the user $user1 is talking to 82 * @param array $blockedusers an array of users blocked by $user1 83 * @param array $onlinecontacts an array of $user1's online contacts 84 * @param array $offlinecontacts an array of $user1's offline contacts 85 * @param array $strangers an array of users who have messaged $user1 who aren't contacts 86 * @param bool $showactionlinks show action links (add/remove contact etc) 87 * @param int $page if there are so many users listed that they have to be split into pages what page are we viewing 88 * @return void 89 */ 90 function message_print_contact_selector($countunreadtotal, $viewing, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showactionlinks, $page=0) { 91 global $PAGE; 92 93 echo html_writer::start_tag('div', array('class' => 'contactselector mdl-align')); 94 95 //if 0 unread messages and they've requested unread messages then show contacts 96 if ($countunreadtotal == 0 && $viewing == MESSAGE_VIEW_UNREAD_MESSAGES) { 97 $viewing = MESSAGE_VIEW_CONTACTS; 98 } 99 100 //if they have no blocked users and they've requested blocked users switch them over to contacts 101 if (count($blockedusers) == 0 && $viewing == MESSAGE_VIEW_BLOCKED) { 102 $viewing = MESSAGE_VIEW_CONTACTS; 103 } 104 105 $onlyactivecourses = true; 106 $courses = enrol_get_users_courses($user1->id, $onlyactivecourses); 107 $coursecontexts = message_get_course_contexts($courses);//we need one of these again so holding on to them 108 109 $strunreadmessages = null; 110 if ($countunreadtotal>0) { //if there are unread messages 111 $strunreadmessages = get_string('unreadmessages','message', $countunreadtotal); 112 } 113 114 message_print_usergroup_selector($viewing, $courses, $coursecontexts, $countunreadtotal, count($blockedusers), $strunreadmessages, $user1); 115 116 if ($viewing == MESSAGE_VIEW_UNREAD_MESSAGES) { 117 message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $PAGE->url, 1, $showactionlinks,$strunreadmessages, $user2); 118 } else if ($viewing == MESSAGE_VIEW_CONTACTS || $viewing == MESSAGE_VIEW_SEARCH || $viewing == MESSAGE_VIEW_RECENT_CONVERSATIONS || $viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) { 119 message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $PAGE->url, 0, $showactionlinks, $strunreadmessages, $user2); 120 } else if ($viewing == MESSAGE_VIEW_BLOCKED) { 121 message_print_blocked_users($blockedusers, $PAGE->url, $showactionlinks, null, $user2); 122 } else if (substr($viewing, 0, 7) == MESSAGE_VIEW_COURSE) { 123 $courseidtoshow = intval(substr($viewing, 7)); 124 125 if (!empty($courseidtoshow) 126 && array_key_exists($courseidtoshow, $coursecontexts) 127 && has_capability('moodle/course:viewparticipants', $coursecontexts[$courseidtoshow])) { 128 129 message_print_participants($coursecontexts[$courseidtoshow], $courseidtoshow, $PAGE->url, $showactionlinks, null, $page, $user2); 130 } 131 } 132 133 // Only show the search button if we're viewing our own contacts. 134 if ($viewing == MESSAGE_VIEW_CONTACTS && $user2 == null) { 135 echo html_writer::start_tag('form', array('action' => 'index.php','method' => 'GET')); 136 echo html_writer::start_tag('fieldset'); 137 $managebuttonclass = 'visible'; 138 $strmanagecontacts = get_string('search','message'); 139 echo html_writer::empty_tag('input', array('type' => 'hidden','name' => 'viewing','value' => MESSAGE_VIEW_SEARCH)); 140 echo html_writer::empty_tag('input', array('type' => 'submit','value' => $strmanagecontacts,'class' => $managebuttonclass)); 141 echo html_writer::end_tag('fieldset'); 142 echo html_writer::end_tag('form'); 143 } 144 145 echo html_writer::end_tag('div'); 146 } 147 148 /** 149 * Print course participants. Called by message_print_contact_selector() 150 * 151 * @param object $context the course context 152 * @param int $courseid the course ID 153 * @param string $contactselecturl the url to send the user to when a contact's name is clicked 154 * @param bool $showactionlinks show action links (add/remove contact etc) next to the users 155 * @param string $titletodisplay Optionally specify a title to display above the participants 156 * @param int $page if there are so many users listed that they have to be split into pages what page are we viewing 157 * @param object $user2 the user $user1 is talking to. They will be highlighted if they appear in the list of participants 158 * @return void 159 */ 160 function message_print_participants($context, $courseid, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null, $page=0, $user2=null) { 161 global $DB, $USER, $PAGE, $OUTPUT; 162 163 if (empty($titletodisplay)) { 164 $titletodisplay = get_string('participants'); 165 } 166 167 $countparticipants = count_enrolled_users($context); 168 169 list($esql, $params) = get_enrolled_sql($context); 170 $params['mcuserid'] = $USER->id; 171 $ufields = user_picture::fields('u'); 172 173 $sql = "SELECT $ufields, mc.id as contactlistid, mc.blocked 174 FROM {user} u 175 JOIN ($esql) je ON je.id = u.id 176 LEFT JOIN {message_contacts} mc ON mc.contactid = u.id AND mc.userid = :mcuserid 177 WHERE u.deleted = 0"; 178 179 $participants = $DB->get_records_sql($sql, $params, $page * MESSAGE_CONTACTS_PER_PAGE, MESSAGE_CONTACTS_PER_PAGE); 180 181 $pagingbar = new paging_bar($countparticipants, $page, MESSAGE_CONTACTS_PER_PAGE, $PAGE->url, 'page'); 182 $pagingbar->maxdisplay = 4; 183 echo $OUTPUT->render($pagingbar); 184 185 echo html_writer::start_tag('div', array('id' => 'message_participants', 'class' => 'boxaligncenter')); 186 187 echo html_writer::tag('div' , $titletodisplay, array('class' => 'heading')); 188 189 $users = ''; 190 foreach ($participants as $participant) { 191 if ($participant->id != $USER->id) { 192 193 $iscontact = false; 194 $isblocked = false; 195 if ( $participant->contactlistid ) { 196 if ($participant->blocked == 0) { 197 // Is contact. Is not blocked. 198 $iscontact = true; 199 $isblocked = false; 200 } else { 201 // Is blocked. 202 $iscontact = false; 203 $isblocked = true; 204 } 205 } 206 207 $participant->messagecount = 0;//todo it would be nice if the course participant could report new messages 208 $content = message_print_contactlist_user($participant, $iscontact, $isblocked, 209 $contactselecturl, $showactionlinks, $user2); 210 $users .= html_writer::tag('li', $content); 211 } 212 } 213 if (strlen($users) > 0) { 214 echo html_writer::tag('ul', $users, array('id' => 'message-courseparticipants', 'class' => 'message-contacts')); 215 } 216 217 echo html_writer::end_tag('div'); 218 } 219 220 /** 221 * Retrieve users blocked by $user1 222 * 223 * @param object $user1 the user whose messages are being viewed 224 * @param object $user2 the user $user1 is talking to. If they are being blocked 225 * they will have a variable called 'isblocked' added to their user object 226 * @return array the users blocked by $user1 227 */ 228 function message_get_blocked_users($user1=null, $user2=null) { 229 global $DB, $USER; 230 231 if (empty($user1)) { 232 $user1 = $USER; 233 } 234 235 if (!empty($user2)) { 236 $user2->isblocked = false; 237 } 238 239 $blockedusers = array(); 240 241 $userfields = user_picture::fields('u', array('lastaccess')); 242 $blockeduserssql = "SELECT $userfields, COUNT(m.id) AS messagecount 243 FROM {message_contacts} mc 244 JOIN {user} u ON u.id = mc.contactid 245 LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = :user1id1 246 WHERE u.deleted = 0 AND mc.userid = :user1id2 AND mc.blocked = 1 247 GROUP BY $userfields 248 ORDER BY u.firstname ASC"; 249 $rs = $DB->get_recordset_sql($blockeduserssql, array('user1id1' => $user1->id, 'user1id2' => $user1->id)); 250 251 foreach($rs as $rd) { 252 $blockedusers[] = $rd; 253 254 if (!empty($user2) && $user2->id == $rd->id) { 255 $user2->isblocked = true; 256 } 257 } 258 $rs->close(); 259 260 return $blockedusers; 261 } 262 263 /** 264 * Print users blocked by $user1. Called by message_print_contact_selector() 265 * 266 * @param array $blockedusers the users blocked by $user1 267 * @param string $contactselecturl the url to send the user to when a contact's name is clicked 268 * @param bool $showactionlinks show action links (add/remove contact etc) next to the users 269 * @param string $titletodisplay Optionally specify a title to display above the participants 270 * @param object $user2 the user $user1 is talking to. They will be highlighted if they appear in the list of blocked users 271 * @return void 272 */ 273 function message_print_blocked_users($blockedusers, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null, $user2=null) { 274 global $OUTPUT; 275 276 $countblocked = count($blockedusers); 277 278 echo html_writer::start_tag('div', array('id' => 'message_contacts', 'class' => 'boxaligncenter')); 279 280 if (!empty($titletodisplay)) { 281 echo html_writer::tag('div', $titletodisplay, array('class' => 'heading')); 282 } 283 284 if ($countblocked) { 285 echo html_writer::tag('div', get_string('blockedusers', 'message', $countblocked), array('class' => 'heading')); 286 287 $isuserblocked = true; 288 $isusercontact = false; 289 $blockeduserslist = ''; 290 foreach ($blockedusers as $blockeduser) { 291 $content = message_print_contactlist_user($blockeduser, $isusercontact, $isuserblocked, 292 $contactselecturl, $showactionlinks, $user2); 293 $blockeduserslist .= html_writer::tag('li', $content); 294 } 295 echo html_writer::tag('ul', $blockeduserslist, array('id' => 'message-blockedusers', 'class' => 'message-contacts')); 296 } 297 298 echo html_writer::end_tag('div'); 299 } 300 301 /** 302 * Retrieve $user1's contacts (online, offline and strangers) 303 * 304 * @param object $user1 the user whose messages are being viewed 305 * @param object $user2 the user $user1 is talking to. If they are a contact 306 * they will have a variable called 'iscontact' added to their user object 307 * @return array containing 3 arrays. array($onlinecontacts, $offlinecontacts, $strangers) 308 */ 309 function message_get_contacts($user1=null, $user2=null) { 310 global $DB, $CFG, $USER; 311 312 if (empty($user1)) { 313 $user1 = $USER; 314 } 315 316 if (!empty($user2)) { 317 $user2->iscontact = false; 318 } 319 320 $timetoshowusers = 300; //Seconds default 321 if (isset($CFG->block_online_users_timetosee)) { 322 $timetoshowusers = $CFG->block_online_users_timetosee * 60; 323 } 324 325 // time which a user is counting as being active since 326 $timefrom = time()-$timetoshowusers; 327 328 // people in our contactlist who are online 329 $onlinecontacts = array(); 330 // people in our contactlist who are offline 331 $offlinecontacts = array(); 332 // people who are not in our contactlist but have sent us a message 333 $strangers = array(); 334 335 $userfields = user_picture::fields('u', array('lastaccess')); 336 337 // get all in our contactlist who are not blocked in our contact list 338 // and count messages we have waiting from each of them 339 $contactsql = "SELECT $userfields, COUNT(m.id) AS messagecount 340 FROM {message_contacts} mc 341 JOIN {user} u ON u.id = mc.contactid 342 LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = ? 343 WHERE u.deleted = 0 AND mc.userid = ? AND mc.blocked = 0 344 GROUP BY $userfields 345 ORDER BY u.firstname ASC"; 346 347 $rs = $DB->get_recordset_sql($contactsql, array($user1->id, $user1->id)); 348 foreach ($rs as $rd) { 349 if ($rd->lastaccess >= $timefrom) { 350 // they have been active recently, so are counted online 351 $onlinecontacts[] = $rd; 352 353 } else { 354 $offlinecontacts[] = $rd; 355 } 356 357 if (!empty($user2) && $user2->id == $rd->id) { 358 $user2->iscontact = true; 359 } 360 } 361 $rs->close(); 362 363 // get messages from anyone who isn't in our contact list and count the number 364 // of messages we have from each of them 365 $strangersql = "SELECT $userfields, count(m.id) as messagecount 366 FROM {message} m 367 JOIN {user} u ON u.id = m.useridfrom 368 LEFT OUTER JOIN {message_contacts} mc ON mc.contactid = m.useridfrom AND mc.userid = m.useridto 369 WHERE u.deleted = 0 AND mc.id IS NULL AND m.useridto = ? 370 GROUP BY $userfields 371 ORDER BY u.firstname ASC"; 372 373 $rs = $DB->get_recordset_sql($strangersql, array($USER->id)); 374 // Add user id as array index, so supportuser and noreply user don't get duplicated (if they are real users). 375 foreach ($rs as $rd) { 376 $strangers[$rd->id] = $rd; 377 } 378 $rs->close(); 379 380 // Add noreply user and support user to the list, if they don't exist. 381 $supportuser = core_user::get_support_user(); 382 if (!isset($strangers[$supportuser->id])) { 383 $supportuser->messagecount = message_count_unread_messages($USER, $supportuser); 384 if ($supportuser->messagecount > 0) { 385 $strangers[$supportuser->id] = $supportuser; 386 } 387 } 388 389 $noreplyuser = core_user::get_noreply_user(); 390 if (!isset($strangers[$noreplyuser->id])) { 391 $noreplyuser->messagecount = message_count_unread_messages($USER, $noreplyuser); 392 if ($noreplyuser->messagecount > 0) { 393 $strangers[$noreplyuser->id] = $noreplyuser; 394 } 395 } 396 return array($onlinecontacts, $offlinecontacts, $strangers); 397 } 398 399 /** 400 * Print $user1's contacts. Called by message_print_contact_selector() 401 * 402 * @param array $onlinecontacts $user1's contacts which are online 403 * @param array $offlinecontacts $user1's contacts which are offline 404 * @param array $strangers users which are not contacts but who have messaged $user1 405 * @param string $contactselecturl the url to send the user to when a contact's name is clicked 406 * @param int $minmessages The minimum number of unread messages required from a user for them to be displayed 407 * Typically 0 (show all contacts) or 1 (only show contacts from whom we have a new message) 408 * @param bool $showactionlinks show action links (add/remove contact etc) next to the users 409 * @param string $titletodisplay Optionally specify a title to display above the participants 410 * @param object $user2 the user $user1 is talking to. They will be highlighted if they appear in the list of contacts 411 * @return void 412 */ 413 function message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $contactselecturl=null, $minmessages=0, $showactionlinks=true, $titletodisplay=null, $user2=null) { 414 global $CFG, $PAGE, $OUTPUT; 415 416 $countonlinecontacts = count($onlinecontacts); 417 $countofflinecontacts = count($offlinecontacts); 418 $countstrangers = count($strangers); 419 $isuserblocked = null; 420 421 if ($countonlinecontacts + $countofflinecontacts == 0) { 422 echo html_writer::tag('div', get_string('contactlistempty', 'message'), array('class' => 'heading')); 423 } 424 425 if (!empty($titletodisplay)) { 426 echo html_writer::tag('div', $titletodisplay, array('class' => 'heading')); 427 } 428 429 if($countonlinecontacts) { 430 // Print out list of online contacts. 431 432 if (empty($titletodisplay)) { 433 echo html_writer::tag('div', 434 get_string('onlinecontacts', 'message', $countonlinecontacts), 435 array('class' => 'heading')); 436 } 437 438 $isuserblocked = false; 439 $isusercontact = true; 440 $contacts = ''; 441 foreach ($onlinecontacts as $contact) { 442 if ($minmessages == 0 || $contact->messagecount >= $minmessages) { 443 $content = message_print_contactlist_user($contact, $isusercontact, $isuserblocked, 444 $contactselecturl, $showactionlinks, $user2); 445 $contacts .= html_writer::tag('li', $content); 446 } 447 } 448 if (strlen($contacts) > 0) { 449 echo html_writer::tag('ul', $contacts, array('id' => 'message-onlinecontacts', 'class' => 'message-contacts')); 450 } 451 } 452 453 if ($countofflinecontacts) { 454 // Print out list of offline contacts. 455 456 if (empty($titletodisplay)) { 457 echo html_writer::tag('div', 458 get_string('offlinecontacts', 'message', $countofflinecontacts), 459 array('class' => 'heading')); 460 } 461 462 $isuserblocked = false; 463 $isusercontact = true; 464 $contacts = ''; 465 foreach ($offlinecontacts as $contact) { 466 if ($minmessages == 0 || $contact->messagecount >= $minmessages) { 467 $content = message_print_contactlist_user($contact, $isusercontact, $isuserblocked, 468 $contactselecturl, $showactionlinks, $user2); 469 $contacts .= html_writer::tag('li', $content); 470 } 471 } 472 if (strlen($contacts) > 0) { 473 echo html_writer::tag('ul', $contacts, array('id' => 'message-offlinecontacts', 'class' => 'message-contacts')); 474 } 475 476 } 477 478 // Print out list of incoming contacts. 479 if ($countstrangers) { 480 echo html_writer::tag('div', get_string('incomingcontacts', 'message', $countstrangers), array('class' => 'heading')); 481 482 $isuserblocked = false; 483 $isusercontact = false; 484 $contacts = ''; 485 foreach ($strangers as $stranger) { 486 if ($minmessages == 0 || $stranger->messagecount >= $minmessages) { 487 $content = message_print_contactlist_user($stranger, $isusercontact, $isuserblocked, 488 $contactselecturl, $showactionlinks, $user2); 489 $contacts .= html_writer::tag('li', $content); 490 } 491 } 492 if (strlen($contacts) > 0) { 493 echo html_writer::tag('ul', $contacts, array('id' => 'message-incommingcontacts', 'class' => 'message-contacts')); 494 } 495 496 } 497 498 if ($countstrangers && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help 499 echo html_writer::tag('div','('.get_string('addsomecontactsincoming', 'message').')',array('class' => 'note')); 500 } 501 } 502 503 /** 504 * Print a select box allowing the user to choose to view new messages, course participants etc. 505 * 506 * Called by message_print_contact_selector() 507 * @param int $viewing What page is the user viewing ie MESSAGE_VIEW_UNREAD_MESSAGES, MESSAGE_VIEW_RECENT_CONVERSATIONS etc 508 * @param array $courses array of course objects. The courses the user is enrolled in. 509 * @param array $coursecontexts array of course contexts. Keyed on course id. 510 * @param int $countunreadtotal how many unread messages does the user have? 511 * @param int $countblocked how many users has the current user blocked? 512 * @param stdClass $user1 The user whose messages we are viewing. 513 * @param string $strunreadmessages a preconstructed message about the number of unread messages the user has 514 * @return void 515 */ 516 function message_print_usergroup_selector($viewing, $courses, $coursecontexts, $countunreadtotal, $countblocked, $strunreadmessages, $user1 = null) { 517 global $PAGE; 518 $options = array(); 519 520 if ($countunreadtotal>0) { //if there are unread messages 521 $options[MESSAGE_VIEW_UNREAD_MESSAGES] = $strunreadmessages; 522 } 523 524 $str = get_string('contacts', 'message'); 525 $options[MESSAGE_VIEW_CONTACTS] = $str; 526 527 $options[MESSAGE_VIEW_RECENT_CONVERSATIONS] = get_string('mostrecentconversations', 'message'); 528 $options[MESSAGE_VIEW_RECENT_NOTIFICATIONS] = get_string('mostrecentnotifications', 'message'); 529 530 if (!empty($courses)) { 531 $courses_options = array(); 532 533 foreach($courses as $course) { 534 if (has_capability('moodle/course:viewparticipants', $coursecontexts[$course->id])) { 535 //Not using short_text() as we want the end of the course name. Not the beginning. 536 $shortname = format_string($course->shortname, true, array('context' => $coursecontexts[$course->id])); 537 if (core_text::strlen($shortname) > MESSAGE_MAX_COURSE_NAME_LENGTH) { 538 $courses_options[MESSAGE_VIEW_COURSE.$course->id] = '...'.core_text::substr($shortname, -MESSAGE_MAX_COURSE_NAME_LENGTH); 539 } else { 540 $courses_options[MESSAGE_VIEW_COURSE.$course->id] = $shortname; 541 } 542 } 543 } 544 545 if (!empty($courses_options)) { 546 $options[] = array(get_string('courses') => $courses_options); 547 } 548 } 549 550 if ($countblocked>0) { 551 $str = get_string('blockedusers','message', $countblocked); 552 $options[MESSAGE_VIEW_BLOCKED] = $str; 553 } 554 555 $select = new single_select($PAGE->url, 'viewing', $options, $viewing, false); 556 $select->set_label(get_string('messagenavigation', 'message')); 557 558 $renderer = $PAGE->get_renderer('core'); 559 echo $renderer->render($select); 560 } 561 562 /** 563 * Load the course contexts for all of the users courses 564 * 565 * @param array $courses array of course objects. The courses the user is enrolled in. 566 * @return array of course contexts 567 */ 568 function message_get_course_contexts($courses) { 569 $coursecontexts = array(); 570 571 foreach($courses as $course) { 572 $coursecontexts[$course->id] = context_course::instance($course->id); 573 } 574 575 return $coursecontexts; 576 } 577 578 /** 579 * strip off action parameters like 'removecontact' 580 * 581 * @param moodle_url/string $moodleurl a URL. Typically the current page URL. 582 * @return string the URL minus parameters that perform actions (like adding/removing/blocking a contact). 583 */ 584 function message_remove_url_params($moodleurl) { 585 $newurl = new moodle_url($moodleurl); 586 $newurl->remove_params('addcontact','removecontact','blockcontact','unblockcontact'); 587 return $newurl->out(); 588 } 589 590 /** 591 * Count the number of messages with a field having a specified value. 592 * if $field is empty then return count of the whole array 593 * if $field is non-existent then return 0 594 * 595 * @param array $messagearray array of message objects 596 * @param string $field the field to inspect on the message objects 597 * @param string $value the value to test the field against 598 */ 599 function message_count_messages($messagearray, $field='', $value='') { 600 if (!is_array($messagearray)) return 0; 601 if ($field == '' or empty($messagearray)) return count($messagearray); 602 603 $count = 0; 604 foreach ($messagearray as $message) { 605 $count += ($message->$field == $value) ? 1 : 0; 606 } 607 return $count; 608 } 609 610 /** 611 * Returns the count of unread messages for user. Either from a specific user or from all users. 612 * 613 * @param object $user1 the first user. Defaults to $USER 614 * @param object $user2 the second user. If null this function will count all of user 1's unread messages. 615 * @return int the count of $user1's unread messages 616 */ 617 function message_count_unread_messages($user1=null, $user2=null) { 618 global $USER, $DB; 619 620 if (empty($user1)) { 621 $user1 = $USER; 622 } 623 624 if (!empty($user2)) { 625 return $DB->count_records_select('message', "useridto = ? AND useridfrom = ?", 626 array($user1->id, $user2->id), "COUNT('id')"); 627 } else { 628 return $DB->count_records_select('message', "useridto = ?", 629 array($user1->id), "COUNT('id')"); 630 } 631 } 632 633 /** 634 * Count the number of users blocked by $user1 635 * 636 * @param object $user1 user object 637 * @return int the number of blocked users 638 */ 639 function message_count_blocked_users($user1=null) { 640 global $USER, $DB; 641 642 if (empty($user1)) { 643 $user1 = $USER; 644 } 645 646 $sql = "SELECT count(mc.id) 647 FROM {message_contacts} mc 648 WHERE mc.userid = :userid AND mc.blocked = 1"; 649 $params = array('userid' => $user1->id); 650 651 return $DB->count_records_sql($sql, $params); 652 } 653 654 /** 655 * Print the search form and search results if a search has been performed 656 * 657 * @param boolean $advancedsearch show basic or advanced search form 658 * @param object $user1 the current user 659 * @return boolean true if a search was performed 660 */ 661 function message_print_search($advancedsearch = false, $user1=null) { 662 $frm = data_submitted(); 663 664 $doingsearch = false; 665 if ($frm) { 666 if (confirm_sesskey()) { 667 $doingsearch = !empty($frm->combinedsubmit) || !empty($frm->keywords) || (!empty($frm->personsubmit) and !empty($frm->name)); 668 } else { 669 $frm = false; 670 } 671 } 672 673 if (!empty($frm->combinedsearch)) { 674 $combinedsearchstring = $frm->combinedsearch; 675 } else { 676 //$combinedsearchstring = get_string('searchcombined','message').'...'; 677 $combinedsearchstring = ''; 678 } 679 680 if ($doingsearch) { 681 if ($advancedsearch) { 682 683 $messagesearch = ''; 684 if (!empty($frm->keywords)) { 685 $messagesearch = $frm->keywords; 686 } 687 $personsearch = ''; 688 if (!empty($frm->name)) { 689 $personsearch = $frm->name; 690 } 691 include ('search_advanced.html'); 692 } else { 693 include ('search.html'); 694 } 695 696 $showicontext = false; 697 message_print_search_results($frm, $showicontext, $user1); 698 699 return true; 700 } else { 701 702 if ($advancedsearch) { 703 $personsearch = $messagesearch = ''; 704 include ('search_advanced.html'); 705 } else { 706 include ('search.html'); 707 } 708 return false; 709 } 710 } 711 712 /** 713 * Get the users recent conversations meaning all the people they've recently 714 * sent or received a message from plus the most recent message sent to or received from each other user 715 * 716 * @param object $user the current user 717 * @param int $limitfrom can be used for paging 718 * @param int $limitto can be used for paging 719 * @return array 720 */ 721 function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) { 722 global $DB; 723 724 $userfields = user_picture::fields('otheruser', array('lastaccess')); 725 726 // This query retrieves the most recent message received from or sent to 727 // seach other user. 728 // 729 // If two messages have the same timecreated, we take the one with the 730 // larger id. 731 // 732 // There is a separate query for read and unread messages as they are stored 733 // in different tables. They were originally retrieved in one query but it 734 // was so large that it was difficult to be confident in its correctness. 735 $uniquefield = $DB->sql_concat('message.useridfrom', "'-'", 'message.useridto'); 736 $sql = "SELECT $uniquefield, $userfields, 737 message.id as mid, message.notification, message.smallmessage, message.fullmessage, 738 message.fullmessagehtml, message.fullmessageformat, message.timecreated, 739 contact.id as contactlistid, contact.blocked 740 FROM {message_read} message 741 JOIN ( 742 SELECT MAX(id) AS messageid, 743 matchedmessage.useridto, 744 matchedmessage.useridfrom 745 FROM {message_read} matchedmessage 746 INNER JOIN ( 747 SELECT MAX(recentmessages.timecreated) timecreated, 748 recentmessages.useridfrom, 749 recentmessages.useridto 750 FROM {message_read} recentmessages 751 WHERE ( 752 (recentmessages.useridfrom = :userid1 AND recentmessages.timeuserfromdeleted = 0) OR 753 (recentmessages.useridto = :userid2 AND recentmessages.timeusertodeleted = 0) 754 ) 755 GROUP BY recentmessages.useridfrom, recentmessages.useridto 756 ) recent ON matchedmessage.useridto = recent.useridto 757 AND matchedmessage.useridfrom = recent.useridfrom 758 AND matchedmessage.timecreated = recent.timecreated 759 WHERE ( 760 (matchedmessage.useridfrom = :userid6 AND matchedmessage.timeuserfromdeleted = 0) OR 761 (matchedmessage.useridto = :userid7 AND matchedmessage.timeusertodeleted = 0) 762 ) 763 GROUP BY matchedmessage.useridto, matchedmessage.useridfrom 764 ) messagesubset ON messagesubset.messageid = message.id 765 JOIN {user} otheruser ON (message.useridfrom = :userid4 AND message.useridto = otheruser.id) 766 OR (message.useridto = :userid5 AND message.useridfrom = otheruser.id) 767 LEFT JOIN {message_contacts} contact ON contact.userid = :userid3 AND contact.contactid = otheruser.id 768 WHERE otheruser.deleted = 0 AND message.notification = 0 769 ORDER BY message.timecreated DESC"; 770 $params = array( 771 'userid1' => $user->id, 772 'userid2' => $user->id, 773 'userid3' => $user->id, 774 'userid4' => $user->id, 775 'userid5' => $user->id, 776 'userid6' => $user->id, 777 'userid7' => $user->id 778 ); 779 $read = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); 780 781 // We want to get the messages that have not been read. These are stored in the 'message' table. It is the 782 // exact same query as the one above, except for the table we are querying. So, simply replace references to 783 // the 'message_read' table with the 'message' table. 784 $sql = str_replace('{message_read}', '{message}', $sql); 785 $unread = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); 786 787 // Union the 2 result sets together looking for the message with the most 788 // recent timecreated for each other user. 789 // $conversation->id (the array key) is the other user's ID. 790 $conversations = array(); 791 $conversation_arrays = array($unread, $read); 792 foreach ($conversation_arrays as $conversation_array) { 793 foreach ($conversation_array as $conversation) { 794 if (!isset($conversations[$conversation->id])) { 795 $conversations[$conversation->id] = $conversation; 796 } else { 797 $current = $conversations[$conversation->id]; 798 if ($current->timecreated < $conversation->timecreated) { 799 $conversations[$conversation->id] = $conversation; 800 } else if ($current->timecreated == $conversation->timecreated) { 801 if ($current->mid < $conversation->mid) { 802 $conversations[$conversation->id] = $conversation; 803 } 804 } 805 } 806 } 807 } 808 809 // Sort the conversations by $conversation->timecreated, newest to oldest 810 // There may be multiple conversations with the same timecreated 811 // The conversations array contains both read and unread messages (different tables) so sorting by ID won't work 812 $result = core_collator::asort_objects_by_property($conversations, 'timecreated', core_collator::SORT_NUMERIC); 813 $conversations = array_reverse($conversations); 814 815 return $conversations; 816 } 817 818 /** 819 * Get the users recent event notifications 820 * 821 * @param object $user the current user 822 * @param int $limitfrom can be used for paging 823 * @param int $limitto can be used for paging 824 * @return array 825 */ 826 function message_get_recent_notifications($user, $limitfrom=0, $limitto=100) { 827 global $DB; 828 829 $userfields = user_picture::fields('u', array('lastaccess')); 830 $sql = "SELECT mr.id AS message_read_id, $userfields, mr.notification, mr.smallmessage, mr.fullmessage, mr.fullmessagehtml, mr.fullmessageformat, mr.timecreated as timecreated, mr.contexturl, mr.contexturlname 831 FROM {message_read} mr 832 JOIN {user} u ON u.id=mr.useridfrom 833 WHERE mr.useridto = :userid1 AND u.deleted = '0' AND mr.notification = :notification 834 ORDER BY mr.timecreated DESC"; 835 $params = array('userid1' => $user->id, 'notification' => 1); 836 837 $notifications = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); 838 return $notifications; 839 } 840 841 /** 842 * Print the user's recent conversations 843 * 844 * @param stdClass $user the current user 845 * @param bool $showicontext flag indicating whether or not to show text next to the action icons 846 */ 847 function message_print_recent_conversations($user1 = null, $showicontext = false, $showactionlinks = true) { 848 global $USER; 849 850 echo html_writer::start_tag('p', array('class' => 'heading')); 851 echo get_string('mostrecentconversations', 'message'); 852 echo html_writer::end_tag('p'); 853 854 if (empty($user1)) { 855 $user1 = $USER; 856 } 857 858 $conversations = message_get_recent_conversations($user1); 859 860 // Attach context url information to create the "View this conversation" type links 861 foreach($conversations as $conversation) { 862 $conversation->contexturl = new moodle_url("/message/index.php?user1={$user1->id}&user2={$conversation->id}"); 863 $conversation->contexturlname = get_string('thisconversation', 'message'); 864 } 865 866 $showotheruser = true; 867 message_print_recent_messages_table($conversations, $user1, $showotheruser, $showicontext, false, $showactionlinks); 868 } 869 870 /** 871 * Print the user's recent notifications 872 * 873 * @param stdClass $user the current user 874 */ 875 function message_print_recent_notifications($user=null) { 876 global $USER; 877 878 echo html_writer::start_tag('p', array('class' => 'heading')); 879 echo get_string('mostrecentnotifications', 'message'); 880 echo html_writer::end_tag('p'); 881 882 if (empty($user)) { 883 $user = $USER; 884 } 885 886 $notifications = message_get_recent_notifications($user); 887 888 $showicontext = false; 889 $showotheruser = false; 890 message_print_recent_messages_table($notifications, $user, $showotheruser, $showicontext, true); 891 } 892 893 /** 894 * Print a list of recent messages 895 * 896 * @access private 897 * 898 * @param array $messages the messages to display 899 * @param stdClass $user the current user 900 * @param bool $showotheruser display information on the other user? 901 * @param bool $showicontext show text next to the action icons? 902 * @param bool $forcetexttohtml Force text to go through @see text_to_html() via @see format_text() 903 * @param bool $showactionlinks 904 * @return void 905 */ 906 function message_print_recent_messages_table($messages, $user = null, $showotheruser = true, $showicontext = false, $forcetexttohtml = false, $showactionlinks = true) { 907 global $OUTPUT; 908 static $dateformat; 909 910 if (empty($dateformat)) { 911 $dateformat = get_string('strftimedatetimeshort'); 912 } 913 914 echo html_writer::start_tag('div', array('class' => 'messagerecent')); 915 foreach ($messages as $message) { 916 echo html_writer::start_tag('div', array('class' => 'singlemessage')); 917 918 if ($showotheruser) { 919 $strcontact = $strblock = $strhistory = null; 920 921 if ($showactionlinks) { 922 if ( $message->contactlistid ) { 923 if ($message->blocked == 0) { // The other user isn't blocked. 924 $strcontact = message_contact_link($message->id, 'remove', true, null, $showicontext); 925 $strblock = message_contact_link($message->id, 'block', true, null, $showicontext); 926 } else { // The other user is blocked. 927 $strcontact = message_contact_link($message->id, 'add', true, null, $showicontext); 928 $strblock = message_contact_link($message->id, 'unblock', true, null, $showicontext); 929 } 930 } else { 931 $strcontact = message_contact_link($message->id, 'add', true, null, $showicontext); 932 $strblock = message_contact_link($message->id, 'block', true, null, $showicontext); 933 } 934 935 //should we show just the icon or icon and text? 936 $histicontext = 'icon'; 937 if ($showicontext) { 938 $histicontext = 'both'; 939 } 940 $strhistory = message_history_link($user->id, $message->id, true, '', '', $histicontext); 941 } 942 echo html_writer::start_tag('span', array('class' => 'otheruser')); 943 944 echo html_writer::start_tag('span', array('class' => 'pix')); 945 echo $OUTPUT->user_picture($message, array('size' => 20, 'courseid' => SITEID)); 946 echo html_writer::end_tag('span'); 947 948 echo html_writer::start_tag('span', array('class' => 'contact')); 949 950 $link = new moodle_url("/message/index.php?user1={$user->id}&user2=$message->id"); 951 $action = null; 952 echo $OUTPUT->action_link($link, fullname($message), $action, array('title' => get_string('sendmessageto', 'message', fullname($message)))); 953 954 echo html_writer::end_tag('span');//end contact 955 956 if ($showactionlinks) { 957 echo $strcontact.$strblock.$strhistory; 958 } 959 echo html_writer::end_tag('span');//end otheruser 960 } 961 962 $messagetext = message_format_message_text($message, $forcetexttohtml); 963 964 echo html_writer::tag('span', userdate($message->timecreated, $dateformat), array('class' => 'messagedate')); 965 echo html_writer::tag('span', $messagetext, array('class' => 'themessage')); 966 echo message_format_contexturl($message); 967 echo html_writer::end_tag('div');//end singlemessage 968 } 969 echo html_writer::end_tag('div');//end messagerecent 970 } 971 972 /** 973 * Try to guess how to convert the message to html. 974 * 975 * @access private 976 * 977 * @param stdClass $message 978 * @param bool $forcetexttohtml 979 * @return string html fragment 980 */ 981 function message_format_message_text($message, $forcetexttohtml = false) { 982 // Note: this is a very nasty hack that tries to work around the weird messaging rules and design. 983 984 $options = new stdClass(); 985 $options->para = false; 986 $options->blanktarget = true; 987 988 $format = $message->fullmessageformat; 989 990 if (strval($message->smallmessage) !== '') { 991 if ($message->notification == 1) { 992 if (strval($message->fullmessagehtml) !== '' or strval($message->fullmessage) !== '') { 993 $format = FORMAT_PLAIN; 994 } 995 } 996 $messagetext = $message->smallmessage; 997 998 } else if ($message->fullmessageformat == FORMAT_HTML) { 999 if (strval($message->fullmessagehtml) !== '') { 1000 $messagetext = $message->fullmessagehtml; 1001 } else { 1002 $messagetext = $message->fullmessage; 1003 $format = FORMAT_MOODLE; 1004 } 1005 1006 } else { 1007 if (strval($message->fullmessage) !== '') { 1008 $messagetext = $message->fullmessage; 1009 } else { 1010 $messagetext = $message->fullmessagehtml; 1011 $format = FORMAT_HTML; 1012 } 1013 } 1014 1015 if ($forcetexttohtml) { 1016 // This is a crazy hack, why not set proper format when creating the notifications? 1017 if ($format === FORMAT_PLAIN) { 1018 $format = FORMAT_MOODLE; 1019 } 1020 } 1021 return format_text($messagetext, $format, $options); 1022 } 1023 1024 /** 1025 * Add the selected user as a contact for the current user 1026 * 1027 * @param int $contactid the ID of the user to add as a contact 1028 * @param int $blocked 1 if you wish to block the contact 1029 * @return bool/int false if the $contactid isnt a valid user id. True if no changes made. 1030 * Otherwise returns the result of update_record() or insert_record() 1031 */ 1032 function message_add_contact($contactid, $blocked=0) { 1033 global $USER, $DB; 1034 1035 if (!$DB->record_exists('user', array('id' => $contactid))) { // invalid userid 1036 return false; 1037 } 1038 1039 // Check if a record already exists as we may be changing blocking status. 1040 if (($contact = $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid))) !== false) { 1041 // Check if blocking status has been changed. 1042 if ($contact->blocked != $blocked) { 1043 $contact->blocked = $blocked; 1044 $DB->update_record('message_contacts', $contact); 1045 1046 if ($blocked == 1) { 1047 // Trigger event for blocking a contact. 1048 $event = \core\event\message_contact_blocked::create(array( 1049 'objectid' => $contact->id, 1050 'userid' => $contact->userid, 1051 'relateduserid' => $contact->contactid, 1052 'context' => context_user::instance($contact->userid) 1053 )); 1054 $event->add_record_snapshot('message_contacts', $contact); 1055 $event->trigger(); 1056 } else { 1057 // Trigger event for unblocking a contact. 1058 $event = \core\event\message_contact_unblocked::create(array( 1059 'objectid' => $contact->id, 1060 'userid' => $contact->userid, 1061 'relateduserid' => $contact->contactid, 1062 'context' => context_user::instance($contact->userid) 1063 )); 1064 $event->add_record_snapshot('message_contacts', $contact); 1065 $event->trigger(); 1066 } 1067 1068 return true; 1069 } else { 1070 // No change to blocking status. 1071 return true; 1072 } 1073 1074 } else { 1075 // New contact record. 1076 $contact = new stdClass(); 1077 $contact->userid = $USER->id; 1078 $contact->contactid = $contactid; 1079 $contact->blocked = $blocked; 1080 $contact->id = $DB->insert_record('message_contacts', $contact); 1081 1082 $eventparams = array( 1083 'objectid' => $contact->id, 1084 'userid' => $contact->userid, 1085 'relateduserid' => $contact->contactid, 1086 'context' => context_user::instance($contact->userid) 1087 ); 1088 1089 if ($blocked) { 1090 $event = \core\event\message_contact_blocked::create($eventparams); 1091 } else { 1092 $event = \core\event\message_contact_added::create($eventparams); 1093 } 1094 // Trigger event. 1095 $event->trigger(); 1096 1097 return true; 1098 } 1099 } 1100 1101 /** 1102 * remove a contact 1103 * 1104 * @param int $contactid the user ID of the contact to remove 1105 * @return bool returns the result of delete_records() 1106 */ 1107 function message_remove_contact($contactid) { 1108 global $USER, $DB; 1109 1110 if ($contact = $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid))) { 1111 $DB->delete_records('message_contacts', array('id' => $contact->id)); 1112 1113 // Trigger event for removing a contact. 1114 $event = \core\event\message_contact_removed::create(array( 1115 'objectid' => $contact->id, 1116 'userid' => $contact->userid, 1117 'relateduserid' => $contact->contactid, 1118 'context' => context_user::instance($contact->userid) 1119 )); 1120 $event->add_record_snapshot('message_contacts', $contact); 1121 $event->trigger(); 1122 1123 return true; 1124 } 1125 1126 return false; 1127 } 1128 1129 /** 1130 * Unblock a contact. Note that this reverts the previously blocked user back to a non-contact. 1131 * 1132 * @param int $contactid the user ID of the contact to unblock 1133 * @return bool returns the result of delete_records() 1134 */ 1135 function message_unblock_contact($contactid) { 1136 return message_add_contact($contactid, 0); 1137 } 1138 1139 /** 1140 * Block a user. 1141 * 1142 * @param int $contactid the user ID of the user to block 1143 * @return bool 1144 */ 1145 function message_block_contact($contactid) { 1146 return message_add_contact($contactid, 1); 1147 } 1148 1149 /** 1150 * Checks if a user can delete a message. 1151 * 1152 * @param stdClass $message the message to delete 1153 * @param string $userid the user id of who we want to delete the message for (this may be done by the admin 1154 * but will still seem as if it was by the user) 1155 * @return bool Returns true if a user can delete the message, false otherwise. 1156 */ 1157 function message_can_delete_message($message, $userid) { 1158 global $USER; 1159 1160 if ($message->useridfrom == $userid) { 1161 $userdeleting = 'useridfrom'; 1162 } else if ($message->useridto == $userid) { 1163 $userdeleting = 'useridto'; 1164 } else { 1165 return false; 1166 } 1167 1168 $systemcontext = context_system::instance(); 1169 1170 // Let's check if the user is allowed to delete this message. 1171 if (has_capability('moodle/site:deleteanymessage', $systemcontext) || 1172 ((has_capability('moodle/site:deleteownmessage', $systemcontext) && 1173 $USER->id == $message->$userdeleting))) { 1174 return true; 1175 } 1176 1177 return false; 1178 } 1179 1180 /** 1181 * Deletes a message. 1182 * 1183 * This function does not verify any permissions. 1184 * 1185 * @param stdClass $message the message to delete 1186 * @param string $userid the user id of who we want to delete the message for (this may be done by the admin 1187 * but will still seem as if it was by the user) 1188 * @return bool 1189 */ 1190 function message_delete_message($message, $userid) { 1191 global $DB; 1192 1193 // The column we want to alter. 1194 if ($message->useridfrom == $userid) { 1195 $coltimedeleted = 'timeuserfromdeleted'; 1196 } else if ($message->useridto == $userid) { 1197 $coltimedeleted = 'timeusertodeleted'; 1198 } else { 1199 return false; 1200 } 1201 1202 // Don't update it if it's already been deleted. 1203 if ($message->$coltimedeleted > 0) { 1204 return false; 1205 } 1206 1207 // Get the table we want to update. 1208 if (isset($message->timeread)) { 1209 $messagetable = 'message_read'; 1210 } else { 1211 $messagetable = 'message'; 1212 } 1213 1214 // Mark the message as deleted. 1215 $updatemessage = new stdClass(); 1216 $updatemessage->id = $message->id; 1217 $updatemessage->$coltimedeleted = time(); 1218 $success = $DB->update_record($messagetable, $updatemessage); 1219 1220 if ($success) { 1221 // Trigger event for deleting a message. 1222 \core\event\message_deleted::create_from_ids($message->useridfrom, $message->useridto, 1223 $userid, $messagetable, $message->id)->trigger(); 1224 } 1225 1226 return $success; 1227 } 1228 1229 /** 1230 * Load a user's contact record 1231 * 1232 * @param int $contactid the user ID of the user whose contact record you want 1233 * @return array message contacts 1234 */ 1235 function message_get_contact($contactid) { 1236 global $USER, $DB; 1237 return $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid)); 1238 } 1239 1240 /** 1241 * Print the results of a message search 1242 * 1243 * @param mixed $frm submitted form data 1244 * @param bool $showicontext show text next to action icons? 1245 * @param object $currentuser the current user 1246 * @return void 1247 */ 1248 function message_print_search_results($frm, $showicontext=false, $currentuser=null) { 1249 global $USER, $DB, $OUTPUT; 1250 1251 if (empty($currentuser)) { 1252 $currentuser = $USER; 1253 } 1254 1255 echo html_writer::start_tag('div', array('class' => 'mdl-left')); 1256 1257 $personsearch = false; 1258 $personsearchstring = null; 1259 if (!empty($frm->personsubmit) and !empty($frm->name)) { 1260 $personsearch = true; 1261 $personsearchstring = $frm->name; 1262 } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) { 1263 $personsearch = true; 1264 $personsearchstring = $frm->combinedsearch; 1265 } 1266 1267 // Search for person. 1268 if ($personsearch) { 1269 if (optional_param('mycourses', 0, PARAM_BOOL)) { 1270 $users = array(); 1271 $mycourses = enrol_get_my_courses('id'); 1272 $mycoursesids = array(); 1273 foreach ($mycourses as $mycourse) { 1274 $mycoursesids[] = $mycourse->id; 1275 } 1276 $susers = message_search_users($mycoursesids, $personsearchstring); 1277 foreach ($susers as $suser) { 1278 $users[$suser->id] = $suser; 1279 } 1280 } else { 1281 $users = message_search_users(SITEID, $personsearchstring); 1282 } 1283 1284 if (!empty($users)) { 1285 echo html_writer::start_tag('p', array('class' => 'heading searchresultcount')); 1286 echo get_string('userssearchresults', 'message', count($users)); 1287 echo html_writer::end_tag('p'); 1288 1289 echo html_writer::start_tag('table', array('class' => 'messagesearchresults')); 1290 foreach ($users as $user) { 1291 1292 if ( $user->contactlistid ) { 1293 if ($user->blocked == 0) { // User is not blocked. 1294 $strcontact = message_contact_link($user->id, 'remove', true, null, $showicontext); 1295 $strblock = message_contact_link($user->id, 'block', true, null, $showicontext); 1296 } else { // blocked 1297 $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext); 1298 $strblock = message_contact_link($user->id, 'unblock', true, null, $showicontext); 1299 } 1300 } else { 1301 $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext); 1302 $strblock = message_contact_link($user->id, 'block', true, null, $showicontext); 1303 } 1304 1305 // Should we show just the icon or icon and text? 1306 $histicontext = 'icon'; 1307 if ($showicontext) { 1308 $histicontext = 'both'; 1309 } 1310 $strhistory = message_history_link($USER->id, $user->id, true, '', '', $histicontext); 1311 1312 echo html_writer::start_tag('tr'); 1313 1314 echo html_writer::start_tag('td', array('class' => 'pix')); 1315 echo $OUTPUT->user_picture($user, array('size' => 20, 'courseid' => SITEID)); 1316 echo html_writer::end_tag('td'); 1317 1318 echo html_writer::start_tag('td',array('class' => 'contact')); 1319 $action = null; 1320 $link = new moodle_url("/message/index.php?id=$user->id"); 1321 echo $OUTPUT->action_link($link, fullname($user), $action, array('title' => get_string('sendmessageto', 'message', fullname($user)))); 1322 echo html_writer::end_tag('td'); 1323 1324 echo html_writer::tag('td', $strcontact, array('class' => 'link')); 1325 echo html_writer::tag('td', $strblock, array('class' => 'link')); 1326 echo html_writer::tag('td', $strhistory, array('class' => 'link')); 1327 1328 echo html_writer::end_tag('tr'); 1329 } 1330 echo html_writer::end_tag('table'); 1331 1332 } else { 1333 echo html_writer::start_tag('p', array('class' => 'heading searchresultcount')); 1334 echo get_string('userssearchresults', 'message', 0).'<br /><br />'; 1335 echo html_writer::end_tag('p'); 1336 } 1337 } 1338 1339 // search messages for keywords 1340 $messagesearch = false; 1341 $messagesearchstring = null; 1342 if (!empty($frm->keywords)) { 1343 $messagesearch = true; 1344 $messagesearchstring = clean_text(trim($frm->keywords)); 1345 } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) { 1346 $messagesearch = true; 1347 $messagesearchstring = clean_text(trim($frm->combinedsearch)); 1348 } 1349 1350 if ($messagesearch) { 1351 if ($messagesearchstring) { 1352 $keywords = explode(' ', $messagesearchstring); 1353 } else { 1354 $keywords = array(); 1355 } 1356 $tome = false; 1357 $fromme = false; 1358 $courseid = 'none'; 1359 1360 if (empty($frm->keywordsoption)) { 1361 $frm->keywordsoption = 'allmine'; 1362 } 1363 1364 switch ($frm->keywordsoption) { 1365 case 'tome': 1366 $tome = true; 1367 break; 1368 case 'fromme': 1369 $fromme = true; 1370 break; 1371 case 'allmine': 1372 $tome = true; 1373 $fromme = true; 1374 break; 1375 case 'allusers': 1376 $courseid = SITEID; 1377 break; 1378 case 'courseusers': 1379 $courseid = $frm->courseid; 1380 break; 1381 default: 1382 $tome = true; 1383 $fromme = true; 1384 } 1385 1386 if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) { 1387 1388 // Get a list of contacts. 1389 if (($contacts = $DB->get_records('message_contacts', array('userid' => $USER->id), '', 'contactid, blocked') ) === false) { 1390 $contacts = array(); 1391 } 1392 1393 // Print heading with number of results. 1394 echo html_writer::start_tag('p', array('class' => 'heading searchresultcount')); 1395 $countresults = count($messages); 1396 if ($countresults == MESSAGE_SEARCH_MAX_RESULTS) { 1397 echo get_string('keywordssearchresultstoomany', 'message', $countresults).' ("'.s($messagesearchstring).'")'; 1398 } else { 1399 echo get_string('keywordssearchresults', 'message', $countresults); 1400 } 1401 echo html_writer::end_tag('p'); 1402 1403 // Print table headings. 1404 echo html_writer::start_tag('table', array('class' => 'messagesearchresults', 'cellspacing' => '0')); 1405 1406 $headertdstart = html_writer::start_tag('td', array('class' => 'messagesearchresultscol')); 1407 $headertdend = html_writer::end_tag('td'); 1408 echo html_writer::start_tag('tr'); 1409 echo $headertdstart.get_string('from').$headertdend; 1410 echo $headertdstart.get_string('to').$headertdend; 1411 echo $headertdstart.get_string('message', 'message').$headertdend; 1412 echo $headertdstart.get_string('timesent', 'message').$headertdend; 1413 echo html_writer::end_tag('tr'); 1414 1415 $blockedcount = 0; 1416 $dateformat = get_string('strftimedatetimeshort'); 1417 $strcontext = get_string('context', 'message'); 1418 foreach ($messages as $message) { 1419 1420 // Ignore messages to and from blocked users unless $frm->includeblocked is set. 1421 if (!optional_param('includeblocked', 0, PARAM_BOOL) and ( 1422 ( isset($contacts[$message->useridfrom]) and ($contacts[$message->useridfrom]->blocked == 1)) or 1423 ( isset($contacts[$message->useridto] ) and ($contacts[$message->useridto]->blocked == 1)) 1424 ) 1425 ) { 1426 $blockedcount ++; 1427 continue; 1428 } 1429 1430 // Load user-to record. 1431 if ($message->useridto !== $USER->id) { 1432 $userto = core_user::get_user($message->useridto); 1433 if ($userto === false) { 1434 $userto = core_user::get_noreply_user(); 1435 } 1436 $tocontact = (array_key_exists($message->useridto, $contacts) and 1437 ($contacts[$message->useridto]->blocked == 0) ); 1438 $toblocked = (array_key_exists($message->useridto, $contacts) and 1439 ($contacts[$message->useridto]->blocked == 1) ); 1440 } else { 1441 $userto = false; 1442 $tocontact = false; 1443 $toblocked = false; 1444 } 1445 1446 // Load user-from record. 1447 if ($message->useridfrom !== $USER->id) { 1448 $userfrom = core_user::get_user($message->useridfrom); 1449 if ($userfrom === false) { 1450 $userfrom = core_user::get_noreply_user(); 1451 } 1452 $fromcontact = (array_key_exists($message->useridfrom, $contacts) and 1453 ($contacts[$message->useridfrom]->blocked == 0) ); 1454 $fromblocked = (array_key_exists($message->useridfrom, $contacts) and 1455 ($contacts[$message->useridfrom]->blocked == 1) ); 1456 } else { 1457 $userfrom = false; 1458 $fromcontact = false; 1459 $fromblocked = false; 1460 } 1461 1462 // Find date string for this message. 1463 $date = usergetdate($message->timecreated); 1464 $datestring = $date['year'].$date['mon'].$date['mday']; 1465 1466 // Print out message row. 1467 echo html_writer::start_tag('tr', array('valign' => 'top')); 1468 1469 echo html_writer::start_tag('td', array('class' => 'contact')); 1470 message_print_user($userfrom, $fromcontact, $fromblocked, $showicontext); 1471 echo html_writer::end_tag('td'); 1472 1473 echo html_writer::start_tag('td', array('class' => 'contact')); 1474 message_print_user($userto, $tocontact, $toblocked, $showicontext); 1475 echo html_writer::end_tag('td'); 1476 1477 echo html_writer::start_tag('td', array('class' => 'summary')); 1478 echo message_get_fragment($message->smallmessage, $keywords); 1479 echo html_writer::start_tag('div', array('class' => 'link')); 1480 1481 // If the user clicks the context link display message sender on the left. 1482 // EXCEPT if the current user is in the conversation. Current user == always on the left. 1483 $leftsideuserid = $rightsideuserid = null; 1484 if ($currentuser->id == $message->useridto) { 1485 $leftsideuserid = $message->useridto; 1486 $rightsideuserid = $message->useridfrom; 1487 } else { 1488 $leftsideuserid = $message->useridfrom; 1489 $rightsideuserid = $message->useridto; 1490 } 1491 message_history_link($leftsideuserid, $rightsideuserid, false, 1492 $messagesearchstring, 'm'.$message->id, $strcontext); 1493 echo html_writer::end_tag('div'); 1494 echo html_writer::end_tag('td'); 1495 1496 echo html_writer::tag('td', userdate($message->timecreated, $dateformat), array('class' => 'date')); 1497 1498 echo html_writer::end_tag('tr'); 1499 } 1500 1501 1502 if ($blockedcount > 0) { 1503 echo html_writer::start_tag('tr'); 1504 echo html_writer::tag('td', get_string('blockedmessages', 'message', $blockedcount), array('colspan' => 4, 'align' => 'center')); 1505 echo html_writer::end_tag('tr'); 1506 } 1507 echo html_writer::end_tag('table'); 1508 1509 } else { 1510 echo html_writer::tag('p', get_string('keywordssearchresults', 'message', 0), array('class' => 'heading')); 1511 } 1512 } 1513 1514 if (!$personsearch && !$messagesearch) { 1515 //they didn't enter any search terms 1516 echo $OUTPUT->notification(get_string('emptysearchstring', 'message')); 1517 } 1518 1519 echo html_writer::end_tag('div'); 1520 } 1521 1522 /** 1523 * Print information on a user. Used when printing search results. 1524 * 1525 * @param object/bool $user the user to display or false if you just want $USER 1526 * @param bool $iscontact is the user being displayed a contact? 1527 * @param bool $isblocked is the user being displayed blocked? 1528 * @param bool $includeicontext include text next to the action icons? 1529 * @return void 1530 */ 1531 function message_print_user ($user=false, $iscontact=false, $isblocked=false, $includeicontext=false) { 1532 global $USER, $OUTPUT; 1533 1534 $userpictureparams = array('size' => 20, 'courseid' => SITEID); 1535 1536 if ($user === false) { 1537 echo $OUTPUT->user_picture($USER, $userpictureparams); 1538 } else if (core_user::is_real_user($user->id)) { 1539 echo $OUTPUT->user_picture($user, $userpictureparams); 1540 1541 $link = new moodle_url("/message/index.php?id=$user->id"); 1542 echo $OUTPUT->action_link($link, fullname($user), null, array('title' => 1543 get_string('sendmessageto', 'message', fullname($user)))); 1544 1545 $return = false; 1546 $script = null; 1547 if ($iscontact) { 1548 message_contact_link($user->id, 'remove', $return, $script, $includeicontext); 1549 } else { 1550 message_contact_link($user->id, 'add', $return, $script, $includeicontext); 1551 } 1552 1553 if ($isblocked) { 1554 message_contact_link($user->id, 'unblock', $return, $script, $includeicontext); 1555 } else { 1556 message_contact_link($user->id, 'block', $return, $script, $includeicontext); 1557 } 1558 } else { 1559 // If not real user, then don't show any links. 1560 $userpictureparams['link'] = false; 1561 // Stock profile picture should be displayed. 1562 echo $OUTPUT->user_picture($user, $userpictureparams); 1563 } 1564 } 1565 1566 /** 1567 * Print a message contact link 1568 * 1569 * @param int $userid the ID of the user to apply to action to 1570 * @param string $linktype can be add, remove, block or unblock 1571 * @param bool $return if true return the link as a string. If false echo the link. 1572 * @param string $script the URL to send the user to when the link is clicked. If null, the current page. 1573 * @param bool $text include text next to the icons? 1574 * @param bool $icon include a graphical icon? 1575 * @return string if $return is true otherwise bool 1576 */ 1577 function message_contact_link($userid, $linktype='add', $return=false, $script=null, $text=false, $icon=true) { 1578 global $OUTPUT, $PAGE; 1579 1580 //hold onto the strings as we're probably creating a bunch of links 1581 static $str; 1582 1583 if (empty($script)) { 1584 //strip off previous action params like 'removecontact' 1585 $script = message_remove_url_params($PAGE->url); 1586 } 1587 1588 if (empty($str->blockcontact)) { 1589 $str = new stdClass(); 1590 $str->blockcontact = get_string('blockcontact', 'message'); 1591 $str->unblockcontact = get_string('unblockcontact', 'message'); 1592 $str->removecontact = get_string('removecontact', 'message'); 1593 $str->addcontact = get_string('addcontact', 'message'); 1594 } 1595 1596 $command = $linktype.'contact'; 1597 $string = $str->{$command}; 1598 1599 $safealttext = s($string); 1600 1601 $safestring = ''; 1602 if (!empty($text)) { 1603 $safestring = $safealttext; 1604 } 1605 1606 $img = ''; 1607 if ($icon) { 1608 $iconpath = null; 1609 switch ($linktype) { 1610 case 'block': 1611 $iconpath = 't/block'; 1612 break; 1613 case 'unblock': 1614 $iconpath = 't/unblock'; 1615 break; 1616 case 'remove': 1617 $iconpath = 't/removecontact'; 1618 break; 1619 case 'add': 1620 default: 1621 $iconpath = 't/addcontact'; 1622 } 1623 1624 $img = '<img src="'.$OUTPUT->pix_url($iconpath).'" class="iconsmall" alt="'.$safealttext.'" />'; 1625 } 1626 1627 $output = '<span class="'.$linktype.'contact">'. 1628 '<a href="'.$script.'&'.$command.'='.$userid. 1629 '&sesskey='.sesskey().'" title="'.$safealttext.'">'. 1630 $img. 1631 $safestring.'</a></span>'; 1632 1633 if ($return) { 1634 return $output; 1635 } else { 1636 echo $output; 1637 return true; 1638 } 1639 } 1640 1641 /** 1642 * echo or return a link to take the user to the full message history between themselves and another user 1643 * 1644 * @param int $userid1 the ID of the user displayed on the left (usually the current user) 1645 * @param int $userid2 the ID of the other user 1646 * @param bool $return true to return the link as a string. False to echo the link. 1647 * @param string $keywords any keywords to highlight in the message history 1648 * @param string $position anchor name to jump to within the message history 1649 * @param string $linktext optionally specify the link text 1650 * @return string|bool. Returns a string if $return is true. Otherwise returns a boolean. 1651 */ 1652 function message_history_link($userid1, $userid2, $return=false, $keywords='', $position='', $linktext='') { 1653 global $OUTPUT, $PAGE; 1654 static $strmessagehistory; 1655 1656 if (empty($strmessagehistory)) { 1657 $strmessagehistory = get_string('messagehistory', 'message'); 1658 } 1659 1660 if ($position) { 1661 $position = "#$position"; 1662 } 1663 if ($keywords) { 1664 $keywords = "&search=".urlencode($keywords); 1665 } 1666 1667 if ($linktext == 'icon') { // Icon only 1668 $fulllink = '<img src="'.$OUTPUT->pix_url('t/messages') . '" class="iconsmall" alt="'.$strmessagehistory.'" />'; 1669 } else if ($linktext == 'both') { // Icon and standard name 1670 $fulllink = '<img src="'.$OUTPUT->pix_url('t/messages') . '" class="iconsmall" alt="" />'; 1671 $fulllink .= ' '.$strmessagehistory; 1672 } else if ($linktext) { // Custom name 1673 $fulllink = $linktext; 1674 } else { // Standard name only 1675 $fulllink = $strmessagehistory; 1676 } 1677 1678 $popupoptions = array( 1679 'height' => 500, 1680 'width' => 500, 1681 'menubar' => false, 1682 'location' => false, 1683 'status' => true, 1684 'scrollbars' => true, 1685 'resizable' => true); 1686 1687 $link = new moodle_url('/message/index.php?history='.MESSAGE_HISTORY_ALL."&user1=$userid1&user2=$userid2$keywords$position"); 1688 if ($PAGE->url && $PAGE->url->get_param('viewing')) { 1689 $link->param('viewing', $PAGE->url->get_param('viewing')); 1690 } 1691 $action = null; 1692 $str = $OUTPUT->action_link($link, $fulllink, $action, array('title' => $strmessagehistory)); 1693 1694 $str = '<span class="history">'.$str.'</span>'; 1695 1696 if ($return) { 1697 return $str; 1698 } else { 1699 echo $str; 1700 return true; 1701 } 1702 } 1703 1704 1705 /** 1706 * Search through course users. 1707 * 1708 * If $courseids contains the site course then this function searches 1709 * through all undeleted and confirmed users. 1710 * 1711 * @param int|array $courseids Course ID or array of course IDs. 1712 * @param string $searchtext the text to search for. 1713 * @param string $sort the column name to order by. 1714 * @param string|array $exceptions comma separated list or array of user IDs to exclude. 1715 * @return array An array of {@link $USER} records. 1716 */ 1717 function message_search_users($courseids, $searchtext, $sort='', $exceptions='') { 1718 global $CFG, $USER, $DB; 1719 1720 // Basic validation to ensure that the parameter $courseids is not an empty array or an empty value. 1721 if (!$courseids) { 1722 $courseids = array(SITEID); 1723 } 1724 1725 // Allow an integer to be passed. 1726 if (!is_array($courseids)) { 1727 $courseids = array($courseids); 1728 } 1729 1730 $fullname = $DB->sql_fullname(); 1731 $ufields = user_picture::fields('u'); 1732 1733 if (!empty($sort)) { 1734 $order = ' ORDER BY '. $sort; 1735 } else { 1736 $order = ''; 1737 } 1738 1739 $params = array( 1740 'userid' => $USER->id, 1741 'query' => "%$searchtext%" 1742 ); 1743 1744 if (empty($exceptions)) { 1745 $exceptions = array(); 1746 } else if (!empty($exceptions) && is_string($exceptions)) { 1747 $exceptions = explode(',', $exceptions); 1748 } 1749 1750 // Ignore self and guest account. 1751 $exceptions[] = $USER->id; 1752 $exceptions[] = $CFG->siteguest; 1753 1754 // Exclude exceptions from the search result. 1755 list($except, $params_except) = $DB->get_in_or_equal($exceptions, SQL_PARAMS_NAMED, 'param', false); 1756 $except = ' AND u.id ' . $except; 1757 $params = array_merge($params_except, $params); 1758 1759 if (in_array(SITEID, $courseids)) { 1760 // Search on site level. 1761 return $DB->get_records_sql("SELECT $ufields, mc.id as contactlistid, mc.blocked 1762 FROM {user} u 1763 LEFT JOIN {message_contacts} mc 1764 ON mc.contactid = u.id AND mc.userid = :userid 1765 WHERE u.deleted = '0' AND u.confirmed = '1' 1766 AND (".$DB->sql_like($fullname, ':query', false).") 1767 $except 1768 $order", $params); 1769 } else { 1770 // Search in courses. 1771 1772 // Getting the context IDs or each course. 1773 $contextids = array(); 1774 foreach ($courseids as $courseid) { 1775 $context = context_course::instance($courseid); 1776 $contextids = array_merge($contextids, $context->get_parent_context_ids(true)); 1777 } 1778 list($contextwhere, $contextparams) = $DB->get_in_or_equal(array_unique($contextids), SQL_PARAMS_NAMED, 'context'); 1779 $params = array_merge($params, $contextparams); 1780 1781 // Everyone who has a role assignment in this course or higher. 1782 // TODO: add enabled enrolment join here (skodak) 1783 $users = $DB->get_records_sql("SELECT DISTINCT $ufields, mc.id as contactlistid, mc.blocked 1784 FROM {user} u 1785 JOIN {role_assignments} ra ON ra.userid = u.id 1786 LEFT JOIN {message_contacts} mc 1787 ON mc.contactid = u.id AND mc.userid = :userid 1788 WHERE u.deleted = '0' AND u.confirmed = '1' 1789 AND (".$DB->sql_like($fullname, ':query', false).") 1790 AND ra.contextid $contextwhere 1791 $except 1792 $order", $params); 1793 1794 return $users; 1795 } 1796 } 1797 1798 /** 1799 * Search a user's messages 1800 * 1801 * Returns a list of posts found using an array of search terms 1802 * eg word +word -word 1803 * 1804 * @param array $searchterms an array of search terms (strings) 1805 * @param bool $fromme include messages from the user? 1806 * @param bool $tome include messages to the user? 1807 * @param mixed $courseid SITEID for admins searching all messages. Other behaviour not yet implemented 1808 * @param int $userid the user ID of the current user 1809 * @return mixed An array of messages or false if no matching messages were found 1810 */ 1811 function message_search($searchterms, $fromme=true, $tome=true, $courseid='none', $userid=0) { 1812 global $CFG, $USER, $DB; 1813 1814 // If user is searching all messages check they are allowed to before doing anything else. 1815 if ($courseid == SITEID && !has_capability('moodle/site:readallmessages', context_system::instance())) { 1816 print_error('accessdenied','admin'); 1817 } 1818 1819 // If no userid sent then assume current user. 1820 if ($userid == 0) $userid = $USER->id; 1821 1822 // Some differences in SQL syntax. 1823 if ($DB->sql_regex_supported()) { 1824 $REGEXP = $DB->sql_regex(true); 1825 $NOTREGEXP = $DB->sql_regex(false); 1826 } 1827 1828 $searchcond = array(); 1829 $params = array(); 1830 $i = 0; 1831 1832 // Preprocess search terms to check whether we have at least 1 eligible search term. 1833 // If we do we can drop words around it like 'a'. 1834 $dropshortwords = false; 1835 foreach ($searchterms as $searchterm) { 1836 if (strlen($searchterm) >= 2) { 1837 $dropshortwords = true; 1838 } 1839 } 1840 1841 foreach ($searchterms as $searchterm) { 1842 $i++; 1843 1844 $NOT = false; // Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle. 1845 1846 if ($dropshortwords && strlen($searchterm) < 2) { 1847 continue; 1848 } 1849 // Under Oracle and MSSQL, trim the + and - operators and perform simpler LIKE search. 1850 if (!$DB->sql_regex_supported()) { 1851 if (substr($searchterm, 0, 1) == '-') { 1852 $NOT = true; 1853 } 1854 $searchterm = trim($searchterm, '+-'); 1855 } 1856 1857 if (substr($searchterm,0,1) == "+") { 1858 $searchterm = substr($searchterm,1); 1859 $searchterm = preg_quote($searchterm, '|'); 1860 $searchcond[] = "m.fullmessage $REGEXP :ss$i"; 1861 $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; 1862 1863 } else if (substr($searchterm,0,1) == "-") { 1864 $searchterm = substr($searchterm,1); 1865 $searchterm = preg_quote($searchterm, '|'); 1866 $searchcond[] = "m.fullmessage $NOTREGEXP :ss$i"; 1867 $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; 1868 1869 } else { 1870 $searchcond[] = $DB->sql_like("m.fullmessage", ":ss$i", false, true, $NOT); 1871 $params['ss'.$i] = "%$searchterm%"; 1872 } 1873 } 1874 1875 if (empty($searchcond)) { 1876 $searchcond = " ".$DB->sql_like('m.fullmessage', ':ss1', false); 1877 $params['ss1'] = "%"; 1878 } else { 1879 $searchcond = implode(" AND ", $searchcond); 1880 } 1881 1882 // There are several possibilities 1883 // 1. courseid = SITEID : The admin is searching messages by all users 1884 // 2. courseid = ?? : A teacher is searching messages by users in 1885 // one of their courses - currently disabled 1886 // 3. courseid = none : User is searching their own messages; 1887 // a. Messages from user 1888 // b. Messages to user 1889 // c. Messages to and from user 1890 1891 if ($fromme && $tome) { 1892 $searchcond .= " AND ((useridto = :useridto AND timeusertodeleted = 0) OR 1893 (useridfrom = :useridfrom AND timeuserfromdeleted = 0))"; 1894 $params['useridto'] = $userid; 1895 $params['useridfrom'] = $userid; 1896 } else if ($fromme) { 1897 $searchcond .= " AND (useridfrom = :useridfrom AND timeuserfromdeleted = 0)"; 1898 $params['useridfrom'] = $userid; 1899 } else if ($tome) { 1900 $searchcond .= " AND (useridto = :useridto AND timeusertodeleted = 0)"; 1901 $params['useridto'] = $userid; 1902 } 1903 if ($courseid == SITEID) { // Admin is searching all messages. 1904 $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated 1905 FROM {message_read} m 1906 WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); 1907 $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated 1908 FROM {message} m 1909 WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); 1910 1911 } else if ($courseid !== 'none') { 1912 // This has not been implemented due to security concerns. 1913 $m_read = array(); 1914 $m_unread = array(); 1915 1916 } else { 1917 1918 if ($fromme and $tome) { 1919 $searchcond .= " AND (m.useridfrom=:userid1 OR m.useridto=:userid2)"; 1920 $params['userid1'] = $userid; 1921 $params['userid2'] = $userid; 1922 1923 } else if ($fromme) { 1924 $searchcond .= " AND m.useridfrom=:userid"; 1925 $params['userid'] = $userid; 1926 1927 } else if ($tome) { 1928 $searchcond .= " AND m.useridto=:userid"; 1929 $params['userid'] = $userid; 1930 } 1931 1932 $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated 1933 FROM {message_read} m 1934 WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); 1935 $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated 1936 FROM {message} m 1937 WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); 1938 1939 } 1940 1941 /// The keys may be duplicated in $m_read and $m_unread so we can't 1942 /// do a simple concatenation 1943 $messages = array(); 1944 foreach ($m_read as $m) { 1945 $messages[] = $m; 1946 } 1947 foreach ($m_unread as $m) { 1948 $messages[] = $m; 1949 } 1950 1951 return (empty($messages)) ? false : $messages; 1952 } 1953 1954 /** 1955 * Given a message object that we already know has a long message 1956 * this function truncates the message nicely to the first 1957 * sane place between $CFG->forum_longpost and $CFG->forum_shortpost 1958 * 1959 * @param string $message the message 1960 * @param int $minlength the minimum length to trim the message to 1961 * @return string the shortened message 1962 */ 1963 function message_shorten_message($message, $minlength = 0) { 1964 $i = 0; 1965 $tag = false; 1966 $length = strlen($message); 1967 $count = 0; 1968 $stopzone = false; 1969 $truncate = 0; 1970 if ($minlength == 0) $minlength = MESSAGE_SHORTLENGTH; 1971 1972 1973 for ($i=0; $i<$length; $i++) { 1974 $char = $message[$i]; 1975 1976 switch ($char) { 1977 case "<": 1978 $tag = true; 1979 break; 1980 case ">": 1981 $tag = false; 1982 break; 1983 default: 1984 if (!$tag) { 1985 if ($stopzone) { 1986 if ($char == '.' or $char == ' ') { 1987 $truncate = $i+1; 1988 break 2; 1989 } 1990 } 1991 $count++; 1992 } 1993 break; 1994 } 1995 if (!$stopzone) { 1996 if ($count > $minlength) { 1997 $stopzone = true; 1998 } 1999 } 2000 } 2001 2002 if (!$truncate) { 2003 $truncate = $i; 2004 } 2005 2006 return substr($message, 0, $truncate); 2007 } 2008 2009 2010 /** 2011 * Given a string and an array of keywords, this function looks 2012 * for the first keyword in the string, and then chops out a 2013 * small section from the text that shows that word in context. 2014 * 2015 * @param string $message the text to search 2016 * @param array $keywords array of keywords to find 2017 */ 2018 function message_get_fragment($message, $keywords) { 2019 2020 $fullsize = 160; 2021 $halfsize = (int)($fullsize/2); 2022 2023 $message = strip_tags($message); 2024 2025 foreach ($keywords as $keyword) { // Just get the first one 2026 if ($keyword !== '') { 2027 break; 2028 } 2029 } 2030 if (empty($keyword)) { // None found, so just return start of message 2031 return message_shorten_message($message, 30); 2032 } 2033 2034 $leadin = $leadout = ''; 2035 2036 /// Find the start of the fragment 2037 $start = 0; 2038 $length = strlen($message); 2039 2040 $pos = strpos($message, $keyword); 2041 if ($pos > $halfsize) { 2042 $start = $pos - $halfsize; 2043 $leadin = '...'; 2044 } 2045 /// Find the end of the fragment 2046 $end = $start + $fullsize; 2047 if ($end > $length) { 2048 $end = $length; 2049 } else { 2050 $leadout = '...'; 2051 } 2052 2053 /// Pull out the fragment and format it 2054 2055 $fragment = substr($message, $start, $end - $start); 2056 $fragment = $leadin.highlight(implode(' ',$keywords), $fragment).$leadout; 2057 return $fragment; 2058 } 2059 2060 /** 2061 * Retrieve the messages between two users 2062 * 2063 * @param object $user1 the current user 2064 * @param object $user2 the other user 2065 * @param int $limitnum the maximum number of messages to retrieve 2066 * @param bool $viewingnewmessages are we currently viewing new messages? 2067 */ 2068 function message_get_history($user1, $user2, $limitnum=0, $viewingnewmessages=false) { 2069 global $DB, $CFG; 2070 2071 $messages = array(); 2072 2073 //we want messages sorted oldest to newest but if getting a subset of messages we need to sort 2074 //desc to get the last $limitnum messages then flip the order in php 2075 $sort = 'asc'; 2076 if ($limitnum>0) { 2077 $sort = 'desc'; 2078 } 2079 2080 $notificationswhere = null; 2081 //we have just moved new messages to read. If theyre here to see new messages dont hide notifications 2082 if (!$viewingnewmessages && $CFG->messaginghidereadnotifications) { 2083 $notificationswhere = 'AND notification=0'; 2084 } 2085 2086 //prevent notifications of your own actions appearing in your own message history 2087 $ownnotificationwhere = ' AND NOT (useridfrom=? AND notification=1)'; 2088 2089 $sql = "((useridto = ? AND useridfrom = ? AND timeusertodeleted = 0) OR 2090 (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = 0))"; 2091 if ($messages_read = $DB->get_records_select('message_read', $sql . $notificationswhere . $ownnotificationwhere, 2092 array($user1->id, $user2->id, $user2->id, $user1->id, $user1->id), 2093 "timecreated $sort", '*', 0, $limitnum)) { 2094 foreach ($messages_read as $message) { 2095 $messages[] = $message; 2096 } 2097 } 2098 if ($messages_new = $DB->get_records_select('message', $sql . $ownnotificationwhere, 2099 array($user1->id, $user2->id, $user2->id, $user1->id, $user1->id), 2100 "timecreated $sort", '*', 0, $limitnum)) { 2101 foreach ($messages_new as $message) { 2102 $messages[] = $message; 2103 } 2104 } 2105 2106 $result = core_collator::asort_objects_by_property($messages, 'timecreated', core_collator::SORT_NUMERIC); 2107 2108 //if we only want the last $limitnum messages 2109 $messagecount = count($messages); 2110 if ($limitnum > 0 && $messagecount > $limitnum) { 2111 $messages = array_slice($messages, $messagecount - $limitnum, $limitnum, true); 2112 } 2113 2114 return $messages; 2115 } 2116 2117 /** 2118 * Print the message history between two users 2119 * 2120 * @param object $user1 the current user 2121 * @param object $user2 the other user 2122 * @param string $search search terms to highlight 2123 * @param int $messagelimit maximum number of messages to return 2124 * @param string $messagehistorylink the html for the message history link or false 2125 * @param bool $viewingnewmessages are we currently viewing new messages? 2126 */ 2127 function message_print_message_history($user1, $user2 ,$search = '', $messagelimit = 0, $messagehistorylink = false, $viewingnewmessages = false, $showactionlinks = true) { 2128 global $OUTPUT, $PAGE; 2129 2130 $PAGE->requires->yui_module( 2131 array('moodle-core_message-toolbox'), 2132 'M.core_message.toolbox.deletemsg.init', 2133 array(array()) 2134 ); 2135 2136 echo $OUTPUT->box_start('center', 'message_user_pictures'); 2137 echo $OUTPUT->box_start('user'); 2138 echo $OUTPUT->box_start('generalbox', 'user1'); 2139 echo $OUTPUT->user_picture($user1, array('size' => 100, 'courseid' => SITEID)); 2140 echo html_writer::tag('div', fullname($user1), array('class' => 'heading')); 2141 echo $OUTPUT->box_end(); 2142 echo $OUTPUT->box_end(); 2143 2144 $imgattr = array('src' => $OUTPUT->pix_url('i/twoway'), 'alt' => '', 'width' => 16, 'height' => 16); 2145 echo $OUTPUT->box(html_writer::empty_tag('img', $imgattr), 'between'); 2146 2147 echo $OUTPUT->box_start('user'); 2148 echo $OUTPUT->box_start('generalbox', 'user2'); 2149 // Show user picture with link is real user else without link. 2150 if (core_user::is_real_user($user2->id)) { 2151 echo $OUTPUT->user_picture($user2, array('size' => 100, 'courseid' => SITEID)); 2152 } else { 2153 echo $OUTPUT->user_picture($user2, array('size' => 100, 'courseid' => SITEID, 'link' => false)); 2154 } 2155 echo html_writer::tag('div', fullname($user2), array('class' => 'heading')); 2156 2157 if ($showactionlinks && isset($user2->iscontact) && isset($user2->isblocked)) { 2158 2159 $script = null; 2160 $text = true; 2161 $icon = false; 2162 2163 $strcontact = message_get_contact_add_remove_link($user2->iscontact, $user2->isblocked, $user2, $script, $text, $icon); 2164 $strblock = message_get_contact_block_link($user2->iscontact, $user2->isblocked, $user2, $script, $text, $icon); 2165 $useractionlinks = $strcontact.' | '.$strblock; 2166 2167 echo html_writer::tag('div', $useractionlinks, array('class' => 'useractionlinks')); 2168 } 2169 echo $OUTPUT->box_end(); 2170 echo $OUTPUT->box_end(); 2171 echo $OUTPUT->box_end(); 2172 2173 if (!empty($messagehistorylink)) { 2174 echo $messagehistorylink; 2175 } 2176 2177 /// Get all the messages and print them 2178 if ($messages = message_get_history($user1, $user2, $messagelimit, $viewingnewmessages)) { 2179 $tablecontents = ''; 2180 2181 $current = new stdClass(); 2182 $current->mday = ''; 2183 $current->month = ''; 2184 $current->year = ''; 2185 $messagedate = get_string('strftimetime'); 2186 $blockdate = get_string('strftimedaydate'); 2187 $messagenumber = 0; 2188 foreach ($messages as $message) { 2189 $messagenumber++; 2190 if ($message->notification) { 2191 $notificationclass = ' notification'; 2192 } else { 2193 $notificationclass = null; 2194 } 2195 $date = usergetdate($message->timecreated); 2196 if ($current->mday != $date['mday'] | $current->month != $date['month'] | $current->year != $date['year']) { 2197 $current->mday = $date['mday']; 2198 $current->month = $date['month']; 2199 $current->year = $date['year']; 2200 2201 $datestring = html_writer::empty_tag('a', array('name' => $date['year'].$date['mon'].$date['mday'])); 2202 $tablecontents .= html_writer::tag('div', $datestring, array('class' => 'mdl-align heading')); 2203 2204 $tablecontents .= $OUTPUT->heading(userdate($message->timecreated, $blockdate), 4, 'mdl-align'); 2205 } 2206 2207 if ($message->useridfrom == $user1->id) { 2208 $formatted_message = message_format_message($message, $messagedate, $search, 'me'); 2209 $side = 'left'; 2210 } else { 2211 $formatted_message = message_format_message($message, $messagedate, $search, 'other'); 2212 $side = 'right'; 2213 } 2214 2215 // Check if it is a read message or not. 2216 if (isset($message->timeread)) { 2217 $type = 'message_read'; 2218 } else { 2219 $type = 'message'; 2220 } 2221 2222 if (message_can_delete_message($message, $user1->id)) { 2223 $usergroup = optional_param('usergroup', MESSAGE_VIEW_UNREAD_MESSAGES, PARAM_ALPHANUMEXT); 2224 $viewing = optional_param('viewing', $usergroup, PARAM_ALPHANUMEXT); 2225 $deleteurl = new moodle_url('/message/index.php', array('user1' => $user1->id, 'user2' => $user2->id, 2226 'viewing' => $viewing, 'deletemessageid' => $message->id, 'deletemessagetype' => $type, 2227 'sesskey' => sesskey())); 2228 2229 $deleteicon = $OUTPUT->action_icon($deleteurl, new pix_icon('t/delete', get_string('delete'))); 2230 $deleteicon = html_writer::tag('div', $deleteicon, array('class' => 'deleteicon accesshide')); 2231 $formatted_message .= $deleteicon; 2232 } 2233 2234 $tablecontents .= html_writer::tag('div', $formatted_message, array('class' => "mdl-left messagecontent 2235 $side $notificationclass", 'id' => 'message_' . $messagenumber)); 2236 } 2237 2238 echo html_writer::nonempty_tag('div', $tablecontents, array('class' => 'mdl-left messagehistory')); 2239 } else { 2240 echo html_writer::nonempty_tag('div', '('.get_string('nomessagesfound', 'message').')', array('class' => 'mdl-align messagehistory')); 2241 } 2242 } 2243 2244 /** 2245 * Format a message for display in the message history 2246 * 2247 * @param object $message the message object 2248 * @param string $format optional date format 2249 * @param string $keywords keywords to highlight 2250 * @param string $class CSS class to apply to the div around the message 2251 * @return string the formatted message 2252 */ 2253 function message_format_message($message, $format='', $keywords='', $class='other') { 2254 2255 static $dateformat; 2256 2257 //if we haven't previously set the date format or they've supplied a new one 2258 if ( empty($dateformat) || (!empty($format) && $dateformat != $format) ) { 2259 if ($format) { 2260 $dateformat = $format; 2261 } else { 2262 $dateformat = get_string('strftimedatetimeshort'); 2263 } 2264 } 2265 $time = userdate($message->timecreated, $dateformat); 2266 2267 $messagetext = message_format_message_text($message, false); 2268 2269 if ($keywords) { 2270 $messagetext = highlight($keywords, $messagetext); 2271 } 2272 2273 $messagetext .= message_format_contexturl($message); 2274 2275 $messagetext = clean_text($messagetext, FORMAT_HTML); 2276 2277 return <<<TEMPLATE 2278 <div class='message $class'> 2279 <a name="m{$message->id}"></a> 2280 <span class="message-meta"><span class="time">$time</span></span>: <span class="text">$messagetext</span> 2281 </div> 2282 TEMPLATE; 2283 } 2284 2285 /** 2286 * Format a the context url and context url name of a message for display 2287 * 2288 * @param object $message the message object 2289 * @return string the formatted string 2290 */ 2291 function message_format_contexturl($message) { 2292 $s = null; 2293 2294 if (!empty($message->contexturl)) { 2295 $displaytext = null; 2296 if (!empty($message->contexturlname)) { 2297 $displaytext= $message->contexturlname; 2298 } else { 2299 $displaytext= $message->contexturl; 2300 } 2301 $s .= html_writer::start_tag('div',array('class' => 'messagecontext')); 2302 $s .= get_string('view').': '.html_writer::tag('a', $displaytext, array('href' => $message->contexturl)); 2303 $s .= html_writer::end_tag('div'); 2304 } 2305 2306 return $s; 2307 } 2308 2309 /** 2310 * Send a message from one user to another. Will be delivered according to the message recipients messaging preferences 2311 * 2312 * @param object $userfrom the message sender 2313 * @param object $userto the message recipient 2314 * @param string $message the message 2315 * @param int $format message format such as FORMAT_PLAIN or FORMAT_HTML 2316 * @return int|false the ID of the new message or false 2317 */ 2318 function message_post_message($userfrom, $userto, $message, $format) { 2319 global $SITE, $CFG, $USER; 2320 2321 $eventdata = new stdClass(); 2322 $eventdata->component = 'moodle'; 2323 $eventdata->name = 'instantmessage'; 2324 $eventdata->userfrom = $userfrom; 2325 $eventdata->userto = $userto; 2326 2327 //using string manager directly so that strings in the message will be in the message recipients language rather than the senders 2328 $eventdata->subject = get_string_manager()->get_string('unreadnewmessage', 'message', fullname($userfrom), $userto->lang); 2329 2330 if ($format == FORMAT_HTML) { 2331 $eventdata->fullmessagehtml = $message; 2332 //some message processors may revert to sending plain text even if html is supplied 2333 //so we keep both plain and html versions if we're intending to send html 2334 $eventdata->fullmessage = html_to_text($eventdata->fullmessagehtml); 2335 } else { 2336 $eventdata->fullmessage = $message; 2337 $eventdata->fullmessagehtml = ''; 2338 } 2339 2340 $eventdata->fullmessageformat = $format; 2341 $eventdata->smallmessage = $message;//store the message unfiltered. Clean up on output. 2342 2343 $s = new stdClass(); 2344 $s->sitename = format_string($SITE->shortname, true, array('context' => context_course::instance(SITEID))); 2345 $s->url = $CFG->wwwroot.'/message/index.php?user='.$userto->id.'&id='.$userfrom->id; 2346 2347 $emailtagline = get_string_manager()->get_string('emailtagline', 'message', $s, $userto->lang); 2348 if (!empty($eventdata->fullmessage)) { 2349 $eventdata->fullmessage .= "\n\n---------------------------------------------------------------------\n".$emailtagline; 2350 } 2351 if (!empty($eventdata->fullmessagehtml)) { 2352 $eventdata->fullmessagehtml .= "<br /><br />---------------------------------------------------------------------<br />".$emailtagline; 2353 } 2354 2355 $eventdata->timecreated = time(); 2356 $eventdata->notification = 0; 2357 return message_send($eventdata); 2358 } 2359 2360 /** 2361 * Print a row of contactlist displaying user picture, messages waiting and 2362 * block links etc 2363 * 2364 * @param object $contact contact object containing all fields required for $OUTPUT->user_picture() 2365 * @param bool $incontactlist is the user a contact of ours? 2366 * @param bool $isblocked is the user blocked? 2367 * @param string $selectcontacturl the url to send the user to when a contact's name is clicked 2368 * @param bool $showactionlinks display action links next to the other users (add contact, block user etc) 2369 * @param object $selecteduser the user the current user is viewing (if any). They will be highlighted. 2370 * @return void 2371 */ 2372 function message_print_contactlist_user($contact, $incontactlist = true, $isblocked = false, $selectcontacturl = null, $showactionlinks = true, $selecteduser=null) { 2373 global $OUTPUT, $USER, $COURSE; 2374 $fullname = fullname($contact); 2375 $fullnamelink = $fullname; 2376 $output = ''; 2377 2378 $linkclass = ''; 2379 if (!empty($selecteduser) && $contact->id == $selecteduser->id) { 2380 $linkclass = 'messageselecteduser'; 2381 } 2382 2383 // Are there any unread messages for this contact? 2384 if ($contact->messagecount > 0 ){ 2385 $fullnamelink = '<strong>'.$fullnamelink.' ('.$contact->messagecount.')</strong>'; 2386 } 2387 2388 $strcontact = $strblock = $strhistory = null; 2389 2390 if ($showactionlinks) { 2391 // Show block and delete links if user is real user. 2392 if (core_user::is_real_user($contact->id)) { 2393 $strcontact = message_get_contact_add_remove_link($incontactlist, $isblocked, $contact); 2394 $strblock = message_get_contact_block_link($incontactlist, $isblocked, $contact); 2395 } 2396 $strhistory = message_history_link($USER->id, $contact->id, true, '', '', 'icon'); 2397 } 2398 2399 $output .= html_writer::start_tag('div', array('class' => 'pix')); 2400 $output .= $OUTPUT->user_picture($contact, array('size' => 20, 'courseid' => $COURSE->id)); 2401 $output .= html_writer::end_tag('div'); 2402 2403 $popupoptions = array( 2404 'height' => MESSAGE_DISCUSSION_HEIGHT, 2405 'width' => MESSAGE_DISCUSSION_WIDTH, 2406 'menubar' => false, 2407 'location' => false, 2408 'status' => true, 2409 'scrollbars' => true, 2410 'resizable' => true); 2411 2412 $link = $action = null; 2413 if (!empty($selectcontacturl)) { 2414 $link = new moodle_url($selectcontacturl.'&user2='.$contact->id); 2415 } else { 2416 //can $selectcontacturl be removed and maybe the be removed and hardcoded? 2417 $link = new moodle_url("/message/index.php?id=$contact->id"); 2418 $action = new popup_action('click', $link, "message_$contact->id", $popupoptions); 2419 } 2420 2421 2422 if (strlen($strcontact . $strblock . $strhistory) > 0) { 2423 $output .= html_writer::tag('div', $strcontact . $strblock . $strhistory, array('class' => 'link')); 2424 2425 $output .= html_writer::start_tag('div', array('class' => 'contact')); 2426 $linkattr = array('class' => $linkclass, 'title' => get_string('sendmessageto', 'message', $fullname)); 2427 $output .= $OUTPUT->action_link($link, $fullnamelink, $action, $linkattr); 2428 $output .= html_writer::end_tag('div'); 2429 } else { 2430 $output .= html_writer::start_tag('div', array('class' => 'contact nolinks')); 2431 $linkattr = array('class' => $linkclass, 'title' => get_string('sendmessageto', 'message', $fullname)); 2432 $output .= $OUTPUT->action_link($link, $fullnamelink, $action, $linkattr); 2433 $output .= html_writer::end_tag('div'); 2434 } 2435 2436 return $output; 2437 } 2438 2439 /** 2440 * Constructs the add/remove contact link to display next to other users 2441 * 2442 * @param bool $incontactlist is the user a contact 2443 * @param bool $isblocked is the user blocked 2444 * @param stdClass $contact contact object 2445 * @param string $script the URL to send the user to when the link is clicked. If null, the current page. 2446 * @param bool $text include text next to the icons? 2447 * @param bool $icon include a graphical icon? 2448 * @return string 2449 */ 2450 function message_get_contact_add_remove_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { 2451 $strcontact = ''; 2452 2453 if($incontactlist){ 2454 $strcontact = message_contact_link($contact->id, 'remove', true, $script, $text, $icon); 2455 } else if ($isblocked) { 2456 $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); 2457 } else{ 2458 $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); 2459 } 2460 2461 return $strcontact; 2462 } 2463 2464 /** 2465 * Constructs the block contact link to display next to other users 2466 * 2467 * @param bool $incontactlist is the user a contact? 2468 * @param bool $isblocked is the user blocked? 2469 * @param stdClass $contact contact object 2470 * @param string $script the URL to send the user to when the link is clicked. If null, the current page. 2471 * @param bool $text include text next to the icons? 2472 * @param bool $icon include a graphical icon? 2473 * @return string 2474 */ 2475 function message_get_contact_block_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { 2476 $strblock = ''; 2477 2478 //commented out to allow the user to block a contact without having to remove them first 2479 /*if ($incontactlist) { 2480 //$strblock = ''; 2481 } else*/ 2482 if ($isblocked) { 2483 $strblock = message_contact_link($contact->id, 'unblock', true, $script, $text, $icon); 2484 } else{ 2485 $strblock = message_contact_link($contact->id, 'block', true, $script, $text, $icon); 2486 } 2487 2488 return $strblock; 2489 } 2490 2491 /** 2492 * Moves messages from a particular user from the message table (unread messages) to message_read 2493 * This is typically only used when a user is deleted 2494 * 2495 * @param object $userid User id 2496 * @return boolean success 2497 */ 2498 function message_move_userfrom_unread2read($userid) { 2499 global $DB; 2500 2501 // move all unread messages from message table to message_read 2502 if ($messages = $DB->get_records_select('message', 'useridfrom = ?', array($userid), 'timecreated')) { 2503 foreach ($messages as $message) { 2504 message_mark_message_read($message, 0); //set timeread to 0 as the message was never read 2505 } 2506 } 2507 return true; 2508 } 2509 2510 /** 2511 * marks ALL messages being sent from $fromuserid to $touserid as read 2512 * 2513 * @param int $touserid the id of the message recipient 2514 * @param int $fromuserid the id of the message sender 2515 * @return void 2516 */ 2517 function message_mark_messages_read($touserid, $fromuserid) { 2518 global $DB; 2519 2520 $sql = 'SELECT m.* FROM {message} m WHERE m.useridto=:useridto AND m.useridfrom=:useridfrom'; 2521 $messages = $DB->get_recordset_sql($sql, array('useridto' => $touserid,'useridfrom' => $fromuserid)); 2522 2523 foreach ($messages as $message) { 2524 message_mark_message_read($message, time()); 2525 } 2526 2527 $messages->close(); 2528 } 2529 2530 /** 2531 * Mark a single message as read 2532 * 2533 * @param stdClass $message An object with an object property ie $message->id which is an id in the message table 2534 * @param int $timeread the timestamp for when the message should be marked read. Usually time(). 2535 * @param bool $messageworkingempty Is the message_working table already confirmed empty for this message? 2536 * @return int the ID of the message in the message_read table 2537 */ 2538 function message_mark_message_read($message, $timeread, $messageworkingempty=false) { 2539 global $DB; 2540 2541 $message->timeread = $timeread; 2542 2543 $messageid = $message->id; 2544 unset($message->id);//unset because it will get a new id on insert into message_read 2545 2546 //If any processors have pending actions abort them 2547 if (!$messageworkingempty) { 2548 $DB->delete_records('message_working', array('unreadmessageid' => $messageid)); 2549 } 2550 $messagereadid = $DB->insert_record('message_read', $message); 2551 2552 $DB->delete_records('message', array('id' => $messageid)); 2553 2554 // Get the context for the user who received the message. 2555 $context = context_user::instance($message->useridto, IGNORE_MISSING); 2556 // If the user no longer exists the context value will be false, in this case use the system context. 2557 if ($context === false) { 2558 $context = context_system::instance(); 2559 } 2560 2561 // Trigger event for reading a message. 2562 $event = \core\event\message_viewed::create(array( 2563 'objectid' => $messagereadid, 2564 'userid' => $message->useridto, // Using the user who read the message as they are the ones performing the action. 2565 'context' => $context, 2566 'relateduserid' => $message->useridfrom, 2567 'other' => array( 2568 'messageid' => $messageid 2569 ) 2570 )); 2571 $event->trigger(); 2572 2573 return $messagereadid; 2574 } 2575 2576 /** 2577 * Get all message processors, validate corresponding plugin existance and 2578 * system configuration 2579 * 2580 * @param bool $ready only return ready-to-use processors 2581 * @param bool $reset Reset list of message processors (used in unit tests) 2582 * @param bool $resetonly Just reset, then exit 2583 * @return mixed $processors array of objects containing information on message processors 2584 */ 2585 function get_message_processors($ready = false, $reset = false, $resetonly = false) { 2586 global $DB, $CFG; 2587 2588 static $processors; 2589 if ($reset) { 2590 $processors = array(); 2591 2592 if ($resetonly) { 2593 return $processors; 2594 } 2595 } 2596 2597 if (empty($processors)) { 2598 // Get all processors, ensure the name column is the first so it will be the array key 2599 $processors = $DB->get_records('message_processors', null, 'name DESC', 'name, id, enabled'); 2600 foreach ($processors as &$processor){ 2601 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php'; 2602 if (is_readable($processorfile)) { 2603 include_once($processorfile); 2604 $processclass = 'message_output_' . $processor->name; 2605 if (class_exists($processclass)) { 2606 $pclass = new $processclass(); 2607 $processor->object = $pclass; 2608 $processor->configured = 0; 2609 if ($pclass->is_system_configured()) { 2610 $processor->configured = 1; 2611 } 2612 $processor->hassettings = 0; 2613 if (is_readable($CFG->dirroot.'/message/output/'.$processor->name.'/settings.php')) { 2614 $processor->hassettings = 1; 2615 } 2616 $processor->available = 1; 2617 } else { 2618 print_error('errorcallingprocessor', 'message'); 2619 } 2620 } else { 2621 $processor->available = 0; 2622 } 2623 } 2624 } 2625 if ($ready) { 2626 // Filter out enabled and system_configured processors 2627 $readyprocessors = $processors; 2628 foreach ($readyprocessors as $readyprocessor) { 2629 if (!($readyprocessor->enabled && $readyprocessor->configured)) { 2630 unset($readyprocessors[$readyprocessor->name]); 2631 } 2632 } 2633 return $readyprocessors; 2634 } 2635 2636 return $processors; 2637 } 2638 2639 /** 2640 * Get all message providers, validate their plugin existance and 2641 * system configuration 2642 * 2643 * @return mixed $processors array of objects containing information on message processors 2644 */ 2645 function get_message_providers() { 2646 global $CFG, $DB; 2647 2648 $pluginman = core_plugin_manager::instance(); 2649 2650 $providers = $DB->get_records('message_providers', null, 'name'); 2651 2652 // Remove all the providers whose plugins are disabled or don't exist 2653 foreach ($providers as $providerid => $provider) { 2654 $plugin = $pluginman->get_plugin_info($provider->component); 2655 if ($plugin) { 2656 if ($plugin->get_status() === core_plugin_manager::PLUGIN_STATUS_MISSING) { 2657 unset($providers[$providerid]); // Plugins does not exist 2658 continue; 2659 } 2660 if ($plugin->is_enabled() === false) { 2661 unset($providers[$providerid]); // Plugin disabled 2662 continue; 2663 } 2664 } 2665 } 2666 return $providers; 2667 } 2668 2669 /** 2670 * Get an instance of the message_output class for one of the output plugins. 2671 * @param string $type the message output type. E.g. 'email' or 'jabber'. 2672 * @return message_output message_output the requested class. 2673 */ 2674 function get_message_processor($type) { 2675 global $CFG; 2676 2677 // Note, we cannot use the get_message_processors function here, becaues this 2678 // code is called during install after installing each messaging plugin, and 2679 // get_message_processors caches the list of installed plugins. 2680 2681 $processorfile = $CFG->dirroot . "/message/output/{$type}/message_output_{$type}.php"; 2682 if (!is_readable($processorfile)) { 2683 throw new coding_exception('Unknown message processor type ' . $type); 2684 } 2685 2686 include_once($processorfile); 2687 2688 $processclass = 'message_output_' . $type; 2689 if (!class_exists($processclass)) { 2690 throw new coding_exception('Message processor ' . $type . 2691 ' does not define the right class'); 2692 } 2693 2694 return new $processclass(); 2695 } 2696 2697 /** 2698 * Get messaging outputs default (site) preferences 2699 * 2700 * @return object $processors object containing information on message processors 2701 */ 2702 function get_message_output_default_preferences() { 2703 return get_config('message'); 2704 } 2705 2706 /** 2707 * Translate message default settings from binary value to the array of string 2708 * representing the settings to be stored. Also validate the provided value and 2709 * use default if it is malformed. 2710 * 2711 * @param int $plugindefault Default setting suggested by plugin 2712 * @param string $processorname The name of processor 2713 * @return array $settings array of strings in the order: $permitted, $loggedin, $loggedoff. 2714 */ 2715 function translate_message_default_setting($plugindefault, $processorname) { 2716 // Preset translation arrays 2717 $permittedvalues = array( 2718 0x04 => 'disallowed', 2719 0x08 => 'permitted', 2720 0x0c => 'forced', 2721 ); 2722 2723 $loggedinstatusvalues = array( 2724 0x00 => null, // use null if loggedin/loggedoff is not defined 2725 0x01 => 'loggedin', 2726 0x02 => 'loggedoff', 2727 ); 2728 2729 // define the default setting 2730 $processor = get_message_processor($processorname); 2731 $default = $processor->get_default_messaging_settings(); 2732 2733 // Validate the value. It should not exceed the maximum size 2734 if (!is_int($plugindefault) || ($plugindefault > 0x0f)) { 2735 debugging(get_string('errortranslatingdefault', 'message')); 2736 $plugindefault = $default; 2737 } 2738 // Use plugin default setting of 'permitted' is 0 2739 if (!($plugindefault & MESSAGE_PERMITTED_MASK)) { 2740 $plugindefault = $default; 2741 } 2742 2743 $permitted = $permittedvalues[$plugindefault & MESSAGE_PERMITTED_MASK]; 2744 $loggedin = $loggedoff = null; 2745 2746 if (($plugindefault & MESSAGE_PERMITTED_MASK) == MESSAGE_PERMITTED) { 2747 $loggedin = $loggedinstatusvalues[$plugindefault & MESSAGE_DEFAULT_LOGGEDIN]; 2748 $loggedoff = $loggedinstatusvalues[$plugindefault & MESSAGE_DEFAULT_LOGGEDOFF]; 2749 } 2750 2751 return array($permitted, $loggedin, $loggedoff); 2752 } 2753 2754 /** 2755 * Return a list of page types 2756 * @param string $pagetype current page type 2757 * @param stdClass $parentcontext Block's parent context 2758 * @param stdClass $currentcontext Current context of block 2759 */ 2760 function message_page_type_list($pagetype, $parentcontext, $currentcontext) { 2761 return array('messages-*'=>get_string('page-message-x', 'message')); 2762 } 2763 2764 /** 2765 * Get messages sent or/and received by the specified users. 2766 * Please note that this function return deleted messages too. 2767 * 2768 * @param int $useridto the user id who received the message 2769 * @param int $useridfrom the user id who sent the message. -10 or -20 for no-reply or support user 2770 * @param int $notifications 1 for retrieving notifications, 0 for messages, -1 for both 2771 * @param bool $read true for retrieving read messages, false for unread 2772 * @param string $sort the column name to order by including optionally direction 2773 * @param int $limitfrom limit from 2774 * @param int $limitnum limit num 2775 * @return external_description 2776 * @since 2.8 2777 */ 2778 function message_get_messages($useridto, $useridfrom = 0, $notifications = -1, $read = true, 2779 $sort = 'mr.timecreated DESC', $limitfrom = 0, $limitnum = 0) { 2780 global $DB; 2781 2782 $messagetable = $read ? '{message_read}' : '{message}'; 2783 $params = array('deleted' => 0); 2784 2785 // Empty useridto means that we are going to retrieve messages send by the useridfrom to any user. 2786 if (empty($useridto)) { 2787 $userfields = get_all_user_name_fields(true, 'u', '', 'userto'); 2788 $joinsql = "JOIN {user} u ON u.id = mr.useridto"; 2789 $usersql = "mr.useridfrom = :useridfrom AND u.deleted = :deleted"; 2790 $params['useridfrom'] = $useridfrom; 2791 } else { 2792 $userfields = get_all_user_name_fields(true, 'u', '', 'userfrom'); 2793 // Left join because useridfrom may be -10 or -20 (no-reply and support users). 2794 $joinsql = "LEFT JOIN {user} u ON u.id = mr.useridfrom"; 2795 $usersql = "mr.useridto = :useridto AND (u.deleted IS NULL OR u.deleted = :deleted)"; 2796 $params['useridto'] = $useridto; 2797 if (!empty($useridfrom)) { 2798 $usersql .= " AND mr.useridfrom = :useridfrom"; 2799 $params['useridfrom'] = $useridfrom; 2800 } 2801 } 2802 2803 // Now, if retrieve notifications, conversations or both. 2804 $typesql = ""; 2805 if ($notifications !== -1) { 2806 $typesql = "AND mr.notification = :notification"; 2807 $params['notification'] = ($notifications) ? 1 : 0; 2808 } 2809 2810 $sql = "SELECT mr.*, $userfields 2811 FROM $messagetable mr 2812 $joinsql 2813 WHERE $usersql 2814 $typesql 2815 ORDER BY $sort"; 2816 2817 $messages = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum); 2818 return $messages; 2819 } 2820 2821 /** 2822 * Requires the JS libraries to send a message using a dialog. 2823 * 2824 * @return void 2825 */ 2826 function message_messenger_requirejs() { 2827 global $PAGE; 2828 2829 static $done = false; 2830 if ($done) { 2831 return; 2832 } 2833 2834 $PAGE->requires->yui_module( 2835 array('moodle-core_message-messenger'), 2836 'Y.M.core_message.messenger.init', 2837 array(array()) 2838 ); 2839 $PAGE->requires->strings_for_js(array( 2840 'errorwhilesendingmessage', 2841 'messagesent', 2842 'messagetosend', 2843 'sendingmessage', 2844 'sendmessage', 2845 'viewconversation', 2846 ), 'core_message'); 2847 $PAGE->requires->strings_for_js(array( 2848 'userisblockingyou', 2849 'userisblockingyounoncontact' 2850 ), 'message'); 2851 $PAGE->requires->string_for_js('error', 'core'); 2852 2853 $done = true; 2854 } 2855 2856 /** 2857 * Returns the attributes to place on a link to open the 'Send message' dialog. 2858 * 2859 * @param object $user User object. 2860 * @return void 2861 */ 2862 function message_messenger_sendmessage_link_params($user) { 2863 $params = array( 2864 'data-trigger' => 'core_message-messenger::sendmessage', 2865 'data-fullname' => fullname($user), 2866 'data-userid' => $user->id, 2867 'role' => 'button' 2868 ); 2869 2870 if (message_is_user_non_contact_blocked($user)) { 2871 $params['data-blocked-string'] = 'userisblockingyounoncontact'; 2872 } else if (message_is_user_blocked($user)) { 2873 $params['data-blocked-string'] = 'userisblockingyou'; 2874 } 2875 2876 return $params; 2877 } 2878 2879 /** 2880 * Determines if a user is permitted to send another user a private message. 2881 * If no sender is provided then it defaults to the logged in user. 2882 * 2883 * @param object $recipient User object. 2884 * @param object $sender User object. 2885 * @return bool true if user is permitted, false otherwise. 2886 */ 2887 function message_can_post_message($recipient, $sender = null) { 2888 global $USER, $DB; 2889 2890 if (is_null($sender)) { 2891 // The message is from the logged in user, unless otherwise specified. 2892 $sender = $USER; 2893 } 2894 2895 if (!has_capability('moodle/site:sendmessage', context_system::instance(), $sender)) { 2896 return false; 2897 } 2898 2899 // The recipient blocks messages from non-contacts and the 2900 // sender isn't a contact. 2901 if (message_is_user_non_contact_blocked($recipient, $sender)) { 2902 return false; 2903 } 2904 2905 // The recipient has specifically blocked this sender. 2906 if (message_is_user_blocked($recipient, $sender)) { 2907 return false; 2908 } 2909 2910 return true; 2911 } 2912 2913 /** 2914 * Checks if the recipient is allowing messages from users that aren't a 2915 * contact. If not then it checks to make sure the sender is in the 2916 * recipient's contacts. 2917 * 2918 * @param object $recipient User object. 2919 * @param object $sender User object. 2920 * @return bool true if $sender is blocked, false otherwise. 2921 */ 2922 function message_is_user_non_contact_blocked($recipient, $sender = null) { 2923 global $USER, $DB; 2924 2925 if (is_null($sender)) { 2926 // The message is from the logged in user, unless otherwise specified. 2927 $sender = $USER; 2928 } 2929 2930 $blockednoncontacts = get_user_preferences('message_blocknoncontacts', '', $recipient->id); 2931 if (!empty($blockednoncontacts)) { 2932 // Confirm the sender is a contact of the recipient. 2933 $exists = $DB->record_exists('message_contacts', array('userid' => $recipient->id, 'contactid' => $sender->id)); 2934 if ($exists) { 2935 // All good, the recipient is a contact of the sender. 2936 return false; 2937 } else { 2938 // Oh no, the recipient is not a contact. Looks like we can't send the message. 2939 return true; 2940 } 2941 } 2942 2943 return false; 2944 } 2945 2946 /** 2947 * Checks if the recipient has specifically blocked the sending user. 2948 * 2949 * Note: This function will always return false if the sender has the 2950 * readallmessages capability at the system context level. 2951 * 2952 * @param object $recipient User object. 2953 * @param object $sender User object. 2954 * @return bool true if $sender is blocked, false otherwise. 2955 */ 2956 function message_is_user_blocked($recipient, $sender = null) { 2957 global $USER, $DB; 2958 2959 if (is_null($sender)) { 2960 // The message is from the logged in user, unless otherwise specified. 2961 $sender = $USER; 2962 } 2963 2964 $systemcontext = context_system::instance(); 2965 if (has_capability('moodle/site:readallmessages', $systemcontext, $sender)) { 2966 return false; 2967 } 2968 2969 if ($contact = $DB->get_record('message_contacts', array('userid' => $recipient->id, 'contactid' => $sender->id))) { 2970 if ($contact->blocked) { 2971 return true; 2972 } 2973 } 2974 2975 return false; 2976 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |