<?php
include "scrinit.php";
?>
<head>
<html xmlns="https://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:2px;font-size:10pt; }
.bt1 {
  color: green;
  border-radius: 5px;
  font-size: 15px;
  font-weight: bold;
  border: 1px solid ;
  padding:4px;
}
<p style="font-size:10px;"></p>
</style>
<script type="text/javascript">
    function do_this(){

        var checkboxes = document.getElementsByName('att[]');
        var button = document.getElementById('toggle');

        if(button.value == 'Επιλογή όλων των μαθητών/τριών'){
            for (var i in checkboxes){
                checkboxes[i].checked = 'FALSE';
            }
            button.value = 'Αποεπιλογή όλων των μαθητών/τριών'
        }else{
            for (var i in checkboxes){
                checkboxes[i].checked = '';
            }
            button.value = 'Επιλογή όλων των μαθητών/τριών';
        }
    }
</script>

</head>
<body>
<?php
require "dbconnect.php"; 
$dt=$_SESSION['dt'];
$name = $_SESSION['n'];
$gym=$_SESSION['gym'];
$km=$_GET['kathmath'];
$arr = explode("/", $km);
$lesson = trim($arr[1]);
$usr = trim($arr[0]);
$sb=trim($arr[2]);
$day = date('D', strtotime($dt));  

$less=$lesson;
$result = $dbcon->prepare("SELECT mitroo,eponimo,onoma FROM allstudentinfo WHERE tmima=? ORDER BY eponimo,onoma");
$result->execute([$lesson]);
$crow1 = $result->rowCount();
if ($crow1 > 0) 
{
  $i=1;
  while ($row = $result->fetch(PDO::FETCH_ASSOC))
     {
	  $mt[$i]=$row['mitroo'];
	  $epo[$i]=$row['eponimo']." ".$row['onoma'];
	  $tmim[$i]=$lesson;
	  $i++;
	  }
}
else // den eine amiges tmima alla syndidaskalia
{
  $result = $dbcon->prepare("SELECT distinct sc.synd,sy.mitroo,al.eponimo,al.onoma,al.tmima FROM 
  syndidaskalies as sy,allstudentinfo as al,syndcodes as sc WHERE sc.syndid=sy.syndid and trim(sc.synd) = ? AND sy.mitroo=al.mitroo 
  ORDER BY al.eponimo,al.onoma");
  $result->execute([$lesson]);
  $crow1 = $result->rowCount();
  if ($crow1 > 0) 
  {
	$cr=1;
	while ($row = $result->fetch(PDO::FETCH_ASSOC))
	{	
		$mt[$cr]=$row['mitroo'];
	    $epo[$cr]=$row['eponimo']." ".$row['onoma'];	
		$tmim[$cr]=$row['tmima'];
		$cr++;}
  }
  else
  {
    	$apr=$mt=$epo=$tmim=$apr=array();
		$pos=strpos($lesson,"Α");
		$les=substr($lesson,$pos,2);
		$pos=$pos+2;
		$les1=substr($lesson,$pos,1);	
		$les=trim($les)."0".trim($les1);
		$les=ltrim(rtrim($les));
		$result = $dbcon->prepare("SELECT mitroo,eponimo,onoma FROM allstudentinfo WHERE tmima=? ORDER BY eponimo,onoma");
		$result->execute([$les]);
		$crow1 = $result->rowCount();
		$i=1;
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
		 $mt[$i]=$row['mitroo'];
		 $epo[$i]=$row['eponimo']." ".$row['onoma'];
		 $tmim[$i]=$les;
		 $i++;
		}
  }		
}
$result = $dbcon->prepare("SELECT bathmos,sxolio FROM proforikos WHERE mathima=? and pmpteacher=? and date=? and subject=? ");
$result->execute([$lesson,$usr,$dt,$sb]);
$crow2 = $result->rowCount();
$i=1;
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
 $bt[$i]=$row['bathmos'];
 $sxolia[$i]=$row['sxolio']." ".$row['onoma'];
 $i++;
}
?>
<form  name="prof" method="post">
<br><h4>ΠΡΟΦΟΡΙΚΗ ΑΞΙΟΛΟΓΗΣΗ ΓΙΑ:<?php echo $lesson." - ".$sb ?></h4>	<?php
echo "Ημερομηνία:".date('d-m-Y', strtotime($dt));
echo "<br>";
echo "<table class='myTable' >
<tr style='border:1px solid black' >
<tr bgcolor='#66CC99'>
<th>α/α</th>
<th>Α.Μ</th>
<th>Τμήμα</th>
<th>Όνοματεπώνυμο</th>
<th>Βαθμός</th>
<th>Σχόλια</th>
</tr>";
echo "<tr>";
echo "<td colspan=4>".""."</td>";
echo "<td colspan=2 align=center>".date('d-m-Y', strtotime($dt))."</td>";
$i=1;
while($i<=$crow1)
{
 //////////////////////////////// display apousies periodon///////////////////////
 $pro=$bt[$i];
 echo "<tr>";
 echo "<td>" .$i;
 echo "<td>". "<input type='text' name='mt[]' value='$mt[$i]' size='3' >" . "</td>";
 echo "<td>". "<input type='text' name='tmim' value='$tmim[$i]' size='3' >" . "</td>";
 echo "<td>". "<input type='text' name='epo[]' value='$epo[$i]' size='30' >" . "</td>"; 
 echo "<td>". "<input type='text' name='pbathmos[]' value='$bt[$i]' size='6'  >" ."</td>";
 echo "<td>". "<input type='text' name='sxol[]' value='$sxolia[$i]' size='50'>" ."</td>";
 echo "</tr>";
 $i++;
}
echo "</table>";

$tm=$usr."/".$lesson."/".$sb;

?>
<input type="hidden" name="tm1" value='<?php echo $tm; ?>' >
<br>
<input type="Submit" name="Submitpro" value="Καταχώρηση Αξιολόγησης" class="sb">
</form> 

<?php
if (isset($_POST['Submitpro'])) 
{
   
   $tm=$_POST['tm1']; 
   $arr = explode("/", $tm);
   $usr = ltrim(rtrim($arr[0]));
   $lesson = ltrim(rtrim($arr[1]));
   $sub=ltrim(rtrim($arr[2]));
   $tetramino=$_SESSION['tetramino'];
   $sxolio=$_POST['sxol'];
   $mti = $_POST['mt'];
   $tmima = $_POST['tmim'];
   $pb =$_POST['pbathmos'];
   $dt=$_SESSION['dt'];
   $c = count($mti);
   
   $sql=$dbcon->prepare("DELETE FROM proforikos WHERE date=? AND pmpteacher=? AND mathima=? AND subject='$sub' ");
   $sql->execute([$dt,$usr,$lesson]);
	
   $sq1=$dbcon->prepare("INSERT INTO proforikos (pmpteacher,mitroo,date,mathima,subject,bathmos,sxolio,tetramino) VALUES (?,?,?,?,?,?,?,?) ");
   for ($i=0; $i<$c; $i++) 
   {
	 $sq1->execute([$usr,$mti[$i],$dt,$lesson,$sub,$pb[$i],$sxolio[$i],$tetramino]);
	 
   }
   If ($i==$c)
   {
     echo "<script>
     alert('Η καταχώριση έγινε με επιτυχία!');
     </script>";
	 echo  "<script type='text/javascript'>";
echo "window.close();";
echo "</script>";
   }
}
?>
<input type="button" name="epistrofi" value="Επιστροφή" onClick="self.close()"> 
</body>
</html>