diff --git a/lib/db/services.php b/lib/db/services.php index a0e7357209d..0219c58ccc6 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1147,6 +1147,7 @@ $services = array( 'mod_chat_login_user', 'mod_chat_get_chat_users', 'mod_chat_send_chat_message', + 'mod_chat_get_chat_latest_messages', ), 'enabled' => 0, 'restrictedusers' => 0, diff --git a/mod/chat/chat_ajax.php b/mod/chat/chat_ajax.php index 076371a7a52..02bd5d493ea 100644 --- a/mod/chat/chat_ajax.php +++ b/mod/chat/chat_ajax.php @@ -109,13 +109,7 @@ switch ($action) { $chatlasttime = time() - $CFG->chat_old_ping; } - $params = array('groupid' => $chatuser->groupid, 'chatid' => $chatuser->chatid, 'lasttime' => $chatlasttime); - - $groupselect = $chatuser->groupid ? " AND (groupid=".$chatuser->groupid." OR groupid=0) " : ""; - - $messages = $DB->get_records_select('chat_messages_current', - 'chatid = :chatid AND timestamp > :lasttime '.$groupselect, $params, - 'timestamp ASC'); + $messages = chat_get_latest_messages($chatuser, $chatlasttime); if (!empty($messages)) { $num = count($messages); diff --git a/mod/chat/classes/external.php b/mod/chat/classes/external.php index dc627d7a5db..7cc74822dca 100644 --- a/mod/chat/classes/external.php +++ b/mod/chat/classes/external.php @@ -312,4 +312,121 @@ 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_latest_messages_parameters() { + return new external_function_parameters( + array( + 'chatsid' => new external_value(PARAM_ALPHANUM, 'chat session id (obtained via mod_chat_login_user)'), + 'chatlasttime' => new external_value(PARAM_INT, 'last time messages were retrieved (epoch time)', VALUE_DEFAULT, 0) + ) + ); + } + + /** + * Get the latest messages from the given chat session. + * + * @param int $chatsid the chat session id + * @param int $chatlasttime last time messages were retrieved (epoch time) + * @return array of warnings and the new message id (0 if the message was empty) + * @since Moodle 3.0 + * @throws moodle_exception + */ + public static function get_chat_latest_messages($chatsid, $chatlasttime = 0) { + global $DB, $CFG; + + $params = self::validate_parameters(self::get_chat_latest_messages_parameters(), + array( + 'chatsid' => $chatsid, + 'chatlasttime' => $chatlasttime + )); + $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); + + $chatlasttime = $params['chatlasttime']; + if ((time() - $chatlasttime) > $CFG->chat_old_ping) { + chat_delete_old_users(); + } + + // Set default chat last time (to not retrieve all the conversations). + if ($chatlasttime == 0) { + $chatlasttime = time() - $CFG->chat_old_ping; + } + + if ($latestmessage = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) { + $chatnewlasttime = $latestmessage->timestamp; + } else { + $chatnewlasttime = 0; + } + + $messages = chat_get_latest_messages($chatuser, $chatlasttime); + $returnedmessages = array(); + + foreach ($messages as $message) { + + // FORMAT_MOODLE is mandatory in the chat plugin. + list($messageformatted, $format) = external_format_text($message->message, FORMAT_MOODLE, $context->id, 'mod_chat', + '', 0); + + $returnedmessages[] = array( + 'id' => $message->id, + 'userid' => $message->userid, + 'system' => (bool) $message->system, + 'message' => $messageformatted, + 'timestamp' => $message->timestamp, + ); + } + + // Update our status since we are active in the chat. + $DB->set_field('chat_users', 'lastping', time(), array('id' => $chatuser->id)); + + $result = array(); + $result['messages'] = $returnedmessages; + $result['chatnewlasttime'] = $chatnewlasttime; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 3.0 + */ + public static function get_chat_latest_messages_returns() { + return new external_single_structure( + array( + 'messages' => new external_multiple_structure( + new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'message id'), + 'userid' => new external_value(PARAM_INT, 'user id'), + 'system' => new external_value(PARAM_BOOL, 'true if is a system message (like user joined)'), + 'message' => new external_value(PARAM_RAW, 'message text'), + 'timestamp' => new external_value(PARAM_INT, 'timestamp for the message'), + ) + ), + 'list of users' + ), + 'chatnewlasttime' => new external_value(PARAM_INT, 'new last time'), + 'warnings' => new external_warnings() + ) + ); + } + } diff --git a/mod/chat/db/services.php b/mod/chat/db/services.php index a295a37becb..c38367e80db 100644 --- a/mod/chat/db/services.php +++ b/mod/chat/db/services.php @@ -52,4 +52,12 @@ $functions = array( 'capabilities' => 'mod/chat:chat' ), + 'mod_chat_get_chat_latest_messages' => array( + 'classname' => 'mod_chat_external', + 'methodname' => 'get_chat_latest_messages', + 'description' => 'Get the latest messages from the given chat session.', + 'type' => 'read', + 'capabilities' => 'mod/chat:chat' + ), + ); diff --git a/mod/chat/lib.php b/mod/chat/lib.php index c575fa8e7a5..4230e4789c0 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -1304,3 +1304,22 @@ function chat_page_type_list($pagetype, $parentcontext, $currentcontext) { $modulepagetype = array('mod-chat-*' => get_string('page-mod-chat-x', 'chat')); return $modulepagetype; } + +/** + * Return a list of the latest messages in the given chat session. + * + * @param stdClass $chatuser chat user session data + * @param int $chatlasttime last time messages were retrieved + * @return array list of messages + * @since Moodle 3.0 + */ +function chat_get_latest_messages($chatuser, $chatlasttime) { + global $DB; + + $params = array('groupid' => $chatuser->groupid, 'chatid' => $chatuser->chatid, 'lasttime' => $chatlasttime); + + $groupselect = $chatuser->groupid ? " AND (groupid=" . $chatuser->groupid . " OR groupid=0) " : ""; + + return $DB->get_records_select('chat_messages_current', 'chatid = :chatid AND timestamp > :lasttime ' . $groupselect, + $params, 'timestamp ASC'); +}