<?php
include "scrinit.php";
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style1.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">

function popupCenter(pageURL, title, w, h) {
    var left = (screen.width / 2)  - (w / 2);
    var top  = (screen.height / 2) - (h / 2);
    var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800, height=800, top='+top+', left='+left);
}
</script>

<script type="text/javascript">
var prompt=true;

		$(document).ready(function() {

				$('input:not(submit),textarea,select').change(function () {
					window.onbeforeunload = function () {
						if (prompt == true) 
							return 'Δεν έχετε κάμει έλεγχο για την ημερομηνία και δε θα γίνει καταχώρηση';
						}
					});


				$('input:submit').click(function(e) {
					prompt = false;
					});
	    });
		
</script>
<?php include 'logo.php'?>
<h4 class="p1">ΔΗΛΩΣΗ ΔΙΑΓΩΝΙΣΜΑΤΟΣ</h4>
<div id='cssmenu'>   
<ul id="navbar">
<li class='has-sub'><a href='#' onClick="popupCenter('diagoreport1.php')"><span>ΛΙΣΤΑ ΔΙΑΓΩΝ.</span></a>
   </li> 
 
<li class='has-sub'><a href='#' onClick="popupCenter('diagotmima.php')"><span>ΔΙΑΓ. ΤΜΗΜΑΤΟΣ</span></a>
   </li>  

 <li class='has-sub'><a href='#' onClick="popupCenter('diagoreport3.php')"><span>ΟΙ ΔΗΛΩΣΕΙΣ ΜΟΥ</span></a>
   </li>  

 <li class='has-sub'><a href='#' onClick="popupCenter('diagodiorthosi.php')"><span>ΕΠΙΣΤΡΟΦΗ ΔΙΑΓ. </span></a>
   </li> 
      
<li class='has-sub'><a href='#' onClick="popupCenter('diagodel.php')"><span>ΔΙΑΓΡΑΦΗ ΔΙΑΓ.</span></a>
   </li> 
                  
<li class='has-sub'><a href='#'><span><?php echo "Date: ".date('d-m-Y');?> </span></a>
   </li>  
</div>

</head>
<body>

<?php

require("dbconnect.php");
$usr = $_SESSION['usr'];
$tetramino=$_SESSION['tetramino'];
$xronia=$_SESSION['xronia'];
$gym=$_SESSION['gym'];
$dt= date("Y-m-d h:i:sa");

$strenc2= $_GET['data'];
$arr = unserialize(urldecode($strenc2));
$math=$arr[1]  ;
$period =$arr[2] ;
$day=$arr[3];
$thisdp=$day.$period; 

$fl1=mb_substr($math,0,1);
$fl2=mb_substr($math,0,3);
$flagtaxi=$fl1;

//******************************************************************
$_SESSION['flagtaxi']=$flagtaxi;
//////////////////////////////////////////
$dates=rangeWeek($dt);
$startdate=$dates['start'];
$enddate=$dates['end'];
$q = mysqli_query($con,"SELECT class FROM declarations WHERE class='$math' and (testdate>='$startdate' and testdate<= '$enddate')");
$cc = mysqli_num_rows($q);

 function rangeWeek($datestr) 
 {
    date_default_timezone_set(date_default_timezone_get());
    $dt = strtotime($datestr);
    $res['start'] = date('N', $dt)==1 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('last monday', $dt));
    $res['end'] = date('N', $dt)==7 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('next sunday', $dt));
    return $res;
 }
//.......... get the subject
$q = $dbcon->prepare("SELECT id FROM timetable WHERE pmp = ?");
$q->execute([$usr]);
$row=$q->fetch(PDO::FETCH_ASSOC);
$id=$row['id']+1;

$q = mysqli_query($con,"SELECT ".$thisdp." FROM timetable WHERE id = '$id'");
$row = mysqli_fetch_array($q);
$d1=$row["$thisdp"];
$ar=explode("/",$d1);
$subject = $ar[1];

$qt=mysqli_query($con,"select distinct tmima from allstudentinfo order by tmima asc" );
$tmimata=array();
$i=0;
while($rowt=mysqli_fetch_array($qt))
{
	$i++;
	$tmimata[$i]=$rowt['tmima'];
}
$tmin=$tmimata[1];
$tmax=$tmimata[$i];
//................................
?>

<form  action="diagonisma3.php" method="post">
<table>
<tr>
<td>
<fieldset class='fx' >
<legend > ΣΤΟΙΧΕΙΑ ΔΙΑΓΩΝΙΣΜΑΤΟΣ</legend>
<br > 
Ημερομηνία Δήλωσης :<input type="text"  name ='dt' size='15' value= <?php echo date('d-m-Y', strtotime($dt)); ?> readonly='readonly'>
&nbsp;&nbsp;&nbspΤμήμα/Συνδιδασκαλία:<input type='text' name='mathx' size = '20' value=<?php echo $math; ?> readonly='readonly'/><br >
<br>Μάθημα :<input type='text' name='subject' size = '20' value="<?php echo $subject; ?>" readonly='readonly'/>
&nbsp;&nbsp;&nbspΘέμα:<input type='text' name='themax' size = '50' value='<?php echo $thema; ?>' required='required'  /><br>
<br>Διάρκεια:<input type='text' name='duration' size = '2' value='' required='required' /> λεπτά
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbspΑπό περίοδο :<input type='text' name='perapo' size = '3' value="<?php echo $period; ?>" /> 
 μέχρι:<input type='text' name='permexri' size ='3' value="<?php echo $period; ?>" />    
<br >
</fieldset>
</td>
<td>
<?php 
echo "<b>".'ΕΠΕΞΗΓΗΣΗ ΧΡΩΜΑΤΩΝ ΣΤΑ ΚΕΛΙΑ'.'</b><BR>';
echo '<span style="color: red;">Κόκκινο: Δεν επιτρέπεται καμιά δήλωση </span>'."<br>";
echo '<span style="color: violet;">Βιολετί: Σύγκρουση μαθητών </span>'."<br>";
echo '<span style="color: DodgerBlue;">Μπλε: Υπάρχει εκδήλωση σε άλλη περίοδο. Επιτρέπεται η δήλωση </span>'."<br>";
echo '<span style="color: tomato;">Τοματί: Υπάρχει εκδήλωση στο τμήμα την επιλεγμένη περίοδο </span>'."<br>";
echo '<span style="color: orange;">Πορτοκαλί κείμενο: Δεν υπάρχει μάθημα στο επιλεγμένο τμήμα αυτή την ημερομηνία </span>'."<br>";
?>
</td>
</tr>
</table>
<?php

$day=array("Mon","Tue","Wed","Thu","Fri");
$sql = $dbcon->prepare("SELECT * FROM timetable WHERE pmp=?  " ); 
$sql->execute([$usr]);
$row=$sql->fetch(PDO::FETCH_ASSOC);
$c=$sql->rowCount();
   
$i=0;
while($i < 5)
{
	$per=1;     
	while ($per <= $gym) 
	{
	  $dayf=$day[$i].$per;
	  if ($math == $row["$dayf"]) 
	  {
		  $mera[$i]=$day[$i];
		  $thisdp=$dayf;
	  }  
	  $per++;
	}
	$i++;
}
echo "<p class='p1'>"."ΕΠΙΛΟΓΗ ΗΜΕΡΟΜΗΝΙΑΣ ΔΙΑΓΩΝΙΣΜΑΤΟΣ/ΑΣΚΗΣΗΣ"."</p> "; 

//...................................... display  week schedule.......


echo "<table class='mytable3' >";
//build 2d array with dates 
$diagarr=array();
$dd=1;
$startdate=strtotime("now");
/////////////////

while ($dd<=7) 
{
	$new=$startdate;
	$kk=1;
	for ($i=1;$i<=7;$i++)
	{
		$dave =	date("D", $new);
		$ddd=date("D, d F Y", $new);
		$tday=date('Y-m-d', strtotime($ddd));
		if ($dave<>'Sat' and $dave<>'Sun') 
		{
			$diagarr[$dd][$kk]=$tday;
			$kk++;
		}
		$new = strtotime("+7 day", $new);
	}// end for
	$startdate = strtotime("+1 day", $startdate);
	
	if ($dave<>'Sat' and $dave<>'Sun') 
		{
			$dd++;
		}
}// end while

$amiges=0;
if (in_array($math,$tmimata)) $amiges=1;  
$_SESSION['amiges']=$amiges; 
//////////
$dd=1;
$startdate=strtotime("now");
while ($dd<=5) 
{
echo "<tr>	";
for ($i=1;$i<=7;$i++)
{
	$tday=$diagarr[$dd][$i];
	$ddd=date("D, d F Y", strtotime($tday));
	$q2 = mysqli_query($con,"SELECT perapo,permexri,tmimafrom,tmimamexri,description FROM offperiods WHERE (datefrom <= '$tday') AND (dateto >= '$tday') ");
    $c2=mysqli_num_rows($q2);
    $ekd=0;
	if ($c2 > 0)
	{
		$row2=mysqli_fetch_array($q2);
		$papo=$row2['perapo'];
		$pmexri=$row2['permexri'];
		$tfrom=$row2['tmimafrom']; 
		$tf=array_search($tfrom,$tmimata);
		$tmexri=$row2['tmimamexri'];
		$tm=array_search($tmexri,$tmimata);
		$descr=$row2['description'];
		$l=$tm-$tf+1;
		$tf=$tf-1;
		$parr=array_slice($tmimata,$tf,$l);
		if(array_search($math,$parr) and ($period >=$papo and $period <=$pmexri)) $ekd=1;
	}
	
	
	
	$dis=15;
	$diag=0; 
	$color='#fffae6';
	$mes='';
	
		$q1 = mysqli_query($con,"SELECT testdate,class,teacher,subject FROM declarations WHERE testdate = '$tday' AND class='$math' ");
		$c3=mysqli_num_rows($q1); 
		$row1=mysqli_fetch_array($q1);
		$sub=$row1['subject'];
		
		
	//////////++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		
		
	if ($c3>0) // yparxei diagonisma stin idia taksi / amiges tmima
		  {		  	 
		   $mes="Τμήμα: ".$math."/".$sub;
		   $color='red';
		   $dis=1;  
	}
	else
	{
	if($amiges > 0)
	{	  
		if ($ekd > 0) // yparxei ekdilosi sto tmima afti tin hmerominia  
		{
			  $color='tomato';
			  $mes="**ΕΚΔ:".$descr." [".$math." ,".$period."περ ]";
			  $dis=2;
			  
		}
		if ($c2 >0 and $ekd==0)
		{
			  $color='DodgerBlue';
			  $mes="**ΕΚΔ:".$descr." [".$math." ,".$period."περ ]";
			  $dis=7;	
		}
	}
	else //gia syndidaskalies*****************************************
	{
		$mit=array();
		$q6=mysqli_query($con,"select s.mitroo from syndidaskalies as s, syndcodes as sc  where sc.synd='$math' and sc.syndid=s.syndid ");
		$i1=0;
			while($row6=mysqli_fetch_array($q6))
			{
				$i1++;
				$mit[$i1]=$row6['mitroo'];
			}
		$qc = mysqli_query($con,"SELECT class,teacher,subject FROM declarations WHERE testdate = '$tday'  ");
		$cc=mysqli_num_rows($qc); 
		if ($cc > 0)
		{
			
		   while($rowc=mysqli_fetch_array($qc))
		   {
		   	$cl=trim($rowc['class']);
		   	if (in_array($cl,$tmimata)) 
		   	{
		   	   $qlc=mysqli_query($con,"select mitroo from allstudentinfo where tmima='$cl'"); 
		   	}
		   	else
		   	{
		   		$qlc=mysqli_query($con,"select s.mitroo from syndidaskalies as s,syndcodes as sc where sc.synd='$cl' and s.syndid=sc.syndid order by s.mitroo");	
		   	}
		   	$kk=0;$mitr=array();
		   	
		   	while ($row7=mysqli_fetch_array($qlc))
		   	{
		   		$kk++;
		   		$mitr[$kk]=$row7['mitroo'];
		   	}
		   	$ca=count(array_intersect($mit,$mitr));
		   	if ( $ca > 0)
			{
				
				$color='Violet';
			    $mes="**Διαγώνισμα με κοινούς μαθητές** ".$cl."(".$ca.")";
				$dis=7;
			}				
		   }
		}
	
	
	
	
	
	
	
	
	
		$q4=mysqli_query($con," select code,perapo,permexri from ekdilosi where date='$tday'  ");
		if (mysqli_num_rows($q4) > 0)
		{
			$codex=$tm=$per=array();
			$i1=0;
			while($row4=mysqli_fetch_array($q4))
			{
				$i1++;
				$codex[$i1]=$row4['code'];
				$cd=$row4['code'];
				$per[1][$i1]=$row4['perapo'];
				$per[2][$i1]=$row4['permexri'];
				$q5=mysqli_query($con," select tmima,pmp from ekdilosistu where code='$cd' order by tmima ");
				while($row5=mysqli_fetch_array($q5))
				{
				   $i2++;
				   $tm[$i2]=$row5['pmp'];
				}
			}
			$tm=array_unique($tm);
			if (count(array_intersect($tm,$mit) > 0))
			{
				$color='MediumSeaGreen';
			    $mes="**Δραστηριότητα με μαθητές του τμήματος**";
				$dis=11;
			}				
		}
	  if ($c2 >0 and $ekd==0)
	  {
	    $r = mysqli_query($con,"SELECT a.tmima FROM allstudentinfo as a,syndidaskalies as s,syndcodes as sc 
		WHERE sc.synd = '$math' and sc.syndid=s.syndid and s.mitroo=a.mitroo ORDER BY a.tmima");
        $c=mysqli_num_rows($r);
		$k=0;$tm=array();$ekd=0;
		while ($row1=mysqli_fetch_array($r)) 
		    {
			   $k++;
			   $tm[$k]=$row1['tmima'];
		    }	 
		if(count(array_intersect($tm,$parr)) and ($period >=$papo and $period <=$pmexri)) $ekd=1;
		$color='DodgerBlue';
		  $mes="**ΕΚΔ:".$descr." [".$period." περ ]";
		if ($ekd > 0)
		{			
		  
	      $dis=5;
		}
		else
		{
			$dis=6;
		}
	  }
	}
	}//************************************************************************
	if ($dis < 6)	  
	{
	   echo "<pre>"."<td bgcolor=".$color."> ";
	   echo "<input type='checkbox' name='check' disabled value='$tday' />".$ddd."<br>";
	   echo "<br>"."<font color='#FFFFFF'> $mes </font>";
	   echo "</td>"."</pre>"; 
	}
	elseif ($dis < 10)
	{
	   echo "<pre>"."<td bgcolor=".$color."> ";
	   echo "<input type='checkbox' name='check' value='$tday' />".$ddd."<br>";
	   echo "<br>"."<font color='#FFFFFF'> $mes </font>";
	   echo "</td>"."</pre>";   
	   
	}
	elseif ($dis < 15)
	{
	   echo "<pre>"."<td bgcolor=".$color."> ";
	   echo "<input type='checkbox' name='check' value='$tday' />".$ddd."<br>";
	   echo "<br>"."<font color='#FFFFFF'> $mes </font>";
	   echo "</td>"."</pre>";   
	   
	}
	else 
	{
	   echo "<pre>"."<td bgcolor=".$color."> ";
	   $dave=date("D", strtotime($tday));
	   if (in_array($dave,$mera))
	   {
	     echo "<input type='checkbox' name='check' value='$tday' />";
	     echo "<font face='arial' size=' 2pt' >$ddd</font>"; 
	   }
	   else
	   {
		  echo "<input type='checkbox' name='check' value='$tday' disabled/>";
		  echo "<font face='arial' size=' 2pt' color='orange'>$ddd  </font>";  
	   }
	   echo "</td>"; 	
	}  
		      
	

echo "</td>";
}// end for

$dd++;
}// end while

echo "</tr>	";
echo "</table>";
?>
<br>
<div class="blink_me">ΜΕΤΑ ΤΗΝ ΕΠΙΛΟΓΗ ΗΜΕΡΟΜΗΝΙΑΣ ΠΡΕΠΕΙ ΝΑ ΓΙΝΕΙ ΕΛΕΓΧΟΣ ΗΜΕΡΟΜΗΝΙΑΣ</div>
<input type="text"  name = 'dt' HIDDEN value= <?php echo $dt; ?> > <br >
<fieldset class="fx" >
<input type="submit" name= "epilogi1" value="Έλεγχος Ημερομηνίας" class="sb" >
&nbsp&nbsp&nbsp
<input type="submit" name= "epilogix3" value='Δήλωση για συμφωνημένη ("καρφωτή") ημερομηνία' class="sb1" >
</fieldset>
</form>

<?php
?>
<a  href='diagonismanew.php'><button  type="button" class="sb1">Ακύρωση Δήλωσης</button></a>
<a  href='<?php echo $menu;?>'><button  type="button" class="sb1">Επιστροφή στο Μενού</button></a>
	
</body>
</html>