Beispiel #1
0
 /**
  * Create contacts.
  *
  * @param array $userids array of user IDs.
  * @return external_description
  * @since Moodle 2.5
  */
 public static function create_contacts($userids)
 {
     global $CFG;
     // Check if messaging is enabled.
     if (!$CFG->messaging) {
         throw new moodle_exception('disabled', 'message');
     }
     $params = array('userids' => $userids);
     $params = self::validate_parameters(self::create_contacts_parameters(), $params);
     $warnings = array();
     foreach ($params['userids'] as $id) {
         if (!message_add_contact($id)) {
             $warnings[] = array('item' => 'user', 'itemid' => $id, 'warningcode' => 'contactnotcreated', 'message' => 'The contact could not be created');
         }
     }
     return $warnings;
 }
    require_login($courseid);
    require_capability('moodle/course:viewparticipants', context_course::instance($courseid));
    $PAGE->set_pagelayout('incourse');
} else {
    $PAGE->set_pagelayout('standard');
    $PAGE->set_context(context_user::instance($user1->id));
}
if (!empty($user1->id) && $user1->id != $USER->id) {
    $PAGE->navigation->extend_for_user($user1);
}
if (!empty($user2->id) && $user2realuser && $user2->id != $USER->id) {
    $PAGE->navigation->extend_for_user($user2);
}
/// Process any contact maintenance requests there may be
if ($addcontact and confirm_sesskey()) {
    message_add_contact($addcontact);
    redirect($CFG->wwwroot . '/message/index.php?viewing=contacts&id=' . $addcontact);
}
if ($removecontact and confirm_sesskey()) {
    message_remove_contact($removecontact);
}
if ($blockcontact and confirm_sesskey()) {
    message_block_contact($blockcontact);
}
if ($unblockcontact and confirm_sesskey()) {
    message_unblock_contact($unblockcontact);
}
//was a message sent? Do NOT allow someone looking at someone else's messages to send them.
$messageerror = null;
if ($currentuser && !empty($user2) && has_capability('moodle/site:sendmessage', $systemcontext)) {
    // Check that the user is not blocking us!!
Beispiel #3
0
/**
 * Block a user.
 *
 * @param int $contactid the user ID of the user to block
 * @param int $userid the user ID of the user we want to unblock the contact for, defaults to current user
 *  if not specified.
 * @return bool
 */
function message_block_contact($contactid, $userid = 0) {
    return message_add_contact($contactid, 1, $userid);
}
Beispiel #4
0
/**
 * block a user
 * @param int $contactid the user ID of the user to block
 */
function message_block_contact($contactid) {
    return message_add_contact($contactid, 1);
}
Beispiel #5
0
    /**
     * Create contacts.
     *
     * @param array $userids array of user IDs.
     * @return external_description
     * @since 2.5
     */
    public static function create_contacts($userids) {
        $params = array('userids' => $userids);
        $params = self::validate_parameters(self::create_contacts_parameters(), $params);

        $warnings = array();
        foreach ($params['userids'] as $id) {
            if (!message_add_contact($id)) {
                $warnings[] = array(
                    'item' => 'user',
                    'itemid' => $id,
                    'warningcode' => 'contactnotcreated',
                    'message' => 'The contact could not be created'
                );
            }
        }
        return $warnings;
    }
 /**
  * Test message_search_users.
  */
 public function test_message_search_users()
 {
     // Set this user as the admin.
     $this->setAdminUser();
     // Create a user to add to the admin's contact list.
     $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
     $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
     // Add users to the admin's contact list.
     message_add_contact($user1->id);
     message_add_contact($user2->id);
     // Add blocked contact.
     $this->assertCount(1, message_search_users(0, 'Test1'));
     $this->assertCount(2, message_search_users(0, 'Test'));
     $this->assertCount(1, message_search_users(0, 'user1'));
     $this->assertCount(2, message_search_users(0, 'user'));
 }
Beispiel #7
0
 /**
  * Test the message contact unblocked event.
  */
 public function test_message_contact_unblocked()
 {
     // Set this user as the admin.
     $this->setAdminUser();
     // Create a user to add to the admin's contact list.
     $user = $this->getDataGenerator()->create_user();
     // Add the user to the admin's contact list.
     message_add_contact($user->id);
     // Block the user.
     message_block_contact($user->id);
     // Make sure that we have 1 blocked user.
     $this->assertEquals(1, message_count_blocked_users());
     // Trigger and capture the event when unblocking a contact.
     $sink = $this->redirectEvents();
     message_unblock_contact($user->id);
     $events = $sink->get_events();
     $event = reset($events);
     // Check that the event data is valid.
     $this->assertInstanceOf('\\core\\event\\message_contact_unblocked', $event);
     $this->assertEquals(context_user::instance(2), $event->get_context());
     $expected = array(SITEID, 'message', 'unblock contact', 'index.php?user1=' . $user->id . '&user2=2', $user->id);
     $this->assertEventLegacyLogData($expected, $event);
     $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
     $this->assertEquals($url, $event->get_url());
     // Make sure that we have no blocked users.
     $this->assertEmpty(message_count_blocked_users());
     // Make sure that the contact unblocked event is not triggered again.
     $sink->clear();
     message_unblock_contact($user->id);
     $events = $sink->get_events();
     $event = reset($events);
     $this->assertEmpty($event);
     // Make sure that we still have no blocked users.
     $this->assertEmpty(message_count_blocked_users());
 }
 /**
  * Test that message_is_user_non_contact_blocked returns false if the recipient doesn't
  * allow messages from non-contacts but the sender is a contact.
  */
 public function test_message_is_user_non_contact_blocked_false_with_if_contact()
 {
     $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
     $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
     $this->setUser($recipient);
     set_user_preference('message_blocknoncontacts', true, $recipient);
     message_add_contact($sender->id);
     $this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender));
 }
Beispiel #9
0
 /**
  * Tests retrieving contacts as another user.
  */
 public function test_messagearea_contacts_as_other_user()
 {
     $this->resetAfterTest(true);
     $this->setAdminUser();
     // Create some users.
     $user1 = self::getDataGenerator()->create_user();
     $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.
     $result = core_message_external::data_for_messagearea_contacts($user1->id);
     // We need to execute the return values cleaning process to simulate the web service server.
     $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(), $result);
     // Confirm the data is correct.
     $contacts = $result['contacts'];
     $this->assertCount(3, $contacts);
     $contact1 = $contacts[0];
     $contact2 = $contacts[1];
     $contact3 = $contacts[2];
     $this->assertEquals($user2->id, $contact1['userid']);
     $this->assertFalse($contact1['ismessaging']);
     $this->assertFalse($contact1['sentfromcurrentuser']);
     $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->assertFalse($contact2['ismessaging']);
     $this->assertFalse($contact2['sentfromcurrentuser']);
     $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->assertFalse($contact3['ismessaging']);
     $this->assertFalse($contact3['sentfromcurrentuser']);
     $this->assertNull($contact3['lastmessage']);
     $this->assertNull($contact3['messageid']);
     $this->assertFalse($contact3['isonline']);
     $this->assertFalse($contact3['isread']);
     $this->assertFalse($contact3['isblocked']);
     $this->assertNull($contact3['unreadcount']);
 }
Beispiel #10
0
 /**
  * Tests that when blocking messages from non-contacts is enabled that
  * non-contacts trying to send a message return false.
  */
 public function test_is_user_non_contact_blocked()
 {
     // Create some users.
     $user1 = self::getDataGenerator()->create_user();
     $user2 = self::getDataGenerator()->create_user();
     // Set as the first user.
     $this->setUser($user1);
     // User hasn't sent their preference to block non-contacts, so should return false.
     $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
     // Set the second user's preference to not receive messages from non-contacts.
     set_user_preference('message_blocknoncontacts', 1, $user2->id);
     // Check that the return result is now true.
     $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
     // Add the first user as a contact for the second user.
     message_add_contact($user1->id, 0, $user2->id);
     // Check that the return result is now false.
     $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
 }
Beispiel #11
0
 /**
  * Create contacts.
  *
  * @param array $userids array of user IDs.
  * @param int $userid The id of the user we are creating the contacts for
  * @return external_description
  * @since Moodle 2.5
  */
 public static function create_contacts($userids, $userid = 0)
 {
     global $CFG, $USER;
     // Check if messaging is enabled.
     if (empty($CFG->messaging)) {
         throw new moodle_exception('disabled', 'message');
     }
     if (empty($userid)) {
         $userid = $USER->id;
     }
     // Validate context.
     $context = context_system::instance();
     self::validate_context($context);
     $capability = 'moodle/site:manageallmessaging';
     if ($USER->id != $userid && !has_capability($capability, $context)) {
         throw new required_capability_exception($context, $capability, 'nopermissions', '');
     }
     $params = array('userids' => $userids, 'userid' => $userid);
     $params = self::validate_parameters(self::create_contacts_parameters(), $params);
     $warnings = array();
     foreach ($params['userids'] as $id) {
         if (!message_add_contact($id, 0, $userid)) {
             $warnings[] = array('item' => 'user', 'itemid' => $id, 'warningcode' => 'contactnotcreated', 'message' => 'The contact could not be created');
         }
     }
     return $warnings;
 }