<?php
include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."/mystery/init.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<head>
  <title>Mystery Report</title>
  <style type="text/css">
  td {
    vertical-align: top;
    border: 1px solid #CECECE;
     }
  .reportTable TD {
    vertical-align: top;
   } 
  </style>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<?php 

date_default_timezone_set('Europe/Ljubljana');





require_once (PATH_TO_ROOT.'_class/createQuestion/Question.php');
		
$groups = explode('_',$_GET['projTable']);
array_shift($groups);

$cols = explode(',',$_GET['columns']);
$rowQ = $_GET['row'];

//get all projects for each group
$db->ChangeDatabase(DB_WARPIT_MYSTERY);


foreach ($groups as $gIndex => $gId) {
	$sql = $db->SQLexecute("SELECT a.id,b.proj_name AS idLab FROM _Groups a INNER JOIN ".DB_WARPIT_WEBCATI_BASE."._Projects b ON a.id_project = b.id WHERE a.id_sup = $gId");
	while ($row = $db->fetchRow($sql))
	{
		$grp[$gIndex][] = $row[1];
	}
	$grName[$gIndex] = $db->get('_Groups',$gId,'idLab');
}
	
$db->ChangeDatabase(DB_WARPIT_WEBCATI);
//we have an example project to take our questions and shit
$proj	= $grp[0][0];	
foreach ($cols as $col) {
	$qId = $db->get($proj,"q_name='$col'");
	$q = Question::createQuestionFromTable($db,$proj,$qId[0]['id']);
	$colAns[] = $q->getAnswerArray();
	$colLabels[] = $q->vpr_sql;
	
}


$filter = $_GET['filter'];

$qId = $db->get($proj,"q_name='$rowQ'");
$q = Question::createQuestionFromTable($db,$proj,$qId[0]['id']);
$rowAns= $q->getAnswerArray();
$rowLabel = $q->vpr_sql;

$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);

if($_GET['splitBy'] == 2)
{

//pa zacnima...najprej table

foreach ($grp as $grIndex => $projGroup) {

echo "<div style='float:left;'><div style='float:left;width:30px'></div>".$grName[$grIndex]."</div>";
?>
<div style='float:left'><table class=reportTable cellpadding=4 cellspacing=0><tr><td>&nbsp;</td>
<?php 
//display column question names
$totalCol = array();
$totalTotal = 0;
foreach ($cols as $cId => $col) {
	echo "<td colspan=".count( $colAns[$cId]).">". ($_GET['showVarName'] ? "$col " : '')."{$colLabels[$cId]}</td>";
}

echo "</tr><tr><td>". ($_GET['showVarName'] ? "$rowQ " : '')."$rowLabel</td>";
//display answers
foreach ($cols as $cId => $col) {
	$t = $colAns[$cId];
	foreach ($t as $ans) {
		$display = $ans[1];
		if($_GET['showVarValue'])
			$display = $ans[0].' '.$display;
		echo "<td>$display</td>";
	}
	
}
echo "<td>TOTAL</td>";
//go trough all the anwers of the row
foreach ($rowAns as $rowA) {
	echo "</tr><tr>";
	echo "<td>". ($_GET['showVarValue'] ? "{$rowA[0]} " : '')."{$rowA[1]}</td>";
	$totalRow = 0;
	foreach ($cols as $cId => $col) {
		$t = $colAns[$cId];
		foreach ($t as $aInd => $ans) {
			//time to do some fucking calculations!!!
			$num = 0;
			foreach ($projGroup as $proj) {
			
			
			$sql = "SELECT count(*) FROM $proj"."_structure WHERE $rowQ = {$rowA[0]} AND $col = {$ans[0]}";
			if($filter)
			$sql.=" AND (".$filter.")";
			$numF = $db->fetchRow( $db->SQLexecute($sql) );
			$num += $numF[0];
				
			}
			$totalRow += $num;
			$totalCol[$aInd.'_'.$cId] += $num;
			$totalTotal += $num;
			echo "<td>$num</td>";
		}
		
	}
	echo "<td>$totalRow</td>";
}
// display total by columns
echo "</tr><tr><td>TOTAL</td>";
foreach ($cols as $cId => $col) {
		$t = $colAns[$cId];
		foreach ($t as $aInd => $ans) {
			echo "<td>{$totalCol[$aInd.'_'.$cId]}</td>";
		}
	}
echo "<td>$totalTotal</td>";

echo "</tr></table></div><div style=' clear: both;'></div><hr>";

	;
}
}
else //split by row :)
{
foreach ($rowAns as $rowA) {
	

echo "<div style='float:left;'><div style='float:left;width:30px'></div>". ($_GET['showVarValue'] ? "{$rowA[0]} " : '')."{$rowA[1]}</div>";
?>
<div style='float:left'><table class=reportTable cellpadding=4 cellspacing=0><tr><td>&nbsp;</td>
<?php 
//display column question names
$totalCol = array();
$totalTotal = 0;
foreach ($cols as $cId => $col) {
	echo "<td colspan=".count( $colAns[$cId]).">". ($_GET['showVarName'] ? "$col " : '')."{$colLabels[$cId]}</td>";
}

echo "</tr><tr><td>". ($_GET['showVarName'] ? "$rowQ " : '')."$rowLabel</td>";
//display answers
foreach ($cols as $cId => $col) {
	$t = $colAns[$cId];
	foreach ($t as $ans) {
		$display = $ans[1];
		if($_GET['showVarValue'])
			$display = $ans[0].' '.$display;
		echo "<td>$display</td>";
	}
	
}
echo "<td>TOTAL</td>";
//go trough all the anwers of the row
foreach ($grp as $grIndex => $projGroup) {

	echo "</tr><tr>";
	echo "<td>".$grName[$grIndex]."</td>";
	$totalRow = 0;
	foreach ($cols as $cId => $col) {
		$t = $colAns[$cId];
		foreach ($t as $aInd => $ans) {
			//time to do some fucking calculations!!!
			$num = 0;
			foreach ($projGroup as $proj) {
			
			
			$sql = "SELECT count(*) FROM $proj"."_structure WHERE $rowQ = {$rowA[0]} AND $col = {$ans[0]}";
			if($filter)
				$sql.=" AND (".$filter.")";
			$numF = $db->fetchRow( $db->SQLexecute($sql) );
			$num += $numF[0];
				
			}
			$totalRow += $num;
			$totalCol[$aInd.'_'.$cId] += $num;
			$totalTotal += $num;
			echo "<td>$num</td>";
		}
		
	}
	echo "<td>$totalRow</td>";
}
// display total by columns
echo "</tr><tr><td>TOTAL</td>";
foreach ($cols as $cId => $col) {
		$t = $colAns[$cId];
		foreach ($t as $aInd => $ans) {
			echo "<td>{$totalCol[$aInd.'_'.$cId]}</td>";
		}
	}
echo "<td>$totalTotal</td>";

echo "</tr></table></div><div style=' clear: both;'></div><hr>";

	;
}
}
?>