Last active
August 29, 2015 14:13
-
-
Save jleyva/0ffd88f5e25bad319d0c to your computer and use it in GitHub Desktop.
core_message_get_blocked_users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// message/externallib.php | |
/** | |
* Retrieve a list of users blocked | |
* | |
* @param int $userid the user whose blocked users we want to retrieve | |
* @return external_description | |
* @since 2.9 | |
*/ | |
public static function get_blocked_users($userid) { | |
global $CFG, $USER; | |
require_once($CFG->dirroot . "/message/lib.php"); | |
// Warnings array, it can be empty at the end but is mandatory. | |
$warnings = array(); | |
// Validate params. | |
$params = array( | |
'userid' => $userid | |
); | |
$params = self::validate_parameters(self::get_blocked_users_parameters(), $params); | |
$userid = $params['userid']; | |
// Validate context. | |
$context = context_system::instance(); | |
self::validate_context($context); | |
// Check if private messaging between users is allowed. | |
if (empty($CFG->messaging)) { | |
throw new moodle_exception('disabled', 'message'); | |
} | |
$user = core_user::get_user($userid, 'id', MUST_EXIST); | |
// Check if we have permissions for retrieve the information. | |
if ($userid != $USER->id and !has_capability('moodle/site:readallmessages', $context)) { | |
throw new moodle_exception('accessdenied', 'admin'); | |
} | |
// Now, we can get safely all the blocked users. | |
$users = message_get_blocked_users($user); | |
$blockedusers = array(); | |
foreach ($users as $user) { | |
$newuser = array( | |
'id' => $user->id, | |
'fullname' => fullname($user), | |
); | |
$newuser['profileimageurl'] = moodle_url::make_webservice_pluginfile_url( | |
context_user::instance($user->id)->id, 'user', 'icon', null, '/', 'f1')->out(false); | |
$blockedusers[] = $newuser; | |
} | |
$results = array( | |
'users' => $blockedusers, | |
'warnings' => $warnings | |
); | |
return $results; | |
} | |
// Webservices | |
// message/test/externallib_text.php | |
/** | |
* Test get_blocked_users. | |
*/ | |
public function test_get_blocked_users() { | |
$this->resetAfterTest(true); | |
$user1 = self::getDataGenerator()->create_user(); | |
$user_stranger = self::getDataGenerator()->create_user(); | |
$user_offline1 = self::getDataGenerator()->create_user(); | |
$user_offline2 = self::getDataGenerator()->create_user(); | |
$user_blocked = self::getDataGenerator()->create_user(); | |
// Login as user1. | |
$this->setUser($user1); | |
$this->assertEquals(array(), core_message_external::create_contacts( | |
array($user_offline1->id, $user_offline2->id))); | |
// User_stranger sends a couple of messages to user1. | |
$this->send_message($user_stranger, $user1, 'Hello there!'); | |
$this->send_message($user_stranger, $user1, 'How you goin?'); | |
// User_blocked sends a message to user1. | |
// Note that this user is not blocked at this point. | |
$this->send_message($user_blocked, $user1, 'Here, have some spam.'); | |
// Retrieve the list of blocked users. | |
$this->setUser($user1); | |
$blockedusers = core_message_external::get_blocked_users($user1->id); | |
$blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); | |
$this->assertCount(0, $blockedusers['users']); | |
// Block the $user_blocked and retrieve again the list. | |
core_message_external::block_contacts(array($user_blocked->id)); | |
$blockedusers = core_message_external::get_blocked_users($user1->id); | |
$blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); | |
$this->assertCount(1, $blockedusers['users']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment