<?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 mitroo,bathmos,sxolio,biblio,tetradio,ergasia,symmetoxi FROM proforikos WHERE mathima=? and pmpteacher=? and date=? and subject=? ");
$result->execute([$lesson,$usr,$dt,$sb]);
$crow2 = $result->rowCount();

$chk1=$chk2=$chk3=$chk4=array();
$c=count($mt);
if ($crow2 >0)
{
 while ($row = $result->fetch(PDO::FETCH_ASSOC))
 {
 $mit=$row['mitroo'];
 for ($j=1;$j<=$c;$j++)
 {
	if ($mit==$mt[$j])
	{
		$bt[$j]=$row['bathmos'];
		$sxolia[$j]=$row['sxolio']." ".$row['onoma'];
		$bib[$j]=$row['biblio'];if ($bib[$j]==1) $chk1[$j]='checked';
		$tet[$j]=$row['tetradio'];if ($tet[$j]==1) $chk2[$j]='checked';
		$sym[$j]=$row['symmetoxi'];if ($sym[$j]==1) $chk4[$j]='checked';
		$erg[$j]=$row['ergasia'];if ($erg[$j]==1) $chk3[$j]='checked';
	}
 }
}
}
?>
<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>
<th colspan=4> ΔΕΝ ΕΦΕΡΕ/ΕΚΑΝΕ </th>
</tr>
<tr>
<th colspan=4></th>
<th colspan=2 align=center>".date('d-m-Y', strtotime($dt))."</th>
<th>Βιβλίο</th>
<th>Τετράδιο</th>
<th>Εργασίες</th>
<th>Συμμετοχή</th>
";

$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' readonly>" . "</td>";
 echo "<td>". "<input type='text' name='tmim' value='$tmim[$i]' size='3' readonly>" . "</td>";
 echo "<td>". "<input type='text' name='epo[]' value='$epo[$i]' size='30' readonly>" . "</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='40'>" ."</td>";
 echo "<td ALIGN=center>"."<input type='checkbox' name='bibx[$i]' value='1' $chk1[$i]>"."</td>"; 
 echo "<td ALIGN=center>"."<input type='checkbox' name='tetx[$i]' value='1' $chk2[$i]>"."</td>";
 echo "<td ALIGN=center>"."<input type='checkbox' name='ergx[$i]' value='1' $chk3[$i]>"."</td>";
 echo "<td ALIGN=center>"."<input type='checkbox' name='symx[$i]' value='1' $chk4[$i]>"."</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'])) 
{
   $bib=$tet=$sym=$erg=array();
   $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'];
   $bib=$_POST['bibx'];
   $tet=$_POST['tetx'];
   $sym=$_POST['symx'];
   $erg=$_POST['ergx'];
   $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,biblio,tetradio,ergasia,symmetoxi)
   VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ");
   $k=1;
   for ($i=0; $i<$c; $i++) 
   {
   if(!empty($pb[$i]) or !empty($sxolio[$i]) or !empty($bib[$k]) or !empty($tet[$k]) or !empty($sym[$k]) or !empty($erg[$k]))
   {
	 if (empty($pb[$i])) $pb[$i]=0;if (empty($bib[$k])) $bib[$k]=0;if (empty($tet[$k])) $tet[$k]=0;
	 if (empty($sym[$k])) $sym[$k]=0;if (empty($erg[$k])) $erg[$k]=0;
	 $sq1->execute([$usr,$mti[$i],$dt,$lesson,$sub,$pb[$i],$sxolio[$i],$tetramino,$bib[$k],$tet[$k],$erg[$k],$sym[$k]]);
   }
   $k++;
   }
  If ($i > 0 )
   {
     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>