<?php 
header ("Content-type: text/html; charset=utf-8");
?>
<head>
  <title>Basic Statistic</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <style type="text/css">
  table
	{ 
	margin-left: auto;
	margin-right: auto;
	}
  </style>
</head>

<?php 
set_time_limit(900);

include '_dirinfo.php';
include_once(PATH_TO_ROOT."_config/dbConfig.php");
include_once PATH_TO_ROOT.'_class/createQuestion/Question.php';
if($_GET['loginSID'])
{
	$expSID = explode("-",$_GET['loginSID']);
			
	if (!is_numeric($expSID[1])) { 
		echo "Warning! Wrong link or project does not exist!"; 
		exit; 
	} 
				
	$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE."._www_externalLinks WHERE id = ".$expSID[1]." LIMIT 1 ";
	//echo $sql;
	$res = $db->SQLexecute($sql);
	$zad = $db->fetchAssoc($res);
	// preverim ali se loginSID (kljuc do ankete) ki je poslan preko URL ujema s tistim v bazi
	// ce se ne ujema vrnem status 97
	if ($zad['loginSID'] != $_GET['loginSID']) { 
		echo "Warning! Wrong link or project does not exist!"; 
		exit;
	}
	
	$projectId = $zad['id_project']; 
	$linkType = $zad['linkType'];
			
} else {
	$projectId = $_GET['projectId'];
}

$projData = $db->get('_Projects',$projectId,'proj_name,language',DB_WARPIT_WEBCATI_BASE);
$tableName = $projData['proj_name'];
$id_language = $projData['languge'];

$tmpName = "warpitStatistic";


$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
//20151009 Tanis I have add vaiables for displaying filters in statistics
$sql = "SELECT stat_show_var, stat_show_val, disablebasic,language,stat_show_filter_in,stat_show_filter_out FROM _Projects WHERE proj_name='$tableName'";

If (!$res = $db->SQLexecute($sql)) {
			Echo '{success:false,error:"'.$db->GetError().'", STATEMENT: "'.$sql.'"}';
			exit;	
	}
$projProps = $db->fetchAssoc($res);
$showVar = $projProps['stat_show_var'];

$id_language = $projProps['language'];

$sql = "DROP TEMPORARY TABLE IF EXISTS $tmpName";
$db->SQLexecute($sql);
if($db->IsError())
{
	Echo '{success:false,error:"'.$db->GetError().'", STATEMENT: "'.$sql.'"}';
	exit;
}
$db->SQLexecute("CREATE TEMPORARY TABLE $tmpName LIKE {$tableName}_structure");
if($db->IsError())
{
	Echo '{success:false,error:"'.$db->GetError().'"}';
	exit;
}
if($linkType == 2) $confWhere = " AND flag_bad = 101";
$db->SQLexecute("INSERT INTO $tmpName SELECT * FROM {$tableName}_structure WHERE status = 1 AND status_rec = 1$confWhere");

if($db->IsError())
{
	Echo '{success:false,error:"'.$db->GetError().'"}';
	exit;
}

//tu bomo pol naredli SQL z WHERE za izbrane iz dreve in menija
$checkedIds = $_GET['checkedId'];
if( $checkedIds) {
	$whereArr[] = 'id in ( '.$checkedIds.')';
	
	//tu bomo pol naredli SQL z WHERE za neizbrane iz dreve 
	$checkedIds = $_GET['unCheckedId'];
	if ($checkedIds ) {
		$whereArrNot[] = 'id not in ('.$checkedIds.')';
	}
}

//tu bomo pol naredli SQL z WHERE za izbrane iz menija
$checkedIds = $_GET['checkedTypes'];
if ($checkedIds) {
	$whereArr[] = 'tip_sql in ('.$checkedIds.')';
}
if($whereArr)
{
	$whereTmp[] = '( '.implode(' OR ',$whereArr).' )';
}
if($whereArrNot)
{
	$whereTmp[] = '( '.implode(' AND ',$whereArrNot).' )';
}

if($whereTmp)
{
	$where = 'WHERE '.implode(' AND ',$whereTmp);
}
//check if compare user is enabled
if($user = $_POST['compare_user'])
{
	if(substr($user,0,5) == 'admin')
	{
		$userFilter = "user_id = ".substr($user,6)." AND user_type = 1";
	}
	else 
		$userFilter = "user_id = ".$user." AND user_type = 0";
}

$db->ChangeDatabase(DB_WARPIT_WEBCATI);
$sql = "SELECT * FROM $tableName $where ORDER BY q_pos";
If (!$res = $db->SQLexecute($sql)) {
			//Echo '{success:false,error:"'.$db->GetError().'", STATEMENT: "'.$sql.'"}';
			echo "alert('SQL problem: '".$db->getError()." );";
			exit;	
	}
//lets get the language data
   	$sql = "SELECT * FROM $tableName"."_language INNER JOIN _LanguageText ON id_text_type = id_text WHERE id_language = $id_language";
	$resLng = $db->SQLexecute($sql);
	while ($rowLng = $db->fetchAssoc($resLng))
	{
		$lngArray[ $rowLng['id_question'] ][ $rowLng['variable'] ] = $rowLng['id_text_type'] == 2 ? addslashes( $rowLng['lngText']) : $rowLng['lngText'] ; 
	}
	
$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);			
   while ($rowQuestion = $db->fetchAssoc($res)) 
   {
   	
   	if($lngArray[ $rowQuestion['id'] ])
   	foreach ($lngArray[$rowQuestion['id']] as $lngKey => $lngVal) {
   		$rowQuestion[$lngKey] = $lngVal;
   	}
   	
   	
   	$struct = array();
   	$arr = array();
   	$q = Question::createQuestion($rowQuestion['tip_sql'],$db,$rowQuestion);
   	$q->showVar = $showVar;
	//20151009 Tanis 
	$q->stat_show_filter_in= $projProps['stat_show_filter_in'];
	$q->stat_show_filter_out= $projProps['stat_show_filter_out'];
	//20151009 Tanis END 
   	$q->rowQuestion = &$rowQuestion;
   	if($filter = $_GET['custom_filter'])
   	{
   			$q->filter = 'AND ('.stripslashes( $filter) .')';
   	}
   	if($userFilter)
   	{
   		$q->compareFilter =  'AND ('.$userFilter .')';
   		$q->compareUser = true;
   	}
   	$q->HTMLstat = true;
   	$q->createStatistics($rowQuestion,$tmpName );
   }



?>