MDL-30070 message: Web Services for contact list

This commit is contained in:
Frederic Massart 2012-12-13 16:25:36 +08:00
parent a03330bd16
commit d6731600c8
6 changed files with 744 additions and 4 deletions

View File

@ -430,6 +430,7 @@ $string['prefixcannotbeempty'] = '<p>Error: database table prefix cannot be empt
$string['prefixtoolong'] = '<p>Error: database table prefix is too long ({$a->dbfamily})</p>
<p>The site administrator must fix this problem. Maximum length for table prefixes in {$a->dbfamily} is {$a->maxlength} characters.</p>';
$string['processingstops'] = 'Processing stops here. Remaining records ignored.';
$string['querystringcannotbeempty'] = 'The query string cannot be empty.';
$string['redirecterrordetected'] = 'Unsupported redirect detected, script execution terminated';
$string['refoundto'] = 'Can be refunded to {$a}';
$string['refoundtoorigi'] = 'Refunded to original amount: {$a}';

View File

@ -581,6 +581,60 @@ $functions = array(
'capabilities'=> 'moodle/site:sendmessage',
),
'core_message_create_contacts' => array(
'classname' => 'core_message_external',
'methodname' => 'create_contacts',
'classpath' => 'message/externallib.php',
'description' => 'Add contacts to the contact list',
'type' => 'write',
'capabilities'=> '',
),
'core_message_delete_contacts' => array(
'classname' => 'core_message_external',
'methodname' => 'delete_contacts',
'classpath' => 'message/externallib.php',
'description' => 'Remove contacts from the contact list',
'type' => 'write',
'capabilities'=> '',
),
'core_message_block_contacts' => array(
'classname' => 'core_message_external',
'methodname' => 'block_contacts',
'classpath' => 'message/externallib.php',
'description' => 'Block contacts',
'type' => 'write',
'capabilities'=> '',
),
'core_message_unblock_contacts' => array(
'classname' => 'core_message_external',
'methodname' => 'unblock_contacts',
'classpath' => 'message/externallib.php',
'description' => 'Unblock contacts',
'type' => 'write',
'capabilities'=> '',
),
'core_message_get_contacts' => array(
'classname' => 'core_message_external',
'methodname' => 'get_contacts',
'classpath' => 'message/externallib.php',
'description' => 'Retrieve the contact list',
'type' => 'read',
'capabilities'=> '',
),
'core_message_search_contacts' => array(
'classname' => 'core_message_external',
'methodname' => 'search_contacts',
'classpath' => 'message/externallib.php',
'description' => 'Search for contacts',
'type' => 'read',
'capabilities'=> '',
),
// === notes related functions ===
'moodle_notes_create_notes' => array(

View File

@ -185,6 +185,389 @@ class core_message_external extends external_api {
);
}
/**
* Create contacts parameters description.
*
* @return external_function_parameters
* @since 2.5
*/
public static function create_contacts_parameters() {
return new external_function_parameters(
array(
'userids' => new external_multiple_structure(
new external_value(PARAM_INT, 'User ID'),
'List of user IDs'
)
)
);
}
/**
* 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;
}
/**
* Create contacts return description.
*
* @return external_description
* @since 2.5
*/
public static function create_contacts_returns() {
return new external_warnings();
}
/**
* Delete contacts parameters description.
*
* @return external_function_parameters
* @since 2.5
*/
public static function delete_contacts_parameters() {
return new external_function_parameters(
array(
'userids' => new external_multiple_structure(
new external_value(PARAM_INT, 'User ID'),
'List of user IDs'
)
)
);
}
/**
* Delete contacts.
*
* @param array $userids array of user IDs.
* @return null
* @since 2.5
*/
public static function delete_contacts($userids) {
$params = array('userids' => $userids);
$params = self::validate_parameters(self::delete_contacts_parameters(), $params);
foreach ($params['userids'] as $id) {
message_remove_contact($id);
}
return null;
}
/**
* Delete contacts return description.
*
* @return external_description
* @since 2.5
*/
public static function delete_contacts_returns() {
return null;
}
/**
* Block contacts parameters description.
*
* @return external_function_parameters
* @since 2.5
*/
public static function block_contacts_parameters() {
return new external_function_parameters(
array(
'userids' => new external_multiple_structure(
new external_value(PARAM_INT, 'User ID'),
'List of user IDs'
)
)
);
}
/**
* 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;
}
/**
* Block contacts return description.
*
* @return external_description
* @since 2.5
*/
public static function block_contacts_returns() {
return new external_warnings();
}
/**
* Unblock contacts parameters description.
*
* @return external_function_parameters
* @since 2.5
*/
public static function unblock_contacts_parameters() {
return new external_function_parameters(
array(
'userids' => new external_multiple_structure(
new external_value(PARAM_INT, 'User ID'),
'List of user IDs'
)
)
);
}
/**
* Unblock contacts.
*
* @param array $userids array of user IDs.
* @return null
* @since 2.5
*/
public static function unblock_contacts($userids) {
$params = array('userids' => $userids);
$params = self::validate_parameters(self::unblock_contacts_parameters(), $params);
foreach ($params['userids'] as $id) {
message_unblock_contact($id);
}
return null;
}
/**
* Unblock contacts return description.
*
* @return external_description
* @since 2.5
*/
public static function unblock_contacts_returns() {
return null;
}
/**
* Get contacts parameters description.
*
* @return external_function_parameters
* @since 2.5
*/
public static function get_contacts_parameters() {
return new external_function_parameters(array());
}
/**
* Get contacts.
*
* @param array $userids array of user IDs.
* @return external_description
* @since 2.5
*/
public static function get_contacts() {
global $CFG;
require_once($CFG->dirroot . '/user/lib.php');
list($online, $offline, $strangers) = message_get_contacts();
$allcontacts = array('online' => $online, 'offline' => $offline, 'strangers' => $strangers);
foreach ($allcontacts as $mode => $contacts) {
foreach ($contacts as $key => $contact) {
$newcontact = array(
'id' => $contact->id,
'fullname' => fullname($contact),
'unread' => $contact->messagecount
);
// Try to get the user picture, but sometimes this method can return null.
$userdetails = user_get_user_details($contact, null, array('profileimageurl', 'profileimageurlsmall'));
if (!empty($userdetails)) {
$newcontact['profileimageurl'] = $userdetails['profileimageurl'];
$newcontact['profileimageurlsmall'] = $userdetails['profileimageurlsmall'];
}
$allcontacts[$mode][$key] = $newcontact;
}
}
return $allcontacts;
}
/**
* Get contacts return description.
*
* @return external_description
* @since 2.5
*/
public static function get_contacts_returns() {
return new external_single_structure(
array(
'online' => 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),
'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL),
'unread' => new external_value(PARAM_INT, 'Unread message count')
)
),
'List of online contacts'
),
'offline' => 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),
'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL),
'unread' => new external_value(PARAM_INT, 'Unread message count')
)
),
'List of offline contacts'
),
'strangers' => 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),
'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL),
'unread' => new external_value(PARAM_INT, 'Unread message count')
)
),
'List of users that are not in the user\'s contact list but have sent a message'
)
)
);
}
/**
* Search contacts parameters description.
*
* @return external_function_parameters
* @since 2.5
*/
public static function search_contacts_parameters() {
return new external_function_parameters(
array(
'searchtext' => new external_value(PARAM_CLEAN, 'String the user\'s fullname has to match to be found'),
'onlymycourses' => new external_value(PARAM_BOOL, 'Limit search to the user\'s courses',
VALUE_DEFAULT, false)
)
);
}
/**
* Search contacts.
*
* @param string $searchtext query string.
* @param bool $onlymycourses limit the search to the user's courses only.
* @return external_description
* @since 2.5
*/
public static function search_contacts($searchtext, $onlymycourses = false) {
global $CFG, $USER;
require_once($CFG->libdir . '/enrollib.php');
$params = array('searchtext' => $searchtext, 'onlymycourses' => $onlymycourses);
$params = self::validate_parameters(self::search_contacts_parameters(), $params);
// Extra validation, we do not allow empty queries.
if ($params['searchtext'] === '') {
throw new moodle_exception('querystringcannotbeempty');
}
$courseids = array();
if ($params['onlymycourses']) {
$mycourses = enrol_get_my_courses(array('id'));
foreach ($mycourses as $mycourse) {
$courseids[] = $mycourse->id;
}
} else {
$courseids[] = SITEID;
}
// Retrieving the users matching the query.
$users = message_search_users($courseids, $params['searchtext']);
$results = array();
foreach ($users as $user) {
$results[$user->id] = $user;
}
// Reorganising information.
foreach ($results as &$user) {
$newuser = array(
'id' => $user->id,
'fullname' => fullname($user)
);
// Avoid undefined property notice as phone not specified.
$user->phone1 = null;
$user->phone2 = null;
// Try to get the user picture, but sometimes this method can return null.
$userdetails = user_get_user_details($user, null, array('profileimageurl', 'profileimageurlsmall'));
if (!empty($userdetails)) {
$newuser['profileimageurl'] = $userdetails['profileimageurl'];
$newuser['profileimageurlsmall'] = $userdetails['profileimageurlsmall'];
}
$user = $newuser;
}
return $results;
}
/**
* Search contacts return description.
*
* @return external_description
* @since 2.5
*/
public static function search_contacts_returns() {
return 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),
'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL)
)
),
'List of contacts'
);
}
}
/**

View File

@ -32,6 +32,28 @@ require_once($CFG->dirroot . '/message/externallib.php');
class core_message_external_testcase extends externallib_advanced_testcase {
/**
* Send a fake message.
*
* {@link message_send()} does not support transaction, this function will simulate a message
* sent from a user to another. We should stop using it once {@link message_send()} will support
* transactions. This is not clean at all, this is just used to add rows to the table.
*
* @param stdClass $userfrom user object of the one sending the message.
* @param stdClass $userto user object of the one receiving the message.
* @param string $message message to send.
*/
protected function send_message($userfrom, $userto, $message = 'Hello world!') {
global $DB;
$record = new stdClass();
$record->useridfrom = $userfrom->id;
$record->useridto = $userto->id;
$record->subject = 'No subject';
$record->fullmessage = $message;
$record->timecreated = time();
$insert = $DB->insert_record('message', $record);
}
/**
* Test send_instant_messages
*/
@ -72,4 +94,284 @@ class core_message_external_testcase extends externallib_advanced_testcase {
$this->assertEquals($themessage->smallmessage, $message1['text']);
$this->assertEquals($sentmessages[0]['clientmsgid'], $message1['clientmsgid']);
}
}
/**
* Test create_contacts.
*/
public function test_create_contacts() {
$this->resetAfterTest(true);
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
$user5 = self::getDataGenerator()->create_user();
$this->setUser($user1);
// Adding a contact.
$return = core_message_external::create_contacts(array($user2->id));
$return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
$this->assertEquals(array(), $return);
// Adding a contact who is already a contact.
$return = core_message_external::create_contacts(array($user2->id));
$return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
$this->assertEquals(array(), $return);
// Adding multiple contacts.
$return = core_message_external::create_contacts(array($user3->id, $user4->id));
$return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
$this->assertEquals(array(), $return);
// Adding a non-existing user.
$return = core_message_external::create_contacts(array(99999));
$return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
$this->assertCount(1, $return);
$return = array_pop($return);
$this->assertEquals($return['warningcode'], 'contactnotcreated');
$this->assertEquals($return['itemid'], 99999);
// Adding contacts with valid and invalid parameters.
$return = core_message_external::create_contacts(array($user5->id, 99999));
$return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
$this->assertCount(1, $return);
$return = array_pop($return);
$this->assertEquals($return['warningcode'], 'contactnotcreated');
$this->assertEquals($return['itemid'], 99999);
}
/**
* Test delete_contacts.
*/
public function test_delete_contacts() {
$this->resetAfterTest(true);
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
$user5 = self::getDataGenerator()->create_user();
$user6 = self::getDataGenerator()->create_user();
$this->setUser($user1);
$this->assertEquals(array(), core_message_external::create_contacts(
array($user3->id, $user4->id, $user5->id, $user6->id)));
// Removing a non-contact.
$return = core_message_external::delete_contacts(array($user2->id));
$this->assertNull($return);
// Removing one contact.
$return = core_message_external::delete_contacts(array($user3->id));
$this->assertNull($return);
// Removing multiple contacts.
$return = core_message_external::delete_contacts(array($user4->id, $user5->id));
$this->assertNull($return);
// Removing contact from unexisting user.
$return = core_message_external::delete_contacts(array(99999));
$this->assertNull($return);
// Removing mixed valid and invalid data.
$return = core_message_external::delete_contacts(array($user6->id, 99999));
$this->assertNull($return);
}
/**
* Test block_contacts.
*/
public function test_block_contacts() {
$this->resetAfterTest(true);
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
$user5 = self::getDataGenerator()->create_user();
$this->setUser($user1);
$this->assertEquals(array(), core_message_external::create_contacts(array($user3->id, $user4->id, $user5->id)));
// Blocking a contact.
$return = core_message_external::block_contacts(array($user2->id));
$return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
$this->assertEquals(array(), $return);
// Blocking a contact who is already a contact.
$return = core_message_external::block_contacts(array($user2->id));
$return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
$this->assertEquals(array(), $return);
// Blocking multiple contacts.
$return = core_message_external::block_contacts(array($user3->id, $user4->id));
$return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
$this->assertEquals(array(), $return);
// Blocking a non-existing user.
$return = core_message_external::block_contacts(array(99999));
$return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
$this->assertCount(1, $return);
$return = array_pop($return);
$this->assertEquals($return['warningcode'], 'contactnotblocked');
$this->assertEquals($return['itemid'], 99999);
// Blocking contacts with valid and invalid parameters.
$return = core_message_external::block_contacts(array($user5->id, 99999));
$return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
$this->assertCount(1, $return);
$return = array_pop($return);
$this->assertEquals($return['warningcode'], 'contactnotblocked');
$this->assertEquals($return['itemid'], 99999);
}
/**
* Test unblock_contacts.
*/
public function test_unblock_contacts() {
$this->resetAfterTest(true);
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
$user5 = self::getDataGenerator()->create_user();
$user6 = self::getDataGenerator()->create_user();
$this->setUser($user1);
$this->assertEquals(array(), core_message_external::create_contacts(
array($user3->id, $user4->id, $user5->id, $user6->id)));
// Removing a non-contact.
$return = core_message_external::unblock_contacts(array($user2->id));
$this->assertNull($return);
// Removing one contact.
$return = core_message_external::unblock_contacts(array($user3->id));
$this->assertNull($return);
// Removing multiple contacts.
$return = core_message_external::unblock_contacts(array($user4->id, $user5->id));
$this->assertNull($return);
// Removing contact from unexisting user.
$return = core_message_external::unblock_contacts(array(99999));
$this->assertNull($return);
// Removing mixed valid and invalid data.
$return = core_message_external::unblock_contacts(array($user6->id, 99999));
$this->assertNull($return);
}
/**
* Test get_contacts.
*/
public function test_get_contacts() {
$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_offline3 = self::getDataGenerator()->create_user();
$user_online = new stdClass();
$user_online->lastaccess = time();
$user_online = self::getDataGenerator()->create_user($user_online);
$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_offline3->id, $user_online->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?');
$this->send_message($user_stranger, $user1, 'Cya!');
// User_blocked sends a message to user1.
$this->send_message($user_blocked, $user1, 'Here, have some spam.');
// Retrieve the contacts of the user.
$this->setUser($user1);
$contacts = core_message_external::get_contacts();
$contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
$this->assertCount(3, $contacts['offline']);
$this->assertCount(1, $contacts['online']);
$this->assertCount(2, $contacts['strangers']);
core_message_external::block_contacts(array($user_blocked->id));
$contacts = core_message_external::get_contacts();
$contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
$this->assertCount(3, $contacts['offline']);
$this->assertCount(1, $contacts['online']);
$this->assertCount(1, $contacts['strangers']);
// Checking some of the fields returned.
$stranger = array_pop($contacts['strangers']);
$this->assertEquals($user_stranger->id, $stranger['id']);
$this->assertEquals(3, $stranger['unread']);
}
/**
* Test search_contacts.
*/
public function test_search_contacts() {
global $DB;
$this->resetAfterTest(true);
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$user1 = new stdClass();
$user1->firstname = 'X';
$user1->lastname = 'X';
$user1 = $this->getDataGenerator()->create_user($user1);
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
$this->getDataGenerator()->enrol_user($user1->id, $course2->id);
$user2 = new stdClass();
$user2->firstname = 'Eric';
$user2->lastname = 'Cartman';
$user2 = self::getDataGenerator()->create_user($user2);
$user3 = new stdClass();
$user3->firstname = 'Stan';
$user3->lastname = 'Marsh';
$user3 = self::getDataGenerator()->create_user($user3);
self::getDataGenerator()->enrol_user($user3->id, $course1->id);
$user4 = new stdClass();
$user4->firstname = 'Kyle';
$user4->lastname = 'Broflovski';
$user4 = self::getDataGenerator()->create_user($user4);
$user5 = new stdClass();
$user5->firstname = 'Kenny';
$user5->lastname = 'McCormick';
$user5 = self::getDataGenerator()->create_user($user5);
self::getDataGenerator()->enrol_user($user5->id, $course2->id);
// Searching for users, keep in mind that 'Admin User' and 'Guest user' can be returned for now.
// See MDL-37164 which should fix that. Once fixed, remove the +2's.
$this->setUser($user1);
$results = core_message_external::search_contacts('r');
$results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
$this->assertCount(4 + 2, $results);
$results = core_message_external::search_contacts('r', true);
$results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
$this->assertCount(2, $results);
$results = core_message_external::search_contacts('Kyle', false);
$results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
$this->assertCount(1, $results);
$result = reset($results);
$this->assertEquals($user4->id, $result['id']);
$results = core_message_external::search_contacts('y', false);
$results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
$this->assertCount(2, $results);
$results = core_message_external::search_contacts('y', true);
$results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
$this->assertCount(1, $results);
$result = reset($results);
$this->assertEquals($user5->id, $result['id']);
// Empty query, will throw an exception.
$this->setExpectedException('moodle_exception');
$results = core_message_external::search_contacts('');
}
}

View File

@ -195,7 +195,7 @@ function user_get_default_fields() {
* @param stdClass $context context object
* @param stdClass $course moodle course
* @param array $userfields required fields
* @return array
* @return array|null
*/
function user_get_user_details($user, $course = null, array $userfields = array()) {
global $USER, $DB, $CFG;
@ -545,4 +545,4 @@ function can_view_user_details_cap($user, $course = null) {
*/
function user_page_type_list($pagetype, $parentcontext, $currentcontext) {
return array('user-profile'=>get_string('page-user-profile', 'pagetype'));
}
}

View File

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