Example #1
0
/**
 * Posts a message sent by a user into a session.
 * @param int $session_id - the session id.
 * @param int $user_id - the current user unique identifier.
 * @param string $message_text - the message text.
 * @return int $userto_id - the other user id to send back the message, 
 * or 0 if the message is empty, or if session doesn't exists.
 */
function block_gchat_post_message($session_id, $user_id, $message_text)
{
    global $DB;
    if (strlen($message_text) > 0) {
        //Get session
        $session = block_gchat_get_session($session_id, $user_id);
        if ($session) {
            //Create Message
            $record = new stdClass();
            $record->text = $message_text;
            $record->timecreated = $_SERVER['REQUEST_TIME'];
            $record->sessionid = $session_id;
            $record->userid = $user_id;
            //Store message
            $DB->insert_record(CHAT_MESSAGES_TABLE, $record);
            //Open session to other user
            $userto_id = $session->userfrom == $user_id ? $session->userto : $session->userfrom;
            $user_session = block_gchat_open_session($session_id, $userto_id);
            //Set unseen messages for other user
            block_gchat_set_session_seen_status($session_id, $userto_id, 1);
            return $userto_id;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
 /**
  * Marks a session as seen. Equivalent to marking all messages as seen.
  * @param object $params - the data received from the client.
  */
 private function seen_session($params)
 {
     if (isset($params->session_id) && isset($params->user_id)) {
         block_gchat_set_session_seen_status($params->session_id, $params->user_id, 0);
     }
 }