<?php
include "scrinit.php";
include "logo.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">
function closeit(val){
    window.opener.document.forms['myform'].elements['passed_value'].value=val;
    window.close(this);
}
</script>
</head>

<?php
require("dbconnect.php");
$name = $_SESSION['n'];
$tetramino=$_SESSION['tetramino'];
$xronia=$_SESSION['xronia'];
$amiges=$_SESSION['amiges'];
$dt= date("Y-m-d h:i:sa");

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;
}

if (isset($_POST['epilogi1'])) 
{
	$perapo=$_POST['perapo'];
	$permexri=$_POST['permexri'];
	$math=$_POST['mathx'];
    $duration=$_POST['duration'];
    $subject=$_POST['subject'];
	$thema=$_POST['themax']; 
   	$check=$_POST['check'];
	
if ($check < $dt){ 
  
   echo '<script type="text/javascript">';
   echo 'alert("ΠΡΕΠΕΙ ΑΠΑΡΑΙΤΗΤΑ ΝΑ ΕΠΙΛΕΞΕΤΕ ΗΜΕΡΟΜΗΝΙΑ ΓΙΑ ΔΙΑΓΩΝΙΣΜΑ")';
   echo '</script>';	
   $time = 1; //Time (in seconds) to wait. 
   $url = "diagonismanew.php"; //Location to send to. 
   header("Refresh: $time; url=$url");}
   else
   {
   
	$perapo=$_POST['perapo'];
	$permexri=$_POST['permexri'];
    $testd=date('Y-m-d', strtotime($check));
    $testdshow=date('d-m-Y', strtotime($check));
///////////////////////////////////////////////////
$dates=rangeWeek($check);
$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);

$finsert=0;
$student=array();
//........................... prepare the array with student names 
if ($amiges == 0)
{
  $result = mysqli_query($con,"SELECT s.mitroo FROM syndidaskalies as s,syndcodes as sc 
  WHERE sc.synd = '$math' and sc.syndid=s.syndid ORDER BY s.mitroo");
  $crow1=mysqli_num_rows($result);
  $i=1;
  while ($row1=mysqli_fetch_array($result)) 
  {
  $student[$i]=$row1['mitroo'];
  $i++;
  }
}
else
{
  $result = mysqli_query($con,"SELECT mitroo,tmima FROM allstudentinfo WHERE tmima = '$math' ORDER BY mitroo");	
  $crow1=mysqli_num_rows($result);
  $i=1;
  while ($row1=mysqli_fetch_array($result)) 
     {
         $student[$i]=$row1['mitroo'];
         $i++;
     }
}

//..............

$q1 = mysqli_query($con,"SELECT d.testdate,d.class,d.teacher,d.subject,t.Name FROM declarations as d,teachers as t
 WHERE testdate = '$testd' and d.teacher=t.PMP");
$c1=mysqli_num_rows($q1);

if ($c1==0)
{  $finsert=1;  }      // den exei diagonismata katholou
else
{
 $i=1;
  while ($row1=mysqli_fetch_array($q1)) 
	{
	  $class[$i]=$row1['class'];
	  $teach[$i]=$row1['Name'];
	  $i++;
	}
 $plithos=count($class);
 $i=1;
	 $flag=0;
	 //........ prepare student list of other lesson
	 while ($i<= $plithos) 
	 {
	 $math1=$class[$i];$teach1=$teach[$i];
	 
		$result = mysqli_query($con,"SELECT s.mitroo FROM syndidaskalies as s,syndcodes as sc 
		WHERE sc.synd = '$math1' and sc.syndid=s.syndid ORDER BY s.mitroo");
        $crow1=mysqli_num_rows($result);
		if ($crow1 > 0){
		   $student1=array();
		  $k=1;
		  while ($row1=mysqli_fetch_array($result)) 
		    {
			   $student1[$k]=$row1['mitroo'];
		       $k++;
		    }	 
		}
		else
		{	
          $result = mysqli_query($con,"SELECT mitroo,tmima FROM allstudentinfo WHERE tmima = '$math1' ORDER BY mitroo");	
          $crow1=mysqli_num_rows($result);
		  $student1=array();
		  $j=1;
          while ($row1=mysqli_fetch_array($result)) 
            {
                $student1[$j]=$row1['mitroo'];
                $j++;
            }
         }
		 //................end prepare $student1 array ........
         $celement = array_intersect($student,$student1);
		 $ll=count($celement);	 
		
		 if ($ll > 0) 
		 {
			 echo "<p> ΥΠΑΡΧΕΙ ΣΥΓΚΡΟΥΣΗ ΜΕ :<font color=red>".$math1."</font>".' ('.$teach1.' )'." </p>";
             echo "<table class='myTable' >
			 <tr style='border:1px solid black' bgcolor='#d6ff73'  >
             <th>Μητρώο</th>
             <th>Μαθητής / τρια</th>
             <th>Τμήμα</th>";
			 $cnt=1;
			 foreach ($celement as $value) {
			   $result = mysqli_query($con,"SELECT eponimo,onoma,tmima FROM allstudentinfo WHERE mitroo='$value' ");
               $row = mysqli_fetch_array($result);
			   echo "<tr>";
               echo "<td>".$value."</td>"."<td>".$row['eponimo']." ".$row['onoma']."</td>"."<td>".$row['tmima']."</td>";	
			   echo "</tr>";			   
	  	       $cnt++;}
			 echo "</table>";
			 $finsert=0;
			 $flag=1;
		 }
	 $i++;
	 }
	 if ($flag==0){  $finsert=1;   }	
}
//*************** drastiriotita//////////////////////
$q2 = mysqli_query($con,"SELECT perapo,permexri,date,code,onoma FROM ekdilosi 
WHERE date = '$testd' and (perapo <='$perapo' AND permexri >='$perapo')");
$c2=mysqli_num_rows($q2);
if ($c2 > 0)
{
  $row2=mysqli_fetch_array($q2);
  $descr=$row2['onoma'];
  $code=$row2['code'];
  $q3 = mysqli_query($con,"SELECT pmp,tmima FROM ekdilosistu where code='$code' ");
  $student1=array();
  $j=1;
  while($row3=mysqli_fetch_array($q3))
  {
	$student1[$j]=$row3['pmp'];
    $j++;  
  }	  
	//................end prepare $student1 array ........
	 $celement = array_intersect($student,$student1);
	 $ll=count($celement);	 
	
	 if ($ll > 0) 
	 {
		 echo "<p> ΥΠΑΡΧΕΙ ΣΥΓΚΡΟΥΣΗ ΜΕ ΔΡΑΣΤΗΡΙΟΤΗΤΑ:<font color=red>".$descr."</font>"." </p>";
		 echo "<table class='myTable' >
		 <tr style='border:1px solid black' bgcolor='#d6ff73'  >
		 <th>Μητρώο</th>
		 <th>Μαθητής / τρια</th>
		 <th>Τμήμα</th>";
		 $cnt=1;
		 foreach ($celement as $value) {
		   $result = mysqli_query($con,"SELECT eponimo,onoma,tmima FROM allstudentinfo WHERE mitroo='$value' ");
		   $row = mysqli_fetch_array($result);
		   echo "<tr>";
		   echo "<td>".$value."</td>"."<td>".$row['eponimo']." ".$row['onoma']."</td>"."<td>".$row['tmima']."</td>";	
		   echo "</tr>";			   
		   $cnt++;}
		 echo "</table>";
		echo "<br>";
	 } 
}

$q2 = mysqli_query($con,"SELECT perapo,permexri,tmimafrom,tmimamexri,description FROM offperiods WHERE (datefrom <= '$testd') AND (dateto >= '$testd') ");
$c2=mysqli_num_rows($q2);
$row2=mysqli_fetch_array($q2);
$papo=$row2['perapo'];
$pmexri=$row2['permexri'];
$tfrom=$row2['tmimafrom'];$tmexri=$row2['tmimamexri'];$descr=$row2['description'];

if ($c2 > 0) {
	if(($math <= $tmexri AND $math >= $tfrom) AND ($perapo>=$papo AND $perapo <=$pmexri)){
	echo "<br>";
	echo "<b>"." ΥΠΑΡΧΕΙ ΣΥΓΚΡΟΥΣΗ ΠΕΡΙΟΔΩΝ ΜΕ ΠΡΟΓΡΑΜΑΜΤΙΣΜΕΝΗ ΕΚΔΗΛΩΣΗ "."</b>";
$finsert=0;}}

if ($finsert==1)
{
$_SESSION['karfi']=" ";
?>
<form  method="post" action="diagonismain.php">
<fieldset class="fx" >
<legend class='l1'> ΕΠΙΒΕΒΑΙΩΣΗ ΣΤΟΙΧΕΙΩΝ ΔΙΑΓΩΝΙΣΜΑΤΟΣ</legend>
<br > Ημερομηνία Δήλωσης:<input type="text"  name = 'dt' value= <?php echo date('d-m-Y', strtotime($dt)); ?> readonly="readonly"> 
&nbsp;&nbsp;&nbspΗμερομηνία Διαγωνίσματος : <input type="text"  name = 'testd'  value= <?php echo date('d-m-Y', strtotime($testd)); ?> readonly="readonly"><br >
<br >Τμήμα/Συνδιδασκαλία :<input type='text' name='mathx' size = '20' value="<?php echo $math; ?>" readonly="readonly"/>
ΜΑΘΗΜΑ  :<input type='text' name='subject' size = '20' value="<?php echo $subject; ?>" readonly="readonly"/><br >
<br>ΘΕΜΑ:<input type='text' name='themax' size = '40' value="<?php echo $thema; ?>" readonly="readonly"/><br >
<br >Διάρκεια  :<input type='text' name='duration' size = '3' value=<?php echo $duration; ?> readonly='readonly'/> λεπτά
&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbspΑπό περίοδο :<input type='text' name='perapo' size = '3' value="<?php echo $perapo; ?>" /> 
 μέχρι :<input type='text' name='permexri' size = '3' value="<?php echo $permexri; ?>" readonly='readonly'/> 
<input type="text"  name = 'dt' HIDDEN value= <?php echo $dt; ?> > <br >   
<br ><br >

</fieldset>

<br ><br >
<input type="submit"  name ="epilogi3" value="Αποδοχή και Καταχώρηση Στοιχείων" class="sb">
<input type="button" class="bt3" value="Ακύρωση και Επιστροφή" onclick="window.location.href='diagonismanew.php'" />
</form>

<?php
}
else
{echo "<br>";
//echo  "<a href=\"diagonisma1.php?pmp=".$mitr."\"> <button> Επιστροφή". "</a>";}
echo  "<a href=\"diagonismanew.php\">  <button> Η δήλωση ήταν ανεπιτυχής - Επιστροφή</button>". "</a>";}
}}


if (isset($_POST['epilogix3'])) 
{
    $perapo=$_POST['perapo'];
	$permexri=$_POST['permexri'];
	$math=$_POST['mathx'];
	$thema=$_POST['themax'];
    $duration=$_POST['duration'];
    $subject=$_POST['subject'];
    $_SESSION['karfi']="K";

?>
<form  method="post" action="diagonismain.php">
<fieldset class='fx'>
<legend class='l1'> ΕΠΙΒΕΒΑΙΩΣΗ ΣΤΟΙΧΕΙΩΝ ΔΙΑΓΩΝΙΣΜΑΤΟΣ</legend>
<input type="hidden"  name = 'dt' value= <?php echo date('d-m-Y', strtotime($dt)); ?> > <br >
<br >Επέλεξε ημερομηνία Διαγωνίσματος<input type="date"  name = 'testd' readonly="readonly" ><br >
<br >Τμήμα/Συνδιδασκαλία :<input type='text' name='mathx' size = '20' value="<?php echo $math; ?>" readonly="readonly"/>
ΜΑΘΗΜΑ  :<input type='text' name='subject' size = '20' value="<?php echo $subject; ?>" readonly="readonly"/><br >
<br>ΘΕΜΑ:<input type='text' name='themax' size = '40' value="<?php echo $thema; ?>" readonly="readonly"/><br >
<br >Διάρκεια  :<input type='text' name='duration' size = '3' value=<?php echo $duration; ?> /> λεπτά
&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbspΑπό περίοδο :<input type='text' name='perapo' size = '3' value="<?php echo $perapo; ?>" /> 
 μέχρι :<input type='text' name='permexri' size = '3' value="<?php echo $permexri; ?>" /> 
<br ><br >

</fieldset>
<br ><br >
<input type="submit"  name ="epilogi3" value="Αποδοχή και Καταχώρηση Στοιχείων" class="sb">
<input type="button" class="bt3" value="Ακύρωση και Επιστροφή" onclick="window.location.href='diagonismanew.php'" />
</form>


<?php
}
?>	
</body>
</html>