/** * Get messagearea contacts parameters. * * @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_contacts($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_contacts_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.'); } $contacts = \core_message\api::get_contacts($userid, $limitfrom, $limitnum); $contacts = new \core_message\output\messagearea\contacts(null, $contacts); $renderer = $PAGE->get_renderer('core_message'); return $contacts->export_for_template($renderer); }
/** * Tests retrieving contacts. */ public function test_get_contacts() { // Create some users. $user1 = self::getDataGenerator()->create_user(); // Set as the user. $this->setUser($user1); $user2 = new stdClass(); $user2->firstname = 'User'; $user2->lastname = 'A'; $user2 = self::getDataGenerator()->create_user($user2); $user3 = new stdClass(); $user3->firstname = 'User'; $user3->lastname = 'B'; $user3 = self::getDataGenerator()->create_user($user3); $user4 = new stdClass(); $user4->firstname = 'User'; $user4->lastname = 'C'; $user4 = self::getDataGenerator()->create_user($user4); $user5 = new stdClass(); $user5->firstname = 'User'; $user5->lastname = 'D'; $user5 = self::getDataGenerator()->create_user($user5); // Add some users as contacts. message_add_contact($user2->id, 0, $user1->id); message_add_contact($user3->id, 0, $user1->id); message_add_contact($user4->id, 0, $user1->id); // Retrieve the contacts. $contacts = \core_message\api::get_contacts($user1->id); // Confirm the data is correct. $this->assertEquals(3, count($contacts)); $contact1 = $contacts[0]; $contact2 = $contacts[1]; $contact3 = $contacts[2]; $this->assertEquals($user2->id, $contact1->userid); $this->assertEmpty($contact1->useridfrom); $this->assertFalse($contact1->ismessaging); $this->assertNull($contact1->lastmessage); $this->assertNull($contact1->messageid); $this->assertFalse($contact1->isonline); $this->assertFalse($contact1->isread); $this->assertFalse($contact1->isblocked); $this->assertNull($contact1->unreadcount); $this->assertEquals($user3->id, $contact2->userid); $this->assertEmpty($contact2->useridfrom); $this->assertFalse($contact2->ismessaging); $this->assertNull($contact2->lastmessage); $this->assertNull($contact2->messageid); $this->assertFalse($contact2->isonline); $this->assertFalse($contact2->isread); $this->assertFalse($contact2->isblocked); $this->assertNull($contact2->unreadcount); $this->assertEquals($user4->id, $contact3->userid); $this->assertEmpty($contact3->useridfrom); $this->assertFalse($contact3->ismessaging); $this->assertNull($contact3->lastmessage); $this->assertNull($contact3->messageid); $this->assertFalse($contact3->isonline); $this->assertFalse($contact3->isread); $this->assertFalse($contact3->isblocked); $this->assertNull($contact3->unreadcount); }
$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; if ($contactsfirst) { $conversations = \core_message\api::get_contacts($user1->id, 0, 20); } else { $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