<?php

include('eval_select_common.php');

$ccr = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'eval_perso'");
$ccr_info = mysqli_fetch_assoc($ccr);
if($ccr_info['valeur']=="niveau" || substr($choixgrille,5,1)==0) {
	// Récupération des données :
	$tabsav_select = 
	"SELECT sav.nom_savoir nom_savoir, SUBSTR(sav.nom_savoir,1,50) savsub, SUBSTR(sav.s".$div."_id,-3) sidsub, SUBSTR(sav.s".$div."_id,1,3) cadre, sav.s".$div."_id savdiv_id, sc.bgcolor bg
	FROM ".$prefix."_savoirs".$div." sav
	INNER JOIN ".$prefix."_savchamps sc
	ON sav.champs_id = sc.champs_id
	".$savchoix."
	ORDER BY s".$div."_id ASC";
	$tabsav = mysqli_query($bdd,$tabsav_select);
	$tabsav_nb = mysqli_num_rows($tabsav);
}
else if($ccr_info['valeur']=="global" && substr($choixgrille,5,1)==1) {
	if($choixgrille!="A0E0P1"){
		// Récupération des données des grilles non perso :
		$tabsav_select = 
		"SELECT sav.nom_savoir nom_savoir, SUBSTR(sav.nom_savoir,1,50) savsub, SUBSTR(sav.s".$div."_id,-3) sidsub, SUBSTR(sav.s".$div."_id,1,3) cadre, sav.s".$div."_id savdiv_id, sc.bgcolor bg
		FROM ".$prefix."_savoirs".$div." sav
		INNER JOIN ".$prefix."_savchamps sc
		ON sav.champs_id = sc.champs_id
		".$savchoix_alt."
		ORDER BY s".$div."_id ASC";
		$tabsav = mysqli_query($bdd,$tabsav_select);
		$tabsav_nb = mysqli_num_rows($tabsav);
	}
	$tabsav_nb = 1;
}
?>
<script>
    var savIds = [
        <?php
        $savIdsArray = [];
        while ($savoir = mysqli_fetch_assoc($tabsav)) {
            $savIdsArray[] = '"' . addslashes($savoir['savdiv_id']) . '"';
        }
        echo implode(", ", $savIdsArray);
        ?>
    ];
</script>
<?php	
$tabdiv_select = "SELECT e.nom nom, e.prenom prenom, e.eleves_id FROM ".$prefix."_eleves e WHERE e.eleves_id='$eleve_id' GROUP BY nom";

// Si aucun savoir à évaluer :
if($tabsav_nb == 0) {
	if(!isset($evalprint)) {
		echo EDTEL_STATS6;
	}
}
// Sinon :
else {
	// Affichage :
	$tabdiv = mysqli_query($bdd,$tabdiv_select);
	$tab_eleve = '<table>
	<thead class="eval_thead">
		<tr>
		<th class="eval_th eval_th_ele"><p />'.SAV1.'</th>';
	foreach($tabdiv as $eleve) {
		$tab_eleve .= '<th class="eleve"';
		if (preg_match("#Chrome#", getenv("HTTP_USER_AGENT"))) { 
			$tab_eleve .= ' style="min-width:60px;max-width:60px;" ';
		}
		else {
			$tab_eleve .= ' style="min-width:52px;max-width:52px;" ';
		}
		$tab_eleve .= '>'.$eleve['nom'].' '.$eleve['prenom'].'</th>';
	}
	$tab_eleve .= '</tr></thead><tbody class="eval_tbody">';
	$tab_eleve_pdf = '<h2>'.$eleve_info['nom'].' '.$eleve_info['prenom'].' '.$diveval.'</h2>';
	$_SESSION['namepdf'] = $diveval.'_'.$eleve_info['nom'].' '.$eleve_info['prenom'];
	$tab_eleve_pdf .= $tab_eleve;
	if(@$evalprint!=2) {
	?>
	<script type="text/javascript">
		function evaluated(note,savoir,eleve){
		  var note = note.options[note.selectedIndex].value;
		  $.ajax({
		    type: "POST",
		    data: { note: note, savoir: savoir, eleve: eleve},
		    url: "js/idl/eval_elesav.php",
		    
		   });
		}
		function evaluatedall(note, eleve, savIds) {
		    var note = note.options[note.selectedIndex].value;

		    var savData = {};
		    savIds.forEach(function(id) {
		        savData[id] = id;
		    });

		    $.ajax({
		        type: "POST",
		        data: Object.assign({ note: note, eleve: eleve }, savData),
		        url: "js/idl/eval_elesavall.php"
		    });
		}
		function selection(pSelect,pDiv){
			var couleur;
			if(pSelect.options[pSelect.selectedIndex].value == "0"){
				couleur = "#eee";
			}
			else if(pSelect.options[pSelect.selectedIndex].value == "1"){
				couleur = "red";
			}
			else if(pSelect.options[pSelect.selectedIndex].value == "2"){
				couleur = "orange";
			}
			<?php
			if($nb_degres==4) {
				echo 'else if(pSelect.options[pSelect.selectedIndex].value == "3"){
					couleur = "yellow";
				}';
			}
			?>
			else if(pSelect.options[pSelect.selectedIndex].value == "4"){
				couleur = "green";
			}
			document.getElementById(pDiv).style.backgroundColor = couleur;
		}
		function selectionall(pSelect,pOption,pDiv){
			var couleur;
			var valueSelect = pSelect.options[pSelect.selectedIndex].value;
			if(valueSelect == "0"){
				couleur = "#eee";
			}
			else if(valueSelect == "1"){
				couleur = "red";
			}
			else if(valueSelect == "2"){
				couleur = "orange";
			}
			<?php
			if($nb_degres==4) {
				echo 'else if(valueSelect == "3"){
					couleur = "yellow";
				}';
			}
			?>
			else if(valueSelect == "4"){
				couleur = "green";
			}
			for(var g=0;g<document.getElementsByName(pDiv).length;g++) {
				document.getElementsByName(pDiv)[g].style.backgroundColor = couleur;				
			}
			for(var g=0;g<document.getElementsByName(pOption).length;g++) {
				document.getElementsByName(pOption)[g].selectedIndex = valueSelect;
			}
		}
	</script>
	<?php
	}
	if(isset($tabsav)) {
		foreach ($tabsav as $savoir) {
			$tab_eleve .= '<tr><td class="eval_sav" style="background-color:'.$savoir['bg'].';">
			<span title="'.$savoir['nom_savoir'].'">'.$savoir['cadre'].'-'.$savoir['sidsub'].'. '.mb_ereg_replace("[?]","",mb_convert_encoding($savoir['savsub'],"utf-8")).'...</td>';
			$tab_eleve_pdf .= '<tr><td class="eval_sav" style="background-color:'.$savoir['bg'].';">
			<span title="'.$savoir['nom_savoir'].'">'.$savoir['cadre'].'-'.$savoir['nom_savoir'].'</td>';
			foreach ($tabdiv as $eleve) {
				$tabeval = mysqli_query($bdd,"SELECT eval FROM ".$prefix."_savele WHERE eleves_id='".$eleve['eleves_id']."' AND s_id='".$savoir['savdiv_id']."'");
				$tabeval_info = mysqli_fetch_assoc($tabeval);
				$savid_opt = ''.$eleve['eleves_id'].':'.$savoir['savdiv_id'].'';
				$savid_opta = ''.$eleve['eleves_id'].':all';
				if(@$tabeval_info['eval']==0) {
					include('eval_select_past.php');
					$tab_elevea = $tab_past;
				}
				else if(isset($tabeval_info['eval'])) {
					$tab_elevea = '<td class="evalc evalniv'.$tabeval_info['eval'].'" id="'.$savid_opt.'" name="td'.$eleve['eleves_id'].'">';
				}				
				$tab_eleve .= $tab_elevea;
				$tab_eleve_pdf .= $tab_elevea;
				$_SESSION['eid'] = $eleve['eleves_id'];
				$_SESSION['sid'] = $savoir['savdiv_id'];
				if($eval_select=='1') {
					$tab_eleveopt = '<select id="'.$eleve['eleves_id'].':'.$savoir['savdiv_id'].'" name="v'.$eleve['eleves_id'].'" onchange="evaluated(this,\''.$savoir['savdiv_id'].'\',\''.$eleve['eleves_id'].'\');selection(this,\''.$savid_opt.'\')">';
					for($e=0;$e<count($value);$e++) {
						$tab_eleveopt .= '<option value="'.$value[$e].'" class="evalniv'.$value[$e].'"';
						if(@$tabeval_info['eval']== $value[$e]) { $tab_eleveopt .= ' selected '; }
						$tab_eleveopt .=  '><span class="evalniv'.$value[$e].'"';
						$tab_eleveopt .= '>'.$evalv[$e].'</span></option>';
					}
					$tab_eleveopt .= '</select>';
					$tab_eleve .= $tab_eleveopt;
				}
				$tab_eleve .= '</td>';
				$tab_eleve_pdf .= "</td>";
			}
			$tab_eleve .= '</tr>';
			$tab_eleve_pdf .= "</tr>";
		}
		$tab_eleve .= '<tr><td class="eval_sav" style="background-color:#fff;">
			<span title="tous">'.SAV4.'</td>';
		foreach ($tabdiv as $eleve) { 
			$tab_eleve .= '<td class="evalc" id="'.$savid_opta.'" name="td'.$eleve['eleves_id'].'">';
			if($eval_select=='1') {
				$tab_eleve .= '<select id="'.$eleve['eleves_id'].':all" name="'.$eleve['eleves_id'].':all" onchange="evaluatedall(this,\''.$eleve['eleves_id'].'\', savIds);selectionall(this,\'v'.$eleve['eleves_id'].'\',\'td'.$eleve['eleves_id'].'\')"><option></option>';
				for($e=0;$e<count($value);$e++) {
					$tab_eleve .= '<option value="'.$value[$e].'" class="evalniv'.$value[$e].'"';
					$tab_eleve .=  '><span class="evalniv'.$value[$e].'"';
					$tab_eleve .= '>'.$evalv[$e].'</span></option>';
				}
				$tab_eleve .= '</select>';
			}
			$tab_eleve .= '</td>';
		}
		$tab_eleve .= '</tr>';
	}
	if($ccr_info['valeur']=="global" && substr($choixgrille,5,1)==1) {
		// Récupération de toute la grille perso
		$tabDebniveau = array('6', '5', '4', '3', '2', '1', 't');
		foreach($tabDebniveau as $divdeb) {
			$tabsavper_select = 
			"SELECT sav.nom_savoir nom_savoir, SUBSTR(sav.nom_savoir,1,50) savsub, SUBSTR(sav.s".$divdeb."_id,-3) sidsub, SUBSTR(sav.s".$divdeb."_id,1,4) cadre, sav.s".$divdeb."_id savdiv_id, sc.bgcolor bg
			FROM ".$prefix."_savoirs".$divdeb." sav
			INNER JOIN ".$prefix."_savchamps sc
			ON sav.champs_id = sc.champs_id
			WHERE sav.s".$divdeb."_id LIKE 'PER%' 
			ORDER BY s".$divdeb."_id ASC";
			$tabsavper = mysqli_query($bdd,$tabsavper_select);
			$tabsavper_nb = mysqli_num_rows($tabsavper);
			foreach ($tabsavper as $savoirper) {
				$tab_eleve = '<tr><td class="eval_sav" style="background-color:'.$savoirper['bg'].';"><span title="'.$savoirper['nom_savoir'].'">'.$savoirper['cadre'].'-'.$savoirper['sidsub'].'. '.mb_ereg_replace("[?]","",mb_convert_encoding($savoirper['savsub'],"utf-8")).'...</td>';
				$tab_eleve_pdf = '<tr><td class="eval_sav" style="background-color:'.$savoirper['bg'].';"><span title="'.$savoirper['nom_savoir'].'">'.$savoirper['cadre'].'-'.$savoirper['sidsub'].'. '.mb_ereg_replace("[?]","",mb_convert_encoding($savoirper['savsub'],"utf-8")).'...</td>';
				foreach ($tabdiv as $eleve) {
					$tabeval = mysqli_query($bdd,"SELECT eval FROM ".$prefix."_savele WHERE eleves_id='".$eleve['eleves_id']."' AND s_id='".$savoirper['savdiv_id']."'");
					$tabeval_info = mysqli_fetch_assoc($tabeval);
					$savid_opt = ''.$eleve['eleves_id'].':'.$savoirper['savdiv_id'];
					$savid_opta = ''.$eleve['eleves_id'].':all';
					if(isset($tabeval_info['eval'])) {
						$tab_elevega = '<td class="evalc evalniv'.$tabeval_info['eval'].'" id="'.$savid_opt.'" name="td'.$eleve['eleves_id'].'">';
					}					
					$tab_eleve .= $tab_elevega;
					$tab_eleve_pdf .= $tab_elevega;
					$_SESSION['eid_alt'] = $eleve['eleves_id'];
					$_SESSION['sid_alt'] = $savoirper['savdiv_id'];
					if($eval_select=='1') {
						$tab_elevegopt = '<select id="'.$eleve['eleves_id'].':'.$savoirper['savdiv_id'].'" name="v'.$eleve['eleves_id'].'" onchange="evaluated(this,\''.$savoir['savdiv_id'].'\',\''.$eleve['eleves_id'].'\');selection(this,\''.$savid_opt.'\')">';
						for($e=0;$e<count($value);$e++) {
							$tab_elevegopt .= '<option value="'.$value[$e].'" class="evalniv'.$value[$e].'"';
							if(@$tabeval_info['eval']== $value[$e]) { $tab_elevegopt .= ' selected '; }
							$tab_elevegopt .=  '><span class="evalniv'.$value[$e].'"';
							$tab_elevegopt .= '>'.$evalv[$e].'</span></option>';
						}
						$tab_elevegopt .= '</select>';					
						$tab_eleve .= $tab_elevegopt;
					}
					$tab_eleve .= '</td>';
					$tab_eleve_pdf .= '</td>';
				}
				$tab_eleve .= '</tr>';
				$tab_eleve_pdf .= '</tr>';
			}
			$tab_eleve .= '<tr><td class="eval_sav" style="background-color:#fff;">
			<span title="tous">'.SAV4.'</td>';
			foreach ($tabdiv as $eleve) { 
				$tab_eleve .= '<td class="evalc" id="'.$savid_opta.'" name="td'.$eleve['eleves_id'].'">';
				if($eval_select=='1') {
					$tab_eleve .= '<select id="'.$eleve['eleves_id'].':all" name="'.$eleve['eleves_id'].':all" onchange="evaluatedall(this,\''.$eleve['eleves_id'].'\', savIds);selectionall(this,\'v'.$eleve['eleves_id'].'\',\'td'.$eleve['eleves_id'].'\')"><option></option>';
					for($e=0;$e<count($value);$e++) {
						$tab_eleve .= '<option value="'.$value[$e].'" class="evalniv'.$value[$e].'"';
						$tab_eleve .=  '><span class="evalniv'.$value[$e].'"';
						$tab_eleve .= '>'.$evalv[$e].'</span></option>';
					}
					$tab_eleve .= '</select>';
				}
				$tab_eleve .= '</td>';
			}
			$tab_eleve .= '</tr>';
		}
	}	
	$tab_eleve .= '</tbody></table>';
	$tab_eleve_pdf .= '</tbody></table>';
	$_SESSION['evaluation'] = $tab_eleve_pdf;
	if(!isset($evalprint)) { // si on ne fait pas une impression par lots
		echo $tab_eleve;
	}
}
?>
