<?php
// ===============
// PLANNING AVANCE
// ===============

require($_SERVER['DOCUMENT_ROOT'].'/espace_personnel/inc/head.php');

$date_get = new MyDateTime($_GET['mois']);

// ----------------
//Fonction utilisés
// ----------------

// 2011-06-13 devient Lundi 6
//function jour_textuel($date)

// 2011-06-13 devient Semaine du 13 au 19 juin 2011
//function semaine_textuel($date)

//function regroupement_chat_examen($date,$zone)


function reduce_planning_width($datetime_debut,$datetime_fin,$id_salle)
{
	$sql_planning = dbquery("SELECT DISTINCT(CONCAT(datetime_debut,datetime_fin,planning_salles.id_salle))
		FROM planning_salles,planning_groupes,auditeurs_ue,auditeurs_diplomes,formation_salles
		WHERE planning_salles.id_ue = auditeurs_ue.id_ue
			AND auditeurs_ue.id_auditeur=auditeurs_diplomes.id_auditeur
			AND auditeurs_diplomes.annee_universitaire=planning_salles.annee_universitaire
			AND planning_salles.id_salle = formation_salles.id_salle
			AND auditeurs_diplomes.id='".$_SESSION['user']['id']."'
			AND planning_salles.id_groupe = planning_groupes.id_groupe
			AND planning_salles.id_salle = '".$id_salle."'
			AND datetime_debut <> '".$datetime_fin."'
			AND datetime_fin <> '".$datetime_debut."'
			AND	(((datetime_debut >= '".$datetime_debut."' AND datetime_debut < '".$datetime_fin."')
					OR (datetime_debut > '".$datetime_debut."' AND datetime_debut <= '".$datetime_fin."'))
				OR ((datetime_fin >= '".$datetime_debut."' AND datetime_fin < '".$datetime_fin."')
					OR (datetime_fin > '".$datetime_debut."' AND datetime_fin <= '".$datetime_fin."')))
			AND planning_salles.modification <> 'annulé'");

	if (dbnumrows($sql_planning) > 1)	return true;
	else return false;
}
?>

<style>
	.planning		{border-collapse:collapse;font-size:11px;margin-bottom:25px;}
	.planning a		{font-size:11px;font-style:italic;text-transform:uppercase;}
	.planning td	{border:1px solid #ccc;width:93px;text-align:center;height:50px}
	.tab			{padding-right:0;width:560px;height:auto;overflow:visible}
	.cours a		{font-size:11px;}
	td.semaine		{border:1px solid #222 inset;border-bottom:0;text-align:left;padding-left:10px;height:20px;width:100px;background:#333;}
	.semaine a		{color:#fff;font-weight:bold}
	.jours			{background:#ddd;font-weight:bold;color:#333;}
	.jours td		{height:30px;}
	.horaires		{color:#555;font-weight:bold;}
	.ue				{color:#c1002a;font-weight:bold}
	.couleur		{height:20px;width:40px;border:1px solid #ccc;}
	.regroupement	{background:#FFB85B}
	.chat			{background:#CCCCFF}
	.examen			{background:#F26049}
</style>
	
<?php	
$sql_auditeurs_diplomes = dbquery("SELECT auditeurs_diplomes.*,diplomes.institut
	FROM auditeurs_diplomes,diplomes
	WHERE auditeurs_diplomes.id_diplome = diplomes.id_diplome
		AND auditeurs_diplomes.id = '".$_SESSION['user']['id']."'
		AND auditeurs_diplomes.annee_universitaire = '".$annee_universitaire."'");

if (dbnumrows($sql_auditeurs_diplomes) > 0)
{
	if (dbresult_utf8($sql_auditeurs_diplomes,0,"diplomes.institut") == "ICH")
	{
		// Planning ICH partie 2
		$excel_planning_link = "/base_adm/super/formation/planning/xls_planning_annee_ich.php?partie=2&annee_universitaire=".dbresult_utf8($sql_auditeurs_diplomes,0,"annee_universitaire")."&id_auditeur=".dbresult_utf8($sql_auditeurs_diplomes,0,"id_auditeur");
	}
	else
	{
		// Planning classique
		$excel_planning_link = "/base_adm/super/formation/planning/xls_planning_annee_universitaire.php?annee_universitaire=".dbresult_utf8($sql_auditeurs_diplomes,0,"annee_universitaire")."&id_auditeur=".dbresult_utf8($sql_auditeurs_diplomes,0,"id_auditeur");
	}
}
else deconnexion();


$presentation_text = "Présentation<br>classique";
$presentation_icon = "/images/icones/32px/loupe_off.png";
?>

<div style='position: relative; top:-40px; left:350px; width:140px; border: 1px'>
	
	<!-- REGROUPEMENT -->
	<div style='position:absolute;
	top:-5px;left:150px;
	width:48px;height:26px;
	font-size:7pt; font-weight:bold; text-align:center; overflow: inherit;
	padding:0px;
	border-left:solid #FF8C34 2px;
	border-right:solid #FFD196 2px;
	border-bottom:solid #FFD196 2px;
	border-top:solid #FFD196 2px;
	background-color: #FFD196;
	background: -webkit-gradient(linear, 0 0, 0 bottom, from(#FFD196), to(#FFB85B));
	background: -webkit-linear-gradient(#FFD196, #FFB85B);
	background: -moz-linear-gradient(#FFD196, #FFB85B);
	background: -ms-linear-gradient(#FFD196, #FFB85B);
	background: -o-linear-gradient(#FFD196, #FFB85B);
	background: linear-gradient(#FFD196, #FFB85B);
	filter:alpha(opacity=85); opacity:0.85;'><div style='position:absolute;top:5px; width:48px;'>Regroup.</div></div>

	
	<!-- CHAT -->
	<div style='position:absolute;
	top:-5px;left:215px;
	width:48px;height:26px;
	font-size:7pt; font-weight:bold; text-align:center; overflow: inherit;
	padding:0px;
	border-left:solid #B266FF 2px;
	border-right:solid #CCCCFF 2px;
	border-bottom:solid #CCCCFF 2px;
	border-top:solid #CCCCFF 2px;
	background-color: #CCCCFF;
	background: -webkit-gradient(linear, 0 0, 0 bottom, from(#CCCCFF), to(#CC99FF));
	background: -webkit-linear-gradient(#CCCCFF, #CC99FF);
	background: -moz-linear-gradient(#CCCCFF, #CC99FF);
	background: -ms-linear-gradient(#CCCCFF, #CC99FF);
	background: -o-linear-gradient(#CCCCFF, #CC99FF);
	background: linear-gradient(#CCCCFF, #CC99FF);
	filter:alpha(opacity=85); opacity:0.85;'><div style='position:absolute;top:5px; width:48px;'>Chat</div></div>



	
	
	<!-- EXCEL -->
	<div style='position: absolute; top:-5px; left:-80px; width:100px; border: 0px; border:0px;text-align:center'>
		<!-- <a href="<?php echo $excel_planning_link ?> "> --><i style='background-color:white;color:green' class='fa fa-file-excel-o fa-2x'></i><!-- </a> -->
	</div>

	<div style='border:solid 1px red; position: absolute; top:-1px; left:-10px; width:140px; border: 1px; border:0px;color:#555555;font-size:12px;line-height:13px'><a href="<?php echo $excel_planning_link ?> ">Version téléchargeable<br>(Format Excel)</a></div>


	<!-- PRESENTATION -->
	<!-- <div style='position: absolute; top:0px; left:190px; width:100px; border: 0px; border:0px;text-align:center'>
		<img style="cursor:pointer" id="avance" src="<?php echo $presentation_icon ?>" width="32" height="32" border="0" alt="">
	</div>

	<div style='border:solid 1px red; position: absolute; top:40px; left:190px; width:100px; border: 0px; border:0px;color:#555555;font-weight:bold;font-size:8pt;text-align:center'><?php echo $presentation_text ?></div>
 --></div>


<div style='position: relative; top:20px; left:20px; border:0px'>

	<?php
	// week
	$sql_planning_week = dbquery("SELECT DISTINCT(MONTH(week)) AS mois,YEAR(week) AS annee
			FROM planning_week
			WHERE regroupement_chat_examen = 'regroupement_chat'
				AND MONTH(week)	= '".$date_get->format('m')."'
				AND YEAR(week)	= '".$date_get->format('Y')."'
				AND annee_universitaire='".$annee_universitaire."'");


	for ($i=0;$i<dbnumrows($sql_planning_week);$i++)
	{
		$annee	= dbresult_utf8($sql_planning_week,$i,"annee");
		$mois	= dbresult_utf8($sql_planning_week,$i,"mois");
		?>

		<div class='tab' id='tab<?php echo $i ?>'>

			<?php
	
			// 2012-2013 et semestre 2
			$sql_planning_week2 = dbquery("SELECT week
				FROM planning_week
				WHERE regroupement_chat_examen = 'regroupement_chat'
					AND MONTH(week)	= '".$mois."'
					AND YEAR(week)	= '".$annee."'
					AND annee_universitaire='".$annee_universitaire."'");
			for ($j=0;$j<dbnumrows($sql_planning_week2);$j++)
			{
				$week = new DateTime(dbresult_utf8($sql_planning_week2,$j,"week"));
			
				if (dbresult_utf8($sql_auditeurs_diplomes,0,"diplomes.institut") == "CNE")
				{
					$heure_debut	= 13;
					$heure_fin		= 20;
				}
				else
				{
					$heure_debut	= 17;
					$heure_fin		= 21;
				}
				
				// ====================
				// Planning réservation
				// --> par mois
				// ====================

				// Mois precedent, actuel et suivant (le 1er)


				// -------------------------------
				// On détermine le lundi precedent
				// du jour en cours
				// -------------------------------

				// Si la date de $week n'es pas un lundi, on recupere le lundi precedent
				if ($week->format('l') == "Monday")
				{
					$date_jour_encours = $week;
				}
				else
				{
					$date_jour_encours = $week->modify("last Monday");
				}
			
				$div_height =  84 + ($heure_fin - $heure_debut) * 50;
				?>


				<div name="jour_bar" style="position:relative; top:0px; left:-40px;
				width:680px;
				height:<?php echo $div_height ?>px;
				background-color: #FFFFFF;
				border:0px;
				overflow-x:hidden;
				overflow-y:auto;margin-top:20px">
				
				<?php
				echo "<div class='bg-dark' style='position:absolute;top:5px;left:40px;width:600px;height:20px; border:0px; text-align:left;font-size:8pt;padding:7px 0 18px 10px'>".utf8_encode(strtoupper(semaine_textuel($date_jour_encours->format('Y-m-d'))))."</div>";
				for ($k=1;$k<=6;$k++)
				{
					//Samedi
					if ($k == 6)
					{
						if (dbresult_utf8($sql_auditeurs_diplomes,0,"diplomes.institut") == "CNE")
						{
							$heure_debut	= 8;
							$heure_fin		= 15;
						}
						else
						{
							$heure_debut	= 8;
							$heure_fin		= 12;
						}
					}
					else
					{
						if (dbresult_utf8($sql_auditeurs_diplomes,0,"diplomes.institut") == "CNE")
						{
							$heure_debut	= 13;
							$heure_fin		= 20;
						}
						else
						{
							$heure_debut	= 17;
							$heure_fin		= 21;
						}
					}

					$x_encours = 40+100*($k-1);

					echo "<div class='bg-light' style='position:absolute;top:30px;left:".$x_encours."px;width:100px;height:26px; border:solid #ddd 1px ; color:#333333; text-align:center;padding-top:8px;font-size:8pt'>";
					echo jour_textuel($date_jour_encours->format("Y-m-d"))."</div>";

					for ($heure=$heure_debut;$heure<=$heure_fin;$heure++)
					{
						$y_encours_heure = 48 + 52*($heure-$heure_debut);
						$y_encours_point = 71 + 52*($heure-$heure_debut);
						
						$y_encours_ligne1 = 56 + 52*($heure-$heure_debut);
						$y_encours_ligne2 = 82 + 52*($heure-$heure_debut);
						

						// -----
						// HEURE
						// -----
						// Lundi
						if ($k == 1)	echo "<div style='position:absolute;top:".$y_encours_heure."px;left:14px;width:20px;height:16px;background-color: #FFFFFF; color:#B7B7B7; text-align:right'>".$heure."</div>";
						
						// Samedi hors CNE
						if ($k == 6)	echo "<div style='position:absolute;top:".$y_encours_heure."px;left:644px;width:20px;height:16px;background-color: #FFFFFF; color:#E30031; text-align:right'>".$heure."</div>";

						if ($heure == $heure_fin)	continue;

						// -------------
						// HEURE ET DEMI
						// -------------
						// Lundi
						if ($k == 1)	echo "<div style='position:absolute;top:".$y_encours_point."px;left:13px;width:20px;height:16px;background-color: #FFFFFF; color:#B7B7B7; text-align:right'>.</div>";	
						
						// Samedi
						if ($k == 6)	echo "<div style='position:absolute;top:".$y_encours_point."px;left:640px;width:20px;height:16px;background-color: #FFFFFF; color:#E30031; text-align:right'>.</div>";	

						$x_encours = 40+100*($k-1);

						for ($l=0;$l<=1;$l++)
						{			
							// Samedi (vert)
							if ($k == 6)
							{
								// LIGNE HEURE
								echo "<div style='position:absolute;top:".$y_encours_ligne1."px;left:".$x_encours."px;width:50px;height:26px;background-color: #FFFFFF; border-top:solid #ddd 1px; border-bottom:dotted #f88 1px;border-left:solid #f88 1px;border-right:solid #f88 1px; color:#B7B7B7;'></div>";

								// LIGNE HEURE ET DEMI
								echo "<div style='position:absolute;top:".$y_encours_ligne2."px;left:".$x_encours."px;width:50px;height:26px;background-color: #FFFFFF; border-top:dotted #f88 1px; border-bottom:solid #f88 1px; border-left:solid #f88 1px;border-right:solid #f88 1px; color:#B7B7B7;'></div>";
							}
							else
							{					
								// LIGNE HEURE
								echo "<div style='position:absolute;top:".$y_encours_ligne1."px;left:".$x_encours."px;width:50px;height:26px;background-color: #FFFFFF; border-top:solid #ddd 1px; border-bottom:dotted #ddd 1px;border-left:solid #ddd 1px;border-right:solid #ddd 1px; color:#B7B7B7;'></div>";

								// LIGNE HEURE ET DEMI
								echo "<div style='position:absolute;top:".$y_encours_ligne2."px;left:".$x_encours."px;width:50px;height:26px;background-color: #FFFFFF; border-top:dotted #ddd 1px; border-bottom:solid #ddd 1px; border-left:solid #ddd 1px;border-right:solid #ddd 1px; color:#B7B7B7;'></div>";
							}

							$x_encours += 50;
						}
					}

					$date_jour_encours -> modify ("+1 day");
				}

				$date_jour_encours -> modify ("-6 day");



				for ($k=1;$k<=6;$k++)
				{
					if (dbresult_utf8($sql_auditeurs_diplomes,0,"diplomes.institut") == "CNE")
					{
						$heure_debut	= 13;
						$heure_fin		= 20;
					}
					else
					{
						//Samedi
						if ($k == 6)
						{
							$heure_debut	= 8;
							$heure_fin		= 12;
						}
						else
						{
							$heure_debut	= 17;
							$heure_fin		= 21;
						}
					}
					
					
					
					$x_encours = 40+100*($k-1);

					// ----------------------------
					// Groupes a afficher
					// Regroupement (5) et Chat (6)
					// ----------------------------

					$tab_chat_regroup = array('5'=>'Regroup.','6'=>'Chat');
					
					foreach($tab_chat_regroup as $id_groupe => $nom_abrege_groupe)
					{
						$sql_planning_salles_regroup = dbquery("SELECT planning_salles.id_planning_salle,planning_salles.id_ue,datetime_debut,datetime_fin,planning_salles.id_salle,planning_groupes.nom,background_color_top,background_color_bottom,border_color,formation_salles.abrege,planning_groupes.nom
							FROM planning_salles,planning_groupes,auditeurs_ue,auditeurs_diplomes,formation_salles
							WHERE planning_salles.id_ue = auditeurs_ue.id_ue
								AND auditeurs_ue.id_auditeur=auditeurs_diplomes.id_auditeur
								AND auditeurs_diplomes.annee_universitaire=planning_salles.annee_universitaire
								AND planning_salles.id_salle = formation_salles.id_salle
								AND (auditeurs_ue.validation = 'J' OR auditeurs_ue.validation = '')
								AND auditeurs_diplomes.id='".$_SESSION['user']['id']."'
								AND planning_salles.id_groupe=planning_groupes.id_groupe
								AND DATE(datetime_debut) = '".$date_jour_encours->format('Y-m-d')."'
								AND (planning_groupes.id_groupe = '".$id_groupe."')
								AND planning_salles.modification NOT LIKE 'annul%'
							ORDER BY datetime_fin,datetime_debut DESC");

						for ($l=0;$l<dbnumrows($sql_planning_salles_regroup);$l++)
						{
							$id_planning_salle			= dbresult_utf8($sql_planning_salles_regroup,$l,"id_planning_salle");
							$id_ue						= dbresult_utf8($sql_planning_salles_regroup,$l,"id_ue");
							$salle_abrege				= dbresult_utf8($sql_planning_salles_regroup,$l,"formation_salles.abrege");
							$nom_groupe					= dbresult_utf8($sql_planning_salles_regroup,$l,"planning_groupes.nom");

							$datetime_debut				= dbresult_utf8($sql_planning_salles_regroup,$l,"planning_salles.datetime_debut");
							$datetime_fin				= dbresult_utf8($sql_planning_salles_regroup,$l,"planning_salles.datetime_fin");


							$background_color_top		= dbresult_utf8($sql_planning_salles_regroup,$l,"planning_groupes.background_color_top");
							$background_color_bottom	= dbresult_utf8($sql_planning_salles_regroup,$l,"planning_groupes.background_color_bottom");
							$border_color				= dbresult_utf8($sql_planning_salles_regroup,$l,"planning_groupes.border_color");

							
							$x_position = $x_encours;

							$width = 48;

							// Difference entre la fin de la précédente plannification et la nouvelle
							// supérieure stricte a 600 secondes = 10 minutes
							/*if (strtotime($mem_datetime_fin[$id_salle]) - strtotime($datetime_debut) > 600)
							{
								$x_position += 15;
							}*/

							if (reduce_planning_width($datetime_debut,$datetime_fin,$id_salle) == true)
							{
								$width -= 15;
							}

							$mem_datetime_fin[$id_salle] = $datetime_fin;


							$display = "";

							if ($nom_groupe == "Chat")	$x_position += 50;

							$top = 10;
							$display .= "<br><div style='position:absolute;top:".$top."px; width:".$width."px;'>";

							if (preg_match("/_CNE/i", $id_ue))		$display .= "<span style='color:red'>CNE</span>\n";

							$display .= $id_ue;
							
							if ($nom_groupe == "Regroupement")	$display .= "<br>".$salle_abrege;

							$display .= "</div><br>";




							$y_top = 56+((substr($datetime_debut,11,2)+substr($datetime_debut,14,2)/60)-$heure_debut)*52;
							$height = (strtotime($datetime_fin) - strtotime($datetime_debut))/3600*50;


							$style = "position:absolute;
								top:".$y_top."px;left:".$x_position."px;
								width:".$width."px;height:".$height."px;
								font-size:7pt; font-weight:bold; text-align:center; overflow: inherit;
								padding:0px;
								border-left:solid ".$border_color." 2px;
								border-right:solid ".$background_color_top." 1px;
								border-bottom:solid ".$background_color_top." 1px;
								border-top:solid ".$background_color_top." 2px;
								background-color: ".$background_color_top.";
								background: -webkit-gradient(linear, 0 0, 0 bottom, from(".$background_color_top."), to(".$background_color_bottom."));
								background: -webkit-linear-gradient(".$background_color_top.", ".$background_color_bottom.");
								background: -moz-linear-gradient(".$background_color_top.", ".$background_color_bottom.");
								background: -ms-linear-gradient(".$background_color_top.", ".$background_color_bottom.");
								background: -o-linear-gradient(".$background_color_top.", ".$background_color_bottom.");
								background: linear-gradient(".$background_color_top.", ".$background_color_bottom.");
								filter:alpha(opacity=85); opacity:0.85;";

							// Une seule plannification

							echo "<div style='".$style."'>".$display."</div>";
						}
					}

					$date_jour_encours -> modify ("+1 day");
				}
				echo "</div>";
				// -------------------
				// Fin Planning avancé
				// -------------------
			}
			?>
			</div>
		<?php
	}
	?>

<?php //echo $_SESSION['user']['id']. " / ".$annee_universitaire;?>

</div>	<!-- PARTIE CENTRALE end -->



</body>
</html>