<?php

function recursiveSelect($id,$level)
{
	global $outputTable,$tableName,$db,$record,$scores,$folderScores,$idProject,$idRecord,$mainLevelArray;
	
	/*$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";
	*/
	$sql = "SELECT * FROM $tableName"."_tree a WHERE id_sup = ".$id." ORDER BY orderBy";
	
	
	//exit;
		
	$res = $db->SQLexecute($sql);
	while($row = $db->fetchAssoc($res) )
	{
		$outputTable .= "<TR>";
		if($row['is_leaf'])
		{
			$q = Question::createQuestionFromTable($db,$tableName,$row['id']);
			$ansArr = $q->getAnswerArray();
			$ansCnt = count($ansArr);
			
			$outputTable .= "<TD>".$q->q_name ."</TD>";
			$outputTable .= "<TD>".strip_tags( $q->vpr_sql )."</TD>";
			//$outputTable .= "<TD>".$ansArr[0][1] ."</TD>";
			//$outputTable .= "</TR>";
			if($q->tip_sql == 4)//we make the answer array from actual answers...kinda
			{
				
			}
			$outputTable .= "<TD>";
			$qDetArray = array();
			$qDetArray['name'] = $q->q_name;
			$qDetArray['quest'] = $q->vpr_sql;
							
			
			if($ansCnt > 0)
			{
				//$outputTable .= "11";
				$outputTable .= "<TABLE>";
				for ($cm = 0;$cm<$ansCnt;$cm++) {
					
						$outputTable .= "<TR>";
					
					$bckColor = '';
					if($q->tip_sql == 1 OR $q->tip_sql == 3)
					{
						if($idRecord)
						{
							
							
							if ($q->tip_sql == 1) 
								$questVar = $q->q_name;
							elseif ($q->tip_sql == 3)
								$questVar = $q->q_name."_".($cm+1);

							//$db->LogWrite($q->q_name." - ".$ansArr[$cm][0]." - ".$record[$questVar],true,"mistery inside id record");
								
							if($ansArr[$cm][0] ==  $record[$questVar] )
							{
								//$db->LogWrite("questVar:".$questVar." - row['id']:".$row['id']." - scores[row[id]_ansArr[cm][0]:".$scores[$row['id'].'_'.$ansArr[$cm][0] ],true,"score");

								$sc = $scores[$row['id'].'_'.$ansArr[$cm][0] ];

								//$db->LogWrite($sc,true,"sc variable");
								if($sc > 0 )
								{
									$folderScores[$id]['pos'] += $sc;
									$qDetArray['pos'] += $sc;
									$bckColor = "bgcolor=#C6EFCE";
								}
								else if($sc < 0 )
								{
									$folderScores[$id]['neg'] += abs($sc);
									$qDetArray['neg'] += abs($sc);
									$bckColor = "bgcolor=#FFC7CE";
								}
								else
									$bckColor = "bgcolor=#cccccc";
							}
						}
						else //no records selected...we have to PULL in ALL records! 
						{
								
								foreach ($record as $rec) {
									if ($q->tip_sql == 1) 
										$questVar = $q->q_name;
									elseif ($q->tip_sql == 3)
										$questVar = $q->q_name."_".($cm+1);

										if($ansArr[$cm][0] ==  $rec[$questVar] )
											{
											 	$sc = $scores[$row['id'].'_'.$ansArr[$cm][0] ];
												if($sc > 0 )
												{
													$folderScores[$id]['pos'] += $sc;
													$qDetArray['pos'] += $sc;
												}
												else if($sc < 0 )
												{
													$folderScores[$id]['neg'] += abs($sc);
													$qDetArray['neg'] += abs($sc);
												}
											}
								}
								
								
						}
						
						$outputTable .= "<TD $bckColor>".strip_tags(  $ansArr[$cm][1] )."</TD>";
					
					
					} elseif ($q->tip_sql == 2) {
						$qLegend =  $q->getLegendArray();
						
						$outputTable .= "<TD>".strip_tags(  $ansArr[$cm][1] )."</TD>";
						foreach ($qLegend as $qLegKey => $qLegVal) {
								
								$bckColor = "";

									if($idRecord)
									{
										$t = $q->q_name."_".($cm+1);
										if($qLegVal[0] ==  $record[$t] )
										{
											$myKey = $row['id'].'_'.$qLegVal[0]."_".($cm+1);
											
											$sc = $scores[$myKey];
											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";
										}
									}
									else //no records selected...we have to PULL in ALL records! 
									{
											$t = $q->q_name."_".($cm+1);
											foreach ($record as $rec) {
											
												if($qLegVal[0] ==  $rec[$t] )
												{
											
														$myKey = $row['id'].'_'.$qLegVal[0]."_".($cm+1);
														$sc = $scores[$myKey];
														
														if($sc > 0 )
														{
															$folderScores[$id]['pos'] += $sc;
														}
														else if($sc < 0 )
														{
															$folderScores[$id]['neg'] += abs($sc);
														}
												}
											}
											
									}
										
							
							
							$outputTable .= "<TD ".$bckColor.">"; 
								
								//$outputTable .=$row['id']."-".$record[$t]."-";
								//$outputTable .= $folderScores[$id]['pos']." | ".$folderScores[$id]['neg']." || ";
								$outputTable .= $qLegVal[1]; 	
							$outputTable .= "</TD>";
							}
						}
						$outputTable .= "</TR>";
					}
					
				$outputTable .= "</TABLE>";
			}
			else if($q->tip_sql == 4)
			{
				if($q->open_sql == 1)
				$outputTable .= strip_tags( $record[ $q->q_name.'_1' ] )."&nbsp;";
				else
				{
					
				}
			}
			else if($q->tip_sql == 5)
			{
				$outputTable .= "&nbsp;";
			}
			$folderScores[$id]['questions'][] = $qDetArray; 
			
			$outputTable .= "</TD>";
			
			$outputTable .= "<TD>".($row['qComment'] ? $row['qComment'] : '&nbsp;' )."</TD></TR>";

			
			if($q->open_answers)
			{
				$open = $q->getStructureFields();
				foreach ($open as $openFld) {
					if(strpos($openFld,'_opn') > 0 )
						if ($record[ $openFld ]) 
							$outputTable .="<TR><TD Style=\"vertical-align:top; border-top:0px solid #CECECE\">&nbsp;</TD><td Style=\"vertical-align:top; border-top:0px solid #CECECE\" align=right valign=bottom>&nbsp;</td><td Style=\"vertical-align:top; border-top:0px solid #CECECE\"><table width=100%><tr><TD><i><font color=grey>{$record[ $openFld ]}</font></i></td></tr></table></TD><td Style=\"vertical-align:top; border-top:0px solid #CECECE\">&nbsp;</td></TR>";
				}
				
				
			}
		}
		else 
		{
			$folderScores[$id]['subFolder'][] = $row['id'];
			$folderScores[$row['id']]['name'] = $row['idLab'];
			
			$color = "#cccccc";
			if($level == 1)
				$color = "#FFFF7F";
			else //we store ID's
			{
				$mainLevelArray[] = $row['id'];	
			}
			$outputTable .= "<TD colspan = 5 bgcolor=$color ><b>".strip_tags( $row['idLab'] )."<b></TD>";
			recursiveSelect($row['id'],$level+1);
		}
		$outputTable .= "</TR>";
	}
}
$outputTable .= "<table class=reportTable cellpadding=0 cellspacing=0>";
recursiveSelect(0,0);
$outputTable .= "</table>";
?>