<?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>
</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">
                    <form method='post'>
<div class='col-lg-2'>
<select class='form-control' name='schoolclass' required>
<option value="">Class</option>
<?php
include("connect.php");
$result_scrap = $dbh->query("select * from scrap where type='priclas' ");
$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>
<div class='col-lg-2'>
<select class='form-control' name='subjectid' required>
<option value=''>Subject</option>
  <?php
include("connect.php");
$result_scrap = $dbh->query("select * from scrap where type='prisubps' or type='prisubp'");
$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>
<div class='col-lg-2'>
<select class='form-control' name='setid' required>
<option value=''>Setname</option>
  <?php
include("connect.php");
$rolenumber=$_SESSION["rolenumber"];
$result_setname = $dbh->query("select * from examsets where ouruser='$rolenumber'");
$row_setname = $result_setname->fetchObject();
$count_setname = $result_setname->rowCount();
if($count_setname>0){
do{
echo "<option value='".$row_setname->setid."'>".$row_setname->setname."</option>";
}while($row_setname = $result_setname->fetchObject());
}else{
echo "<option value=''>There is no data here so far. </option>";
}
?>
</select>
    </div>
<div class='col-lg-1'>
<input type='submit' name="class_filter" class='btn btn-sm btn-primary' value='Filter'>
</div>
</form>
<div class='col-lg-12'><br></div>
<?php
if(isset($_POST["submit_marks"])){
include("connect.php");
$schoolclass=$_POST["schoolclass"];
$subjectid=$_POST["subjectid"];
$setid=$_POST["setid"];
$sub_out=$subjectid."marks";

$result_kingslandingin = $dbh->query("select * from kingslanding_p where setid='$setid' and schoolclass='$schoolclass' and $sub_out!='N' order by cid asc");
$row_kingslandingin = $result_kingslandingin->fetchObject();
$count_kingslandingin = $result_kingslandingin->rowCount();

if($count_kingslandingin>0){
do{
$cid=$_POST["cid".$row_kingslandingin->autoid.""];
$autoid=$_POST["autoid".$row_kingslandingin->autoid.""];
$marks=$_POST["marks".$row_kingslandingin->autoid.""];
$grade=$_POST["grade".$row_kingslandingin->autoid.""];
$commentz=$_POST["commentz".$row_kingslandingin->autoid.""];

$fin_subj=$subjectid."marks";
$fin_grad=$subjectid."grade";
$tr=$subjectid."x";
$fin_com=$subjectid."c";

$rolenumber=$_SESSION["rolenumber"];
$result_users = $dbh->query("select * from users where rolenumber='$rolenumber'");
$row_users = $result_users->fetchObject();
$fn=substr($row_users->firstname,0,1);
$ln=substr($row_users->lastname,0,1);
$initials=$fn.$ln;


$update_kingslanding=$dbh->query("update kingslanding_p set $fin_subj='$marks', $fin_grad='$grade', $tr='$initials', $fin_com='$commentz' where cid='$cid' and setid='$setid' and schoolclass='$schoolclass'");

}while($row_kingslandingin = $result_kingslandingin->fetchObject());
}

$result_scrap = $dbh->query("select * from scrap where item='$subjectid'");
$row_scrap = $result_scrap->fetchObject();

$result_scrapsec = $dbh->query("select * from scrap where item='$schoolclass' and (type='secclas' or type='priclas')");
$row_scrapsec = $result_scrapsec->fetchObject();

$result_examsets = $dbh->query("select * from examsets where setid='$setid'");
$row_examsets = $result_examsets->fetchObject();

echo "<div class='col-lg-12'><div class='alert alert-success'>Success: Marks for Class : <u><i>".$row_scrapsec->item2."</i></u> &nbsp;&nbsp;&nbsp; <i>Subject</i> : <i><u>".$row_scrap->item2."</u></i> &nbsp;&nbsp;&nbsp; <i>Exam set</i> : <i><u>".$row_examsets->setname."</u></i> have been successfully submitted. </div></div>";
}
?>
<div class='col-lg-12'>     
<div class='panel panel-yellow'><!--Initiative-->
<?php
include ("connect.php");
if(isset($_POST["class_filter"])){
$schoolclass=$_POST["schoolclass"];
$subjectid=$_POST["subjectid"];
$sub_out=$subjectid."marks";
$setid=$_POST["setid"];

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

if($count_kingslanding>0){
$result_scrap = $dbh->query("select * from scrap where item='$subjectid'");
$row_scrap = $result_scrap->fetchObject();

$result_examsets = $dbh->query("select * from examsets where setid='$setid'");
$row_examsets = $result_examsets->fetchObject();

$result_scrapsec = $dbh->query("select * from scrap where item='$schoolclass' and (type='secclas' or type='priclas')");
$row_scrapsec = $result_scrapsec->fetchObject();

include("class.php");
$title_out="for <i>Term</i> : <u><i>".$row_kingslanding->term."</i></u> &nbsp;&nbsp;&nbsp; <i>Subject</i> : <u><i>".$row_scrap->item2."</i></u> &nbsp;&nbsp;&nbsp; <i>Class</i> : <i><u>".$row_scrapsec->item2."</u></i> &nbsp;&nbsp;&nbsp; <i>Exam set</i> : <i><u>".$row_examsets->setname."</u></i>";
}else{ $title_out=""; }
}else{ $title_out=""; }

echo "
<div class='panel-heading'><h3 class='panel-title'>Marks entry form ".$title_out."</h3></div>
<div class='panel-body'>";

if(isset($_POST["class_filter"])){
$schoolclass=$_POST["schoolclass"];
$subjectid=$_POST["subjectid"];
$setid=$_POST["setid"];

$sub_out=$subjectid."marks";
$grade_out=$subjectid."grade";
$com_out=$subjectid."c";
$result_scrap = $dbh->query("select * from scrap where item='$subjectid'");
$row_scrap = $result_scrap->fetchObject();


echo "
<form method='post'>
<input type='submit' name='submit_marks' style='float:right;' class='btn btn-sm btn-primary' value='Submit'> 
<input type='hidden' name='schoolclass' value='".$schoolclass."'>
<input type='hidden' name='subjectid' value='".$subjectid."'>
<input type='hidden' name='setid' value='".$setid."'>

<table class='table table-bordered table-hover table-striped sortable'>
 <thead>
<tr>
<th width='4%'>No</th>
<th width='20%'>Names</th>
<th width='10%'>".$row_scrap->item2." (Marks)</th>
<th width='10%'>".$row_scrap->item2." (Aggregates)</th>
<th>Comments</th>
</tr>
</thead>
<tbody>";

$result_kingslanding = $dbh->query("select * from kingslanding_p where setid='$setid' and schoolclass='$schoolclass' and $sub_out!='N' order by cid asc");
$row_kingslanding = $result_kingslanding->fetchObject();
$count_kingslanding = $result_kingslanding->rowCount();

if($count_kingslanding>0){
    $x=1;
do{

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

echo "
<tr>
<td>".$x++."</td>
<td>".$row_kingslanding->cid." - ".$row_child->fname." ".$row_child->sname."</td>"; ?>

<input type='hidden' name="cid<?php echo $row_kingslanding->autoid; ?>" value="<?php echo $row_kingslanding->cid; ?>">
<input type='hidden' name="autoid<?php echo $row_kingslanding->autoid; ?>" value="<?php echo $row_kingslanding->autoid; ?>">
<td><input type="text" class="form-control" onkeyup="grades(this.value,'<?php echo $row_kingslanding->autoid; ?>')" name="marks<?php echo $row_kingslanding->autoid; ?>" value="<?php echo $row_kingslanding->$sub_out; ?>"></td>
<?php
echo "
<td><input type='text' class='form-control' id='grades_out".$row_kingslanding->autoid."'  name='grade".$row_kingslanding->autoid."' value='".$row_kingslanding->$grade_out."' readonly></td>
<td><input type='text' list='defcom' class='form-control' name='commentz".$row_kingslanding->autoid."' value='".$row_kingslanding->$com_out."'></td>
<datalist id='defcom'>";
$result_comments=$dbh->query("select * from defaultcomments");
$row_comments=$result_comments->fetchObject();
$count_comments=$result_comments->rowCount();

if($count_comments>0){
    do{
echo "<option value='".$row_comments->comment."'>";
    }while($row_comments=$result_comments->fetchObject());
}
echo "
</datalist>
</tr>";
?>
<script>
function grades(submarks,autoid){
<?php
include("connect.php");
$role=$_SESSION["role"];
if($role=="dosp"){$role_out="type='priclas'";}elseif($role=="doss"){$role_out="type='secclas'";}
$result_grades=$dbh->query("select * from grades ");
$row_grades=$result_grades->fetchObject();

do{
if($schoolclass<=10){$from=$row_grades->pfrom; $to=$row_grades->pto;}
elseif($schoolclass>=21){$from=$row_grades->sfrom; $to=$row_grades->sto;}
?>
if( parseInt(submarks) >= <?php echo $from; ?>  && parseInt(submarks) <= <?php echo $to; ?>){
document.getElementById("grades_out"+autoid).value="<?php echo $row_grades->aggr; ?>";
}
<?php
}while($row_grades=$result_grades->fetchObject());
?>
}
</script>
<?php
}while($row_kingslanding = $result_kingslanding->fetchObject());
}else{
    echo "
<tr>
<td colspan='6' align='center'>There is NO data in the table so far. </td>
</tr>";
}

echo "
</tbody>
</table>
</form>";
}else{
    echo "Select from the options above and select filter";
}
?>
</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>
