Merge branch 'MDL-48719-master' of git://github.com/jleyva/moodle

This commit is contained in:
Andrew Nicols 2015-02-04 12:53:34 +08:00
commit c346996f38
4 changed files with 152 additions and 2 deletions

View File

@ -772,6 +772,15 @@ $functions = array(
'capabilities' => '',
),
'core_message_get_blocked_users' => array(
'classname' => 'core_message_external',
'methodname' => 'get_blocked_users',
'classpath' => 'message/externallib.php',
'description' => 'Retrieve a list of users blocked',
'type' => 'read',
'capabilities' => '',
),
// === notes related functions ===
'moodle_notes_create_notes' => array(
@ -974,7 +983,9 @@ $services = array(
'core_message_block_contacts',
'core_message_unblock_contacts',
'core_message_get_contacts',
'core_message_search_contacts'),
'core_message_search_contacts',
'core_message_get_blocked_users'
),
'enabled' => 0,
'restrictedusers' => 0,
'shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE,

View File

@ -828,6 +828,105 @@ class core_message_external extends external_api {
);
}
/**
* Get blocked users parameters description.
*
* @return external_function_parameters
* @since 2.9
*/
public static function get_blocked_users_parameters() {
return new external_function_parameters(
array(
'userid' => new external_value(PARAM_INT,
'the user whose blocked users we want to retrieve',
VALUE_REQUIRED),
)
);
}
/**
* 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;
}
/**
* Get blocked users return description.
*
* @return external_single_structure
* @since 2.9
*/
public static function get_blocked_users_returns() {
return new external_single_structure(
array(
'users' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'User ID'),
'fullname' => new external_value(PARAM_NOTAGS, 'User full name'),
'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL)
)
),
'List of blocked users'
),
'warnings' => new external_warnings()
)
);
}
}
/**

View File

@ -578,4 +578,44 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
}
}
/**
* Test get_blocked_users.
*/
public function test_get_blocked_users() {
$this->resetAfterTest(true);
$user1 = self::getDataGenerator()->create_user();
$userstranger = self::getDataGenerator()->create_user();
$useroffline1 = self::getDataGenerator()->create_user();
$useroffline2 = self::getDataGenerator()->create_user();
$userblocked = self::getDataGenerator()->create_user();
// Login as user1.
$this->setUser($user1);
$this->assertEquals(array(), core_message_external::create_contacts(
array($useroffline1->id, $useroffline2->id)));
// The userstranger sends a couple of messages to user1.
$this->send_message($userstranger, $user1, 'Hello there!');
$this->send_message($userstranger, $user1, 'How you goin?');
// The userblocked sends a message to user1.
// Note that this user is not blocked at this point.
$this->send_message($userblocked, $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 $userblocked and retrieve again the list.
core_message_external::block_contacts(array($userblocked->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']);
}
}

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2015012900.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015012900.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.