<?php include ("kfunctions.php");
role2("doss","dosp");
?>
<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">
                    <?php
if(isset($_POST["submit"])){
$schoolterm=$_POST["schoolterm"];
$schoolyear=$_POST["schoolyear"];
$setname=$_POST["setname"];
$selectedinput=$_POST["selectedinput"];
$yy=date("Y");
$fyy=substr($yy,2,2);
$mm=date("m");
$dd=date("d");
$hi=date("h");
$mi=date("i");
$sa=date("sa");
$fsa=substr($sa,0,2);   
$setid="set".$fyy.$mm.$dd.$hi.$mi.$fsa;

include("connect.php");
$rolenumber=$_SESSION["rolenumber"];
if($setname!=""){
$insert_examset=$dbh->query("insert into examsets (ouruser,setid,setname,year,term,setclasses) values('$rolenumber','$setid','$setname','$schoolyear','$schoolterm','$selectedinput')");


//pick from scrap where primary or secondary
$role=$_SESSION["role"];
if($role=="dosp"){$role_out="type='priclas'";}elseif($role=="doss"){$role_out="type='secclas'";}
$result_scrap=$dbh->query("select * from scrap where  $role_out ");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

//if its greater than 0
if($count_scrap>0){
do{
if(isset($_POST["cla".$row_scrap->item.""])){
$schoolclass=$_POST["cla".$row_scrap->item.""];


if($schoolclass<=10){
//Primary Level
$result_citadelp=$dbh->query("select * from citadel_p where schoolyear='$schoolyear' and schoolclass='$schoolclass' and active='1'");
$row_citadelp=$result_citadelp->fetchObject();
$count_citadelp=$result_citadelp->rowCount();


if($count_citadelp>0){
do{
$insert_child=$dbh->query("insert into kingslanding_p (schoolyear,term,schoolclass,cid,setid,combination,sub44marks,sub45marks,sub46marks,sub47marks,sub86marks,sub87marks,sub88marks,sub89marks) values('$row_citadelp->schoolyear','$schoolterm','$row_citadelp->schoolclass','$row_citadelp->cid','$setid','$row_citadelp->combination','$row_citadelp->sub44marks','$row_citadelp->sub45marks','$row_citadelp->sub46marks','$row_citadelp->sub47marks','$row_citadelp->sub86marks','$row_citadelp->sub87marks','$row_citadelp->sub88marks','$row_citadelp->sub89marks')");
}while($row_citadelp=$result_citadelp->fetchObject());
}
//Primary Level
}elseif($schoolclass>=21 && $schoolclass<=24){
//O-Level
$result_citadelo=$dbh->query("select * from citadel_o where schoolyear='$schoolyear' and schoolclass='$schoolclass' and active='1'");
$row_citadelo=$result_citadelo->fetchObject();
$count_citadelo=$result_citadelo->rowCount();

if($count_citadelo>0){
do{
$insert_child=$dbh->query("insert into kingslanding_o (schoolyear,term,schoolclass,cid,setid,combination,sub46marks,sub47marks,sub48marks,sub49marks,sub50marks,sub51marks,sub52marks,sub53marks,sub54marks,sub55marks,sub56marks,sub57marks,sub58marks,sub59marks,sub60marks,sub61marks,sub62marks,sub63marks,sub64marks,sub66marks,sub67marks,sub68marks,sub71marks,sub72marks,sub75marks,sub76marks,sub77marks,sub78marks,sub79marks,sub80marks,sub81marks,sub82marks,sub83marks,sub84marks,sub85marks) values('$row_citadelo->schoolyear','$schoolterm','$row_citadelo->schoolclass','$row_citadelo->cid','$setid','$row_citadelo->combination','$row_citadelo->sub46marks','$row_citadelo->sub47marks','$row_citadelo->sub48marks','$row_citadelo->sub49marks','$row_citadelo->sub50marks','$row_citadelo->sub51marks','$row_citadelo->sub52marks','$row_citadelo->sub53marks','$row_citadelo->sub54marks','$row_citadelo->sub55marks','$row_citadelo->sub56marks','$row_citadelo->sub57marks','$row_citadelo->sub58marks','$row_citadelo->sub59marks','$row_citadelo->sub60marks','$row_citadelo->sub61marks','$row_citadelo->sub62marks','$row_citadelo->sub63marks','$row_citadelo->sub64marks','$row_citadelo->sub66marks','$row_citadelo->sub67marks','$row_citadelo->sub68marks','$row_citadelo->sub71marks','$row_citadelo->sub72marks','$row_citadelo->sub75marks','$row_citadelo->sub76marks','$row_citadelo->sub77marks','$row_citadelo->sub78marks','$row_citadelo->sub79marks','$row_citadelo->sub80marks','$row_citadelo->sub81marks','$row_citadelo->sub82marks','$row_citadelo->sub83marks','$row_citadelo->sub84marks','$row_citadelo->sub85marks')");
}while($row_citadelo=$result_citadelo->fetchObject());
}
//O-Level
}elseif($schoolclass>=25){
//A-Level
$result_citadela=$dbh->query("select * from citadel_a where schoolyear='$schoolyear' and schoolclass='$schoolclass'  and active='1'");
$row_citadela=$result_citadela->fetchObject();
$count_citadela=$result_citadela->rowCount();

if($count_citadela>0){
do{
$insert_child=$dbh->query("insert into rivendell (schoolyear,term,schoolclass,cid,stream,setid,combination,sub10_1m,sub10_2m,sub10_3m,sub10_4m,sub11_1m,sub11_2m,sub11_3m,sub12_1m,sub12_2m,sub12_3m,sub13_1m,sub13_2m,sub13_3m,sub14_1m,sub14_2m,sub14_3m,sub14_4m,sub15_1m,sub15_2m,sub15_3m,sub16_1m,sub16_2m,sub16_3m,sub17_1m,sub17_2m,sub17_3m,sub18_1m,sub18_2m,sub18_3m,sub19_1m,sub19_2m,sub20_1m,sub20_2m,sub21_1m,sub21_2m,sub21_3m,sub21_4m,sub21_5m,sub22_m,sub23_m,sub24_m) values('$row_citadela->schoolyear','$schoolterm','$row_citadela->schoolclass','$row_citadela->cid','$row_citadela->stream','$setid','$row_citadela->combination','$row_citadela->sub10_1m','$row_citadela->sub10_2m','$row_citadela->sub10_3m','$row_citadela->sub10_4m','$row_citadela->sub11_1m','$row_citadela->sub11_2m','$row_citadela->sub11_3m','$row_citadela->sub12_1m','$row_citadela->sub12_2m','$row_citadela->sub12_3m','$row_citadela->sub13_1m','$row_citadela->sub13_2m','$row_citadela->sub13_3m','$row_citadela->sub14_1m','$row_citadela->sub14_2m','$row_citadela->sub14_3m','$row_citadela->sub14_4m','$row_citadela->sub15_1m','$row_citadela->sub15_2m','$row_citadela->sub15_3m','$row_citadela->sub16_1m','$row_citadela->sub16_2m','$row_citadela->sub16_3m','$row_citadela->sub17_1m','$row_citadela->sub17_2m','$row_citadela->sub17_3m','$row_citadela->sub18_1m','$row_citadela->sub18_2m','$row_citadela->sub18_3m','$row_citadela->sub19_1m','$row_citadela->sub19_2m','$row_citadela->sub20_1m','$row_citadela->sub20_2m','$row_citadela->sub21_1m','$row_citadela->sub21_2m','$row_citadela->sub21_3m','$row_citadela->sub21_4m','$row_citadela->sub21_5m','$row_citadela->sub22_m','$row_citadela->sub23_m','$row_citadela->sub24_m')");
}while($row_citadela=$result_citadela->fetchObject());
}
//A-Level
}

}

}while($row_scrap=$result_scrap->fetchObject());
}
//if its greater than 0
//pick from scrap where primary or secondary
}
}
?>
                    <form method='post'>
<div class='col-lg-2'>
<select class="form-control" name="schoolyear">
<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">
<?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-4'>
<input type='text' class='form-control' name='setname' placeholder='Set name' required>
</div>
<div class='col-lg-2'>
    <input type='submit' name='submit' class='btn btn-sm btn-primary' value='Submit'>
</div>
<?php
$role=$_SESSION["role"];
if($role=="dosp"){$role_out="type='priclas'";}elseif($role=="doss"){$role_out="type='secclas'";}
$result_scrap=$dbh->query("select * from scrap where  $role_out ");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap>0){
do{
    echo "
<input type='hidden' name='cla".$row_scrap->item."' id='cla".$row_scrap->item."'>";
}while($row_scrap=$result_scrap->fetchObject());
}
?>


<div class='col-lg-12'><br></div>

                    <div class="col-lg-12">
                        <div class="panel panel-default">
                            <div class="panel-body">
                            
                           
<div class='panel panel-yellow'><!--Initiative-->
<div class='panel-heading'><h3 class='panel-title'>Select class to do the set</h3></div>
<div class='panel-body'>
<?php

include("connect.php");
$role=$_SESSION["role"];
if($role=="dosp"){
$result_scrap=$dbh->query("select * from scrap where type='priclas'");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap){
    do{

    echo "
<div class='col-lg-2'>"; ?>
<input type='checkbox' id='sub<?php echo $row_scrap->item; ?>' onclick="sender()" value='<?php echo $row_scrap->item2; ?>'> <?php echo $row_scrap->item2; ?>
<?php echo "</div>"; 
}while($row_scrap=$result_scrap->fetchObject());
}else{
    echo "Please contact your IT department. ";
}   
}elseif($role=="doss"){

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

if($count_scrap){
    do{

    echo "
<div class='col-lg-2'>"; ?>
<input type='checkbox' id='sub<?php echo $row_scrap->item; ?>' onclick="sender()" value='<?php echo $row_scrap->item2; ?>'> <?php echo $row_scrap->item2; ?>
<?php echo "</div>";

}while($row_scrap=$result_scrap->fetchObject());
}else{
    echo "Please contact your IT department. ";
}   
}
?>
</div></div>

</div>

</div>

<input type='text' class='form-control' name='selectedinput' id='selectedclass' placeholder='Selected class(s)' readonly>

</form>

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

<script>
function sender(){
 var selectedclass = document.getElementById("selectedclass");
var values = [];
<?php
$role=$_SESSION["role"];
if($role=="dosp"){$role_out="type='priclas'";}elseif($role=="doss"){$role_out="type='secclas'";}
$result_scrap=$dbh->query("select * from scrap where $role_out ");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();
if($count_scrap>0){
    do{
?>
if(document.getElementById("sub<?php echo $row_scrap->item; ?>").checked) {
 var sub = document.getElementById("sub<?php echo $row_scrap->item; ?>").value;
  values.push(sub);
  
}
<?php
}while($row_scrap=$result_scrap->fetchObject());
}
?>
selectedclass.value = values.join(", ");

setTimeout(auto_sender,500);
}

var auto_sender=function(){
<?php
include("connect.php");
$role=$_SESSION["role"];
if($role=="dosp"){$role_out="type='priclas'";}elseif($role=="doss"){$role_out="type='secclas'";}
$result_scrap=$dbh->query("select * from scrap where $role_out");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();
if($count_scrap>0){
    do{
?>
if(document.getElementById("sub<?php echo $row_scrap->item; ?>").checked) {
 document.getElementById("cla<?php echo $row_scrap->item; ?>").value="<?php echo $row_scrap->item; ?>";
}else{
 document.getElementById("cla<?php echo $row_scrap->item; ?>").value="";   
}
<?php
}while($row_scrap=$result_scrap->fetchObject());
}
?>

}
</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>
