Location: PHPKode > projects > QuickTicket > quickticket/quickticket/qti_stats_d_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);

// DETAIL 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;

$arrT = array();
$arrN = array();
$arrC = array();
$arrT[$intMaxBt+1] = 0;
$arrN[$intMaxBt+1] = 0;
$arrC[$intMaxBt+1] = 0;

// 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/>';

// COUNT NEW TOPIC

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 '.TABTOPIC.' WHERE '.$strSection.' LEFT(firstpostdate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' LEFT(firstpostdate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(firstpostdate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' LEFT(firstpostdate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'mssql':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' LEFT(firstpostdate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' LEFT(firstpostdate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(firstpostdate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' LEFT(firstpostdate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'pg':    
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTRING(firstpostdate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTRING(firstpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(firstpostdate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTRING(firstpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'ibase': 
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTRING(firstpostdate FROM 1 FOR 6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTRING(firstpostdate FROM 1 FOR 6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(firstpostdate FROM 1 FOR 6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTRING(firstpostdate FROM 1 FOR 8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'sqlit':                                                             
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTR(firstpostdate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTR(firstpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(firstpostdate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTR(firstpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'acces': 
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' LEFT(firstpostdate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' LEFT(firstpostdate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(firstpostdate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' LEFT(firstpostdate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'db2':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTR(firstpostdate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTR(firstpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(firstpostdate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTR(firstpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'ifx':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTRING(firstpostdate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTRING(firstpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(firstpostdate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTRING(firstpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'oci':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTR(firstpostdate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTR(firstpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(firstpostdate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' SUBSTR(firstpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  default: die('Unknown db type '.$oDB->type);
  }
  $row = $oDB->Getrow();
  $arrN[$intBt] = intval($row['countid']);
  $arrN[$intMaxBt+1] += $arrN[$intBt];
}

// COUNT CLOSED 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 '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND LEFT(lastpostdate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND LEFT(lastpostdate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(lastpostdate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND LEFT(lastpostdate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'mssql':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND LEFT(lastpostdate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND LEFT(lastpostdate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(lastpostdate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND LEFT(lastpostdate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'pg':    
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTRING(lastpostdate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTRING(lastpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(lastpostdate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTRING(lastpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'ibase': 
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTRING(lastpostdate FROM 1 FOR 6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTRING(lastpostdate FROM 1 FOR 6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(lastpostdate FROM 1 FOR 6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTRING(lastpostdate FROM 1 FOR 8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'sqlit':                                                             
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTR(lastpostdate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTR(lastpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(lastpostdate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTR(lastpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'acces': 
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND LEFT(lastpostdate,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND LEFT(lastpostdate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(lastpostdate,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND LEFT(lastpostdate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'db2':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTR(lastpostdate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTR(lastpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(lastpostdate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTR(lastpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'ifx':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTRING(lastpostdate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTRING(lastpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(lastpostdate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTRING(lastpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  case 'oci':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTR(lastpostdate,1,6)="'.($intYear*100+$intBt).'"' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTR(lastpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(lastpostdate,1,6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' status="Z" AND SUBSTR(lastpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'"' );
    break;
  default: die('Unknown db type '.$oDB->type);
  }
  $row = $oDB->Getrow();
  $arrC[$intBt] = intval($row['countid']);
  $arrC[$intMaxBt+1] += $arrC[$intBt];
}

// COUNT BACKLOG

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 '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,6)="'.($intYear*100+$intBt).'" OR LEFT(lastpostdate,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(lastpostdate,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'" OR LEFT(lastpostdate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  case 'mssql':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,6)="'.($intYear*100+$intBt).'" OR LEFT(lastpostdate,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(lastpostdate,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'" OR LEFT(lastpostdate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  case 'pg':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate,1,6)="'.($intYear*100+$intBt).'" OR SUBSTRING(lastpostdate,1,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(lastpostdate,1,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'" OR SUBSTRING(lastpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  case 'ibase':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate FROM 1 FOR 6)="'.($intYear*100+$intBt).'" OR SUBSTRING(lastpostdate FROM 1 FOR 6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate FROM 1 FOR 6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(lastpostdate FROM 1 FOR 6)<="'.($intYear*100+($intBt*3)).'"' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate FROM 1 FOR 8)="'.(DateAdd($strTendaysago,$intBt,'day')).'" OR SUBSTRING(lastpostdate FROM 1 FOR 8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  case 'sqlit':                                                                                      
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,6)="'.($intYear*100+$intBt).'" OR SUBSTR(lastpostdate,1,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(lastpostdate,1,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'" OR SUBSTR(lastpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  case 'acces':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,6)="'.($intYear*100+$intBt).'" OR LEFT(lastpostdate,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,6)>"'.($intYear*100+($intBt-1)*3).'" AND LEFT(firstpostdate,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'" OR LEFT(lastpostdate,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  case 'db2':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,6)="'.($intYear*100+$intBt).'" OR SUBSTR(lastpostdate,1,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(lastpostdate,1,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'" OR SUBSTR(lastpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  case 'ifx':   
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate,1,6)="'.($intYear*100+$intBt).'" OR SUBSTRING(lastpostdate,1,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTRING(lastpostdate,1,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'" OR SUBSTRING(lastpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  case 'oci':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,6)="'.($intYear*100+$intBt).'" OR SUBSTR(lastpostdate,1,6)="'.($intYear*100+$intBt).'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,6)>"'.($intYear*100+($intBt-1)*3).'" AND SUBSTR(lastpostdate,1,6)<="'.($intYear*100+($intBt*3)).'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'" OR SUBSTR(lastpostdate,1,8)="'.(DateAdd($strTendaysago,$intBt,'day')).'")' );
    break;
  default: die('Unknown db type '.$oDB->type);
  }
  $row = $oDB->Getrow();
  $arrT[$intBt] = intval($row['countid']);
  if ( $bt=='m') $arrGraphT[$L['dateMMM'][$intBt]] = $arrT[$intBt];
  if ( $bt=='q') $arrGraphT['Q'.$intBt] = $arrT[$intBt];
  if ( $bt=='d') $arrGraphT[substr(DateAdd($strTendaysago,$i,'day'),-2,2)] = $arrT[$intBt]; 
}

  // count total
  
  switch(substr($oDB->type,0,5))
  {
  case 'mysql': 
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,4)="'.$intYear.'" OR LEFT(lastpostdate,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,4)="'.$intYear.'" OR LEFT(lastpostdate,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,8)>="'.$strTendaysago.'" OR LEFT(lastpostdate,8)>="'.$strTendaysago.'")' );
    break;
  case 'mssql':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,4)="'.$intYear.'" OR LEFT(lastpostdate,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,4)="'.$intYear.'" OR LEFT(lastpostdate,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,8)>="'.$strTendaysago.'" OR LEFT(lastpostdate,8)>="'.$strTendaysago.'")' );
    break;
  case 'pg':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate,1,4)="'.$intYear.'" OR SUBSTRING(lastpostdate,1,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate,1,4)="'.$intYear.'" OR SUBSTRING(lastpostdate,1,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate,1,8)>="'.$strTendaysago.'" OR SUBSTRING(lastpostdate,1,8)>="'.$strTendaysago.'")' );
    break;
  case 'ibase':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate FROM 1 FOR 4)="'.$intYear.'" OR SUBSTRING(lastpostdate FROM 1 FOR 4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate FROM 1 FOR 4)="'.$intYear.'" OR SUBSTRING(lastpostdate FROM 1 FOR 4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate FROM 1 FOR 8)>="'.$strTendaysago.'" OR SUBSTRING(lastpostdate FROM 1 FOR 8)>="'.$strTendaysago.'")' );
    break;
  case 'sqlit':                                                                                      
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,4)="'.$intYear.'" OR SUBSTR(lastpostdate,1,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,4)="'.$intYear.'" OR SUBSTR(lastpostdate,1,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,8)>="'.$strTendaysago.'" OR SUBSTR(lastpostdate,1,8)>="'.$strTendaysago.'")' );
    break;
  case 'acces':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,4)="'.$intYear.'" OR LEFT(lastpostdate,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,4)="'.$intYear.'" OR LEFT(lastpostdate,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (LEFT(firstpostdate,8)>="'.$strTendaysago.'" OR LEFT(lastpostdate,8)>="'.$strTendaysago.'")' );
    break;
  case 'db2':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,4)="'.$intYear.'" OR SUBSTR(lastpostdate,1,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,4)="'.$intYear.'" OR SUBSTR(lastpostdate,1,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,8)>="'.$strTendaysago.'" OR SUBSTR(lastpostdate,1,8)>="'.$strTendaysago.'")' );
    break;
  case 'ifx':   
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate,1,4)="'.$intYear.'" OR SUBSTRING(lastpostdate,1,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate,1,4)="'.$intYear.'" OR SUBSTRING(lastpostdate,1,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTRING(firstpostdate,1,8)>="'.$strTendaysago.'" OR SUBSTRING(lastpostdate,1,8)>="'.$strTendaysago.'")' );
    break;
  case 'oci':
    if ( $bt=='m') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,4)="'.$intYear.'" OR SUBSTR(lastpostdate,1,4)="'.$intYear.'")' );
    if ( $bt=='q') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,4)="'.$intYear.'" OR SUBSTR(lastpostdate,1,4)="'.$intYear.'")' );
    if ( $bt=='d') $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE '.$strSection.' (SUBSTR(firstpostdate,1,8)>="'.$strTendaysago.'" OR SUBSTR(lastpostdate,1,8)>="'.$strTendaysago.'")' );
    break;
  default: die('Unknown db type '.$oDB->type);
  }
  $row = $oDB->Getrow();
  $arrT[$intMaxBt+1] = intval($row['countid']);

// OUPUT

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

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

$strCSV .= '"'.$L['Backlog'].'";';
for ($intBt=1;$intBt<=$intMaxBt;$intBt++) { $strCSV .= (isset($arrT[$intBt]) ? $arrT[$intBt] : '0').';'; }
$strCSV .= $arrT[$intMaxBt+1].'<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="detail_stat_'.$intYear.'.csv"');
}

echo $strCSV;

?>
Return current item: QuickTicket