<?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"])){

include("connect.php");
$schoolterm=$_POST["schoolterm"];
$schoolyear=$_POST["schoolyear"];
$setselect=$_POST["setselect"];
$setname=$_POST["setname"];
$selectedinput=$_POST["selectedinput"];
$rolenumber=$_SESSION["rolenumber"];

if($setselect==""){
$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);   
if ($role=='doss'){$ouruser="doss,tchs";}
elseif ($role=='dosp'){$ouruser="dosp,tchp";}
$setid="set".$fyy.$mm.$dd.$hi.$mi.$fsa;
$insert_examset=$dbh->query("insert into examsets (ouruser,setid,setname,year,term,setclasses) values('$ouruser','$setid','$setname','$schoolyear','$schoolterm','$selectedinput')");

}
elseif($setname==""){$setid=$setselect;}


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

$result_citadelinp=$dbh->query("select * from kingslanding_p where cid='$row_citadelp->cid' and setid='$setid'");
$count_citadelinp=$result_citadelinp->rowCount();

if($count_citadelinp<=0){
$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{

$result_citadelino=$dbh->query("select * from kingslanding_o where cid='$row_citadelo->cid' and setid='$setid'");
$count_citadelino=$result_citadelino->rowCount();

if($count_citadelino<=0){
$insert_child=$dbh->query("insert into kingslanding_o (schoolyear,term,stream,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->stream','$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{
$result_citadelina=$dbh->query("select * from rivendell where cid='$row_citadela->cid' and setid='$setid'");
$count_citadelina=$result_citadelina->rowCount();

if($count_citadelina<=0){
if($row_citadela->sub10_1m=="N"){$g10_1="";}else{$g10_1="x";}
if($row_citadela->sub10_2m=="N"){$g10_2="";}else{$g10_2="x";}
if($row_citadela->sub10_3m=="N"){$g10_3="";}else{$g10_3="x";}
if($row_citadela->sub10_4m=="N"){$g10_4="";}else{$g10_4="x";}
if($row_citadela->sub11_1m=="N"){$g11_1="";}else{$g11_1="x";}
if($row_citadela->sub11_2m=="N"){$g11_2="";}else{$g11_2="x";}
if($row_citadela->sub11_3m=="N"){$g11_3="";}else{$g11_3="x";}
if($row_citadela->sub12_1m=="N"){$g12_1="";}else{$g12_1="x";}
if($row_citadela->sub12_2m=="N"){$g12_2="";}else{$g12_2="x";}
if($row_citadela->sub12_3m=="N"){$g12_3="";}else{$g12_3="x";}
if($row_citadela->sub13_1m=="N"){$g13_1="";}else{$g13_1="x";}
if($row_citadela->sub13_2m=="N"){$g13_2="";}else{$g13_2="x";}
if($row_citadela->sub13_3m=="N"){$g13_3="";}else{$g13_3="x";}
if($row_citadela->sub14_1m=="N"){$g14_1="";}else{$g14_1="x";}
if($row_citadela->sub14_2m=="N"){$g14_2="";}else{$g14_2="x";}
if($row_citadela->sub14_3m=="N"){$g14_3="";}else{$g14_3="x";}
if($row_citadela->sub14_4m=="N"){$g14_4="";}else{$g14_4="x";}
if($row_citadela->sub15_1m=="N"){$g15_1="";}else{$g15_1="x";}
if($row_citadela->sub15_2m=="N"){$g15_2="";}else{$g15_2="x";}
if($row_citadela->sub15_3m=="N"){$g15_3="";}else{$g15_3="x";}
if($row_citadela->sub16_1m=="N"){$g16_1="";}else{$g16_1="x";}
if($row_citadela->sub16_2m=="N"){$g16_2="";}else{$g16_2="x";}
if($row_citadela->sub16_3m=="N"){$g16_3="";}else{$g16_3="x";}
if($row_citadela->sub17_1m=="N"){$g17_1="";}else{$g17_1="x";}
if($row_citadela->sub17_2m=="N"){$g17_2="";}else{$g17_2="x";}
if($row_citadela->sub17_3m=="N"){$g17_3="";}else{$g17_3="x";}
if($row_citadela->sub18_1m=="N"){$g18_1="";}else{$g18_1="x";}
if($row_citadela->sub18_2m=="N"){$g18_2="";}else{$g18_2="x";}
if($row_citadela->sub18_3m=="N"){$g18_3="";}else{$g18_3="x";}
if($row_citadela->sub19_1m=="N"){$g19_1="";}else{$g19_1="x";}
if($row_citadela->sub19_2m=="N"){$g19_2="";}else{$g19_2="x";}
if($row_citadela->sub20_1m=="N"){$g20_1="";}else{$g20_1="x";}
if($row_citadela->sub20_2m=="N"){$g20_2="";}else{$g20_2="x";}
if($row_citadela->sub21_1m=="N"){$g21_1="";}else{$g21_1="x";}
if($row_citadela->sub21_2m=="N"){$g21_2="";}else{$g21_2="x";}
if($row_citadela->sub21_3m=="N"){$g21_3="";}else{$g21_3="x";}
if($row_citadela->sub21_4m=="N"){$g21_4="";}else{$g21_4="x";}
if($row_citadela->sub21_5m=="N"){$g21_5="";}else{$g21_5="x";}
if($row_citadela->sub22_m=="N"){$g22="";}else{$g22="x";}
if($row_citadela->sub23_m=="N"){$g23="";}else{$g23="x";}
if($row_citadela->sub24_m=="N"){$g24="";}else{$g24="x";}

$insert_child=$dbh->query("insert into rivendell (schoolyear,term,schoolclass,cid,stream,setid,combination,sub10_1m,sub10_1g,sub10_2m,sub10_2g,sub10_3m,sub10_3g,sub10_4m,sub10_4g,sub11_1m,sub11_1g,sub11_2m,sub11_2g,sub11_3m,sub11_3g,sub12_1m,sub12_1g,sub12_2m,sub12_2g,sub12_3m,sub12_3g,sub13_1m,sub13_1g,sub13_2m,sub13_2g,sub13_3m,sub13_3g,sub14_1m,sub14_1g,sub14_2m,sub14_2g,sub14_3m,sub14_3g,sub14_4m,sub14_4g,sub15_1m,sub15_1g,sub15_2m,sub15_2g,sub15_3m,sub15_3g,sub16_1m,sub16_1g,sub16_2m,sub16_2g,sub16_3m,sub16_3g,sub17_1m,sub17_1g,sub17_2m,sub17_2g,sub17_3m,sub17_3g,sub18_1m,sub18_1g,sub18_2m,sub18_2g,sub18_3m,sub18_3g,sub19_1m,sub19_1g,sub19_2m,sub19_2g,sub20_1m,sub20_1g,sub20_2m,sub20_2g,sub21_1m,sub21_1g,sub21_2m,sub21_2g,sub21_3m,sub21_3g,sub21_4m,sub21_4g,sub21_5m,sub21_5g,sub22_m,sub22_g,sub23_m,sub23_g,sub24_m,sub24_g) 

    values('$row_citadela->schoolyear','$schoolterm','$row_citadela->schoolclass','$row_citadela->cid','$row_citadela->stream','$setid','$row_citadela->combination','$row_citadela->sub10_1m','$g10_1','$row_citadela->sub10_2m','$g10_2','$row_citadela->sub10_3m','$g10_3','$row_citadela->sub10_4m','$g10_4','$row_citadela->sub11_1m','$g11_1','$row_citadela->sub11_2m','$g11_2','$row_citadela->sub11_3m','$g11_3','$row_citadela->sub12_1m','$g12_1','$row_citadela->sub12_2m','$g12_2','$row_citadela->sub12_3m','$g12_3','$row_citadela->sub13_1m','$g13_1','$row_citadela->sub13_2m','$g13_2','$row_citadela->sub13_3m','$g13_3','$row_citadela->sub14_1m','$g14_1','$row_citadela->sub14_2m','$g14_2','$row_citadela->sub14_3m','$g14_3','$row_citadela->sub14_4m','$g14_4','$row_citadela->sub15_1m','$g15_1','$row_citadela->sub15_2m','$g15_2','$row_citadela->sub15_3m','$g15_3','$row_citadela->sub16_1m','$g16_1','$row_citadela->sub16_2m','$g16_2','$row_citadela->sub16_3m','$g16_3','$row_citadela->sub17_1m','$g17_1','$row_citadela->sub17_2m','$g17_2','$row_citadela->sub17_3m','$g17_3','$row_citadela->sub18_1m','$g18_1','$row_citadela->sub18_2m','$g18_2','$row_citadela->sub18_3m','$g18_3','$row_citadela->sub19_1m','$g19_1','$row_citadela->sub19_2m','$g19_2','$row_citadela->sub20_1m','$g20_1','$row_citadela->sub20_2m','$g20_2','$row_citadela->sub21_1m','$g21_1','$row_citadela->sub21_2m','$g21_2','$row_citadela->sub21_3m','$g21_3','$row_citadela->sub21_4m','$g21_4','$row_citadela->sub21_5m','$g21_5','$row_citadela->sub22_m','$g22','$row_citadela->sub23_m','$g23','$row_citadela->sub24_m','$g24')");
}
}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' id='setnamecont'>
<input type='text' class='form-control' id='setname' onkeyup="onlyone()" name='setname' placeholder='Set name'>
</div>
<div class='col-lg-2' id='setidcont'>
<select class='form-control' name='setselect' id='setid' onclick="onlyone()">
    <option value=''>Set name </option>
    <?php
include("connect.php");
$rolenumber=$_SESSION["rolenumber"];
$result_setname = $dbh->query("select * from examsets where ouruser like '%$role%'");
$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-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'>
<i><b>Note :</b>Select field adds new students to the selected set and Text field adds the selected classes to the set but <b>NOT BOTH</b>. </i><br><br>
<?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 onlyone(inputz){
var setname=document.getElementById("setname").value;
var setid=document.getElementById("setid").value;

if(setname!=""){$("#setidcont").slideUp();}else{$("#setidcont").slideDown();}
if(setid!=""){$("#setnamecont").slideUp();}else{$("#setnamecont").slideDown();}
}

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>
