<?php include ("kfunctions.php"); ?>
<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>
<script src="js/print.js"></script>
<script src="js/input.js"></script>
<style>
.reptit{
color:#0066cc;
}
</style>
</head>
<body onload="average_out()">


    <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-4">
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <b><i>Sets</i></b>
                                <div style='height:120px;overflow-y: scroll;'>
                                <form method='post'>
<?php
include("connect.php");
$rolenumber=$_SESSION["rolenumber"];
$result_examset=$dbh->query("select * from examsets where ouruser='$rolenumber' order by autoid desc");
$row_examset=$result_examset->fetchObject();
$count_examset=$result_examset->rowCount();

if($count_examset>0){
    $x=1;
do{
$result_kingslanding=$dbh->query("select * from kingslanding where setid='$row_examset->setid'");
$count_kingslanding=$result_kingslanding->rowCount();
if($count_kingslanding>0){
?>
<div style='cursor:pointer;' title="<?php echo $row_examset->setclasses; ?>">
<input type='checkbox' onClick="xpush<?php echo $row_examset->autoid; ?>(this.value,'<?php echo $x++; ?>')" id="checkz<?php echo $row_examset->autoid; ?>" name='examset<?php echo $row_examset->autoid; ?>' value='<?php echo $row_examset->setid; ?>'> <?php echo "<i><b>".$row_examset->setname."</b></i> <i><b>Year :</b> 20".$row_examset->year."</i> <i><b>Term :</b> ".$row_examset->term."</i>"; ?><br>
</div>
<script>
function xpush<?php echo $row_examset->autoid; ?>(value_out,number_out){

if(document.getElementById("checkz<?php echo $row_examset->autoid; ?>").checked){
document.getElementById("text"+number_out).value=value_out;
}else{
document.getElementById("text"+number_out).value="";
}
}
</script>

<?php 
}
}while($row_examset=$result_examset->fetchObject());
}else{
    echo "There is no set so far";
}
?>
                            </div>
                            </div>
                            </div>
                            </div>
<?php
for($x=1;$x<=20;$x++){
echo "<input type='hidden' name='examz".$x."' id='text".$x."'>";
}
?>

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

<div class='col-lg-4' id='class_out'>
<select class='form-control' name='schoolclass' onchange='std(this.value)'>
<option value=''>Class</option>
<?php
include("connect.php");
$result_scrap=$dbh->query("select * from scrap where type='priclas' or type='secclas'");
$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=''>So far, there is NO data here. </option>";
}
?>
</select>
</div>
<div class='col-lg-4' id='std_out'>
<input type='text' list="stdname" name="stdname" class='form-control' placeholder="Student Name or ID">
<datalist id='stdname'>
<?php
include("connect.php");
$result_child=$dbh->query("select cid,fname,sname from children");
$row_child=$result_child->fetchObject();
$count_child=$result_child->rowCount();

if($count_child>0){
    do{
echo "<option value='".$row_child->cid." - ".$row_child->fname." ".$row_child->sname."'>";
    }while($row_child=$result_child->fetchObject());
}
?>
</datalist> 
</div>
<div class='col-lg-2'>
<input type='submit' class='btn btn-sm btn-primary' value='Filter' name='filter'>
    </div>

<div class='col-lg-2'>
<input type="button" value="Print report(s)" style="float:right;" class="btn btn-sm btn-info" onclick="PrintDiv('myDiv')" />
    </div>
</form>

                            </div>
                            </div> </div>

 <div class="col-lg-12">
                        <div class="panel panel-default">
                            <div class="panel-body">
<?php
if(isset($_POST["filter"])){

include("showset.php");
$schoolclass=$_POST["schoolclass"];
$stdname=$_POST["stdname"];
if($stdname!=""){
$fstdname=substr($stdname,0,5);
$std_in="and cid='$fstdname'";
}else{
$std_in="";
}

$result_kingslandingcid=$dbh->query("select distinct(cid) from kingslanding where schoolclass='$schoolclass' $std_in order by totalmarks desc");
$row_kingslandingcid=$result_kingslandingcid->fetchObject();
$count_kingslandingcid=$result_kingslandingcid->rowCount(); 

if($count_kingslandingcid>0){
    $x=1;
echo "<div id='myDiv'>";
do{

$result_kingslanding=$dbh->query("select * from kingslanding where cid='$row_kingslandingcid->cid'");
$row_kingslanding=$result_kingslanding->fetchObject();
$count_kingslanding=$result_kingslanding->rowCount();

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

$schoolclass=$row_children->schoolclass;
include("class.php");

$result_scrap1=$dbh->query("select * from scrap where item2='title1' and type='report'");
$row_scrap1=$result_scrap1->fetchObject();

$result_scrap2=$dbh->query("select * from scrap where item2='title2' and type='report'");
$row_scrap2=$result_scrap2->fetchObject();

$result_scrap3=$dbh->query("select * from scrap where item2='title3' and type='report'");
$row_scrap3=$result_scrap3->fetchObject();

$result_scrap4=$dbh->query("select * from scrap where item2='title4' and type='report'");
$row_scrap4=$result_scrap4->fetchObject();

echo "
<div>
<table class='table'>
<tr>
<td width='20%' align='center'>
<img src='img/badge.png' style='height:150px;width:200px;'>
</td>
<td align='center' with='60%'>
<b style='font-size:30px;font-family:Times New Roman;color:#0066cc;'>".$row_scrap1->item."</b><br>
<b style='font-size:20px;font-family:Times New Roman;color:#0066cc;'>".$row_scrap2->item."</b><br>
<div style='font-size:15px;font-family:Times New Roman;color:#0066cc;'><b>".$row_scrap3->item."</div><br>
<div style='font-size:17px;font-family:Times New Roman;color:#0066cc;'><b>".$row_scrap4->item."</div><br>
</td>
<td width='20%' align='center'>
<div style='height:150px;width:150px;border:2px dotted black;padding-top:70px;'>Photo here</div>
</td>

</tr>
</table>

<table class='table'>
<tr>
<td colspan='5'><b class='reptit'>Name</b> : ".$row_children->fname." ".$row_children->sname."</td>
</tr>
<tr>
<td><b class='reptit'>Class</b> : ".$daclass."</td>
<td><b class='reptit'>House</b> : ...............................</td>
<td><b class='reptit'>Computer No</b> : ".$row_kingslanding->cid."</td>
<td><b class='reptit'>Term</b> : ".$row_kingslanding->term."</td>
</tr>
</table>

<table class='table table-bordered table-hover table-striped' onload='average_out()'>";

$result_examset=$dbh->query("select * from examsets where autoid='' $exam1 $exam2 $exam3 $exam4 $exam5 $exam6 $exam7 $exam8 $exam9 $exam10 $exam11 $exam12 $exam13 $exam14 $exam15 $exam16 $exam17 $exam18 $exam19 $exam20");
$row_examset=$result_examset->fetchObject();
$count_examset=$result_examset->rowCount();
    
echo "
<tr>
<th class='reptit' width='2%'>No</th>
<th class='reptit'>Subject</th>";

if($count_examset>0){
do{
echo " 
<th class='reptit'>".$row_examset->setname."</th>
<th class='reptit'>Grade</th>";
}while($row_examset=$result_examset->fetchObject());
}
echo "
<th class='reptit'>Subject Teacher's comment</th>
<th class='reptit'>Initials</th>
</tr>";

$result_scrap=$dbh->query("select * from scrap where type='secsub' or type='prisubp' or type='prisubps'");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();


if($count_scrap>0){
    $z=1;
do{
    $fsub=$row_scrap->item."marks";
    $gsub=$row_scrap->item."grade";
    $tr=$row_scrap->item."x";
    $comz=$row_scrap->item."c";

$result_kings=$dbh->query("select * from kingslanding where ($fsub>=0 and $fsub!='N') and cid='$row_kingslanding->cid'");
$row_kings=$result_kings->fetchObject();
$count_kings=$result_kings->rowCount();

if($count_kings>0){
echo "
<tr>
<th class='reptit'>".$z++."</th>
<th class='reptit'>".$row_scrap->item2."</th>";

$result_examset=$dbh->query("select * from examsets where autoid='' $exam1 $exam2 $exam3 $exam4 $exam5 $exam6 $exam7 $exam8 $exam9 $exam10 $exam11 $exam12 $exam13 $exam14 $exam15 $exam16 $exam17 $exam18 $exam19 $exam20");
$row_examset=$result_examset->fetchObject();
$count_examset=$result_examset->rowCount();

if($count_examset>0){
    do{
$result_kingsexam=$dbh->query("select * from kingslanding where ($fsub>=0 and $fsub!='N') and setid='$row_examset->setid' and cid='$row_kingslanding->cid'");
$row_kingsexam=$result_kingsexam->fetchObject();
$count_kingsexam=$result_kingsexam->rowCount();

if($count_kingsexam>0){echo "
<td>".$row_kingsexam->$fsub."</td>
<td>".$row_kingsexam->$gsub."</td>";}
else{echo "
<td></td>
<td></td>";}

}while($row_examset=$result_examset->fetchObject());
}

if($row_kings->$comz!=""){echo "<td>".$row_kings->$comz."</td>";}
else{
echo "<td></td>";
}
echo " 
<td>".$row_kings->$tr."</td>
</tr>";
}
}while($row_scrap=$result_scrap->fetchObject());

$result_examset=$dbh->query("select * from examsets where autoid='' $exam1 $exam2 $exam3 $exam4 $exam5 $exam6 $exam7 $exam8 $exam9 $exam10 $exam11 $exam12 $exam13 $exam14 $exam15 $exam16 $exam17 $exam18 $exam19 $exam20");
$row_examset=$result_examset->fetchObject();
$count_examset=$result_examset->rowCount();

if($count_examset>0){
echo "
<tr>
<th colspan='2'>Total Mark(s)</th>";
$r=1;
do{
$result_kingsexam=$dbh->query("select * from kingslanding where setid='$row_examset->setid' and cid='$row_kingslanding->cid'");
$row_kingsexam=$result_kingsexam->fetchObject();
$count_kingsexam=$result_kingsexam->rowCount();

$total = $row_kingsexam->sub44marks + $row_kingsexam->sub45marks + $row_kingsexam->sub46marks + $row_kingsexam->sub47marks + $row_kingsexam->sub48marks + $row_kingsexam->sub49marks + $row_kingsexam->sub50marks + $row_kingsexam->sub51marks + $row_kingsexam->sub52marks + $row_kingsexam->sub53marks + $row_kingsexam->sub54marks + $row_kingsexam->sub55marks + $row_kingsexam->sub56marks + $row_kingsexam->sub57marks + $row_kingsexam->sub58marks + $row_kingsexam->sub59marks + $row_kingsexam->sub60marks + $row_kingsexam->sub61marks + $row_kingsexam->sub62marks + $row_kingsexam->sub63marks + $row_kingsexam->sub64marks + $row_kingsexam->sub65marks + $row_kingsexam->sub66marks + $row_kingsexam->sub67marks + $row_kingsexam->sub68marks + $row_kingsexam->sub69marks + $row_kingsexam->sub70marks + $row_kingsexam->sub71marks + $row_kingsexam->sub72marks + $row_kingsexam->sub73marks + $row_kingsexam->sub74marks + $row_kingsexam->sub75marks + $row_kingsexam->sub76marks + $row_kingsexam->sub77marks + $row_kingsexam->sub78marks + $row_kingsexam->sub79marks + $row_kingsexam->sub80marks + $row_kingsexam->sub81marks + $row_kingsexam->sub82marks + $row_kingsexam->sub83marks + $row_kingsexam->sub84marks + $row_kingsexam->sub85marks + $row_kingsexam->sub86marks + $row_kingsexam->sub87marks + $row_kingsexam->sub88marks + $row_kingsexam->sub89marks + $row_kingsexam->sub90marks;

echo "
<th>".$total."</th>
<th>
<input type='text' id='total".$r++."' value='".$total."'>

</th>";

}while($row_examset=$result_examset->fetchObject());
echo "
<td><input type='text' id='count_out' value='".$count_examset."'></td>
<td></td>
<td></td>
</tr>";}

echo "
<tr>
<th colspan='2'>Position</th>
<td>".$x++."</td>
<th colspan='2'>Average mark</th>
<td id='aver_out'></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan='10'><b class='reptit'>Warden/Matron's comment</b> : </td>
</tr>
<tr>
<td colspan='10'><b class='reptit'>Class teacher's comment on conduct and academics</b> :</td>
</tr>
<tr>
<td colspan='10'><b class='reptit'>Other comment(s)</b> :</td>
</tr>
<tr>
<td colspan='10'><b class='reptit'>Headteacher's comment(s)</b> :</td>
</tr>";
}else{
echo "
<tr>
<td colspan='10'>There is NO exam set has been submitted yet. </td>
<td></td>
</tr>";
}

echo "
</table>
</div>";

?>
<script>
function average_out(){

if(document.getElementById("count_out").value==1){
document.getElementById("aver_out").innerHTML= parseInt(document.getElementById("total1").value);

}else if(document.getElementById("count_out").value==2){
document.getElementById("aver_out").innerHTML= (parseInt(document.getElementById("total1").value) + parseInt(document.getElementById("total2").value))/parseInt(document.getElementById("count_out").value);

}else if(document.getElementById("count_out").value==3){
document.getElementById("aver_out").innerHTML= (parseInt(document.getElementById("total1").value) + parseInt(document.getElementById("total2").value) + parseInt(document.getElementById("total3").value))/parseInt(document.getElementById("count_out").value);

}else if(document.getElementById("count_out").value==4){
document.getElementById("aver_out").innerHTML= (parseInt(document.getElementById("total1").value) + parseInt(document.getElementById("total2").value) + parseInt(document.getElementById("total3").value) + parseInt(document.getElementById("total4").value))/parseInt(document.getElementById("count_out").value);

}else if(document.getElementById("count_out").value==5){
document.getElementById("aver_out").innerHTML= (parseInt(document.getElementById("total1").value) + parseInt(document.getElementById("total2").value) + parseInt(document.getElementById("total3").value) + parseInt(document.getElementById("total4").value) + parseInt(document.getElementById("total5").value))/parseInt(document.getElementById("count_out").value);

}else if(document.getElementById("count_out").value==6){
document.getElementById("aver_out").innerHTML= (parseInt(document.getElementById("total1").value) + parseInt(document.getElementById("total2").value) + parseInt(document.getElementById("total3").value) + parseInt(document.getElementById("total4").value) + parseInt(document.getElementById("total5").value) + parseInt(document.getElementById("total6").value))/parseInt(document.getElementById("count_out").value);
}
}
</script>
<?php

}while($row_kingslandingcid=$result_kingslandingcid->fetchObject());
echo "</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>
