tray/src/messenger/webim/operator/statistics.php

133 lines
5.4 KiB
PHP
Raw Normal View History

<?php
/*
2013-03-07 01:22:53 +04:00
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once('../libs/init.php');
require_once('../libs/chat.php');
require_once('../libs/operator.php');
2012-03-14 00:09:37 +04:00
require_once('../libs/statistics.php');
$operator = check_login();
force_password($operator);
setlocale(LC_TIME, getstring("time.locale"));
$page = array();
$page['operator'] = topage(get_operator_name($operator));
2011-02-26 17:06:19 +03:00
$page['availableDays'] = range(1, 31);
$page['availableMonth'] = get_month_selection(time() - 400 * 24 * 60 * 60, time() + 50 * 24 * 60 * 60);
$page['showresults'] = false;
2012-03-14 00:09:37 +04:00
$statisticstype = verifyparam("type", "/^(bydate|byagent|bypage)$/", "bydate");
$page['type'] = $statisticstype;
$page['showbydate'] = ($statisticstype == 'bydate');
$page['showbyagent'] = ($statisticstype == 'byagent');
$page['showbypage'] = ($statisticstype == 'bypage');
$errors = array();
2011-02-26 17:06:19 +03:00
if (isset($_GET['startday'])) {
$startday = verifyparam("startday", "/^\d+$/");
$startmonth = verifyparam("startmonth", "/^\d{2}.\d{2}$/");
$endday = verifyparam("endday", "/^\d+$/");
$endmonth = verifyparam("endmonth", "/^\d{2}.\d{2}$/");
$start = get_form_date($startday, $startmonth);
$end = get_form_date($endday, $endmonth) + 24 * 60 * 60;
} else {
$curr = getdate(time());
2011-02-26 17:06:19 +03:00
if ($curr['mday'] < 7) {
// previous month
2011-02-26 17:06:19 +03:00
if ($curr['mon'] == 1) {
$month = 12;
$year = $curr['year'] - 1;
} else {
2011-02-26 17:06:19 +03:00
$month = $curr['mon'] - 1;
$year = $curr['year'];
}
2011-02-26 17:06:19 +03:00
$start = mktime(0, 0, 0, $month, 1, $year);
$end = mktime(0, 0, 0, $month, date("t", $start), $year) + 24 * 60 * 60;
} else {
2011-02-26 17:06:19 +03:00
$start = mktime(0, 0, 0, $curr['mon'], 1, $curr['year']);
$end = time() + 24 * 60 * 60;
}
}
set_form_date($start, "start");
2011-02-26 17:06:19 +03:00
set_form_date($end - 24 * 60 * 60, "end");
2011-02-26 17:06:19 +03:00
if ($start > $end) {
$errors[] = getlocal("statistics.wrong.dates");
}
2012-03-14 00:09:37 +04:00
$activetab = 0;
$db = Database::getInstance();
2012-03-14 00:09:37 +04:00
if ($statisticstype == 'bydate') {
$page['reportByDate'] = $db->query(
"select DATE(FROM_UNIXTIME(t.dtmcreated)) as date, COUNT(distinct t.threadid) as threads, SUM(m.ikind = :kind_agent) as agents, SUM(m.ikind = :kind_user) as users, ROUND(AVG(t.dtmchatstarted-t.dtmcreated),1) as avgwaitingtime, ROUND(AVG(tmp.lastmsgtime - t.dtmchatstarted),1) as avgchattime " .
"from {chatmessage} m, {chatthread} t, (SELECT i.threadid, MAX(i.dtmcreated) AS lastmsgtime FROM {chatmessage} i WHERE (ikind = :kind_user OR ikind = :kind_agent) GROUP BY i.threadid) tmp " .
"where m.threadid = t.threadid AND tmp.threadid = t.threadid AND t.dtmchatstarted <> 0 AND m.dtmcreated >= :start AND m.dtmcreated < :end group by DATE(FROM_UNIXTIME(m.dtmcreated)) order by m.dtmcreated desc",
array(
':kind_agent' => Thread::KIND_AGENT,
':kind_user' => Thread::KIND_USER,
':start' => $start,
':end' => $end
),
array('return_rows' => Database::RETURN_ALL_ROWS)
);
$page['reportByDateTotal'] = $db->query(
"select DATE(FROM_UNIXTIME(t.dtmcreated)) as date, COUNT(distinct t.threadid) as threads, SUM(m.ikind = :kind_agent) as agents, SUM(m.ikind = :kind_user) as users, ROUND(AVG(t.dtmchatstarted-t.dtmcreated),1) as avgwaitingtime, ROUND(AVG(tmp.lastmsgtime - t.dtmchatstarted),1) as avgchattime " .
"from {chatmessage} m, {chatthread} t, (SELECT i.threadid, MAX(i.dtmcreated) AS lastmsgtime FROM {chatmessage} i WHERE (ikind = :kind_user OR ikind = :kind_agent) GROUP BY i.threadid) tmp " .
"where m.threadid = t.threadid AND tmp.threadid = t.threadid AND t.dtmchatstarted <> 0 AND m.dtmcreated >= :start AND m.dtmcreated < :end",
array(
':kind_agent' => Thread::KIND_AGENT,
':kind_user' => Thread::KIND_USER,
':start' => $start,
':end' => $end
),
array('return_rows' => Database::RETURN_ONE_ROW)
);
2012-03-14 00:09:37 +04:00
$activetab = 0;
} elseif($statisticstype == 'byagent') {
$page['reportByAgent'] = $db->query(
"select vclocalename as name, COUNT(distinct threadid) as threads, " .
"SUM(ikind = :kind_agent) as msgs, AVG(CHAR_LENGTH(tmessage)) as avglen " .
"from {chatmessage}, {chatoperator} " .
"where agentId = operatorid AND dtmcreated >= :start " .
"AND dtmcreated < :end group by operatorid",
array(
':kind_agent' => Thread::KIND_AGENT,
':start' => $start,
':end' => $end
),
array('return_rows' => Database::RETURN_ALL_ROWS)
);
2012-03-14 00:09:37 +04:00
$activetab = 1;
} elseif($statisticstype == 'bypage') {
$page['reportByPage'] = $db->query(
"SELECT COUNT(DISTINCT p.pageid) as visittimes, p.address, COUNT(DISTINCT t.threadid) as chattimes " .
"FROM {visitedpagestatistics} p LEFT OUTER JOIN {chatthread} t ON (p.address = t.referer AND DATE(FROM_UNIXTIME(p.visittime)) = DATE(FROM_UNIXTIME(t.dtmcreated))) " .
"WHERE p.visittime >= :start AND p.visittime < :end GROUP BY p.address",
array(':start' => $start, ':end' => $end),
array('return_rows' => Database::RETURN_ALL_ROWS)
);
2012-03-14 00:09:37 +04:00
$activetab = 2;
}
$page['showresults'] = count($errors) == 0;
prepare_menu($operator);
2012-03-14 00:09:37 +04:00
setup_statistics_tabs($activetab);
start_html_output();
require('../view/statistics.php');
2013-03-13 01:03:50 +04:00
?>