<?php
if(isset($_POST[submit])){

	$teacher_id=$_GET['teacher'];
	$teacher_name=$db->getVal("select name from staff where idno='".$_GET['teacher']."'");

	 $cls_sub_id=$_POST[sub_data];

	$teacher_ex=$db->getVal("select id from teacher_subject where teacher_id='".$teacher_id."' and session='".$_SESSION["admin"]["session"]."'   ");

		$df=$db->delete("teacher_subject","where teacher_id='".$teacher_id."'");
		$section_data=array();

	foreach($cls_sub_id as  $cls_sub_id1){

    if(count($_POST['section_data'.$cls_sub_id1]) >0){
        $a = $_POST['section_data'.$cls_sub_id1];
    }
    else $a=[];

    $class = $_POST['class'.$cls_sub_id1];

	$newAry= array(
	              "teacher_name"=>$teacher_name,
				  "teacher_id"=>$teacher_id,
				  "cls_sub_id"=>$cls_sub_id1,
				  "session"=>$_SESSION["admin"]["session"],
				  "school_id"=>$_SESSION["admin"]["school_id"],
				  "section"=>json_encode($a),
				  "class"=>$class
				  );
// print_r($newAry[section]);
			$dd=$db->insertAry("teacher_subject",$newAry);
	}
			if($dd) $_SESSION['msg']= 'inserted';

   

	//print_r($_POST);echo "</pre><hr/>";
//	redirect();
}
?>

<style>.top-bar-minimize{display:none;}
thead, tfoot {display: table-header-group;}
.top-bar+.well,.accordion .accordion-heading+.accordion-body{display: inline-block !important;width: 100% !important;overflow: inherit !important;} 
</style>
<?php
$pgMod="teacher_role";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b></b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b></b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
	?>
    <form method="get" action="">
    <input type="hidden" name="src" value="Notice" />
    <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
	<div class="row-fluid">
            <div class="span12">
            	<div class="top-bar">
					<h3><i class="icon-cog"></i>Search Student Record</h3>
                    <div>
						<button type="submit" name="teacher_role"  class="btn btn-primary">Teacher_role</button>
					</div>
				</div>
				<div class="well no-padding">
                	<div class="controls controls-row">
                        
                      <div class="control-group span4">
                            <label class="control-label" for="class">Teacher List</label>
                            <div class="controls">
                                <select name="teacher" id="teacher">
                                    <option value="">Teacher</option>
                                    <?php 
                                        $staff=$db->getRows("select * from staff where session='".$_SESSION["admin"]["session"]."' ");
                                        foreach($staff as $cla){
                                            ?>

                                            <option <?php if($cla["idno"]==$_GET["teacher"])echo 'selected';?> value="<?php echo $cla["idno"] ?>">
                                                <?php echo $cla["name"] ?>
                                            </option>
                                            <?php
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
         
                   
                    </div>
                    <br>
				</div>
				<!-- / Forms: Content -->
			</div>
    </div>
    </form>
	<?php
	$where=array();
	$qry="";
	if(isset($_GET["teacher_role"])){
		?><form method="post" action="">
		<div class="row-fluid">
            <div class="span12">
            	<div class="top-bar">
					<h3><i class="icon-cog"></i>Add Result of students</h3>
                    <div>
						<button type="submit" name='submit' class="btn btn-primary"> Save </button>
					</div>
				</div>
				<div class="well no-padding">
                	<div class="controls controls-row">
					
		
		<?php
		$whereC=array();
		if(isset($_GET["class"]) && $_GET["class"]!="")
			$whereC[]=" class='".$_GET["class"]."' ";
		
		/*if(isset($_GET["medium"]) && $_GET["medium"]!="")
			$whereC[]=" medium='".$_GET["medium"]."' ";
		*/	
		$whereM="";$whereM1="";
		if(is_array($whereC) && count($whereC)>0)
			$whereM1=" where ".implode(" and ",$whereC);
		
		if(isset($_GET["section"]) && $_GET["section"]!="")
			$whereC[]=" section='".$_GET["section"]."' ";
		if(is_array($whereC) && count($whereC)>0)
			$whereM=" where ".implode(" and ",$whereC);

         $allclass=$db->getRows("select class from class where school_id='".$_SESSION["admin"]["school_id"]."' ");
			?>           
			<div style="overflow-x:auto;">
            <table class="table" border="2">

			<tr>          
            <?php 

			if(is_array($allclass) && count($allclass)>0){

				foreach($allclass as $sub){

					?><td border="2">
					<p><?php echo $c1=$sub["class"];?></p>
					<?php

$allclass1=$db->getVal("SELECT  GROUP_CONCAT(subject)  FROM subject where class='".$c1."' and session='".$_SESSION["admin"]["session"]."' group by class ");
if(!empty($allclass1)){
$ar_sub=explode(',',$allclass1);

foreach($ar_sub as $ar_sub1)
{
$tn= $_GET['teacher'];
?>
            <table>
                <tr>
                    <td>
                        <span class="text-primary" style="display:inline-block;color:red;"><?php echo $ar_sub1.' '; ?></span>
                    </td>
                    <td>
                        
                        <input type="checkbox" name="sub_data[]" value="<?php echo $cs= $db->getVal("SELECT  id  FROM subject where class='".$c1."' and subject='".$ar_sub1."' and session='".$_SESSION["admin"]["session"]."' "); ?>" <?php $chck= $db->getVal("SELECT  id  FROM  teacher_subject where cls_sub_id='".$cs."' and teacher_id='".$tn."' ");  if ($chck) echo "checked='checked'"; ?>>
                        <input type="hidden" name="class<?php echo $cs ?>" value="<?php echo $sub["class"]; ?>" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <?php 
                        $checksection= $db->getRow("SELECT  section  FROM  teacher_subject where cls_sub_id='".$cs."' and teacher_id='".$tn."' ");
                     $aa = json_decode($checksection["section"]);
                        $getSection=$db->getRows("SELECT * FROM section where school_id=1 ");
                            foreach($getSection as $section){
                                ?><div style="display:inline-block;">
                                <span style="font-size:12px;"><?php echo $section[name]; ?></span>&nbsp;
                                <input type="checkbox" name="section_data<?php echo $cs ?>[]" value="<?php echo $section[name]; ?>" 
                                <?php if (in_array($section["name"], $aa, TRUE)) {echo 'checked="checked"';} ?> /></div>
                                <?php
                            }
                        ?>

                    </td>
                </tr>
            </table>

				<?php }} ?>
					</td><?php
				}
			}?>
            </tr>


				<?php
			}
			?></table>
            </div>
			<?php
		//}
		?></div></div></div></div></form><?php


	echo $db->getErMsg();
?>
<script type="text/javascript">

$(document).ready(function(){
	/*$("student").change(function(){
		var v=this.val();
		alert(v);
		});*/
});
function validateme(a){
	var v=parseInt(a.value);
	var t=$('#total').val();
//	var r=$(this).val();
	if(v>t){
		alert("Please enter correct marks");
		//return false;
		a.value='';
	}
	else if(isNaN(v)){
		alert("Please enter only number");
		a.value='';
	}
	//alert(r);
}
</script>
<style type="text/css">
@media print {
    #myPrntbtn {
        display :  none;
    }
}

@media screen {
   #myPrntbtn1 {
        display: none;
    }
}
</style>