<?php include ("kfunctions.php");
//role("dos,hmp");
?>
<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="schoolyear" required>
<option value="<?php echo date("y"); ?>"><?php echo date("Y"); ?></option>
<?php
$result_scrap=$dbh->query("select * from scrap where  type='ya'");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap){
    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="schoolterm" required>
<?php
$cm=date("m");
if($cm>='1' and $cm<='4'){$oterm="Term 1"; $iterm="1";}
elseif($cm>='5' and $cm<='8'){$oterm="Term 2"; $iterm="2";}
elseif($cm>='9' and $cm<='12'){$oterm="Term 3"; $iterm="3";}
echo "
<option value='".$iterm."'>".$oterm."</option>";

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

if($count_scrap){
    do{
echo "<option value='".$row_scrap->item."'>Term ".$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="schoolclass" required>
<option value="">All classes</option>
<?php
$role=$_SESSION["role"];
if($role=="doss"){$type_out="type='secclas'";}
elseif($role=="dosp"){$type_out="type='priclas'";}

$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){
    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='secsub' or type='prisubps'");
$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'>
    <input type='submit' name='filter_std' class='btn btn-sm btn-warning' value='Filter'>
</div>
</form>
<?php
 

if(isset($_POST["submit_subjects"])){
$schoolclass=$_POST["schoolclass"];    
$schoolterm=$_POST["schoolterm"];
$schoolyear=$_POST["schoolyear"];
$subjectid=$_POST["subjectid"];
$subjectselect=$subjectid."marks";

include("connect.php");
$result_citadel=$dbh->query("select * from citadel_o where schoolyear='$schoolyear' and term='$schoolterm' and schoolclass='$schoolclass'");
$row_citadel=$result_citadel->fetchObject();
$count_citadel=$result_citadel->rowCount();


if($count_citadel>0){
    do{
       
$cid=$_POST["cid".$row_citadel->autoid.""];
if(isset($_POST["checkcid".$row_citadel->autoid.""])){$fin="0";}else{$fin="N";}

$update_citadel=$dbh->query("update citadel_o set $subjectselect='$fin' where cid='$cid' and schoolclass='$schoolclass' and term='$schoolterm' and schoolyear='$schoolyear'");

$update_kingslanding=$dbh->query("update kingslanding_o set $subjectselect='$fin' where cid='$cid' and schoolclass='$schoolclass' and term='$schoolterm' and schoolyear='$schoolyear' and ($subjectselect='N' or $subjectselect='0')");

    }while($row_citadel=$result_citadel->fetchObject());
}

echo "<div class='col-lg-12'><br><div class='alert alert-success'>Success: Subject has been updated. </div></div>";
}


?>
<form method='post'>
<div class='col-lg-12'><br></div>
<div class="col-lg-12">
<div class='panel panel-yellow'><!--Initiative-->
<div class='panel-heading'><h3 class='panel-title'>Select a subject and period to set students doing that specific subject</h3></div>
<div class='panel-body'>
<table class='table table-bordered table-hover table-striped sortable'>
<tr>
    <th width='2%'>No</th>
    <th width='12%'>Computer No.</th>
    <th width='2%'></th>
    <th >Student name</th>
    </tr>
<?php
if(isset($_POST["filter_std"])){
echo "<input type='checkbox' id='checkallz' onclick='checkall()'> &nbsp; <i><b>Select all</b></i>
<input type='submit' style='float:right;' class='btn btn-sm btn-primary' name='submit_subjects' value='Submit'>";


$schoolclass=$_POST["schoolclass"];    
$schoolterm=$_POST["schoolterm"];
$schoolyear=$_POST["schoolyear"];
$subjectid=$_POST["subjectid"];
$subjectselect=$subjectid."marks";

echo "
<input type='hidden' name='schoolclass' value='".$schoolclass."'>
<input type='hidden' name='schoolterm' value='".$schoolterm."'>
<input type='hidden' name='schoolyear' value='".$schoolyear."'>
<input type='hidden' name='subjectid' value='".$subjectid."'>";

include("connect.php");
$result_citadel=$dbh->query("select * from citadel_o where schoolyear='$schoolyear' and term='$schoolterm' and schoolclass='$schoolclass'");
$row_citadel=$result_citadel->fetchObject();
$count_citadel=$result_citadel->rowCount();

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

echo "
<input type='hidden' name='cid".$row_citadel->autoid."' value='".$row_citadel->cid."'>
<tr>
<td>".$x++."</td>
<td>".$row_child->cid."</td>";

$result_cit=$dbh->query("select * from citadel_o where schoolyear='$schoolyear' and term='$schoolterm' and schoolclass='$schoolclass' and cid='$row_citadel->cid' and $subjectselect!='N'");
$row_cit=$result_cit->fetchObject();
$count_cit=$result_cit->rowCount();

if($count_cit){$checkstatus='checked';}else{$checkstatus='';}

echo "<td><input type='checkbox' name='checkcid".$row_citadel->autoid."' id='cidcheck".$row_citadel->autoid."' value='1'".$checkstatus.">

</td>

<td title='Click to edit student subjects'><a href='subjectlist_o.php?cid=".$row_citadel->cid."'>".$row_child->fname." ".$row_child->sname."</a></td>

</tr>";
}while($row_citadel=$result_citadel->fetchObject());
}else{
echo "<tr>
<td align='center' colspan='4'><b><i>There is NO match. </i></b></td>
</tr>"; }   
}else{
echo "
<tr>    
<td colspan='4' align='center'><b><i>Select the filters above</i></b></td>
</tr>"; }
?>
</table>
</div></div>
</form>

</div>

</div>

</form>

                            </div> 

                <!-- /.row -->

               

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

    </div>
    <!-- /#wrapper -->
    <script>
function checkall(){
<?php
include("connect.php");
$result_citadel=$dbh->query("select * from citadel_o where schoolyear='$schoolyear' and term='$schoolterm' and schoolclass='$schoolclass'");
$row_citadel=$result_citadel->fetchObject();
$count_citadel=$result_citadel->rowCount();

if($count_citadel>0){
    do{
?>
if(document.getElementById('checkallz').checked){
document.getElementById('cidcheck<?php echo $row_citadel->autoid; ?>').checked=true;
}else{
document.getElementById('cidcheck<?php echo $row_citadel->autoid; ?>').checked=false; 
}
<?php
}while($row_citadel=$result_citadel->fetchObject());
}
?>
}
</script>

    <!-- 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>
