function UpdateUserStatus($_internalActivated, $_internalClosed, $_internalDeclined, $_externalActivated, $_externalClose)
 {
     if (!empty($this->ChatId)) {
         $this->Status = $_externalClose || $_internalDeclined || $_internalClosed ? CHAT_CLOSED : $this->Status;
         if ($_internalActivated) {
             queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_VISITOR_CHATS . "` SET `internal_active`='1',`allocated`='" . DBManager::RealEscape(time()) . "' WHERE `internal_active`=0 AND `chat_id`='" . DBManager::RealEscape($this->ChatId) . "' LIMIT 1;");
             if (DBManager::GetAffectedRowCount() == 1) {
                 queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_VISITOR_CHAT_OPERATORS . "` SET `status`=0 WHERE `chat_id`='" . DBManager::RealEscape($this->ChatId) . "' AND `user_id`='" . DBManager::RealEscape(CALLER_SYSTEM_ID) . "';");
                 queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_VISITOR_CHAT_OPERATORS . "` SET `status`=9,`ltime`=" . time() . ",`jtime`=0 WHERE `chat_id`='" . DBManager::RealEscape($this->ChatId) . "' AND `user_id`!='" . DBManager::RealEscape(CALLER_SYSTEM_ID) . "' AND `status`<=1;");
             }
         } else {
             if ($_externalClose && empty($this->InternalClosed)) {
                 $update = "`external_close`='1',`exit`='" . DBManager::RealEscape(time() + 1) . "'";
             } else {
                 if ($_externalClose && !empty($this->InternalClosed)) {
                     $update = "`external_close`='1'";
                 } else {
                     if ($_internalClosed && empty($this->InternalClosed)) {
                         $update = "`internal_closed`='1',`exit`='" . DBManager::RealEscape(time() + 1) . "'";
                     } else {
                         if ($_internalDeclined && empty($this->InternalDeclined)) {
                             $update = "`internal_declined`='1'";
                         } else {
                             $update = "`external_active`='1'";
                         }
                     }
                 }
             }
             if (($_internalClosed || $_externalClose) && !empty($this->AllocatedTime)) {
                 UserGroup::RemoveNonPersistantMember($this->SystemId);
                 //queryDB(true,"DELETE FROM `".DB_PREFIX.DATABASE_GROUP_MEMBERS."` WHERE `user_id`='".DBManager::RealEscape($this->SystemId)."';");
                 $params = $this->CalculateChatResponseTime();
                 $update .= ",`response_time`=" . $params[0] . ",`chat_posts`=" . $params[1];
             }
             queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_VISITOR_CHATS . "` SET " . $update . " WHERE `chat_id`='" . DBManager::RealEscape($this->ChatId) . "' LIMIT 1;");
         }
         queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` SET `endtime`=" . $this->LastActive . (!empty($this->AllocatedTime) ? ",`time`=" . $this->AllocatedTime : "") . " WHERE `chat_id`='" . DBManager::RealEscape($this->ChatId) . "' LIMIT 1;");
     }
     if (!empty($this->ChatVoucherId)) {
         $ticket = new CommercialChatVoucher(null, $this->ChatVoucherId);
         $ticket->UpdateVoucherChatTime(0);
     }
 }
 function Save()
 {
     $result = DBManager::Execute(true, "INSERT INTO `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_VOUCHERS . "` (`id`, `extends`, `tid`, `email`, `info`, `created`, `expires`, `edited`, `chat_sessions_max`, `chat_time_max`,\n\t\t`chat_list`, `visitor_id`, `company`, `tax_id`, `firstname`, `lastname`, `address_1`, `address_2`, `zip`, `state`, `phone`, `city`, `country`, `tn_id`, `price`, `currency`, `vat`, `payment_details`, `language`) \n\t\tVALUES (\n\t\t'" . DBManager::RealEscape($this->Id) . "',\n\t\t'" . DBManager::RealEscape($this->Extends) . "',\n\t\t'" . DBManager::RealEscape($this->TypeId) . "',\n\t\t'" . DBManager::RealEscape($this->Email) . "',\n\t\t'" . DBManager::RealEscape("") . "',\n\t\t'" . DBManager::RealEscape(time()) . "',\n\t\t'" . DBManager::RealEscape(0) . "',\n\t\t'" . DBManager::RealEscape(time()) . "',\n\t\t'" . DBManager::RealEscape($this->ChatSessionsMax) . "',\n\t\t'" . DBManager::RealEscape($this->ChatTimeMax) . "',\n\t\t'" . DBManager::RealEscape(@serialize($this->ChatIdList)) . "',\n\t\t'" . DBManager::RealEscape($this->VisitorId) . "',\n\t\t'" . DBManager::RealEscape($this->Company) . "',\n\t\t'" . DBManager::RealEscape($this->TaxID) . "',\n\t\t'" . DBManager::RealEscape($this->Firstname) . "',\n\t\t'" . DBManager::RealEscape($this->Lastname) . "',\n\t\t'" . DBManager::RealEscape($this->Address1) . "',\n\t\t'" . DBManager::RealEscape($this->Address2) . "',\n\t\t'" . DBManager::RealEscape($this->ZIP) . "',\n\t\t'" . DBManager::RealEscape($this->State) . "',\n\t\t'" . DBManager::RealEscape($this->Phone) . "',\n\t\t'" . DBManager::RealEscape($this->City) . "',\n\t\t'" . DBManager::RealEscape($this->Country) . "',\n\t\t'" . DBManager::RealEscape($this->TransactionId) . "',\n\t\t'" . DBManager::RealEscape($this->Price) . "',\n\t\t'" . DBManager::RealEscape(strtoupper($this->CurrencyISOThreeLetter)) . "',\n\t\t'" . DBManager::RealEscape($this->VAT) . "',\n\t\t'" . DBManager::RealEscape($this->PaymentDetails) . "',\n\t\t'" . DBManager::RealEscape(strtolower($this->Language)) . "');");
     return DBManager::GetAffectedRowCount() == 1;
 }
function saveRating($_rating, $_chatId)
{
    $time = time();
    while (true) {
        queryDB(true, "SELECT time FROM `" . DB_PREFIX . DATABASE_RATINGS . "` WHERE time=" . DBManager::RealEscape($time) . ";");
        if (DBManager::GetAffectedRowCount() > 0) {
            $time++;
        } else {
            break;
        }
    }
    queryDB(true, "INSERT INTO `" . DB_PREFIX . DATABASE_RATINGS . "` (`id` ,`time` ,`user_id` ,`internal_id` ,`fullname` ,`email` ,`company` ,`qualification` ,`politeness` ,`comment`, `ip`, `chat_id`) VALUES ('" . DBManager::RealEscape($_rating->Id) . "', " . DBManager::RealEscape($time) . " , '" . DBManager::RealEscape($_rating->UserId) . "', '" . DBManager::RealEscape($_rating->InternId) . "', '" . DBManager::RealEscape($_rating->Fullname) . "', '" . DBManager::RealEscape($_rating->Email) . "', '" . DBManager::RealEscape($_rating->Company) . "', '" . DBManager::RealEscape($_rating->RateQualification) . "', '" . DBManager::RealEscape($_rating->RatePoliteness) . "', '" . DBManager::RealEscape($_rating->RateComment) . "', '" . DBManager::RealEscape(getIP()) . "', '" . DBManager::RealEscape($_chatId) . "');");
}