<?php 

	require_once 'config.php';
	require_once 'dbh.inc.php'; // has to use proyecto.ema.upr DB!!!
	require_once 'checkLogin.php';
	
// 	include("../conection.php");

		

	if(isset($_POST['export']) && isset($_POST['id_exp'])) {
	
		$id_experience = mysqli_real_escape_string($connection, $_POST['id_exp']);
// 		$id_student = mysqli_real_escape_string($connection, $_POST['id_student']);
// 		$res_type = mysqli_real_escape_string($connection, $_POST['res_type']);
	
		header_remove();
		header('Content-Type: text/csv; charset=utf-8');
		header('Content-Disposition: attachment; filename="Resultados_'.$id_experience.'.csv"');
		$output = fopen('php://output', 'w');
		
		
		
		
// 		$answersQuery = "SELECT a.id_student, a.id_subquestionnair, a.id_question, a.value FROM `answer` a, `question` q WHERE `id_subquestionnair` IN (SELECT `id_subquestionnair` FROM experience_subquestionnair WHERE `id_experience`=10);";
//  	$resultAnswers = mysqli_query($connection, $answersQuery);
// 		while($rowAnswers = mysqli_fetch_assoc()) {
// 			$rowAnswers['value'];
// 		}
// 		$subquestionnairQuery = "SELECT `title` FROM `subquestionnair` WHERE `id`=".id_subquestionnair.";
//		id_subquestionnair: se usará para el nombre del subquestionnair
//		id_question: premise, id_category, id_subcategory
		
		
		
		
		
		
		/*** QUEREMOS: question premise, question category, question subcategory, student id, subquestionnair title, answer value ***/
		
		// (CORRADA) GENERA 1541 FILAS PERO ES CON NÚMEROS NADA MÁS
		$query = "SELECT DISTINCT q.id, q.premise, q.id_category, q.id_subcategory, a.id_student, a.id_subquestionnair, a.value FROM `experience_questionnair` eq, `subquestionnair` qs, `questionnair_question` qq, `question` q, `answer` a WHERE a.id_question=q.id and eq.id_questionnair=qs.id_questionnair and eq.id_questionnair=qq.id_questionnair and qq.id_question=q.id and eq.id_experience=".$id_experience." ORDER BY q.id, a.id_subquestionnair, a.id_student ASC;";
		$header = array('Question ID', 'Question Premise', 'Corresponding Moment', 'Question Category', 'Question Subcategory', 'Student ID', 'Answer');

		// (VÍCTOR) GENERA 1215 FILAS
// 		$query = "SELECT q.premise, a.value, sq.title, c.categoria, sc.subcategoria, a.id_student FROM answer a, question q, subquestionnair sq, category c, subcategory sc WHERE (a.`id_subquestionnair` IN (SELECT `id_subquestionnair` FROM experience_subquestionnair WHERE `id_experience`=".$id_experience.")) AND q.id=a.id_question AND sq.id=a.id_subquestionnair AND c.id=q.id_category AND sc.id=q.id_subcategory;";
//		$header = array('Question Premise', 'Answer', 'Corresponding Moment', 'Question Category', 'Question Subcategory', 'Student ID');		
		

		fputcsv($output, $header);
		
		$result = mysqli_query($connection, $query);
		
		while($row = mysqli_fetch_assoc($result)) {
			
			$queryCategory = "SELECT categoria FROM `category` WHERE id=".$row['id_category'].";";
			$resultCategory = mysqli_query($connection, $queryCategory);
			$rowCategory = mysqli_fetch_assoc($resultCategory);
			
			// QUERY USES SUBCATEGORY ID FROM ANOTHER SELECT BECAUSE $row['id_subcategory'] GIVES AN ERROR
			$querySubcategory = "SELECT subcategoria FROM `subcategory` WHERE id=(SELECT id_subcategory FROM `question` WHERE id=".$row['id'].");";
			$resultSubcategory = mysqli_query($connection, $querySubcategory);
			$rowSubcategory = mysqli_fetch_assoc($resultSubcategory);
			
			$queryMoment = "SELECT title FROM `subquestionnair` WHERE id=".$row['id_subquestionnair'].";";
			$resultMoment = mysqli_query($connection, $queryMoment);
			$rowMoment = mysqli_fetch_assoc($resultMoment);
		
			fputcsv($output, array($row['id'], $row['premise'], $rowMoment['title'], $rowCategory['categoria'], $rowSubcategory['subcategoria'], $row['id_student'], $row['value']));
			
		}
		
		fclose($output);

	}

?>