/** * Print the user's recent conversations * @param object $user1 the current user * @param bool $showicontext flag indicating whether or not to show text next to the action icons */ function message_print_recent_conversations($user=null, $showicontext=false) { global $USER; echo html_writer::start_tag('p', array('class' => 'heading')); echo get_string('mostrecentconversations', 'message'); echo html_writer::end_tag('p'); if (empty($user)) { $user = $USER; } $conversations = message_get_recent_conversations($user); $showotheruser = true; message_print_recent_messages_table($conversations, $user, $showotheruser, $showicontext); }
/** * Print the user's recent conversations * * @param stdClass $user the current user * @param bool $showicontext flag indicating whether or not to show text next to the action icons */ function message_print_recent_conversations($user1 = null, $showicontext = false, $showactionlinks = true) { global $USER; echo html_writer::start_tag('p', array('class' => 'heading')); echo get_string('mostrecentconversations', 'message'); echo html_writer::end_tag('p'); if (empty($user1)) { $user1 = $USER; } $conversations = message_get_recent_conversations($user1); // Attach context url information to create the "View this conversation" type links foreach($conversations as $conversation) { $conversation->contexturl = new moodle_url("/message/index.php?user1={$user1->id}&user2={$conversation->id}"); $conversation->contexturlname = get_string('thisconversation', 'message'); } $showotheruser = true; message_print_recent_messages_table($conversations, $user1, $showotheruser, $showicontext, false, $showactionlinks); }
/** * Test message_get_recent_conversations with a mixture of messages. * * @dataProvider message_get_recent_conversations_provider * @param array $usersdata The list of users to create for this test. * @param array $messagesdata The list of messages to create. * @param array $expectations The list of expected outcomes. */ public function test_message_get_recent_conversations($usersdata, $messagesdata, $expectations) { global $DB; // Create all of the users. $users = array(); foreach ($usersdata as $username) { $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username)); } $defaulttimecreated = time(); foreach ($messagesdata as $messagedata) { $from = $users[$messagedata['from']]; $to = $users[$messagedata['to']]; $subject = $messagedata['subject']; if (isset($messagedata['state']) && $messagedata['state'] == 'unread') { $table = 'message'; $messageid = $this->send_fake_message($from, $to, $subject, FORMAT_PLAIN); } else { // If there is no state, or the state is not 'unread', assume the message is read. $table = 'message_read'; $messageid = message_post_message($from, $to, $subject, FORMAT_PLAIN); } $updatemessage = new stdClass(); $updatemessage->id = $messageid; if (isset($messagedata['timecreated'])) { $updatemessage->timecreated = $messagedata['timecreated']; } else { if (isset($messagedata['timemodifier'])) { $updatemessage->timecreated = $defaulttimecreated + $messagedata['timemodifier']; } else { $updatemessage->timecreated = $defaulttimecreated; } } $DB->update_record($table, $updatemessage); } foreach ($expectations as $username => $data) { // Get the recent conversations for the specified user. $user = $users[$username]; $conversations = message_get_recent_conversations($user); foreach ($data as $expectation) { $otheruser = $users[$expectation['with']]; $conversation = $conversations[$expectation['messageposition']]; $this->assertEquals($otheruser->id, $conversation->id); $this->assertEquals($expectation['subject'], $conversation->smallmessage); } } }
/** * Test message_get_recent_conversations. */ public function test_message_get_recent_conversations() { global $DB, $USER; // Set this user as the admin. $this->setAdminUser(); // Create user's to send messages to/from. $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1')); $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2')); // Add a few messages that have been read and some that are unread. $m1 = $this->send_fake_message($USER, $user1, 'Message 1'); // An unread message. $m2 = $this->send_fake_message($user1, $USER, 'Message 2'); // An unread message. $m3 = $this->send_fake_message($USER, $user1, 'Message 3'); // An unread message. $m4 = message_post_message($USER, $user2, 'Message 4', FORMAT_PLAIN); $m5 = message_post_message($user2, $USER, 'Message 5', FORMAT_PLAIN); $m6 = message_post_message($USER, $user2, 'Message 6', FORMAT_PLAIN); // We want to alter the timecreated values so we can ensure message_get_recent_conversations orders // by timecreated, not the max id, to begin with. However, we also want more than one message to have // the same timecreated value to ensure that when this happens we retrieve the one with the maximum id. // Store the current time. $time = time(); // Set the first and second unread messages to have the same timecreated value. $updatemessage = new stdClass(); $updatemessage->id = $m1; $updatemessage->timecreated = $time; $DB->update_record('message', $updatemessage); $updatemessage->id = $m2; $updatemessage->timecreated = $time; $DB->update_record('message', $updatemessage); // Set the third unread message to have a timecreated value of 0. $updatemessage->id = $m3; $updatemessage->timecreated = 0; $DB->update_record('message', $updatemessage); // Set the first and second read messages to have the same timecreated value. $updatemessage->id = $m4; $updatemessage->timecreated = $time + 1; $DB->update_record('message', $updatemessage); $updatemessage->id = $m5; $updatemessage->timecreated = $time + 1; $DB->update_record('message', $updatemessage); // Set the third read message to have a timecreated value of 0. $updatemessage->id = $m6; $updatemessage->timecreated = 0; $DB->update_record('message_read', $updatemessage); // Get the recent conversations for the current user. $conversations = message_get_recent_conversations($USER); // Confirm that we have received the messages with the maximum timecreated, rather than the max id. $this->assertEquals('Message 2', $conversations[0]->fullmessage); $this->assertEquals('Message 5', $conversations[1]->smallmessage); }
/** * Returns the contacts and their conversation to display in the contacts area. * * @param int $userid The user id * @param int $limitfrom * @param int $limitnum * @return array */ public static function get_conversations($userid, $limitfrom = 0, $limitnum = 0) { $arrconversations = array(); if ($conversations = message_get_recent_conversations($userid, $limitfrom, $limitnum)) { foreach ($conversations as $conversation) { $arrconversations[$conversation->id] = helper::create_contact($conversation); } } return $arrconversations; }