From e4076a6e4cba50958df9c656e9f87efc6156fa88 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 16 Jul 2015 11:58:07 +0200 Subject: [PATCH] MDL-50853 mod_chat: New Web Service mod_chat_get_chat_users --- lib/db/services.php | 1 + mod/chat/classes/external.php | 95 +++++++++++++++++++++++++++++++++++ mod/chat/db/services.php | 8 +++ 3 files changed, 104 insertions(+) diff --git a/lib/db/services.php b/lib/db/services.php index 9ccdb84c2b4..bae4942872b 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1145,6 +1145,7 @@ $services = array( 'mod_resource_view_resource', 'mod_folder_view_folder', 'mod_chat_login_user', + 'mod_chat_get_chat_users', ), 'enabled' => 0, 'restrictedusers' => 0, diff --git a/mod/chat/classes/external.php b/mod/chat/classes/external.php index 588d5eb25ec..fc44003a759 100644 --- a/mod/chat/classes/external.php +++ b/mod/chat/classes/external.php @@ -130,4 +130,99 @@ class mod_chat_external extends external_api { ); } + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 3.0 + */ + public static function get_chat_users_parameters() { + return new external_function_parameters( + array( + 'chatsid' => new external_value(PARAM_ALPHANUM, 'chat session id (obtained via mod_chat_login_user)') + ) + ); + } + + /** + * Get the list of users in the given chat session. + * + * @param int $chatsid the chat session id + * @return array of warnings and the user lists + * @since Moodle 3.0 + * @throws moodle_exception + */ + public static function get_chat_users($chatsid) { + global $DB; + + $params = self::validate_parameters(self::get_chat_users_parameters(), + array( + 'chatsid' => $chatsid + )); + $warnings = array(); + + // Request and permission validation. + if (!$chatuser = $DB->get_record('chat_users', array('sid' => $params['chatsid']))) { + throw new moodle_exception('notlogged', 'chat'); + } + $chat = $DB->get_record('chat', array('id' => $chatuser->chatid), '*', MUST_EXIST); + list($course, $cm) = get_course_and_cm_from_instance($chat, 'chat'); + + $context = context_module::instance($cm->id); + self::validate_context($context); + + require_capability('mod/chat:chat', $context); + + // First, delete old users from the chats. + chat_delete_old_users(); + + $users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid); + $returnedusers = array(); + + foreach ($users as $user) { + $usercontext = context_user::instance($user->id, IGNORE_MISSING); + $profileimageurl = ''; + + if ($usercontext) { + $profileimageurl = moodle_url::make_webservice_pluginfile_url( + $usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false); + } + + $returnedusers[] = array( + 'id' => $user->id, + 'fullname' => fullname($user), + 'profileimageurl' => $profileimageurl + ); + } + + $result = array(); + $result['users'] = $returnedusers; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 3.0 + */ + public static function get_chat_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'), + ) + ), + 'list of users' + ), + 'warnings' => new external_warnings() + ) + ); + } + } diff --git a/mod/chat/db/services.php b/mod/chat/db/services.php index 48dcba4c2ce..1a4562b75ff 100644 --- a/mod/chat/db/services.php +++ b/mod/chat/db/services.php @@ -36,4 +36,12 @@ $functions = array( 'capabilities' => 'mod/chat:chat' ), + 'mod_chat_get_chat_users' => array( + 'classname' => 'mod_chat_external', + 'methodname' => 'get_chat_users', + 'description' => 'Get the list of users in the given chat session.', + 'type' => 'read', + 'capabilities' => 'mod/chat:chat' + ), + );