/** * Test message_count_messages. */ public function test_message_count_messages() { global $DB; // Create users to send and receive message. $userfrom = $this->getDataGenerator()->create_user(); $userto = $this->getDataGenerator()->create_user(); message_post_message($userfrom, $userto, 'Message 1', FORMAT_PLAIN); message_post_message($userfrom, $userto, 'Message 2', FORMAT_PLAIN); message_post_message($userto, $userfrom, 'Message 3', FORMAT_PLAIN); // Return 0 when no message. $messages = array(); $this->assertEquals(0, message_count_messages($messages, 'Test', 'Test')); // Check number of messages from userfrom and userto. $messages = $this->messagesink->get_messages(); $this->assertEquals(2, message_count_messages($messages, 'useridfrom', $userfrom->id)); $this->assertEquals(1, message_count_messages($messages, 'useridfrom', $userto->id)); }
function message_print_contacts() { global $USER, $CFG; $timetoshowusers = 300; //Seconds default if (isset($CFG->block_online_users_timetosee)) { $timetoshowusers = $CFG->block_online_users_timetosee * 60; } $timefrom = time() - $timetoshowusers; /// get lists of contacts and unread messages $onlinecontacts = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture, mc.blocked\n FROM {$CFG->prefix}user u, {$CFG->prefix}message_contacts mc\n WHERE mc.userid='{$USER->id}' AND u.id=mc.contactid AND u.lastaccess>={$timefrom}\n AND mc.blocked='0'\n ORDER BY u.firstname ASC"); $offlinecontacts = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture, mc.blocked\n FROM {$CFG->prefix}user u, {$CFG->prefix}message_contacts mc\n WHERE mc.userid='{$USER->id}' AND u.id=mc.contactid AND u.lastaccess<{$timefrom}\n AND mc.blocked='0'\n ORDER BY u.firstname ASC"); $unreadmessages = get_records_sql("SELECT m.id, m.useridfrom, u.firstname, u.lastname, u.picture\n FROM {$CFG->prefix}user u, {$CFG->prefix}message m\n WHERE m.useridto='{$USER->id}' AND u.id=m.useridfrom"); $blockedcontacts = get_records_select('message_contacts', "userid='{$USER->id}' AND blocked='1'", '', 'contactid, id'); $countonlinecontacts = is_array($onlinecontacts) ? count($onlinecontacts) : 0; $countofflinecontacts = is_array($offlinecontacts) ? count($offlinecontacts) : 0; /// Cycle through messages and extract those that are from unknown contacts /// We can take advantage of the keys for $onlinecontacts and $offlinecontacts /// which are set to the userid and therefore we just need to see if the key /// exists in either of those arrays /// We can also discard any messages from users in our blocked contact list $unknownmessages = array(); if (!empty($unreadmessages)) { /// make sure we have valid arrays to test against - they may be boolean false if (empty($onlinecontacts)) { $onlinecontacts = array(); } if (empty($offlinecontacts)) { $offlinecontacts = array(); } if (empty($blockedcontacts)) { $blockedcontacts = array(); } foreach ($unreadmessages as $unreadmessage) { if (array_key_exists($unreadmessage->useridfrom, $onlinecontacts) or array_key_exists($unreadmessage->useridfrom, $offlinecontacts) or array_key_exists($unreadmessage->useridfrom, $blockedcontacts)) { continue; } if (!isset($unknownmessages[$unreadmessage->useridfrom])) { $message = $unreadmessage; $message->count = 1; $unknownmessages[$unreadmessage->useridfrom] = $message; } else { $unknownmessages[$unreadmessage->useridfrom]->count++; } } } if ($countonlinecontacts + $countofflinecontacts == 0) { echo '<div class="heading">'; print_string('contactlistempty', 'message'); echo '</div>'; echo '<div class="note">'; print_string('addsomecontacts', 'message', $CFG->wwwroot . '/message/index.php?tab=search'); echo '</div>'; } if (!empty($onlinecontacts) || !empty($offlinecontacts) || !empty($unknownmessages)) { echo '<table id="message_contacts" align="center" cellspacing="2" cellpadding="0" border="0">'; if (!empty($onlinecontacts)) { /// print out list of online contacts echo '<tr><td colspan="3" class="heading">'; echo get_string('onlinecontacts', 'message', $countonlinecontacts); echo '</td></tr>'; if (!empty($onlinecontacts)) { foreach ($onlinecontacts as $contact) { if ($contact->blocked == 1) { continue; } $fullname = fullname($contact); $fullnamelink = $fullname; /// are there any unread messages for this contact? if (($unread = message_count_messages($unreadmessages, 'useridfrom', $contact->id)) > 0) { $fullnamelink = '<strong>' . $fullnamelink . ' (' . $unread . ')</strong>'; } /// link to remove from contact list $strcontact = message_contact_link($contact->id, 'remove', true); $strhistory = message_history_link($contact->id, 0, true, '', '', 'icon'); echo '<tr><td class="pix">'; print_user_picture($contact->id, SITEID, $contact->picture, 20, false, true, 'userwindow'); echo '</td>'; echo '<td class="contact">'; link_to_popup_window("/message/discussion.php?id={$contact->id}", "message_{$contact->id}", $fullnamelink, 500, 500, get_string('sendmessageto', 'message', $fullname), 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500'); echo '</td>'; echo '<td class="link">' . $strcontact . ' ' . $strhistory . '</td>'; echo '</tr>'; } } echo '<tr><td colspan="3"> </td></tr>'; } if (!empty($offlinecontacts)) { /// print out list of offline contacts echo '<tr><td colspan="3" class="heading">'; echo get_string('offlinecontacts', 'message', $countofflinecontacts); echo '</td></tr>'; foreach ($offlinecontacts as $contact) { if ($contact->blocked == 1) { continue; } $fullname = fullname($contact); $fullnamelink = $fullname; /// are there any unread messages for this contact? if (($unread = message_count_messages($unreadmessages, 'useridfrom', $contact->id)) > 0) { $fullnamelink = '<strong>' . $fullnamelink . ' (' . $unread . ')</strong>'; } /// link to remove from contact list $strcontact = message_contact_link($contact->id, 'remove', true); $strhistory = message_history_link($contact->id, 0, true, '', '', 'icon'); echo '<tr><td class="pix">'; print_user_picture($contact->id, SITEID, $contact->picture, 20, false, true, 'userwindow'); echo '</td>'; echo '<td class="contact">'; link_to_popup_window("/message/discussion.php?id={$contact->id}", "message_{$contact->id}", $fullnamelink, 500, 500, get_string('sendmessageto', 'message', $fullname), 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500'); echo '</td>'; echo '<td class="link">' . $strcontact . ' ' . $strhistory . '</td>'; echo '</tr>'; } echo '<tr><td colspan="3"> </td></tr>'; } /// print out list of incoming contacts if (!empty($unknownmessages)) { echo '<tr><td colspan="3" class="heading">'; echo get_string('incomingcontacts', 'message', count($unknownmessages)); echo '</td></tr>'; foreach ($unknownmessages as $messageuser) { $fullname = fullname($messageuser); $fullnamelink = $fullname; if ($messageuser->count) { $fullnamelink = '<strong>' . $fullnamelink . ' (' . $messageuser->count . ')</strong>'; } /// link to add to contact list $strcontact = message_contact_link($messageuser->useridfrom, 'add', true); $strblock = message_contact_link($messageuser->useridfrom, 'block', true); $strhistory = message_history_link($messageuser->useridfrom, 0, true, '', '', 'icon'); echo '<tr><td class="pix">'; print_user_picture($messageuser->useridfrom, SITEID, $messageuser->picture, 20, false, true, 'userwindow'); echo '</td>'; echo '<td class="contact">'; link_to_popup_window("/message/discussion.php?id={$messageuser->useridfrom}", "message_{$messageuser->useridfrom}", $fullnamelink, 500, 500, get_string('sendmessageto', 'message', $fullname), 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500'); echo '</td>'; echo '<td class="link"> ' . $strcontact . ' ' . $strblock . ' ' . $strhistory . '</td>'; echo '</tr>'; } } echo '</table>'; if (!empty($unknownmessages) && $countonlinecontacts + $countofflinecontacts == 0) { // Extra help echo '<div class="note">('; print_string('addsomecontactsincoming', 'message'); echo ')</div>'; } } echo '<br />'; $autorefresh = '<p align="center" class="note">' . get_string('pagerefreshes', 'message', $CFG->message_contacts_refresh) . '</p>'; $autorefresh = addslashes_js($autorefresh); // js escaping // gracefully degrade JS autorefresh echo '<script type="text/javascript"> //<![CDATA[ document.write("' . $autorefresh . '") //]]> </script>'; echo '<noscript><div align="center">'; echo print_single_button('index.php', false, get_string('refresh')); echo '</div></noscript>'; }