<?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" />
<title>Βαθμολόγιο Καθηγητή</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".floatNumberField").change(function() {
            $(this).val(parseFloat($(this).val()).toFixed(2));
        });
    });

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

function submitform()
{
    document.forms["bath"].submit();
}

function pop_up(url){
window.open(url,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=800,height=800,directories=no,location=no') 
}

</script>

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

		$(document).ready(function() {

				$('input:not(:button,:submit),textarea,select').change(function () {
					window.onbeforeunload = function () {
						if (prompt == true) 
							return 'Δεν έχετε αποθηκεύσει τους βαθμούς που έχετε καταχωρίσει';
						}
					});


				$('input:submit').click(function(e) {
					prompt = false;
					});
	    });
		
</script>

<style type="text/css">
input.right{  text-align:right;  }
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:1px;font-size:12pt; }
.fieldset
{
    width:900px;
	background: #F5F9F0;
	border:2px solid green;
    -moz-border-radius:4px;
    -webkit-border-radius:4px;	
    border-radius:4px;	
}
</style>

</head>
  
<body>
<p class='p1'>ΣΥΝΟΛΙΚΗ ΒΑΘΜΟΛΟΓΙΑ ΜΑΘΗΜΑΤΟΣ</p>
<?php
require "dbconnect.php"; 
$dt=$_SESSION['dt'];
$teacher=$_SESSION['usr'];
$cd=$_GET['code'];
echo $cd;
if (!empty($cd))
{
    $cc=explode("/",$_GET['code']);
	$codea=$cc[0];
	$codeb=$cc[1];
	$q1=mysqli_query($con, "select * from bathmoposo WHERE id='$codea'");
	$row=mysqli_fetch_array($q1);
	$mathima=$row['mathima'];
	$tmima=$row['tmima'];
}
else
{
$cde=$_GET['mathid'];
$arr = explode("/", $cde);
$tmima=$arr[0];
$mathima=trim($arr[1]);
}
if (mb_substr($mathima,0,5,'UTF-8')=='Γυμν-') $mathima='Γυμν';
$m=$tmima."/".$mathima;

////////////////////////////////////////
$q=mysqli_query($con,"select * from kodikoimathimatos where kodikos='$mathima'");
$row=mysqli_fetch_array($q);
$cl=mb_substr($tmima,0,1,'UTF-8');
$ekset=$row['ekset'];
$p1=$row['pgrapto'];
$p2=$row['pproforiko'];

if ($ekset==0)
{
	$pta=50;
	$ptb=50;
}
else
{
	$pexet=40;
	$pta=60;
	$ptb=60;
}
//include "pososta.php";

	$q1=mysqli_query($con, "select * from bathmoposo WHERE teacher='$teacher' and mathima='$mathima' and tmima='$tmima' order by tetramino");
	if (mysqli_num_rows($q1) == 2)
	{
		$row=mysqli_fetch_array($q1);
		$codea=$row['id'];
		$row=mysqli_fetch_array($q1);
		$codeb=$row['id'];
		
	}


?>
<fieldset class="fieldset" >
<legend style="color: red; font-size: 14pt;"><?php echo $m;?></legend>
<pre><b>
</b></pre>
</fieldset>
<br>
<form method="post" > 
<?php
$result = mysqli_query($con,"SELECT DISTINCT s.mitroo,a.eponimo,a.onoma,a.tmima 
FROM syndidaskalies as s,allstudentinfo as a, syndcodes as sc 
WHERE sc.synd = '$tmima' AND s.mitroo=a.mitroo AND sc.syndid=s.syndid ORDER BY a.eponimo,a.onoma");
$crow1=mysqli_num_rows($result);

if ($crow1 > 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 mitroo,eponimo,onoma,omp FROM allstudentinfo WHERE tmima='$tmima' ORDER BY eponimo,onoma  ");
$crow1=mysqli_num_rows($result);
    $cr=0;
    while($row = mysqli_fetch_array($result))
     {
	 $cr++;
	 $mt[$cr]=$row['mitroo'];
	 $epo[$cr]=$row['eponimo']." ".$row['onoma'];
	 $tmim[$cr]=$tmima;
	 $omp=trim($row['omp']);
	 } 
}
// number of students
$ns=$cr;	

$sql=mysqli_query($con,"SELECT mitroo,bgrapto,bproforiko FROM bathmostetr WHERE code='$codea'");
$i=0;
//$omp='';
//$tetramino='Α';
//include "pososta.php";
if ($ekset==0)
{
	$pta=50;
	$ptb=50;
}
else
{
	$pexet=40;
	$pta=60;
	$ptb=60;
}
while($row=mysqli_fetch_array($sql))
{
	   $i++;
	   $m=$row['mitroo'];
	   
	   $r = mysqli_query($con,"select omp FROM allstudentinfo where mitroo='$m' ");	
	   $row1=mysqli_fetch_array($r);
	   $omp=trim($row1['omp']);
	   
	   //include "pososta.php";
	   
	   $gr[$m]=$row['bgrapto'];
	   $pr[$m]=$row['bproforiko'];
	   $ba[$m]=round($gr[$m]*$p1/100+$pr[$m]*$p2/100);
}
//****************************
$sql=mysqli_query($con,"SELECT mitroo,bexet FROM bathmosexet WHERE code='$codea'");
while($row=mysqli_fetch_array($sql))
{
	   $m=$row['mitroo'];
	   $gra[$m]=$row['bexet'];
}
$sql=mysqli_query($con,"SELECT mitroo,bexet FROM bathmosexet WHERE code='$codeb'");

while($row=mysqli_fetch_array($sql))
{
	   $m=$row['mitroo'];
	   $grb[$m]=$row['bexet'];
}
//****************************

$sql=mysqli_query($con,"SELECT mitroo,bgrapto,bproforiko FROM bathmostetr WHERE code='$codeb'");
$i=0;
//$omp='';
$tetramino='Β';
include "pososta.php";
if ($ekset==0)
{
	$pta=50;
	$ptb=50;
}
else
{
	$pexet=40;
	$pta=60;
	$ptb=60;
}
while($row=mysqli_fetch_array($sql))
{
	   $i++;
	   $m=$row['mitroo'];
	   $r = mysqli_query($con,"select omp FROM allstudentinfo where mitroo='$m' ");	
	   $row1=mysqli_fetch_array($r);
	   $omp=trim($row1['omp']);
	   
	   //include "pososta.php";
	   
	   $gr[$m]=$row['bgrapto'];
	   $pr[$m]=$row['bproforiko'];
	   $bb[$m]=round($gr[$m]*$p1/100+$pr[$m]*$p2/100);
}

echo "<table class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73'  >
<th>α/α</th>
<th>Α.Μ</th>
<th>Τμήμα</th>
<th>Όνοματεπώνυμο</th>";
if ($ekset==0)
{
echo "
<th>Α τετρ</th>
<th>Β τετρ</th>
<th>Τελικός</th>
</tr>
<tr><th colspan='4'><th>".$pta."%"."</th><th>".$ptb."%"."</th><th></th>";
}
else
{
echo "
<th>Α προφ</th><th>Εξέταση</th><th>Α τετρ</th>
<th>Β προφ</th><th>Εξέταση</th><th>Β τετρ</th>
<th>Τελικός</th>
</tr>
<tr><th colspan='4'><th>".$pta."%"."</th><th>".$pexet."%"."</th><th></th> <th>".$ptb."%"."</th><th>".$pexet."%"."</th><th></th><th></th>";

}


$i=1;
while($i <=$ns){
  echo "<tr>";
  echo "<td>" .$i;
  echo "<td>". $mt[$i] . "</td>";
  $m=$mt[$i];
  echo "<td>". $tmim[$i] . "</td>";
  echo "<td>". $epo[$i] . "</td>";
  if ($ekset==0)
  {
  	echo "<td>". $ba[$m] . "</td>";
    echo "<td>". $bb[$m] . "</td>";
    $t1=round($ba[$m]);
    $t2=round($bb[$m]);
    $telikos=round(($t1+$t2)/2);
    echo "<td style='text-align:right'><b>".$telikos."</b></td>";
  }
  else
  {
  	echo "<td>". $ba[$m] . "</td>";
    echo "<td >". "<input type='number' class='right' name='gra1[$m]' value=$gra[$m] min=0 max=20 step=1  >" ."</td>";
    $t1=round($ba[$m]*$pta/100+$gra[$m]*$pexet/100);
    echo "<td>". $t1 . "</td>";
    echo "<td>". $bb[$m] . "</td>";
    echo "<td >". "<input type='number' class='right' name='grb1[$m]' value='$grb[$m]' min=0 max=20 step=1  >" ."</td>";
    $t2=round($bb[$m]*$ptb/100+$grb[$m]*$pexet/100);
    echo "<td>". $t2 . "</td>";
    $telikos=round(($t1+$t2)/2);
    echo "<td style='text-align:right'><b>".$telikos."</b></td>";
    
  }

  
  echo "</tr>";
  $i++;
}
echo "</table>";


echo "<br>";

echo "<input type='hidden' name='tmima' value='$tmima' >";
echo "<input type='hidden' name='mathima' value='$mathima' >";
echo "<input type='hidden' name='codea' value='$codea' >";
echo "<input type='hidden' name='codeb' value='$codeb' >";
?>
<input type="Submit" name="bathmos" value="Καταχώρηση Βαθμολογίας" class="sb">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button style="color:blue " onclick="window.open('bathgram.php?code=<?php echo $code;?>','','width=600,height=500')">Εκτύπωση κατάστασης</button>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" name="epistrofi" value="Επιστροφή" onClick="self.close()"> 
</form>
<?php
if (isset($_POST['bathmos']))
{
$codea=$_POST['codea'];
$codeb=$_POST['codeb'];
$gra=$_POST['gra1']; 
$grb=$_POST['grb1']; 

mysqli_query($con,"DELETE FROM bathmosexet WHERE code='$codea'"); 

	foreach($gra as $mitroo=>$bga) 
	{

	$sql="insert into bathmosexet (code,mitroo,bexet) 
	values ('$codea','$mitroo','$bga')";	
    
	if (!mysqli_query($con, $sql)) 
		{
            echo "Error: " . $sql . "<br>" . mysqli_error($con);
        }
   
	}
	mysqli_query($con,"DELETE FROM bathmosexet WHERE code='$codeb'");

	foreach($grb as $mitroo=>$bgb) 
	{

	$sql="insert into bathmosexet (code,mitroo,bexet) 
	values ('$codeb','$mitroo','$bgb')";	
    
	if (!mysqli_query($con, $sql)) 
		{
            echo "Error: " . $sql . "<br>" . mysqli_error($con);
        }
   
	}
	$cc=$codea."/".$codeb;

header("Location:bathmotel.php?code=".$cc);

}


?>
</body>
</html>
