diff --git a/src/messenger/webim/install/dbinfo.php b/src/messenger/webim/install/dbinfo.php index e92f18b5..bc32caab 100644 --- a/src/messenger/webim/install/dbinfo.php +++ b/src/messenger/webim/install/dbinfo.php @@ -136,7 +136,11 @@ $dbtables = array( "operatorid" => "int NOT NULL", "threads" => "int NOT NULL DEFAULT 0", "messages" => "int NOT NULL DEFAULT 0", - "averagelength" => "FLOAT(10, 1) NOT NULL DEFAULT 0" + "averagelength" => "FLOAT(10, 1) NOT NULL DEFAULT 0", + "sentinvitations" => "int NOT NULL DEFAULT 0", + "acceptedinvitations" => "int NOT NULL DEFAULT 0", + "rejectedinvitations" => "int NOT NULL DEFAULT 0", + "ignoredinvitations" => "int NOT NULL DEFAULT 0" ), "${mysqlprefix}chatrevision" => array( @@ -242,7 +246,7 @@ $dbtables_can_update = array( "${mysqlprefix}chatmessage" => array("agentId"), "${mysqlprefix}indexedchatmessage" => array(), "${mysqlprefix}chatoperator" => array("vcavatar", "vcjabbername", "iperm", "istatus", "idisabled", "vcemail", "dtmrestore", "vcrestoretoken"), - "${mysqlprefix}chatoperatorstatistics" => array(), + "${mysqlprefix}chatoperatorstatistics" => array("sentinvitations", "acceptedinvitations", "rejectedinvitations", "ignoredinvitations"), "${mysqlprefix}chatban" => array(), "${mysqlprefix}chatgroup" => array("vcemail", "iweight", "parent", "vctitle", "vcchattitle", "vclogo", "vchosturl"), "${mysqlprefix}chatgroupoperator" => array(), diff --git a/src/messenger/webim/install/dbperform.php b/src/messenger/webim/install/dbperform.php index 3e3f933b..f4a22249 100644 --- a/src/messenger/webim/install/dbperform.php +++ b/src/messenger/webim/install/dbperform.php @@ -184,6 +184,22 @@ if ($act == "silentcreateall") { runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD vcrestoretoken varchar(64)", $link); } + if (in_array("${mysqlprefix}chatoperatorstatistics.sentinvitations", $absent_columns)) { + runsql("ALTER TABLE ${mysqlprefix}chatoperatorstatistics ADD sentinvitations int NOT NULL DEFAULT 0", $link); + } + + if (in_array("${mysqlprefix}chatoperatorstatistics.acceptedinvitations", $absent_columns)) { + runsql("ALTER TABLE ${mysqlprefix}chatoperatorstatistics ADD acceptedinvitations int NOT NULL DEFAULT 0", $link); + } + + if (in_array("${mysqlprefix}chatoperatorstatistics.rejectedinvitations", $absent_columns)) { + runsql("ALTER TABLE ${mysqlprefix}chatoperatorstatistics ADD rejectedinvitations int NOT NULL DEFAULT 0", $link); + } + + if (in_array("${mysqlprefix}chatoperatorstatistics.ignoredinvitations", $absent_columns)) { + runsql("ALTER TABLE ${mysqlprefix}chatoperatorstatistics ADD ignoredinvitations int NOT NULL DEFAULT 0", $link); + } + if (in_array("${mysqlprefix}chatresponses.vctitle", $absent_columns)) { runsql("ALTER TABLE ${mysqlprefix}chatresponses ADD vctitle varchar(100) NOT NULL DEFAULT '' AFTER groupid", $link); } diff --git a/src/messenger/webim/libs/statistics.php b/src/messenger/webim/libs/statistics.php index 2f30e9b5..79a925b3 100644 --- a/src/messenger/webim/libs/statistics.php +++ b/src/messenger/webim/libs/statistics.php @@ -270,28 +270,121 @@ function calculate_operator_statistics() { $start = empty($result['start']) ? 0 : $result['start']; $today = floor(time() / (24*60*60)) * 24*60*60; + $statistics = array(); + // Caclculate statistics - $db->query( - "INSERT INTO {chatoperatorstatistics} ( " . - "date, operatorid, threads, messages, averagelength" . - ") SELECT (FLOOR(m.dtmcreated / (24*60*60)) * 24*60*60) AS date, " . - "o.operatorid AS opid, " . + // Get base operator's info + $db_results = $db->query( + "SELECT (FLOOR(m.dtmcreated / (24*60*60)) * 24*60*60) AS date, " . + "m.agentId AS operator_id, " . "COUNT(distinct m.threadid) AS threads, " . - "SUM(m.ikind = :kind_agent) AS msgs, " . - "AVG(CHAR_LENGTH(m.tmessage)) AS avglen " . - "FROM {indexedchatmessage} m, {chatoperator} o " . - "WHERE m.agentId = o.operatorid " . + "COUNT(m.messageid) AS messages, " . + "AVG(CHAR_LENGTH(m.tmessage)) AS avg_msg_length " . + // Use {indexedchatmessage} as base table because of one thread can + // be related with more than one operator (they can change each + // other during conversation). + "FROM {indexedchatmessage} m, {chatthread} t " . + "WHERE m.ikind = :kind_agent " . + "AND m.threadid = t.threadid " . "AND (m.dtmcreated - :start) > 24*60*60 " . // Calculate statistics only for messages that older one day "AND (:today - m.dtmcreated) > 24*60*60 " . - "GROUP BY date " . - "ORDER BY date", + // Ignore not accepted invitations + "AND (t.invitationstate = :not_invited " . + "OR t.invitationstate = :invitation_accepted) " . + "GROUP BY date, operator_id", array( - ':kind_agent' => Thread::KIND_AGENT, ':start' => $start, - ':today' => $today - ) + ':today' => $today, + ':not_invited' => Thread::INVITATION_NOT_INVITED, + ':invitation_accepted' => Thread::INVITATION_ACCEPTED, + ':kind_agent' => Thread::KIND_AGENT + ), + array('return_rows' => Database::RETURN_ALL_ROWS) ); + + // Store retrieved data as statistics info + foreach($db_results as $result) { + // Cast types of some fields + $result['avg_msg_length'] = (float)$result['avg_msg_length']; + $result['messages'] = (int)$result['messages']; + + $statistics[$result['date'] . '_' . $result['operator_id']] = $result; + } + + // Get info about invitations + $db_results = $db->query( + "SELECT (FLOOR(dtmcreated / (24*60*60)) * 24*60*60) AS date, " . + "agentId as operator_id, " . + "COUNT(threadid) AS invitation_sent, " . + "SUM(invitationstate = :invitation_accepted) AS invitation_accepted, " . + "SUM(invitationstate = :invitation_rejected) AS invitation_rejected, " . + "SUM(invitationstate = :invitation_ignored) AS invitation_ignored " . + "FROM {chatthread} " . + "WHERE (dtmcreated - :start) > 24*60*60 " . + // Calculate statistics only for threads that older than one day + "AND (:today - dtmcreated) > 24*60*60 " . + // Check if thread has related operator + "AND agentId != 0 " . + // Ignore not accepted invitations + "AND (invitationstate = :invitation_accepted " . + "OR invitationstate = :invitation_rejected " . + "OR invitationstate = :invitation_ignored) " . + "GROUP BY date, operator_id", + array( + ':start' => $start, + ':today' => $today, + ':invitation_accepted' => Thread::INVITATION_ACCEPTED, + ':invitation_rejected' => Thread::INVITATION_REJECTED, + ':invitation_ignored' => Thread::INVITATION_IGNORED + ), + array('return_rows' => Database::RETURN_ALL_ROWS) + ); + + // Store retrieved data as statistics info + foreach($db_results as $result) { + $key = $result['date'] . '_' . $result['operator_id']; + if (empty($statistics[$key])) { + $statistics[$key] = array(); + } + $statistics[$key] += $result; + } + + // Sort statistics by date before save it in the database + ksort($statistics); + + foreach($statistics as $row) { + // Set default values + $row += array( + 'threads' => 0, + 'messages' => 0, + 'avg_msg_length' => 0, + 'invitation_sent' => 0, + 'invitation_accepted' => 0, + 'invitation_rejected' => 0, + 'invitation_ignored' => 0 + ); + + // Prepare data for insert + $insert_data = array(); + foreach($row as $field_name => $field_value) { + $insert_data[':' . $field_name] = $field_value; + } + + $db->query( + "INSERT INTO {chatoperatorstatistics} (" . + "date, operatorid, threads, messages, averagelength, " . + "sentinvitations, acceptedinvitations, " . + "rejectedinvitations, ignoredinvitations " . + ") VALUES (". + ":date, :operator_id, :threads, :messages, " . + ":avg_msg_length, :invitation_sent, " . + ":invitation_accepted, :invitation_rejected, " . + ":invitation_ignored " . + ")", + $insert_data + ); + } } catch(Exception $e) { // Something went wrong: warn and rollback transaction. trigger_error( diff --git a/src/messenger/webim/locales/en/properties b/src/messenger/webim/locales/en/properties index 0012df46..dd73c426 100644 --- a/src/messenger/webim/locales/en/properties +++ b/src/messenger/webim/locales/en/properties @@ -462,6 +462,10 @@ report.byoperator.1=Operator report.byoperator.2=Chat Threads report.byoperator.3=Messages report.byoperator.4=Average message length (in chars) +report.byoperator.5=Invitations sent +report.byoperator.6=Invitations accepted +report.byoperator.7=Invitations rejected +report.byoperator.8=Invitations ignored report.byoperator.title=Threads by operator report.bypage.1=Page report.bypage.2=View times diff --git a/src/messenger/webim/locales/ru/properties b/src/messenger/webim/locales/ru/properties index ad50eb1b..109b5a9f 100644 --- a/src/messenger/webim/locales/ru/properties +++ b/src/messenger/webim/locales/ru/properties @@ -461,6 +461,10 @@ report.byoperator.1= report.byoperator.2=Диалогов report.byoperator.3=Сообщений report.byoperator.4=Средняя длина сообщения (в символах) +report.byoperator.5=Приглашений отправлено +report.byoperator.6=Приглашений принято +report.byoperator.7=Приглашений отклонено +report.byoperator.8=Приглашений проигнорировано report.byoperator.title=Статистика по операторам report.bypage.1=Страница report.bypage.2=Просмотров diff --git a/src/messenger/webim/operator/statistics.php b/src/messenger/webim/operator/statistics.php index 873cd16d..376ef336 100644 --- a/src/messenger/webim/operator/statistics.php +++ b/src/messenger/webim/operator/statistics.php @@ -129,9 +129,15 @@ if ($statisticstype == 'bydate') { } elseif($statisticstype == 'byagent') { $page['reportByAgent'] = $db->query( "SELECT o.vclocalename AS name, " . - "s.threads AS threads, " . - "s.messages AS msgs, " . - "s.averagelength AS avglen " . + "SUM(s.threads) AS threads, " . + "SUM(s.messages) AS msgs, " . + "ROUND( " . + "SUM(s.averagelength * s.messages) / SUM(s.messages), " . + "1) AS avglen, " . + "SUM(sentinvitations) AS sentinvitations, " . + "SUM(acceptedinvitations) AS acceptedinvitations, " . + "SUM(rejectedinvitations) AS rejectedinvitations, " . + "SUM(ignoredinvitations) AS ignoredinvitations " . "FROM {chatoperatorstatistics} s, {chatoperator} o " . "WHERE s.operatorid = o.operatorid " . "AND s.date >= :start " . diff --git a/src/messenger/webim/view/statistics.php b/src/messenger/webim/view/statistics.php index 93b81c18..4796c911 100644 --- a/src/messenger/webim/view/statistics.php +++ b/src/messenger/webim/view/statistics.php @@ -164,7 +164,22 @@ require_once('inc_errors.php'); - + + + + + + + + + + + + + + + + @@ -174,11 +189,17 @@ require_once('inc_errors.php'); + + + + + + - +