<?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" src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
   function prsintx1(codx) 
	{
        window.open("prsintall.php?data="+codx, "_blank", "toolbar=yes, scrollbars=yes, resizable=yes, top=100, left=100, width=800, height=800");
    } 
	
	function prsintx(cod) 
	{
        window.open("prsint.php?data="+cod, "_blank", "toolbar=yes, scrollbars=yes, resizable=yes, top=100, left=100, width=800, height=800");
    }

	
</script>
<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:2px;font-size:11px; }
<p style="font-size:09px;"></p>
</style>

<?php
require "dbconnect.php"; 
$dt=$_SESSION['dt'];

$math=$_GET['mathid'];
$arr = explode("/", $math);
$tmima = trim($arr[0]);
$subject = trim($arr[1]);
$usr=trim($arr[2]);
//find aithousa
$q=mysqli_query($con,"select * from timetable where pmp='$usr' and (id % 2) = 1");
$row=mysqli_fetch_array($q,MYSQLI_NUM);
$qx=mysqli_query($con,"select * from timetable where pmp='$usr' and (id % 2) = 0");
$rowx=mysqli_fetch_array($qx,MYSQLI_NUM);
$i=0;
foreach ($row as $tm)
{
	
	if ($tm==$tmima)
	{
		$arr=explode("/",$rowx[$i]);
		$ls=trim($arr[1]);
		if ($ls==trim($subject))
		{
			$room=$arr[0];
			break;
		}
	}
	$i++;
}

//
echo "<p class='p1'>". "ΣΧΕΔΙΑΓΡΑΜΜΑ ΤΜΗΜΑΤΟΣ: "."<b>".$tmima."/".$subject."</b></p>";
echo "<p class='p2'>". "ΑΙΘΟΥΣΑ: "."<b>".$room."</b></p>";
$q=mysqli_query($con,"select code from roomcode where room='$room' and teacher='$usr' and tmima='$tmima' and subject='$subject'");
$row=mysqli_fetch_array($q);
$n=mysqli_num_rows($q);
if (!empty($row) or $n > 0) 
{
	$code=$row[code];
	$qr=mysqli_query($con,"select * from roomsit where code='$code'");
	
	while($rowr=mysqli_fetch_array($qr))
	{
		$mn=$rowr['student'];
		$i=$rowr['posx'];
		$j=$rowr['posy'];
		$sxediagr[$i][$j]=$mn;
	}
}
else
{	
	$q=mysqli_query($con,"select code from roomcode where room='$room' and tmima='$tmima' ");
	$row=mysqli_fetch_array($q);
	$n=mysqli_num_rows($q);
	if (!empty($row)) 
	{
		$code=$row[code];
		$qr=mysqli_query($con,"select * from roomsit where code='$code'");
	
		while($rowr=mysqli_fetch_array($qr))
		{
			$mn=$rowr['student'];
			$i=$rowr['posx'];
			$j=$rowr['posy'];
			$sxediagr[$i][$j]=$mn;
		}
	}
	else
	{
		$q = mysqli_query($con,"SELECT MAX(code) FROM roomcode ");
		$row = mysqli_fetch_array($q);
		$code = $row['MAX(code)'] + 1;
	}
}
	
$pl=5;
$lg=7;
?>
<pre> 
Με τριπλό κλικ στο όνομα του μαθητή, ή επιλέγοντας κανονικά, το επιλέγετε και μετά κρατώντας πατημένο το αριστερό κουμπί του ποντικιού
το μεταφέρετε και το αφήνετε στο κατάλληλο κουτί. Η μεταφορά μπορεί να γίνει και με copy/paste. Αν συνάδελφος έχει ήδη περάσει το τμήμα
στη συγκεκριμένη αίθουσα θα εμφανιστεί η διάταξη. Κάντε τις δικές σας αλλαγές αν χρειάζεται και καταχωρείστε  </pre>
 
<table><tr><div>
<td>
<?php
echo "<table class='myTable3' >
<tr style='border:4px solid black;padding:10px;font-size:16px'  >
<tr>
<th colspan='3'><th> </th><th colspan='3'></th>
</tr>";	
?><form  method="post"  ><?php
for($i=1;$i<=$pl;$i++)
{
   echo "<tr>";
   for ($jk=1;$jk<=$lg;$jk++)
   {
        if($jk==4) 
		{
			echo "<td>"."   "."</td>";
		}
        else
        {
		  if($jk > 4)
		  {
			  $j=$jk-1;
		  }else {$j=$jk;}
          echo "<td><br><input type='text' name='sxed[$i][$j]' value='".$sxediagr[$i][$j]."' ><br><br></td>";
		}
   }
   echo "</tr>";
}
?>
<input type='hidden' name='coden' value='<?php echo $code;?>' >
</table>
<br>

<table align="left" border="5" cellpadding="10" cellspacing="2">
<tr><td><?php echo "ΕΔΡΑ"?></td>
</tr></table></td>
</div>
<td>
<div>
<?php
$result = mysqli_query($con,"SELECT mitroo,eponimo,onoma,tmima FROM allstudentinfo 
WHERE trim(tmima)='$tmima' ORDER BY eponimo,onoma");
if(mysqli_num_rows($result) > 0)
{
    $cr=0;
	while($row=mysqli_fetch_array($result))
	{	
		$cr++;
		$mt[$cr]=$row['mitroo'];
	    $epo[$cr]=$row['eponimo']." ".$row['onoma'];	
		$tmim[$cr]=$row['tmima'];
	}	
}	
else
{	
$result = mysqli_query($con,"SELECT  s.mitroo FROM syndidaskalies as s,syndcodes as sc 
          WHERE sc.synd='$tmima' and sc.syndid = s.syndid ORDER BY s.mitroo");	
    $cr=0;
	while($row=mysqli_fetch_array($result))
	{	
		$cr++;
		$mt[$cr]=$row['mitroo'];
		$res = mysqli_query($con,"SELECT eponimo,onoma,tmima,kmathiti FROM allstudentinfo 
				WHERE mitroo='$mt[$cr]' ");
		$rows=mysqli_fetch_array($res);	
	    $epo[$cr]=$rows['eponimo']." ".$rows['onoma'];	
		$tmim[$cr]=$rows['tmima'];
	}
}
echo "<table class='myTable3' align='top' >
<tr style='border:1px solid black;font-size:12px'  >
<tr>
<th>Α/Α</th>
<th>Τμήμα</th>
<th>Μητρώο</th>
<th>Όνομα</th>
</tr>";	
$i=1;
while($i<=$cr)
{
	 echo "<tr>";
	 echo "<td>" .$i."</td>";
	 echo "<td>". $tmim[$i]. "</td>";
	 echo "<td>". $mt[$i]. "</td>";
	 echo "<td>". $epo[$i]. "</td>";
	 echo "</tr>";
     $i++;
}
echo "</table>";

?></td></tr></table>
</div>
<br><br>
<input type="Submit" name="sxedr" value="ΚΑΤΑΧΩΡΗΣΗ ΣΧΕΔΙΑΓΡΑΜΜΑΤΟΣ" >
</form>	

<?php

if (isset($_POST['sxedr'])) 
{
		$sxed=$_POST['sxed'];
		$q="insert into roomcode (code,room,teacher,tmima,subject) values ('$code','$room','$usr','$tmima','$subject')";
		if (!mysqli_query($con, $q)) 
		{
			mysqli_query($con,"delete from roomsit where code='$code'");
		}
        
		$i=0;
		foreach ($sxed as $key => $sit)
		{
			$i++;
			$j=0;
			foreach ($sit as $st)
			{
				$j++;
				if(!empty($st) or $st > '')
				{
					$posx=$i;
					$posy=$j;
					$q="insert into roomsit (code,student,posx,posy) values ('$code','$st','$posx','$posy')";
					if (!mysqli_query($con, $q)) 
					{
						echo "Error: " . $q . "<br>" . mysqli_error($con);
					}
				}
			}
		}
		
			echo "<script>
			alert('Η καταχώριση έγινε με επιτυχία!');
			 </script>";
			 echo  "<script type='text/javascript'>";
			 echo "window.close();";
			 echo "</script>";
			 $time = 1; //Time (in seconds) to wait. 
			 $url = "sxediagramma.php"; //Location to send to. 
			 header("Refresh: $time; url=$url");	
}

?>
<input type="button" name="epistrofi" value="Επιστροφή" onClick="self.close()"> 
</body>
</html>