erLhcoreClassChatWorkflow::autoAssign PHP Method

autoAssign() public static method

public static autoAssign ( &$chat, $department )
    public static function autoAssign(&$chat, $department)
    {
        if ($department->active_balancing == 1 && ($chat->user_id == 0 || $department->max_timeout_seconds > 0 && $chat->tslasign < time() - $department->max_timeout_seconds)) {
            $isOnlineUser = (int) erLhcoreClassModelChatConfig::fetch('sync_sound_settings')->data['online_timeout'];
            $appendSQL = '';
            if ($department->max_active_chats > 0) {
                $appendSQL = ' AND active_chats < :max_active_chats';
            }
            $sql = "SELECT user_id FROM lh_userdep WHERE hide_online = 0 AND dep_id = :dep_id AND last_activity > :last_activity AND user_id != :user_id {$appendSQL} ORDER BY last_accepted ASC LIMIT 1";
            $db = ezcDbInstance::get();
            $stmt = $db->prepare($sql);
            $stmt->bindValue(':dep_id', $department->id, PDO::PARAM_INT);
            $stmt->bindValue(':last_activity', time() - $isOnlineUser, PDO::PARAM_INT);
            $stmt->bindValue(':user_id', $chat->user_id, PDO::PARAM_INT);
            if ($department->max_active_chats > 0) {
                $stmt->bindValue(':max_active_chats', $department->max_active_chats, PDO::PARAM_INT);
            }
            $stmt->execute();
            $user_id = $stmt->fetchColumn();
            if ($user_id > 0) {
                erLhcoreClassChat::updateActiveChats($chat->user_id);
                $chat->tslasign = time();
                $chat->user_id = $user_id;
                $chat->updateThis();
                erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.data_changed_auto_assign', array('chat' => &$chat));
                $stmt = $db->prepare('UPDATE lh_userdep SET last_accepted = :last_accepted WHERE user_id = :user_id');
                $stmt->bindValue(':last_accepted', time(), PDO::PARAM_INT);
                $stmt->bindValue(':user_id', $user_id, PDO::PARAM_INT);
                $stmt->execute();
            }
        }
    }

Usage Example

             $baseURL = isset($Params['user_parameters_unordered']['mode']) && $Params['user_parameters_unordered']['mode'] == 'widget' ? erLhcoreClassDesign::baseurl('chat/chatwidget') : erLhcoreClassDesign::baseurl('chat/startchat');
             $ru = $baseURL . '/(department)/' . $department->id . '/(offline)/true/(leaveamessage)/true/(chatprefill)/' . $chat->id . '_' . $chat->hash;
             $msg = new erLhcoreClassModelmsg();
             $msg->msg = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/checkchatstatus', 'Visitor has been redirected to contact form');
             $msg->chat_id = $chat->id;
             $msg->user_id = -1;
             $msg->time = time();
             erLhcoreClassChat::getSession()->save($msg);
             // We do not store last msg time for chat here, because in any case none of opeators has opened it
             $contactRedirected = true;
             if ($chat->status_sub != erLhcoreClassModelChat::STATUS_SUB_CONTACT_FORM) {
                 $chat->status_sub = erLhcoreClassModelChat::STATUS_SUB_CONTACT_FORM;
                 $chat->updateThis();
             }
         } else {
             erLhcoreClassChatWorkflow::autoAssign($chat, $department);
         }
     }
 }
 if (erLhcoreClassChat::isOnline($chat->dep_id, false, array('online_timeout' => (int) erLhcoreClassModelChatConfig::fetch('sync_sound_settings')->data['online_timeout']))) {
     $tpl->set('is_online', true);
 } else {
     $tpl->set('is_online', false);
 }
 if ($chat->chat_initiator == erLhcoreClassModelChat::CHAT_INITIATOR_PROACTIVE) {
     $tpl->set('is_proactive_based', true);
 } else {
     $tpl->set('is_proactive_based', false);
 }
 if ($chat->status == erLhcoreClassModelChat::STATUS_ACTIVE_CHAT) {
     $activated = 'true';