/** * Get messagearea conversations. * * @param int $userid The id of the user who we are viewing conversations for * @param int $limitfrom * @param int $limitnum * @return stdClass * @throws moodle_exception * @since 3.2 */ public static function data_for_messagearea_conversations($userid, $limitfrom = 0, $limitnum = 0) { global $CFG, $PAGE, $USER; // Check if messaging is enabled. if (empty($CFG->messaging)) { throw new moodle_exception('disabled', 'message'); } $systemcontext = context_system::instance(); $params = array('userid' => $userid, 'limitfrom' => $limitfrom, 'limitnum' => $limitnum); self::validate_parameters(self::data_for_messagearea_conversations_parameters(), $params); self::validate_context($systemcontext); if ($USER->id != $userid && !has_capability('moodle/site:readallmessages', $systemcontext)) { throw new moodle_exception('You do not have permission to perform this action.'); } $conversations = \core_message\api::get_conversations($userid, $limitfrom, $limitnum); $conversations = new \core_message\output\messagearea\contacts(null, $conversations); $renderer = $PAGE->get_renderer('core_message'); return $conversations->export_for_template($renderer); }
/** * Tests retrieving conversations. */ public function test_get_conversations() { // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); $user4 = self::getDataGenerator()->create_user(); // The person doing the search. $this->setUser($user1); // Send some messages back and forth, have some different conversations with different users. $time = 1; $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4); $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9); $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10); $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11); // Retrieve the conversations. $conversations = \core_message\api::get_conversations($user1->id); // Confirm the data is correct. $this->assertEquals(3, count($conversations)); $message1 = array_shift($conversations); $message2 = array_shift($conversations); $message3 = array_shift($conversations); $this->assertEquals($user4->id, $message1->userid); $this->assertEquals($user1->id, $message1->useridfrom); $this->assertTrue($message1->ismessaging); $this->assertEquals('Dope.', $message1->lastmessage); $this->assertNull($message1->messageid); $this->assertFalse($message1->isonline); $this->assertTrue($message1->isread); $this->assertFalse($message1->isblocked); $this->assertEquals(0, $message1->unreadcount); $this->assertEquals($user3->id, $message2->userid); $this->assertEquals($user3->id, $message2->useridfrom); $this->assertTrue($message2->ismessaging); $this->assertEquals('Cool.', $message2->lastmessage); $this->assertNull($message2->messageid); $this->assertFalse($message2->isonline); $this->assertFalse($message2->isread); $this->assertFalse($message2->isblocked); $this->assertEquals(2, $message2->unreadcount); $this->assertEquals($user2->id, $message3->userid); $this->assertEquals($user2->id, $message3->useridfrom); $this->assertTrue($message3->ismessaging); $this->assertEquals('Word.', $message3->lastmessage); $this->assertNull($message3->messageid); $this->assertFalse($message3->isonline); $this->assertFalse($message3->isread); $this->assertFalse($message3->isblocked); $this->assertEquals(2, $message3->unreadcount); }
$user2fullname = fullname($user2); $PAGE->set_title("{$strmessages}: {$user2fullname}"); $PAGE->set_heading("{$strmessages}: {$user2fullname}"); } else { $PAGE->set_title("{$SITE->shortname}: {$strmessages}"); $PAGE->set_heading("{$SITE->shortname}: {$strmessages}"); } // Remove the user node from the main navigation for this page. $usernode = $PAGE->navigation->find('users', null); $usernode->remove(); $settings = $PAGE->settingsnav->find('messages', null); $settings->make_active(); // Get the renderer and the information we are going to be use. $renderer = $PAGE->get_renderer('core_message'); $requestedconversation = false; $conversations = \core_message\api::get_conversations($user1->id, 0, 20); $messages = []; if (!$user2realuser) { // If there are conversations, but the user has not chosen a particular one, then render the most recent one. $user2 = new stdClass(); $user2->id = null; if (!empty($conversations)) { $contact = reset($conversations); $user2->id = $contact->userid; } } else { // The user has specifically requested to see a conversation. Add the flag to // the context so that we can render the messaging app appropriately - this is // used for smaller screens as it allows the UI to be responsive. $requestedconversation = true; }