Example #1
0
 /**
  * Block contacts.
  *
  * @param array $userids array of user IDs.
  * @return external_description
  * @since Moodle 2.5
  */
 public static function block_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::block_contacts_parameters(), $params);
     $warnings = array();
     foreach ($params['userids'] as $id) {
         if (!message_block_contact($id)) {
             $warnings[] = array('item' => 'user', 'itemid' => $id, 'warningcode' => 'contactnotblocked', 'message' => 'The contact could not be blocked');
         }
     }
     return $warnings;
 }
Example #2
0
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!!
    if ($contact = $DB->get_record('message_contacts', array('userid' => $user2->id, 'contactid' => $user1->id))) {
        if ($contact->blocked and !has_capability('moodle/site:readallmessages', $systemcontext)) {
            $messageerror = get_string('userisblockingyou', 'message');
        }
    }
    $userpreferences = get_user_preferences(NULL, NULL, $user2->id);
    if (!empty($userpreferences['message_blocknoncontacts'])) {
Example #3
0
    /**
     * Block contacts.
     *
     * @param array $userids array of user IDs.
     * @return external_description
     * @since 2.5
     */
    public static function block_contacts($userids) {
        $params = array('userids' => $userids);
        $params = self::validate_parameters(self::block_contacts_parameters(), $params);

        $warnings = array();
        foreach ($params['userids'] as $id) {
            if (!message_block_contact($id)) {
                $warnings[] = array(
                    'item' => 'user',
                    'itemid' => $id,
                    'warningcode' => 'contactnotblocked',
                    'message' => 'The contact could not be blocked'
                );
            }
        }
        return $warnings;
    }
 /**
  * Test message_block_contact.
  */
 public function test_message_block_contact()
 {
     // Set this user as the admin.
     $this->setAdminUser();
     // Create a user to add to the admin's contact list.
     $user1 = $this->getDataGenerator()->create_user();
     $user2 = $this->getDataGenerator()->create_user();
     // Add users to the admin's contact list.
     message_add_contact($user1->id);
     message_add_contact($user2->id);
     $this->assertEquals(0, message_count_blocked_users());
     // Block 1 user.
     message_block_contact($user2->id);
     $this->assertEquals(1, message_count_blocked_users());
 }
Example #5
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());
 }
Example #6
0
 /**
  * Test that message_is_user_blocked returns true if the sender is a contact that is
  * blocked by the recipient and does not have the moodle/site:readallmessages capability.
  */
 public function test_message_is_user_blocked_true_if_blocked()
 {
     $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
     $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
     $this->setUser($recipient);
     message_block_contact($sender->id);
     $context = context_system::instance();
     $roleid = $this->getDataGenerator()->create_role();
     $this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
     assign_capability('moodle/site:readallmessages', CAP_PROHIBIT, $roleid, $context);
     $this->assertTrue(message_is_user_blocked($recipient, $sender));
 }
Example #7
0
 /**
  * Tests searching users in a course.
  */
 public function test_messagearea_search_users_in_course()
 {
     $this->resetAfterTest(true);
     // Create some users.
     $user1 = new stdClass();
     $user1->firstname = 'User';
     $user1->lastname = 'One';
     $user1 = self::getDataGenerator()->create_user($user1);
     // The person doing the search.
     $this->setUser($user1);
     // Set the second user's status to online by setting their last access to now.
     $user2 = new stdClass();
     $user2->firstname = 'User';
     $user2->lastname = 'Two';
     $user2->lastaccess = time();
     $user2 = self::getDataGenerator()->create_user($user2);
     // Block the second user.
     message_block_contact($user2->id, $user1->id);
     $user3 = new stdClass();
     $user3->firstname = 'User';
     $user3->lastname = 'Three';
     $user3 = self::getDataGenerator()->create_user($user3);
     // Create a course.
     $course1 = new stdClass();
     $course1->fullname = 'Course';
     $course1->shortname = 'One';
     $course1 = $this->getDataGenerator()->create_course();
     // Enrol the user we are doing the search for and one user in the course.
     $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
     $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
     // Perform a search.
     $result = core_message_external::data_for_messagearea_search_users_in_course($user1->id, $course1->id, 'User');
     // We need to execute the return values cleaning process to simulate the web service.
     $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_in_course_returns(), $result);
     // Check that we only retrieved a user that was enrolled, and that the user performing the search was not returned.
     $users = $result['contacts'];
     $this->assertCount(1, $users);
     $user = $users[0];
     $this->assertEquals($user2->id, $user['userid']);
     $this->assertEquals(fullname($user2), $user['fullname']);
     $this->assertFalse($user['ismessaging']);
     $this->assertFalse($user['sentfromcurrentuser']);
     $this->assertNull($user['lastmessage']);
     $this->assertNull($user['messageid']);
     $this->assertTrue($user['isonline']);
     $this->assertFalse($user['isread']);
     $this->assertTrue($user['isblocked']);
     $this->assertNull($user['unreadcount']);
 }
Example #8
0
 /**
  * Tests that the admin is not blocked even if someone has chosen to block them.
  */
 public function test_is_user_blocked_as_admin()
 {
     // Create a user.
     $user1 = self::getDataGenerator()->create_user();
     // Set the user.
     $this->setUser($user1);
     // Block the admin user.
     message_block_contact(2);
     // Now change to the admin user.
     $this->setAdminUser();
     // As the admin you should still be able to send messages to the user.
     $this->assertFalse(\core_message\api::is_user_blocked($user1));
 }
Example #9
0
 /**
  * Block contacts.
  *
  * @param array $userids array of user IDs.
  * @param int $userid The id of the user we are blocking the contacts for
  * @return external_description
  * @since Moodle 2.5
  */
 public static function block_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::block_contacts_parameters(), $params);
     $warnings = array();
     foreach ($params['userids'] as $id) {
         if (!message_block_contact($id, $userid)) {
             $warnings[] = array('item' => 'user', 'itemid' => $id, 'warningcode' => 'contactnotblocked', 'message' => 'The contact could not be blocked');
         }
     }
     return $warnings;
 }