<?php

//header ("Content-type: text/html; charset=utf-8");
//header("Cache-Control: no-cache, must-revalidate"); 
//header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
//
date_default_timezone_set('Europe/Ljubljana');




include_once ("_dirinfo.php");

include_once (PATH_TO_ROOT."init.php");

if($_POST['action'])
{
	$_SESSION['mysteryReportGraph'][$_POST['value']]['checked'] = $_POST['checked'];
	exit; 
}
/*
echo "<pre>";
	var_dump($_SESSION);
echo "</pre>";
*/

require_once (PATH_TO_ROOT.'_class/createQuestion/Question.php');

// Display the graph
//$graph->Stroke();
	

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<head>
  <title>Mystery Report</title>
  <style type="text/css">
  td {
	font-family: "Lucida Sans Unicode","Lucida Grande",Sans-Serif;
	 font-size: 14px;
    vertical-align: top;
    border-bottom: 1px solid #CECECE;
     }
  .reportTable TD {
    font-family: "Lucida Sans Unicode","Lucida Grande",Sans-Serif;
    
     vertical-align: top;
    border-bottom: 0px solid #CECECE;
    border-top: 1px solid #CECECE;
   } 
  </style>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<?php 

//echo "<pre>";
//print_r($_SESSION['mysteryReportGraph']);
//echo "</pre>";

if($_GET['type'] == 1)
	echo $_SESSION['mysteryReportHTML'];
else
	echo $_SESSION['mysteryReportTotal'];
exit;

$idProject = $_GET['id_project'];

$idRecord = $_GET['id_record'];



$tableName = $db->get('_Projects',$idProject,'proj_name',DB_WARPIT_WEBCATI_BASE);

if($idRecord)
{
	$record = $db->get($tableName.'_structure',$idRecord);
}
$db->ChangeDatabase(DB_WARPIT_WEBCATI);

$sql= "SELECT * FROM $tableName"."_mystery";
$res = $db->SQLexecute($sql);
while ($row=$db->fetchAssoc($res) )
{
	$key = $row['id_question'];
	if($row['id_answer'])
		$key .= '_'.$row['id_answer'];
	if($row['id_legend'])
		$key .= '_'.$row['id_legend'];
	$scores[$key] = $row['sc_value'];
}
$folderScores = array();
function recursiveSelect($id,$level)
{
	global $tableName,$db,$record,$scores,$folderScores,$idProject,$idRecord;
	$sql = "SELECT a.*,b.qComment FROM $tableName"."_tree a LEFT JOIN _QuestionComments b ON b.id_question=a.id AND id_project=$idProject AND id_structure='$idRecord' WHERE id_sup = $id ORDER BY orderBy";
	$res = $db->SQLexecute($sql);
	while($row = $db->fetchAssoc($res) )
	{
		echo "<TR>";
		if($row['is_leaf'])
		{
			$q = Question::createQuestionFromTable($db,$tableName,$row['id']);
			$ansArr = $q->getAnswerArray();
			$ansCnt = count($ansArr);
			echo "<TD rowspan = $ansCnt>".$q->q_name ."</TD>";
			echo "<TD rowspan = $ansCnt>".strip_tags( $q->vpr_sql )."</TD>";
			//echo "<TD>".$ansArr[0][1] ."</TD>";
			//echo "</TR>";
			
			for ($cm = 0;$cm<$ansCnt;$cm++) {
				if($cm > 0)
					echo "<TR>";
				
				$bckColor = '';
				if($q->tip_sql == 1)
				{
					if($ansArr[$cm][0] ==  $record[$q->q_name] )
					{
						$sc = $scores[$row['id'].'_'.$ansArr[$cm][0] ];
						if($sc > 0 )
						{
							$folderScores[$id]['pos'] += $sc;
							$bckColor = "bgcolor=#C6EFCE";
						}
						else if($sc < 0 )
						{
							$folderScores[$id]['neg'] += abs($sc);
							$bckColor = "bgcolor=#FFC7CE";
						}
						else
							$bckColor = "bgcolor=#cccccc";
					}	
				}
					echo "<TD $bckColor>".strip_tags(  $ansArr[$cm][1] )."</TD>";
				if($cm==0)
					echo "<TD rowspan = $ansCnt>".($row['qComment'] ? $row['qComment'] : '&nbsp;' )."</TD>";
				echo "</TR>";
			}
			
			
		}
		else 
		{
			$folderScores[$id]['subFolder'][] = $row['id'];
			$folderScores[$row['id']]['name'] = $row['idLab'];
			
			$color = "#cccccc";
			if($level == 1)
				$color = "#FFFF7F";
			echo "<TD colspan = 5 bgcolor=$color ><b>".strip_tags( $row['idLab'] )."<b></TD>";
			recursiveSelect($row['id'],$level+1);
		}
		echo "</TR>";
	}
}
echo "<table >";
recursiveSelect(0,0);
echo "</table>";
echo "<br><br><br>";
unset ($folderScores[0]);
echo "<table cellpadding=20 cellspacing=0><tr><td>Atributi za ocenjivanje<td>Maksimalan<br>broj poena<td>Postignuce<td>Nedostatak<td>Postignuće %<td>Nedostatak %</tr>";
foreach ($folderScores as $frdb) {
	
	$poz = $frdb['pos'];
	$neg = $frdb['neg'];
	unset($bckColor);
	if($frdb['subFolder'])
		{
			$bckColor = 'bgcolor=#75923C';
			foreach ($frdb['subFolder'] as $subFldId) {
				$subFld = $folderScores[$subFldId];
				$poz += $subFld['pos'];
				$neg += $subFld['neg'];;
			}
			
		}
	$totalPoz += $poz;
	$totalNeg += $neg;
	if(!$poz) $poz = '&nbsp;';
	if(!$neg) $neg = '&nbsp;';
	
	echo "<tr $bckColor>";
	echo "<td>".$frdb['name']."</td>";
	$max = ($poz + $neg );
	echo "<td>".$max."</td>"; 
	echo "<td>".$poz."</td>"; 
	echo "<td>".$neg."</td>"; 
	if($max)
	{
		echo "<td>". round($poz/$max,2)."</td>"; 
	
		echo "<td>".round($neg/$max,2)."</td>";
	}
	else 
	{
		echo "<td>&nbsp;</td>"; 
		echo "<td>&nbsp;</td>";
	}
	echo "</tr>";
}
	echo "<tr $bckColor>";
	echo "<td>TOTAL</td>";
	$max = ($totalPoz + $totalNeg );
	echo "<td>".$max."</td>"; 
	echo "<td>".$totalPoz."</td>"; 
	echo "<td>".$totalNeg."</td>"; 
	if($max)
	{
		echo "<td>". round($totalPoz/$max,2)."</td>"; 
	
		echo "<td>".round($totalNeg/$max,2)."</td>";
	}

?>