erLhcoreClassChatWorkflow::automaticChatClosing PHP Method

automaticChatClosing() public static method

public static automaticChatClosing ( )
    public static function automaticChatClosing()
    {
        $closedChatsNumber = 0;
        $timeout = (int) erLhcoreClassModelChatConfig::fetch('autoclose_timeout')->current_value;
        if ($timeout > 0) {
            // Close normal chats
            $delay = time() - $timeout * 60;
            foreach (erLhcoreClassChat::getList(array('limit' => 500, 'filtergt' => array('last_user_msg_time' => 0), 'filterlt' => array('last_user_msg_time' => $delay), 'filter' => array('status' => erLhcoreClassModelChat::STATUS_ACTIVE_CHAT))) as $chat) {
                $chat->status = erLhcoreClassModelChat::STATUS_CLOSED_CHAT;
                $msg = new erLhcoreClassModelmsg();
                $msg->msg = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/syncuser', 'Chat was automatically closed by cron');
                $msg->chat_id = $chat->id;
                $msg->user_id = -1;
                $chat->last_user_msg_time = $msg->time = time();
                erLhcoreClassChat::getSession()->save($msg);
                if ($chat->last_msg_id < $msg->id) {
                    $chat->last_msg_id = $msg->id;
                }
                $chat->chat_duration = erLhcoreClassChat::getChatDurationToUpdateChatID($chat->id);
                $chat->updateThis();
                erLhcoreClassChat::closeChatCallback($chat, $chat->user);
                erLhcoreClassChat::updateActiveChats($chat->user_id);
                $closedChatsNumber++;
            }
            // Close pending chats where the only message is user initial message
            foreach (erLhcoreClassChat::getList(array('limit' => 500, 'filterlt' => array('time' => $delay), 'filterin' => array('status' => array(erLhcoreClassModelChat::STATUS_PENDING_CHAT, erLhcoreClassModelChat::STATUS_ACTIVE_CHAT)), 'filter' => array('last_user_msg_time' => 0))) as $chat) {
                $chat->status = erLhcoreClassModelChat::STATUS_CLOSED_CHAT;
                $msg = new erLhcoreClassModelmsg();
                $msg->msg = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/syncuser', 'Chat was automatically closed by cron');
                $msg->chat_id = $chat->id;
                $msg->user_id = -1;
                $chat->last_user_msg_time = $msg->time = time();
                erLhcoreClassChat::getSession()->save($msg);
                if ($chat->last_msg_id < $msg->id) {
                    $chat->last_msg_id = $msg->id;
                }
                $chat->updateThis();
                erLhcoreClassChat::closeChatCallback($chat, $chat->user);
                erLhcoreClassChat::updateActiveChats($chat->user_id);
                $closedChatsNumber++;
            }
        }
        return $closedChatsNumber;
    }

Usage Example

Example #1
0
 *
 * */
echo "Starting chat/workflow\n";
if (erLhcoreClassModelChatConfig::fetch('run_departments_workflow')->current_value == 1) {
    echo "Starting departments workflow\n";
    $ts = time();
    foreach (erLhcoreClassChat::getList(array('limit' => 500, 'customfilter' => array('transfer_timeout_ts < (' . $ts . '-transfer_timeout_ac)'), 'filter' => array('status' => erLhcoreClassModelChat::STATUS_PENDING_CHAT, 'transfer_if_na' => 1))) as $chat) {
        $canExecuteWorkflow = true;
        if (erLhcoreClassModelChatConfig::fetch('pro_active_limitation')->current_value >= 0) {
            if ($chat->department !== false && $chat->department->department_transfer_id > 0) {
                $canExecuteWorkflow = erLhcoreClassChat::getPendingChatsCountPublic($chat->department->department_transfer_id) <= erLhcoreClassModelChatConfig::fetch('pro_active_limitation')->current_value;
            }
        }
        if ($canExecuteWorkflow == true) {
            erLhcoreClassChatWorkflow::transferWorkflow($chat);
            echo "executing department transfer workflow for - ", $chat->id, "\n";
        } else {
            echo "Skipping transfer because dedicated department queue is full\n";
        }
    }
    echo "Ended departments workflow\n";
}
// Unanswered chats callback
echo erLhcoreClassChatWorkflow::mainUnansweredChatWorkflow();
echo "Closed chats - ", erLhcoreClassChatWorkflow::automaticChatClosing(), "\n";
echo "Purged chats - ", erLhcoreClassChatWorkflow::automaticChatPurge(), "\n";
foreach (erLhcoreClassChat::getList(array('limit' => 500, 'filter' => array('status' => erLhcoreClassModelChat::STATUS_PENDING_CHAT))) as $chat) {
    erLhcoreClassChatWorkflow::autoAssign($chat, $chat->department);
    erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.pending_process_workflow', array('chat' => &$chat));
}
echo "Ended chat/workflow\n";
All Usage Examples Of erLhcoreClassChatWorkflow::automaticChatClosing