<?php include ("kfunctions.php");
//role("overadmin");
?>
<html>
<head>
<?php kheader(); ?>
<?php 
require_once("connect.php");

?>
<link href="css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
<script src="js/sorttable.js"></script>
</head>
<body>


    <div id="wrapper">

        <!-- Navigation -->
        <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <!-- Brand and toggle get grouped for better mobile display -->
            <?php ktopbar(); ?>
            <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
            <?php kleftbar(); ?>
            <!-- /.navbar-collapse -->
        </nav>

        <div id="page-wrapper">

            <div class="container-fluid">

 <!-- Page Heading -->
                

                <div class="row">
                    <div class="col-lg-12">
                        <div class="panel panel-default">
                            <div class="panel-body">

<div class='panel panel-primary'><!--Initiative-->
<div class='panel-heading'><h3 class='panel-title'>Position query</h3></div>
<div class='panel-body'>

<form method='post'>
<div class='col-lg-3'>
<select class='form-control' name='setname'>
<option value=''>Exam set</option>
<?php
include("connect.php");
$result_exam=$dbh->query("select * from examsets");
$row_exam=$result_exam->fetchObject();
$count_exam=$result_exam->rowCount();

if($count_exam>0){
do{
echo "<option value='".$row_exam->setid."'>".$row_exam->setname."</option>";
}while($row_exam=$result_exam->fetchObject());
}else{
    echo "<option value=''>Please contact your IT department. </option>";
}
?>
</select>
</div>

<div class='col-lg-3'>
<select class='form-control' name='schoolclass'>
<option value=''>Class</option>
<?php
include("connect.php");
$result_scrap=$dbh->query("select * from scrap where type='secclas' and (item>=21 and item<25)");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap>0){
do{
echo "<option value='".$row_scrap->item."'>".$row_scrap->item2."</option>";
}while($row_scrap=$result_scrap->fetchObject());
}else{
    echo "<option value=''>Please contact your IT department. </option>";
}
?>
</select>
</div>

<input type='submit' name='filter' class='btn btn-sm btn-primary' value='Filter'>
</form>
<?php

if(isset($_POST["submit_ft"])){
$schoolclass=$_POST["schoolclass"];
$setname=$_POST["setname"];
$result_kingslanding=$dbh->query("select * from kingslanding_o where schoolclass='$schoolclass' and setid='$setname' and active='1'");
$row_kingslanding=$result_kingslanding->fetchObject();
$count_kingslanding=$result_kingslanding->rowCount();

$result_scrapstream=$dbh->query("select * from scrap where type='ostream'");
$row_scrapstream=$result_scrapstream->fetchObject();
$count_scrapstream=$result_scrapstream->rowCount();
do {

$result_scrapstream2=$dbh->query("select * from kingslanding_o where stream='$row_scrapstream->item2' and setid='$setname' and active='1' and schoolclass='$schoolclass'");
$row_scrapstream2=$result_scrapstream2->fetchObject();
$count_scrapstream2=$result_scrapstream2->rowCount();

if ($count_scrapstream2>0){
echo $row_scrapstream2->stream." :".$count_scrapstream2."<br>";
$update_streamtotal=$dbh->query("update kingslanding_o set stream_outof='$count_scrapstream2' where stream='$row_scrapstream2->stream' and active='1' and schoolclass='$schoolclass'");
}
    }while($row_scrapstream=$result_scrapstream->fetchObject());


if($count_kingslanding>0){
    do{
@ $cid=$_POST["finalcid".$row_kingslanding->autoid.""];
@ $position=$_POST["position".$row_kingslanding->autoid.""];
@ $streamposition=$_POST["streamposition".$row_kingslanding->autoid.""];



//xxxxx
$update_kingslanding=$dbh->query("update kingslanding_o set position='$position', stream_position='$streamposition' where cid='$cid' and setid='$setname' and schoolclass='$schoolclass'");
    }while($row_kingslanding=$result_kingslanding->fetchObject());
}

}

if(isset($_POST["submit_f"])){
$schoolclass=$_POST["schoolclass"];
$setname=$_POST["setname"];
$result_kingslanding=$dbh->query("select * from kingslanding_o where schoolclass='$schoolclass' and setid='$setname' and active='1'");
$row_kingslanding=$result_kingslanding->fetchObject();
$count_kingslanding=$result_kingslanding->rowCount();

if($count_kingslanding>0){
    do{

@ $finaltotal=$_POST["finaltotal".$row_kingslanding->autoid.""];
@ $cid=$_POST["finalcid".$row_kingslanding->autoid.""];
@ $aggrz=$_POST["aggrz".$row_kingslanding->autoid.""];
@ $division=$_POST["division".$row_kingslanding->autoid.""];
@ $outof=$_POST["outof".$row_kingslanding->autoid.""];
@ $averagemark=$_POST["averagemark".$row_kingslanding->autoid.""];

if($division<=3){$prom="Promoted";}
else{$prom="Prohibition";}

$update_kingslanding=$dbh->query("update kingslanding_o set totalmarks='$finaltotal', totalaggr='$aggrz', outof='$outof', average='$averagemark', division='$division', promotion='$prom' where cid='$cid' and setid='$setname' and schoolclass='$schoolclass'");
    }while($row_kingslanding=$result_kingslanding->fetchObject());
}

$result_scrapstream=$dbh->query("select * from scrap where type='ostream'");
$row_scrapstream=$result_scrapstream->fetchObject();
$count_scrapstream=$result_scrapstream->rowCount();

echo "<form method='post'>
<input type='submit' style='float:right;' class='btn btn-sm btn-warning' name='submit_ft' value='Submit'>";
do{
//stream position below
$result_kingstream=$dbh->query("select * from kingslanding_o where schoolclass='$schoolclass' and setid='$setname' and stream!='' and stream='$row_scrapstream->item2' and active='1' order by totalmarks desc ");
$row_kingstream=$result_kingstream->fetchObject();
$count_kingsstream=$result_kingstream->rowCount();


if($count_kingsstream>0){

 $x=1;
  $y=1;
    do{
$result_child=$dbh->query("select * from children where cid='$row_kingstream->cid'");
$row_child=$result_child->fetchObject();

echo " 
<input type='hidden' name='schoolclass' value='".$schoolclass."'>
<input type='hidden' name='setname' value='".$setname."'>
<input type='hidden' name='finalcid".$row_kingstream->autoid."' value='".$row_kingstream->cid."'>
<input type='hidden' name='totalmarks".$row_kingstream->autoid."' value='".$row_kingstream->totalmarks."'>
<input type='hidden' name='streamposition".$row_kingstream->autoid."' value='".$y++."'>
";
}while($row_kingstream=$result_kingstream->fetchObject()); 
}
}while($row_scrapstream=$result_scrapstream->fetchObject()); 

//class positions below
$result_kingslandingp=$dbh->query("select * from kingslanding_o where schoolclass='$schoolclass' and setid='$setname' and active='1' order by totalmarks desc ");
$row_kingslandingp=$result_kingslandingp->fetchObject();
$count_kingslandingp=$result_kingslandingp->rowCount();


echo "
<table class='table table-bordered table-hover table-striped sortable'>
<tr>
<th>Names</th>
<th>Total marks</th>
<th>Position in Stream</th>
<th>Position in Class</th>
<th>Combination</th>
<tr>";
if($count_kingslandingp>0){

 $x=1;
  $y=1;
    do{

$result_child=$dbh->query("select * from children where cid='$row_kingslandingp->cid'");
$row_child=$result_child->fetchObject();

echo " 
<input type='hidden' name='schoolclass' value='".$schoolclass."'>
<input type='hidden' name='setname' value='".$setname."'>

<input type='hidden' name='finalcid".$row_kingslandingp->autoid."' value='".$row_kingslandingp->cid."'>
<tr>
<td>".$row_child->fname." ".$row_child->sname."</td>
<td>
".$row_kingslandingp->totalmarks."
<input type='hidden' name='totalmarks".$row_kingslandingp->autoid."' value='".$row_kingslandingp->totalmarks."'>
</td>
<td></td>
<td>
".$x++."
<input type='hidden' name='position".$row_kingslandingp->autoid."' value='".$y++."'>
</td>
<td>".$row_kingslandingp->promotion."</td>
</tr>";


}while($row_kingslandingp=$result_kingslandingp->fetchObject()); 
echo "</form>";
}else{
echo "
<tr>
<td>There is NO data in this table so far. </td>
</tr>";
} 
echo "</table>";

}




if(isset($_POST["filter"])){
$schoolclass=$_POST["schoolclass"];
$setname=$_POST["setname"];

$result_kingslanding=$dbh->query("select * from kingslanding_o where schoolclass='$schoolclass' and setid='$setname' and active='1'");
$row_kingslanding=$result_kingslanding->fetchObject();
$count_kingslanding=$result_kingslanding->rowCount();



echo "
<table class='table table-bordered table-hover table-striped sortable'>
<tr>
<th>No</th>
<th>Names</th>
<th>Total marks</th>
<th>Total aggregates</th>
<th>Out of</th>
<th>Average mark</th>
<th>Subject done</th>
<th>Division</th>
<tr>";

if($count_kingslanding>0){
    $x=1;
echo "<form method='post'>
<input type='submit' style='float:right;' class='btn btn-sm btn-warning' name='submit_f' value='Submit'>
<input type='hidden' name='schoolclass' value='".$schoolclass."'>
<input type='hidden' name='setname' value='".$setname."'>"; 

 do{
$total = $row_kingslanding->sub46marks + $row_kingslanding->sub47marks + $row_kingslanding->sub48marks + $row_kingslanding->sub49marks + $row_kingslanding->sub50marks + $row_kingslanding->sub51marks + $row_kingslanding->sub52marks + $row_kingslanding->sub53marks+ $row_kingslanding->sub54marks + $row_kingslanding->sub55marks + $row_kingslanding->sub56marks + $row_kingslanding->sub57marks + $row_kingslanding->sub58marks + $row_kingslanding->sub59marks + $row_kingslanding->sub60marks + $row_kingslanding->sub61marks + $row_kingslanding->sub62marks + $row_kingslanding->sub63marks + $row_kingslanding->sub64marks + $row_kingslanding->sub66marks + $row_kingslanding->sub67marks + $row_kingslanding->sub68marks + $row_kingslanding->sub71marks + $row_kingslanding->sub72marks + $row_kingslanding->sub75marks + $row_kingslanding->sub76marks + $row_kingslanding->sub77marks + $row_kingslanding->sub78marks + $row_kingslanding->sub79marks + $row_kingslanding->sub80marks + $row_kingslanding->sub81marks + $row_kingslanding->sub82marks + $row_kingslanding->sub83marks + $row_kingslanding->sub84marks + $row_kingslanding->sub85marks;

//$aggrz =  $row_kingslanding->sub46grade + $row_kingslanding->sub47grade + $row_kingslanding->sub48grade + $row_kingslanding->sub49grade + $row_kingslanding->sub50grade + $row_kingslanding->sub51grade + $row_kingslanding->sub52grade + $row_kingslanding->sub53grade + $row_kingslanding->sub54grade + $row_kingslanding->sub55grade + $row_kingslanding->sub56grade + $row_kingslanding->sub57grade + $row_kingslanding->sub58grade + $row_kingslanding->sub59grade + $row_kingslanding->sub60grade + $row_kingslanding->sub61grade + $row_kingslanding->sub62grade + $row_kingslanding->sub63grade + $row_kingslanding->sub64grade + $row_kingslanding->sub66grade + $row_kingslanding->sub67grade + $row_kingslanding->sub68grade + $row_kingslanding->sub71grade + $row_kingslanding->sub72grade + $row_kingslanding->sub75grade + $row_kingslanding->sub76grade + $row_kingslanding->sub77grade + $row_kingslanding->sub78grade + $row_kingslanding->sub79grade + $row_kingslanding->sub80grade + $row_kingslanding->sub81grade + $row_kingslanding->sub82grade + $row_kingslanding->sub83grade + $row_kingslanding->sub84grade + $row_kingslanding->sub85grade;

if ($row_kingslanding->sub46grade==''){$s46a='22';}else{$s46a=$row_kingslanding->sub46grade;}
if ($row_kingslanding->sub47grade==''){$s47a='22';}else{$s47a=$row_kingslanding->sub47grade;}
if ($row_kingslanding->sub48grade==''){$s48a='22';}else{$s48a=$row_kingslanding->sub48grade;}
if ($row_kingslanding->sub49grade==''){$s49a='22';}else{$s49a=$row_kingslanding->sub49grade;}
if ($row_kingslanding->sub50grade==''){$s50a='22';}else{$s50a=$row_kingslanding->sub50grade;}
if ($row_kingslanding->sub51grade==''){$s51a='22';}else{$s51a=$row_kingslanding->sub51grade;}
if ($row_kingslanding->sub52grade==''){$s52a='22';}else{$s52a=$row_kingslanding->sub52grade;}
if ($row_kingslanding->sub53grade==''){$s53a='22';}else{$s53a=$row_kingslanding->sub53grade;}
if ($row_kingslanding->sub54grade==''){$s54a='22';}else{$s54a=$row_kingslanding->sub54grade;}
if ($row_kingslanding->sub55grade==''){$s55a='22';}else{$s55a=$row_kingslanding->sub55grade;}
if ($row_kingslanding->sub56grade==''){$s56a='22';}else{$s56a=$row_kingslanding->sub56grade;}
if ($row_kingslanding->sub57grade==''){$s57a='22';}else{$s57a=$row_kingslanding->sub57grade;}
if ($row_kingslanding->sub58grade==''){$s58a='22';}else{$s58a=$row_kingslanding->sub58grade;}
if ($row_kingslanding->sub59grade==''){$s59a='22';}else{$s59a=$row_kingslanding->sub59grade;}

if ($row_kingslanding->sub60grade==''){$s60a='22';}else{$s60a=$row_kingslanding->sub60grade;}
if ($row_kingslanding->sub61grade==''){$s61a='22';}else{$s61a=$row_kingslanding->sub61grade;}
if ($row_kingslanding->sub62grade==''){$s62a='22';}else{$s62a=$row_kingslanding->sub62grade;}
if ($row_kingslanding->sub63grade==''){$s63a='22';}else{$s63a=$row_kingslanding->sub63grade;}
if ($row_kingslanding->sub64grade==''){$s64a='22';}else{$s64a=$row_kingslanding->sub64grade;}
if ($row_kingslanding->sub66grade==''){$s66a='22';}else{$s66a=$row_kingslanding->sub66grade;}
if ($row_kingslanding->sub67grade==''){$s67a='22';}else{$s67a=$row_kingslanding->sub67grade;}
if ($row_kingslanding->sub68grade==''){$s68a='22';}else{$s68a=$row_kingslanding->sub68grade;}

if ($row_kingslanding->sub71grade==''){$s71a='22';}else{$s71a=$row_kingslanding->sub71grade;}
if ($row_kingslanding->sub72grade==''){$s72a='22';}else{$s72a=$row_kingslanding->sub72grade;}
if ($row_kingslanding->sub75grade==''){$s75a='22';}else{$s75a=$row_kingslanding->sub75grade;}
if ($row_kingslanding->sub76grade==''){$s76a='22';}else{$s76a=$row_kingslanding->sub76grade;}
if ($row_kingslanding->sub77grade==''){$s77a='22';}else{$s77a=$row_kingslanding->sub77grade;}
if ($row_kingslanding->sub78grade==''){$s78a='22';}else{$s78a=$row_kingslanding->sub78grade;}
if ($row_kingslanding->sub79grade==''){$s79a='22';}else{$s79a=$row_kingslanding->sub79grade;}

if ($row_kingslanding->sub80grade==''){$s80a='22';}else{$s80a=$row_kingslanding->sub80grade;}
if ($row_kingslanding->sub81grade==''){$s81a='22';}else{$s81a=$row_kingslanding->sub81grade;}
if ($row_kingslanding->sub82grade==''){$s82a='22';}else{$s82a=$row_kingslanding->sub82grade;}
if ($row_kingslanding->sub83grade==''){$s83a='22';}else{$s83a=$row_kingslanding->sub83grade;}
if ($row_kingslanding->sub84grade==''){$s84a='22';}else{$s84a=$row_kingslanding->sub84grade;}
if ($row_kingslanding->sub85grade==''){$s85a='22';}else{$s85a=$row_kingslanding->sub85grade;}



$a= array($s46a,$s47a,$s48a,$s49a,$s50a,$s51a,$s52a,$s53a,$s54a,$s55a,$s56a,$s57a,$s58a,$s59a,$s60a,$s61a,$s62a,$s63a,$s64a,$s66a,$s67a,$s68a,$s71a,$s72a,$s75a,$s76a,$s77a,$s78a,$s79a,$s80a,$s81a,$s82a,$s83a,$s84a,$s85a);


sort($a, SORT_NUMERIC);

$args2= array($a[0],$a[1],$a[2],$a[3],$a[4],$a[5],$a[6],$a[7]);

if (in_array("22", $args2)){$aggie='NA';}
else {$aggie= $a[0]+$a[1]+$a[2]+$a[3]+$a[4]+$a[5]+$a[6]+$a[7];}

$aggrz=$aggie;

$marks = array($row_kingslanding->sub46marks,$row_kingslanding->sub47marks,$row_kingslanding->sub48marks,$row_kingslanding->sub49marks,$row_kingslanding->sub50marks,$row_kingslanding->sub51marks,$row_kingslanding->sub52marks,$row_kingslanding->sub53marks,$row_kingslanding->sub54marks,$row_kingslanding->sub55marks,$row_kingslanding->sub56marks,$row_kingslanding->sub57marks,$row_kingslanding->sub58marks,$row_kingslanding->sub59marks,$row_kingslanding->sub60marks,$row_kingslanding->sub61marks,$row_kingslanding->sub62marks,$row_kingslanding->sub63marks,$row_kingslanding->sub64marks,$row_kingslanding->sub66marks,$row_kingslanding->sub67marks,$row_kingslanding->sub68marks,$row_kingslanding->sub71marks,$row_kingslanding->sub72marks,$row_kingslanding->sub75marks,$row_kingslanding->sub76marks,$row_kingslanding->sub77marks,$row_kingslanding->sub78marks,$row_kingslanding->sub79marks,$row_kingslanding->sub80marks,$row_kingslanding->sub81marks,$row_kingslanding->sub82marks,$row_kingslanding->sub83marks,$row_kingslanding->sub84marks,$row_kingslanding->sub85marks);
$counter=array_count_values($marks);
$countall=count($marks);
$countn=$counter['N'];
//echo $countn." out of ".$countall;

$does=$countall-$countn;
$faveragemark=$total/$does;
$averagemark=number_format($faveragemark,0);

$result_grades=$dbh->query("select divfrom,divto,aggr,divtotake from grades where divfrom!='' and divto!=''");
$row_grades=$result_grades->fetchObject();
$count_grades=$result_grades->rowCount();

if($count_grades>0){
do{
    if ($aggrz!='NA'){


if($s47a>=9){$adddiv1=1;}else {$adddiv1=0;}
if($s46a>=7){$adddiv2=1;}else {$adddiv2=0;}

if($aggrz>=$row_grades->divfrom && $aggrz<=$row_grades->divto){

if ($row_grades->divtotake<3){$division=$row_grades->divtotake+$adddiv1+$adddiv2;}
else {$division=$row_grades->divtotake;}
}


}
else {$division="U";}
echo "<input type='hidden' name='division".$row_kingslanding->autoid."' value='".$division."'>";

}while($row_grades=$result_grades->fetchObject());
}

$result_child=$dbh->query("select * from children where cid='$row_kingslanding->cid'");
$row_child=$result_child->fetchObject();

echo "
<input type='hidden' name='finalcid".$row_kingslanding->autoid."' value='".$row_kingslanding->cid."'>
<tr>
<td>".$x++."</td>
<td>".$row_child->fname." ".$row_child->sname."</td>
<td>
".$total."
<input type='hidden' name='finaltotal".$row_kingslanding->autoid."' value='".$total."'>
</td>
<td>
".$aggrz.": ".$a[0].",".$a[1].",".$a[2].",".$a[3].",".$a[4].",".$a[5].",".$a[6].",".$a[7]." - ".$division."<p>
<input type='hidden' name='aggrz".$row_kingslanding->autoid."' value='".$aggrz."'>
</td>
<td>
".$count_kingslanding."
<input type='hidden' name='outof".$row_kingslanding->autoid."' value='".$count_kingslanding."'>
</td>
<td>
".$averagemark."
<input type='hidden' name='averagemark".$row_kingslanding->autoid."' value='".$averagemark."'>
</td>
<td>".$does."</td>
<td>".$division."</td>
</tr>";


}while($row_kingslanding=$result_kingslanding->fetchObject()); 
echo "</form>";
}else{
echo "
<tr>
<td>There is NO data in this table so far. </td>
</tr>";
} 
echo "</table>";
}
?>
</div></div>

</div>
                            </div> </div>
                    </div>
                </div>
                <!-- /.row -->

               

            </div>
            <!-- /.container-fluid -->
<?php useractivity("on page", "56"); ?>
        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

    <!-- jQuery -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/jquery-1.8.3.min.js" charset="UTF-8"></script>
</body>

</html>
