/** * 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; }
ob_end_flush(); } break; case 'update': if (time() - $chatlasttime > $CFG->chat_old_ping) { chat_delete_old_users(); } if ($latestmessage = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) { $chatnewlasttime = $latestmessage->timestamp; } else { $chatnewlasttime = 0; } if ($chatlasttime == 0) { $chatlasttime = time() - $CFG->chat_old_ping; } $messages = chat_get_latest_messages($chatuser, $chatlasttime); if (!empty($messages)) { $num = count($messages); } else { $num = 0; } $chatnewrow = ($chatlastrow + $num) % 2; $senduserlist = false; if ($messages && $chatlasttime != $chatnewlasttime) { foreach ($messages as $n => &$message) { $tmp = new stdClass(); // When somebody enter room, user list will be updated. if (!empty($message->system)) { $senduserlist = true; } if ($html = chat_format_message_theme($message, $chatuser, $USER, $cm->groupingid, $theme)) {