<?php
include "scrinit.php";
include "logo.php";
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Βαθμολόγιο Καθηγητή</title>

</script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">

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>
<script language="javascript" type="text/javascript"> 
function windowClose() { 
window.open('','_parent',''); 
window.close();
} 
</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:2px;font-size:10pt; }
</style>

</head>
  
<body>

<?php
require "dbconnect.php"; 
$tetramino=$_SESSION['tetramino'];
$dt=$_SESSION['dt'];
$less=$_GET['mathid'];
$teacher=$_SESSION['usr'];

?>
<h3>ΣΥΝΟΠΤΙΚΟ ΒΑΘΜΟΛΟΓΙΟ ΜΑΘΗΜΑΤΟΣ:  <?php echo " ". $less; ?> </h3>
<?php
$arr = explode("/", $less);
$tmima = trim($arr[0]);
$subject = trim($arr[1]);

$result = mysqli_query($con,"SELECT distinct s.mitroo,a.eponimo,a.onoma,a.tmima FROM syndcodes as c,syndidaskalies as s,allstudentinfo as a 
WHERE c.synd = '$tmima' AND c.syndid=s.syndid AND s.mitroo=a.mitroo ORDER BY a.eponimo,a.onoma");
$crow1=mysqli_num_rows($result);
if ($crow1 > 0)
{
	$cr=1;
	while($row=mysqli_fetch_array($result))
	{	
		$mt[$cr]=$row['mitroo'];
	    $epo[$cr]=$row['eponimo']." ".$row['onoma'];	
		$tmim[$cr]=$row['tmima'];
		$cr++;
	}
}
else
{	
$result = mysqli_query($con,"SELECT mitroo,eponimo,onoma FROM allstudentinfo WHERE tmima='$tmima' ORDER BY eponimo,onoma  ");
$crow1=mysqli_num_rows($result);
    $cr=1;
    while($row = mysqli_fetch_array($result))
     {
	 $mt[$cr]=$row['mitroo'];
	 $epo[$cr]=$row['eponimo']." ".$row['onoma'];
	 $tmim[$cr]=$tmima;
	 $cr++;
	 } 
}
$ns=$cr-1;	
//...................................................

$sql1=mysqli_query($con,"select * from aksinfo where trim(pmpteacher)='$teacher' 
      and trim(tmima)='$tmima' and trim(subject)='$subject' and tetramino='$tetramino' and declaid > 0 order by type");
if (mysqli_num_rows($sql1)>0)
{
  $i=0;
  $type=$code=$percent=$maxmark=array();
  while ($row=mysqli_fetch_array($sql1))
  {
	$i++;
	$type[$i]=$row['type'];
	$prct[$i]=$row['percent'];
	$code[$i]=$row['code'];
	$maxmark[$i]=$row['maxmark'];
  }
  $k=$i;
//print_r($code);
for ($i=1;$i<=$k;$i++)
{
  $sql=mysqli_query($con,"SELECT * FROM aksiologisi WHERE code='$code[$i]' ");
  $nsk=mysqli_num_rows($sql);
  if ($nsk > 0) 
  { 
	while($row=mysqli_fetch_array($sql))
	{
	   $mit=$row['mitroo'];
	   $bathmos[$i][$mit]=$row['bathmos'];
	   
    }
  }
  else
  {
	  $code[$i]=0;
  }
}
echo '<p style="color: blue; text-align: left; font-weight: bold;"> ΑΝΑΛΥΤΙΚΗ ΒΑΘΜΟΛΟΓΙΑ ΜΑΘΗΜΑΤΟΣ  </p>';
echo "<table class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73'  >
<th>No.</th>
<th>A/M</th>
<th>Τ/ΜΑ</th>
<th>ΜΑΘΗΤΗΣ/ΤΡΙΑ</th>";
// print titles
for ($i=1;$i<=$k;$i++)
{ 
  if ($code[$i] <>0 ) echo "<th>".$type[$i]."</th>";
}
  echo "<th>ΟΛΙΚΟ</th> </tr>";
//..............................
echo "<tr>"; 
echo "<td colspan='4'>". " " . "</td>";
for ($i=1;$i<=$k;$i++)
{	
  if ($code[$i] <>0 ) echo "<td>"."<b>"."<p align=right>".number_format($prct[$i],2)."%"."</b>"."</td>";
  $totalp=$totalp+$prct[$i];  
}
  echo "<td>"."<b>"."<p align=right>".number_format($totalp,2)."%"."</b>"."</td>";
  echo "</tr>"; 
echo "<tr>"; 
echo "<td colspan='4'>"."<p align=right>"." Μέγιστη βαθμολογία:". "</td>";
for ($i=1;$i<=$k;$i++)
{
	  if ($code[$i] <>0 ) echo "<td>"."<p align=right>".$maxmark[$i]."--> 20". "</td>";
}
echo "<td>"."<p align=right>". '20' . "</td>";
echo "<tr>"; 	
for ($i=1;$i<=$ns;$i++)
{	
  echo "<tr>";
  echo "<td>". $i;
  echo "<td>". $mt[$i] . "</td>";
  echo "<td>". $tmim[$i] . "</td>";
  echo "<td>". $epo[$i] . "</td>";
  $tot=0;
  $tot1=$perc=$cnt=array(); 
  for ($j=1;$j<=$k;$j++)
  {
  	if ($code[$j] <>0)
	{
	$b=$bathmos[$j][$mt[$i]];
	if ($b==-1) {$b=0;}
    
    if ($maxmark[$j]==0){$maxmark[$j]=20;}
	$b=(float)$b/($maxmark[$j]/20);
	echo "<td>"."<p align=right>". round($b)."</td>";
	$tot=$tot+$b*($prct[$j]/100);
	}
  }
  $totl[$i]=round($tot);
  echo "<td>"."<p align=right>".$totl[$i]."</td>"."</tr>"; 
  
}
echo "</table>";
//***************** store into bathmostetr
$qi=mysqli_query($con,"select * from bathmoposo where code='$less'");
if (mysqli_num_rows($qi) == 0)
{	
	$qx="insert into bathmoposo (code,teacher,tetramino) values ('$less','$teacher','$tetramino')";
	if (!mysqli_query($con, $qx)) {echo "Error: " . $qx . "<br>" . mysqli_error($con);}
}

if ($totalp==100)
{
	$qi=mysqli_query($con,"select id from bathmoposo where trim(code)='$less'");
	if (mysqli_num_rows($qi) > 0)
	{	
		$row=mysqli_fetch_array($qi);
		$id=$row['id'];
		$qf=mysqli_query($con,"select * from bathmostetr where code='$id'");
		if (mysqli_num_rows($qf) > 0)
		{	
			for ($i=1;$i<=$ns;$i++)
			{	
				$qi="update bathmostetr set bgrapto='$totl[$i]' where code='$id' and mitroo='$mt[$i]'";
				if (!mysqli_query($con, $qi)) {echo "Error: " . $qi . "<br>" . mysqli_error($con);}
			}
		}
		else
		{
			for ($i=1;$i<=$ns;$i++)
			{	
				$qi="insert into bathmostetr (code,mitroo,bgrapto) values ('$id','$mt[$i]','$totl[$i]')";
				if (!mysqli_query($con, $qi)) {echo "Error: " . $qi . "<br>" . mysqli_error($con);}
			}
		}
	}
}
//****************************************
}
?>
<br><br>
<input type="button" value="ΚΛΕΙΣΙΜΟ ΠΑΡΑΘΥΡΟΥ ΚΑΙ ΕΠΙΣΤΡΟΦΗ" onclick="windowClose();">

</body>
</html>