<?php
	$tipo_resumen="Sub Categoria";
	//print($id_student);
//////////Busca cantidad de Sub Cuestionarios - Empieza////////////
	$sqlSelect = sprintf("SELECT SQ.* FROM `experience_questionnair` AS EQ JOIN subquestionnair AS SQ WHERE EQ.id_questionnair = SQ.id_questionnair AND EQ.id_experience = %s ORDER BY SQ.date_to_administer",
	GetSQLValueString($id_experiencia, "int")
	);
	$dbresultSel = mysqli_query($connection,$sqlSelect);
	$subQs=array();		
	while($row=mysqli_fetch_array($dbresultSel))
	{
		$subQs[]=$row['id'];
	}
	//$cant_sub_subquestionnair=$row[0];
	$cant_sub_subquestionnair=count($subQs);
	//print_r($subQs);
//////////Termina - Busca cantidad de Sub Cuestionarios -////////////


	//////////Busca Sub Categorias - Empieza////////////
	$sqlSelect = sprintf("SELECT distinct id_subcategory id, subcategoria premise FROM  experience_subquestionnair eq, subquestionnair_question qq, question q, subcategory sc WHERE eq.id_subquestionnair=qq.`id_subquestionnair` and qq.id_question=q.id  and id_experience = %s and q.id_subcategory=sc.id order by id_subcategory",
	GetSQLValueString($id_experiencia, "int")
	);
	$dbresultSel = mysqli_query($connection,$sqlSelect);		
	$preguntas=array();
	$i=1;
	while($row=mysqli_fetch_array($dbresultSel))
	{
		$preguntas[$row['id']]=array($i,$row['premise']);
		//$ema[$row['id']]=array("","");
		$i++;
	}
	$num_pregs=$i--;
	//////////Termina - Busca Sub Categorias////////////
	//print_r($preguntas);

	//////////Busca Resultados de PrePrueba - Empieza////////////
// 	if($id_student=="all")
// 	{
// 		$sqlSelect = sprintf("SELECT id_question, avg(value) value FROM answerPre WHERE id_experience = %s group by id_question order by id_question",
// 		GetSQLValueString($id_experiencia, "int")
// 		);
// 	}
// 	else
// 	{
// 		$sqlSelect = sprintf("SELECT * FROM answerPre WHERE id_student = %s and id_experience = %s order by id_question",
// 		GetSQLValueString($id_student, "int"),
// 		GetSQLValueString($id_experiencia, "int")
// 		);
// 	}
// 	//print($sqlSelect);
// 	$dbresultSel = mysqli_query($connection,$sqlSelect);		
// 	while($row=mysqli_fetch_array($dbresultSel))
// 	{	
// 		$todoPre[$row['id_question']]=siDecimal2($row['value']);
// 	}
// 	foreach($preguntas as $id => $pregs)
// 	{
// 		$preRes[$id]=$todoPre[$id];
// 		$preguntas[$id][]=$preRes[$id];
// 	}
	//////////Termina - Busca Resultados de PrePrueba////////////


	//////////Busca Resultados de EMA - Empieza////////////
	if($id_student=="all")
	{
		$sqlSelect = sprintf("SELECT id_subcategory id_question, a.id_subquestionnair , avg(a.value) value FROM answer a, question q, subquestionnair_question qq, experience_subquestionnair eq WHERE eq.id_subquestionnair=qq.id_subquestionnair and qq.id_question=q.id and a.id_question=q.id and eq.id_experience = %s  group by a.id_subquestionnair,id_subcategory ORDER BY `id_subcategory`,`a`.`id_subquestionnair` ASC",
		GetSQLValueString($id_experiencia, "int")
		);
		//print $sqlSelect;
	}
	else
	{
		$sqlSelect = sprintf("SELECT id_subcategory id_question, a.id_subquestionnair , avg(a.value) value FROM answer a, question q, subquestionnair_question qq, experience_subquestionnair eq WHERE eq.id_subquestionnair=qq.id_subquestionnair and qq.id_question=q.id and a.id_question=q.id and eq.id_experience = %s and a.id_student= %s group BY `id_subcategory`,`a`.`id_subquestionnair` ORDER BY `id_subcategory`,`a`.`id_subquestionnair` ASC",
		GetSQLValueString($id_experiencia, "int"),
		GetSQLValueString($id_student, "int")
		);
		//print $sqlSelect;
		///Si un mismo estudiante puede estar con el mismo identificador en diferentes experiencias habrá que añadirle experiencia al query
	}
	$dbresultSel = mysqli_query($connection,$sqlSelect);		
	while($row=mysqli_fetch_array($dbresultSel))
	{
		$ema[$row['id_question']][$row['id_subquestionnair']]=$row['value'];
	}
	//print_r($preguntas);
	foreach($preguntas as $id => $pregs)
	{
		//for($j=1;$j<=$cant_sub_subquestionnair;$j++)
		foreach($subQs as $idSq)
		{
			if($ema[$id][$idSq])$preguntas[$id][]=siDecimal2($ema[$id][$idSq]);
			else $preguntas[$id][]=" ";
		}
	}
	//print_r($preguntas);
	////////// - Termina - Busca Resultados de EMA////////////
	
	//////////Calcula promedios EMA  - Empieza////////////
// 	foreach($preguntas as $id => $pregs)
// 	{
// 		//print_r($ema[$id]);
// 		$preguntas[$id][]=siDecimal2(average($ema[$id]));
// 	}
	//////////Termina - Calcula promedios EMA ////////////

	//////////Busca Resultados de PostPrueba  - Empieza////////////
// 	if($id_student=="all")
// 	{
// 		$sqlSelect = sprintf("SELECT id_question, avg(value) value FROM answerPost WHERE id_experience = %s group by id_question order by id_question",
// 		GetSQLValueString($id_experiencia, "int")
// 		);
// 	}
// 	else
// 	{
// 		$sqlSelect = sprintf("SELECT * FROM answerPost WHERE id_student = %s and id_experience = %s order by id_question",
// 		GetSQLValueString($id_student, "int"),
// 		GetSQLValueString($id_experiencia, "int")
// 		);
// 		//print($sqlSelect);
// 	}
// 	$dbresultSel = mysqli_query($connection,$sqlSelect);		
// 	while($row=mysqli_fetch_array($dbresultSel))
// 	{	
// 		$todoPost[$row['id_question']]=siDecimal2($row['value']);
// 	}
// 	foreach($preguntas as $id => $pregs)
// 	{
// 		$postRes[$id]=$todoPost[$id];
// 		$preguntas[$id][]=$postRes[$id];
// 	}
	//////////Termina - Busca Resultados de PostPrueba////////////

	//////////Calcula porcientos de cambio  - Empieza////////////
	$posFinal=$cant_sub_subquestionnair;
	$i=0;
	foreach($preguntas as $id => $pregs)
	{
		//print_r($preguntas[$id]);
		//$valores=array($preguntas[$id][$posFinal],$preRes[$id],$postRes[$id]);
		$valores=$ema[$id];
		$ema_vals=array_slice($preguntas[$id],3,$posFinal-2);
		//print_r(array_slice($ema[id],1,$posFinal-1));
		//print_r($ema[id]);
		$preguntas[$id][]=siDecimal2(average($valores));
		$preguntas[$id][]=siDecimal2(stddev($valores));
		$preguntas[$id][]=siDecimal2(porcientoCambio($ema[$id][$subQs[0]],average($ema_vals)));
		//$preguntas[$id][]=siDecimal2(porcientoCambio($preRes[$id],$preguntas[$id][$posFinal]));
		$preguntas[$id][]=siDecimal2(porcientoCambio(average($ema_vals),$ema[$id][$subQs[$posFinal-1]]));
		$preguntas[$id][]=siDecimal2(porcientoCambio($ema[$id][$subQs[0]],$ema[$id][$subQs[$posFinal-1]]));
//		$preguntas[$id][]=siDecimal2($subQs[$posFinal-1]);
	}
		////////// - Termina - Calcula porcientos de cambio////////////

?>