Location: PHPKode > projects > QuickTicket > quickticket/quickticket/qti_stats_gt_csv.php
<?php

// QuickTicket 1.9.0.3 build:20081001

session_start();
require_once('bin/qti_init.php');
if ( !$oVIP->CanAccess('MUV',4) ) $oVIP->EndMessage('!',$L['R_member'],$_SESSION[QT]['skin_dir'],0);

include(Translate('qti_lang_stat.inc'));

// INITIALISE

$strCSV = '';

$intYear = date('Y');
  if ( intval(date('n'))<2 ) $inthYear--;
  if ( isset($_GET['year']) ) $intYear = intval($_GET['year']);
$arrYears = array($intYear-1=>$intYear-1,$intYear=>$intYear);

$s = -1; if ( isset($_GET['s']) ) $s = intval($_GET['s']);
if ( $s>=0 ) { $strSection = 'section='.$s.' AND'; } else { $strSection=''; }

$tt = 'g';
if ( isset($_GET['tt']) ) $tt = strip_tags($_GET['tt']);
if ( isset($_POST['tt']) ) $tt = strip_tags($_POST['tt']);
$bt = 'm';
if ( isset($_GET['bt']) ) $bt = strip_tags($_GET['bt']);
if ( isset($_POST['bt']) ) $bt = strip_tags($_POST['bt']);
$gr = 'a';
if ( isset($_GET['gr']) ) $gr = strip_tags($_GET['gr']);
if ( isset($_POST['gr']) ) $gr = strip_tags($_POST['gr']);
$tr = 'a';
if ( isset($_GET['tr']) ) $tr = strip_tags($_GET['tr']);
if ( isset($_POST['gr']) ) $tr = strip_tags($_POST['tr']);

  // FIRST/LAST DATE

  $query = 'SELECT min(firstpostdate) as startdate, max(firstpostdate) as lastdate FROM '.TABTOPIC;
  $oDB->Query($query);
  $row = $oDB->Getrow();

  if ( empty($row['startdate']) ) $row['startdate']=strval($intYear-1).'0101';
  if ( empty($row['lastdate']) ) $row['lastdate']=strval($intYear).'1231';

  $strFirstdate = QTdate($row['startdate'],$arrQTdate);
  $strLastdate = QTdate($row['lastdate'],$arrQTdate);

  $strLastdaysago = substr($row['lastdate'],0,8);
  $strTendaysago = DateAdd($strLastdaysago,-10,'day');

$intStartyear = intval(date('Y',strtotime($row['startdate'])));
$intStartmonth = intval(date('n',strtotime($row['startdate'])));
$intEndyear = intval(date('Y'));
$intEndmonth = intval(date('n'));

$arrAllYears = array();
for ($i=$intStartyear;$i<=$intEndyear;$i++)
{
  $arrAllYears[$i]=$i;
}
$arrYears = array($intEndyear-1=>$intEndyear-1,$intEndyear=>$intEndyear);

// GLOBAL STATISTICS

switch($bt)
{
case 'q': $intMaxBt=4; break;
case 'd': $intMaxBt=10; break;
case 'm': $intMaxBt=12; break;
default: die('Invalid blocktime');
}

// Initialise array values. When a value is missing the display will show &middot;

$intCurrentYear = $intYear;
$strCurrentTendaysago = $strTendaysago;
$arrT = array();
$arrM = array();
$arrU = array();
$arrT[$intYear] = array();        $arrT[$intYear-1] = array();        
$arrM[$intYear] = array();        $arrM[$intYear-1] = array();        
$arrU[$intYear] = array();        $arrU[$intYear-1] = array();        
$arrT[$intYear][$intMaxBt+1] = 0; $arrT[$intYear-1][$intMaxBt+1] = 0; 
$arrM[$intYear][$intMaxBt+1] = 0; $arrM[$intYear-1][$intMaxBt+1] = 0; 
$arrU[$intYear][$intMaxBt+1] = 0; $arrU[$intYear-1][$intMaxBt+1] = 0; 

// COUNT

// -----
foreach (array($intCurrentYear-1,$intCurrentYear) as $intYear) {
// -----

  if ( $intCurrentYear==$intYear ) { $strTendaysago = $strCurrentTendaysago; } else { $strTendaysago = DateAdd(substr($strTendaysago,0,8),-1,'year'); }

// COUNT TOPICS

for ($intBt=1;$intBt<=$intMaxBt;$intBt++)
{
  // check limits (startdate/enddate)

  if ( $intYear<$intStartyear ) continue;
  if ( $intYear==$intStartyear )
  {
    if ( $bt=='m' ) {
    if ( $intBt<$intStartmonth ) {
    continue;
    }}
    if ( $bt=='q' ) {
    if ( ($intBt==1 && $intStartmonth>3) || ($intBt==2 && $intStartmonth>6) || ($intBt==3 && $intStartmonth>9) ) {
    continue;
    }}
  }
  if ( $intYear>=$intEndyear ) 
  {
    if ( $bt=='m' ) {
    if ( $intBt>$intEndmonth ) {
    continue;
    }}
    if ( $bt=='q' ) {
    if ( ($intBt==2 && $intEndmonth<4) || ($intBt==3 && $intEndmonth<8) || ($intBt==4 && $intEndmonth<12) ) {
    continue;
    }}
  }

  // compute per blocktime

  switch(substr($oDB->type,0,5))
  {
  case 'mysql': 
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND LEFT(issuedate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND LEFT(issuedate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(issuedate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND LEFT(issuedate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'mssql':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND LEFT(issuedate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND LEFT(issuedate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(issuedate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND LEFT(issuedate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'pg':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTRING(issuedate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTRING(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTRING(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'ibase':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTRING(issuedate FROM 1 FOR 6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTRING(issuedate FROM 1 FOR 6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(issuedate FROM 1 FOR 6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTRING(issuedate FROM 1 FOR 8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'sqlit':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTR(issuedate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTR(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTR(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'acces':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND LEFT(issuedate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND LEFT(issuedate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(issuedate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND LEFT(issuedate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'db2':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTR(issuedate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTR(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTR(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'ifx':   
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTRING(issuedate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTRING(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTRING(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'oci':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTR(issuedate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTR(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type="P" AND SUBSTR(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  default: die('Unknown db type '.$oDB->type);
  }
  $row = $oDB->Getrow();
  $arrT[$intYear][$intBt] = intval($row['countid']);
  $arrT[$intYear][$intMaxBt+1] += $arrT[$intYear][$intBt]; // total
}

// COUNT REPLIES

for ($intBt=1;$intBt<=$intMaxBt;$intBt++)
{
  // check limits (startdate/enddate)

  if ( $intYear<$intStartyear ) continue;
  if ( $intYear==$intStartyear )
  {
    if ( $bt=='m' ) {
    if ( $intBt<$intStartmonth ) {
    continue;
    }}
    if ( $bt=='q' ) {
    if ( ($intBt==1 && $intStartmonth>3) || ($intBt==2 && $intStartmonth>6) || ($intBt==3 && $intStartmonth>9) ) {
    continue;
    }}
  }
  if ( $intYear>=$intEndyear ) 
  {
    if ( $bt=='m' ) {
    if ( $intBt>$intEndmonth ) {
    continue;
    }}
    if ( $bt=='q' ) {
    if ( ($intBt==2 && $intEndmonth<4) || ($intBt==3 && $intEndmonth<8) || ($intBt==4 && $intEndmonth<12) ) {
    continue;
    }}
  }

  // compute per blocktime

  switch(substr($oDB->type,0,5))
  {
  case 'mysql':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND LEFT(issuedate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND LEFT(issuedate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(issuedate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND LEFT(issuedate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'mssql':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND LEFT(issuedate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND LEFT(issuedate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(issuedate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND LEFT(issuedate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'pg':    
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTRING(issuedate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTRING(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTRING(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'ibase': 
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTRING(issuedate FROM 1 FOR 6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTRING(issuedate FROM 1 FOR 6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(issuedate FROM 1 FOR 6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTRING(issuedate FROM 1 FOR 8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'sqlit': 
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTR(issuedate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTR(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTR(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'acces': 
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND LEFT(issuedate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND LEFT(issuedate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(issuedate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND LEFT(issuedate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'db2':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTR(issuedate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTR(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTR(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'ifx':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTRING(issuedate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTRING(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTRING(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'oci':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTR(issuedate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTR(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE '.$strSection.' type<>"D" AND SUBSTR(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  default: die('Unknown db type '.$oDB->type);
  }
  $row = $oDB->Getrow();
  $arrM[$intYear][$intBt] = intval($row['countid']);
  $arrM[$intYear][$intMaxBt+1] += $arrM[$intYear][$intBt]; // total
}

// COUNT USERS

for ($intBt=1;$intBt<=$intMaxBt;$intBt++)
{
  // check limits (startdate/enddate)

  if ( $intYear<$intStartyear ) continue;
  if ( $intYear==$intStartyear )
  {
    if ( $bt=='m' ) {
    if ( $intBt<$intStartmonth ) {
    continue;
    }}
    if ( $bt=='q' ) {
    if ( ($intBt==1 && $intStartmonth>3) || ($intBt==2 && $intStartmonth>6) || ($intBt==3 && $intStartmonth>9) ) {
    continue;
    }}
  }
  if ( $intYear>=$intEndyear ) 
  {
    if ( $bt=='m' ) {
    if ( $intBt>$intEndmonth ) {
    continue;
    }}
    if ( $bt=='q' ) {
    if ( ($intBt==2 && $intEndmonth<4) || ($intBt==3 && $intEndmonth<8) || ($intBt==4 && $intEndmonth<12) ) {
    continue;
    }}
  }

  // compute per blocktime

  switch(substr($oDB->type,0,5))
  {
  case 'mysql': 
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(issuedate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'mssql': 
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,6)="'.($intYear*100+$intBt).'"' ); 
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(issuedate,6)<="'.($intYear*100+($intBt*3)).'"' ); 
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' ); 
    break;
  case 'pg':    
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'ibase':
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate FROM 1 FOR 6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate FROM 1 FOR 6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(issuedate FROM 1 FOR 6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate FROM 1 FOR 8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'sqlit':
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  case 'acces':
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(issuedate,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  case 'db2':
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  case 'ifx':
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate,1,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  case 'oci':
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(issuedate,1,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  default: die('Unknown db type '.$oDB->type);
  }
  $row = $oDB->Getrow();
  $arrU[$intYear][$intBt] = intval($row['countid']);
}

  // compute total

  switch(substr($oDB->type,0,5))
  {
  case 'mysql': 
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,4)="'.$intYear.'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,4)="'.$intYear.'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,8)>="'.$strTendaysago.'"' );
    break;
  case 'mssql': 
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,4)="'.$intYear.'"' ); 
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,4)="'.$intYear.'"' ); 
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,8)>="'.$strTendaysago.'"' ); 
    break;
  case 'pg':    
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate,1,4)="'.$intYear.'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate,1,4)="'.$intYear.'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate,1,8)>="'.$strTendaysago.'"' );
    break;
  case 'ibase':
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate FROM 1 FOR 4)="'.$intYear.'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate FROM 1 FOR 4)="'.$intYear.'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(DISTINCT userid) as countid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate FROM 1 FOR 8)>="'.$strTendaysago.'"' );
    break;
  case 'sqlit':
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,8)>="'.$strTendaysago.'")' );
    break;
  case 'acces':
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' LEFT(issuedate,8)>="'.$strTendaysago.'")' );
    break;
  case 'db2':
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,8)>="'.$strTendaysago.'")' );
    break;
  case 'ifx':
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate,1,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate,1,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTRING(issuedate,1,8)>="'.$strTendaysago.'")' );
    break;
  case 'oci':
    if ( $bt=='m') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT COUNT(*) as countid FROM (SELECT DISTINCT userid FROM '.TABPOST.' WHERE '.$strSection.' SUBSTR(issuedate,1,8)>="'.$strTendaysago.'")' );
    break;
  default: die('Unknown db type '.$oDB->type);
  }  
  $row = $oDB->Getrow();
  $arrU[$intYear][$intMaxBt+1] = intval($row['countid']);

// -----
}
// -----

// OUTPUT

// -----
foreach (array($intCurrentYear-1,$intCurrentYear) as $intYear) {
// -----

  // Table header

  $strCSV .= '"'.$intYear.'";';

  switch($bt)
  {
  case 'q': for ($i=1;$i<=$intMaxBt;$i++) { $strCSV .= '"Q'.$i.'";'; } break;
  case 'm': for ($i=1;$i<=$intMaxBt;$i++) { $strCSV .= '"'.$L['dateMM'][$i].'";'; } break;
  case 'd': for ($i=1;$i<=$intMaxBt;$i++) { $strCSV .= '"'.QTdate( DateAdd($strTendaysago,$i,'day'),array('n','date','d M') ).'";'; } break;
  }
  $strCSV .= '"'.($bt=='d' ? '10 days' : $L['Year']).'"<br/>';

$strCSV .= '"'.$L['Topics'].'";';
for ($intBt=1;$intBt<=$intMaxBt;$intBt++) { $strCSV .= (isset($arrT[$intYear][$intBt]) ? $arrT[$intYear][$intBt] : '0').';'; }
$strCSV .= $arrT[$intYear][$intMaxBt+1].'<br/>';

$strCSV .= '"'.$L['Messages'].'";';
for ($intBt=1;$intBt<=$intMaxBt;$intBt++) { $strCSV .= (isset($arrM[$intYear][$intBt]) ? $arrM[$intYear][$intBt] : '0').';'; }
$strCSV .= $arrM[$intYear][$intMaxBt+1].'<br/>';

$strCSV .= '"'.$L['Users'].'";';
for ($intBt=1;$intBt<=$intMaxBt;$intBt++) { $strCSV .= (isset($arrU[$intYear][$intBt]) ? $arrU[$intYear][$intBt] : '0').';'; }
$strCSV .= $arrU[$intYear][$intMaxBt+1].'<br/><br/>';

// -----
}
// -----

// DISPLAY TRENDS

// Table header

$strCSV .= '"'.$L['Trends'].'";';

switch($bt)
{
case 'q': for ($i=1;$i<=$intMaxBt;$i++) { $strCSV .= '"Q'.$i.'";'; } break;
case 'm': for ($i=1;$i<=$intMaxBt;$i++) { $strCSV .= '"'.$L['dateMM'][$i].'";'; } break;
case 'd': for ($i=1;$i<=$intMaxBt;$i++) { $strCSV .= '"'.QTdate( DateAdd($strTendaysago,$i,'day'),array('n','date','d M') ).'";'; } break;
}
$strCSV .= '"'.($bt=='d' ? '10 days' : $L['Year']).'"<br/>';

// Topic trends

$strCSV .= $L['Topics'].';';
for ($intBt=1;$intBt<=$intMaxBt;$intBt++) 
{
  $i = 0;
  if ( isset($arrT[$intCurrentYear][$intBt]) && isset($arrT[$intCurrentYear-1][$intBt]) )
  {
    $i = $arrT[$intCurrentYear][$intBt]-$arrT[$intCurrentYear-1][$intBt];
    if ( $tr=='p' && $i!=0 )
    {
      if ( $arrT[$intCurrentYear-1][$intBt]==0 ) $arrT[$intCurrentYear-1][$intBt]=1;
      $i = intval(($i/$arrT[$intCurrentYear-1][$intBt])*100);
    }
  }
  $strCSV .= $i.';';
}
$i = 0;
if ( isset($arrT[$intCurrentYear][$intMaxBt+1]) && isset($arrT[$intCurrentYear-1][$intMaxBt+1]) )
{
  $i = $arrT[$intCurrentYear][$intMaxBt+1]-$arrT[$intCurrentYear-1][$intMaxBt+1];
  if ( $tr=='p' && $i!=0 )
  {
    if ( $arrT[$intCurrentYear-1][$intMaxBt+1]==0 ) $arrT[$intCurrentYear-1][$intMaxBt+1]=1;
    $i = intval(($i/$arrT[$intCurrentYear-1][$intMaxBt+1])*100);
  }
}
$strCSV .= $i.'<br/>';

// Message trends

$strCSV .= $L['Messages'].';';
for ($intBt=1;$intBt<=$intMaxBt;$intBt++) 
{
  $i = 0;
  if ( isset($arrM[$intCurrentYear][$intBt]) && isset($arrM[$intCurrentYear-1][$intBt]) )
  {
    $i = $arrM[$intCurrentYear][$intBt]-$arrM[$intCurrentYear-1][$intBt];
    if ( $tr=='p' && $i!=0 )
    {
      if ( $arrM[$intCurrentYear-1][$intBt]==0 ) $arrM[$intCurrentYear-1][$intBt]=1;
      $i = intval(($i/$arrM[$intCurrentYear-1][$intBt])*100);
    }
  }
  $strCSV .= $i.';';
}
$i = 0;
if ( isset($arrM[$intCurrentYear][$intMaxBt+1]) && isset($arrM[$intCurrentYear-1][$intMaxBt+1]) )
{
  $i = $arrM[$intCurrentYear][$intMaxBt+1]-$arrM[$intCurrentYear-1][$intMaxBt+1];
  if ( $tr=='p' && $i!=0 )
  {
    if ( $arrM[$intCurrentYear-1][$intMaxBt+1]==0 ) $arrM[$intCurrentYear-1][$intMaxBt+1]=1;
    $i = intval(($i/$arrM[$intCurrentYear-1][$intMaxBt+1])*100);
  }
}
$strCSV .= $i.'<br/>';

// User trends

$strCSV .= $L['Users'].';';
for ($intBt=1;$intBt<=$intMaxBt;$intBt++) 
{
  $i = 0;
  if ( isset($arrU[$intCurrentYear][$intBt]) && isset($arrU[$intCurrentYear-1][$intBt]) )
  {
    $i = $arrU[$intCurrentYear][$intBt]-$arrU[$intCurrentYear-1][$intBt];
    if ( $tr=='p' && $i!=0 )
    {
      if ( $arrU[$intCurrentYear-1][$intBt]==0 ) $arrU[$intCurrentYear-1][$intBt]=1;
      $i = intval(($i/$arrU[$intCurrentYear-1][$intBt])*100);
    }
  }
  $strCSV .= $i.';';
}
$i = 0;
if ( isset($arrU[$intCurrentYear][$intMaxBt+1]) && isset($arrU[$intCurrentYear-1][$intMaxBt+1]) )
{
  $i = $arrU[$intCurrentYear][$intMaxBt+1]-$arrU[$intCurrentYear-1][$intMaxBt+1];
  if ( $tr=='p' && $i!=0 )
  {
    if ( $arrU[$intCurrentYear-1][$intMaxBt+1]==0 ) $arrU[$intCurrentYear-1][$intMaxBt+1]=1;
    $i = intval(($i/$arrU[$intCurrentYear-1][$intMaxBt+1])*100);
  }
}
$strCSV .= $i.'<br/>';

if (!headers_sent())
{
  $strCSV = str_replace('<br/>',"\r\n",$strCSV);
  header('Content-Type: text/csv; charset='.QTI_HTML_CHAR);
  header('Content-Disposition: attachment; filename="global_stat_'.$intYear.'.csv"');
}

echo $strCSV;

?>
Return current item: QuickTicket