<?php
$pgMod="assign_subject";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);


if(isset($_POST['save']))
{
    
    print_r($_POST);
    die();

	$_SESSION['form']=$_POST;
	$flgEr=FALSE; 
	if(!isset($_POST['total']) || trim($_POST['total'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter total."); 
	}
	if($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$flgUp=$db->updateAry("examterm",$_POST,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
		if(!is_null($flgUp))
		{   
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"TimeTable", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	} 
}
if(isset($_POST['save_term'])){

    $res=$db->delete("assign_teacher_subject", "where 1 ");

    $i = 0;
    foreach($_POST['teacher'] as $teacher){

        foreach($_POST["subject_$i"] as $sub){

            $saveary = array(
                "teacher"   =>  $teacher,
                "subject"   =>  $sub,
                "added_by"  =>  $_SESSION["admin"]["uid"],
                "session"   =>  $_SESSION["admin"]["session"]
            );

            // $check = $db->getVal("select id from assign_teacher_subject where name='$_POST[name]' and class='$cls' and section='$section' and subject='$sub' and session='".$_SESSION["admin"]["session"]."' ");

            // if($percent!="" || $total !=""){
            //     if($check > 0){
            //         $flg = $db->updateAry("assign_teacher_subject", array("percent" => $percent, "term_percent"=>$_POST['term_percent'][$i], "total" => $total, "start_date"=>$_POST['start_date'][$i], "end_date"=>$_POST['end_date'][$i]), "where name='$_POST[name]' and class='$cls' and section='$section' and subject='$sub' and session='".$_SESSION["admin"]["session"]."'");
            //     } else {
                    $flg = $db->insertAry("assign_teacher_subject", $saveary);
            //     }
            // }

            // print_r($saveary);

        }

        $i++;
    }

    redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"TimeTable", "page_id"=>$pgMod)));
    
}
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>Notice</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>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
if($pgAct=="edit" && $_GET["id"]!="")
{   
	$aryForm=$db->getRow("SELECT * FROM examterm WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{
?>
<!-- Forms --> 
	<div class="row-fluid">
        <form method="post" action="">
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
					<h3><i class="icon-cog"></i> Terms</h3>
				</div>
				<!-- Forms: Content -->
				<div class="well no-padding">

						<div class="control-group">
							<label class="control-label" for="class">Term</label>
							<div class="controls">
								<?php echo $aryForm["full_name"] ?>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="total">Total</label>
							<div class="controls">
								<input type="text" id="total" name="total" class="input-block-level" value="<?php if($_POST)echo $_POST["total"]; else echo $aryForm["total"] ?>">
							</div>
						</div>
						<div class="form-actions">
							<button type="submit" class="btn btn-primary" name="save"> Save</button>
							<button type="button" class="btn"> Cancel</button>
						</div>
                        
				</div>
				<!-- / Forms: Content -->

			</div>
			<!-- / Forms: Box --> 

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

else{
?>
	<form method="post" action="">
		<div class="row-fluid">
            <div class="span12">
            	<div class="top-bar">
					<h3><i class="icon-cog"></i>Add New Term</h3>
                    <div>
						<button type="submit" name="save_term" class="btn btn-primary"> Save Term</button> 
					</div>
				</div>
				<div class="well no-padding">
                	<div class="controls controls-row">

            <h3 style="text-align:center;"><?php echo $exam['name']; ?></h3>
            <div id="printableDiv">
            <table class="table">
			<tr>
                <th>SNo.</th>
                <th>Teacher</th>
                <th>Subjects</th> 
            </tr>
			<?php $i=0; $j = 0;
			$allclass = $db->getRows("SELECT * from staff where  session='".$_SESSION['admin']['session']."' and status=1 ");
			foreach($allclass as $cls){$i++;

                $subjects = $db->getRows("select * from timetable_subject ");

				?>
				<tr>
                	<td><?php echo $i; ?></td>
                	<td><input type="hidden" name="teacher[]" value="<?php echo $cls['idno']; ?>" />
                	    <?php echo $cls['name']; ?></td>
                	<td>
                	    <?php foreach($subjects as $sub){
                	        $check = $db->getVal("select id from assign_teacher_subject where teacher='$cls[idno]' and subject='$sub[id]' ");
                	    ?>
                	    <div style="display:inline-block;">
                	        <?php echo $sub['subject']; ?>
                    	    <input type="checkbox" <?php if($check){echo "checked";} ?> name="subject_<?php echo $j; ?>[]" value="<?php echo $sub['id']; ?>" />&emsp;
                	    </div>
                	    <?php } ?>
            	    </td>
                </tr>
				<?php $j++;
			}
			?></table>
			</div>
		</div></div></div></div></form><?php
	}
/*
else{
    	$allexam = $db->getRows("SELECT * FROM examname order by id");
	?>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    </div>
    <div>
        <form method="get" action="">

        <input type="hidden" name="src" value="ExamCBSE" />
        <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />

            <div class="control-group" style="display:inline-block;">
                <label class="control-label" style="color:white;">Select Exam</label>
                <div  class="controls">
                    <select name="exam">
                        <option value=""> -- Select -- </option>
                        <?php foreach($allexam as $exam){ ?>
                            <option value="<?php echo $exam['id']; ?>"><?php echo $exam['name']; ?></option>
                        <?php } ?>
                    </select>
                </div>
            </div>

            <button type="submit" name="add_term" class="btn btn-primary"> Add Term</button>
        </form>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"ExamCBSE", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM examterm where school_id='".$_SESSION["admin"]["school_id"]."'"); $i=0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th>S.No.</th>
                    <th>Class</th>
                    <th>section</th>
                    <th>Subject</th>
                    <th>Exam Name</th>
                    <th>Percent</th>
                    <th>Total</th>
                    <!--<th>Action</th>-->
                </tr> 
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["class"] ?></td>
                    <td><?php echo $clist["section"] ?></td>
                    <td><?php echo $clist["subject"] ?></td>
                    <td><?php echo $clist["full_name"]." (".$clist["name"].")"; ?></td>
                    <td><?php echo $clist["percent"] ?></td>
                    <td><?php echo $clist["total"] ?></td>
                    <!--<td>-->
                    <?php //$aryPgAct['action']="edit"; ?>
                         <!--<a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Edit</button></a> -->
                    <!--</td>-->
                </tr>
            <?php } ?>
            </tbody>

        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}
	?>
	</div></div></div>
	<?php
}
*/
?>



