<?php 
session_start();
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
		
if(isset($_SESSION['user_droits'])) {
	
	if($_SESSION['user_droits'][0]!="2") {
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="../../index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		
		$evals = '';
		if(isset($_POST['elesprint'])) { $pagew = 210; $format='P'; } 
		else { $pagew = 297; $format='L'; }

		// récupération des infos demandées par formulaire
		$division = $_POST['division'];
		$div_tab = array();
		foreach($division as $regdiv) {
			array_push($div_tab, $regdiv);
		}
		$div_nb = count($div_tab);

		if(isset($_POST['elesprint'])||isset($_POST['divsprint'])) { $evalprint=1; }

		for($i=0;$i<$div_nb;$i++) {
			if(isset($_POST['elesprint'])) {
				// PAR ELEVE
				$diveval = $div_tab[$i];
				// Récupération de chaque élève de la div : A FAIRE
				//groupe ou division :
				$divgr = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE division = '$diveval'");
				$divgr_nb = mysqli_num_rows($divgr);
				if($divgr_nb==0) {
					$eleves = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves eleves
					INNER JOIN ".$prefix."_groupele groupele
					ON groupele.eleves_id = eleves.eleves_id
					INNER JOIN ".$prefix."_groupes groupes
					ON groupele.groupe_id = groupes.groupe_id 
					WHERE groupe_nom = '$diveval'
					ORDER BY nom, prenom");
				}
				else {
					$eleves = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves eleves WHERE division = '$diveval' GROUP BY replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(	replace(nom,'À','A'),'Á','A'),'Â','A'),'Ã','A'),'Ä','A'),'Å','A'),'Ç','C'),'È','E'),'É','E')	,'Ê','E'),'Ë','E'),'Ì','I'),'Í','I'),'Î','I'),'Ï','I'),'Ò','O'),'Ó','O'),'Ô','O'),'Õ','O'),'Ö','O'),'Ù','U'),'Ú','U'),'Û','U'),'Ü','U'),'Ý','Y'), prenom");
				}
				$k=1;
				foreach($eleves as $eleve_info) {
					// Pour chaque élève créer sa page :
					$eleve_id = $eleve_info['eleves_id'];
					$deb_diveval = substr($eleve_info['division'], 0, 1);
					include('../../db/proc/eval_select_ele.php');
					$evalprint=2;
					$evals .= $tab_eleve_pdf;
					if($i!=($div_nb-1)||$k<mysqli_num_rows($eleves)) { $evals .= '<pagebreak>'; }
					$k++;	
				}
			}
			else if(isset($_POST['divsprint'])) {
				// PAR DIVISION
				$diveval = $div_tab[$i];
				$deb_diveval = substr("$diveval", 0, 1);
				//groupe ou division :
				$divgr = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE division = '$diveval'");
				$divgr_nb = mysqli_num_rows($divgr);
				if($divgr_nb==0) {
					$nbeleves_diveval_select = "SELECT * FROM ".$prefix."_eleves eleves
					INNER JOIN ".$prefix."_groupele groupele
					ON groupele.eleves_id = eleves.eleves_id
					INNER JOIN ".$prefix."_groupes groupes
					ON groupele.groupe_id = groupes.groupe_id 
					WHERE groupe_nom = '$diveval'";
					$nbeleves_diveval = mysqli_query($bdd,$nbeleves_diveval_select);
					$nbeleves = mysqli_num_rows($nbeleves_diveval);
					$divougr = "groupe";
				}
				else {
					$nbeleves_diveval = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE division = '$diveval'");
					$nbeleves = mysqli_num_rows($nbeleves_diveval);
					$divougr = "division";
				}
				include(BASE_PATH . '/db/proc/eval_select_div.php');
				//include('../../db/proc/eval_select_div.php');
				$evalprint=2;
			}
			$evals .= $tab_division_pdf;
			if($i!=($div_nb-1)) { $evals .= '<pagebreak>'; }
		}

		ob_start();

		$html = '
		<html>
		<head>
		<style>
		body {font-family: sans-serif;
			font-size: 10pt;
		}
		h1 {
			font-size:13pt;
		}
		.eval {
			display:block;
			overflow:auto;
			min-width:1000px;
		}
		.evalele_left {
			display:inline-block;
			width:430px;
			margin-top:100px;
			vertical-align:top;
			float:left;
		}
		.evalele_right {
			display:inline-block;
			width:430px;
			margin-top:70px;
			vertical-align:top;
			float:right;
		}
		.eval_thead {
			width: 3000px;
			/*display: block;*/
			/*overflow: auto;*/
			/*overflow : -moz-scrollbars-horizontal;*/
		}
		.eval_tbody {
			display: block;
			height: 400px;
			overflow-y: auto;
			/*overflow-x:display;*/
			width: 2500px;
			text-align:left;
		}
		.eval_tbody_past {
			height:auto;
		}
		.eval_th {
			min-width:350px;
			max-width:350px;
			background-color:#F2EFFB;
			text-align:left;
		}
		.eval_th_ele {
			border:0;
		}
		.eval_sav {
			min-width:350px;
			max-width:350px;
			text-align:left;
		}
		.elechrome {
			/*Safari et chrome */
			-webkit-transform: rotate(-180deg);
			-webkit-writing-mode: vertical-rl;
			border:0;
			height:200px;
		}
		.eleve {
			text-rotate:90;
		    /* pour tous */
		    padding: 3px 1px 5px 15px;
			border: 1px grey solid;
			min-width:52px; /*-6*/
			max-width:52px;
			z-index:5;
		}
		.evalc {
			min-width:58px;
			max-width:58px;
		}
		table {
			border-collapse:collapse;
		}
		table.evalt, thead.evalt, th.evalt, tbody.evalt {
			border:1px solid black;
		}
		td.evaltd {
			border:1px solid black;
			text-align:center;
			width:20px;
		}
		td.evalniv0, option.evalniv0, span.evalniv0 {
			background-color:#eee;
			color:#eee;
		}
		td.evalniv1, option.evalniv1, span.evalniv1 {
			background-color:red;
			color:red;
		}
		td.evalniv2, option.evalniv2, span.evalniv2 {
			background-color:orange;
			color:orange;
		}
		td.evalniv3, option.evalniv3, span.evalniv3 {
			background-color:yellow;
			color:yellow;
		}
		td.evalniv4, option.evalniv4, span.evalniv4 {
			background-color:green;
			color:green;
		}
		td.evalpastniv0 {
			background-color:#eee;
			color:#eee;
		}
		td.evalpastniv1 {
			background-color:#f3a2a2;
			color:#f3a2a2;
		}
		td.evalpastniv2 {
			background-color:#f7d68d;
			color:#f7d68d;
		}
		td.evalpastniv3 {
			background-color:#f7ffb5;
			color:#f7ffb5;
		}
		td.evalpastniv4 {
			background-color:#b0ff9f;
			color:#b0ff9f;
		}
		td {
			border:1px solid black;
		}
		.noprint {
			display:none;
		}
		</style>
		</head>
		<body>
		'.$evals.'
		</body>
		</html>
		';
		
		$path = (getenv('MPDF_ROOT')) ? getenv('MPDF_ROOT') : __DIR__;
		if(phpversion()>=8.0) {
			require_once '../mpdf8/autoload.php';
		}
		else {
			require_once '../mpdf/autoload.php';
		}
		$mpdf = new \Mpdf\Mpdf([
			'orientation' => $format,
			'setAutoTopMargin' => 'pad'
		]);

		$mpdf->WriteHTML($html);
		ob_end_clean();
		$mpdf->Output('eval_print.pdf', \Mpdf\Output\Destination::DOWNLOAD);

	}
}
else {
	include('../noaccess.php');
	echo '<a href="../../index.php">'.ACCUEIL.'</a>.</p>';
}
?>