<?php
//echo "<pre>";print_r($_SERVER);exit;
if($_POST){
	echo "<pre>";
	if(is_array($_POST["student"]) && count($_POST["student"])>0){
		$i=0;
		foreach($_POST["student"] as $stu){
			if(trim($stu)!=""){
				$detail=json_decode(base64_decode($_POST["detail"][$i]),true);
				$db->delete("examcriteriarecord","where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$detail["idno"]." and subcriteria='".$detail["criteria"]."' and criteria='".$_GET["criteria"]."' and session='".$_SESSION["admin"]["session"]."' and term='".$_GET["term"]."' ");
				$newExam=array( "grade"=>$stu,
								"idno"=>$detail["idno"],
								"subcriteria"=>$detail["criteria"],
								"criteria"=>$_GET["criteria"],
								"term"=>$_GET["term"],
								"school_id"=>$_SESSION["admin"]["school_id"],
								"added_by"=>$_SESSION["admin"]["uid"],
								"session"=>$_SESSION["admin"]["session"]
				);
				$db->insertAry("examcriteriarecord",$newExam);
				//echo $db->getErMsg().$db->getLastQuery();
			}
			$i++;
		}
	}
	//print_r($_POST);echo "</pre><hr/>";exit;
	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="criteria_grade";
$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>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
}
	?>
    <form method="get" action="">
    <input type="hidden" name="src" value="ExamCBSE" />
    <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="fee" class="btn btn-primary"> Add Grade</button>
                        <button type="submit" name="create_file_grades" class="btn btn-primary"> Create Excel for grades</button>
					</div>
				</div>
				<div class="well no-padding">
                	<div class="controls controls-row">
                        
                        <div class="control-group span3">
                            
                            <div class="controls span3">
                                <select name="class" id="class" onchange="getCriteria(this.value)">
                                    <option value="">Class</option>
                                    <?php 
                                        $class=$db->getRows("select * from class where school_id='".$_SESSION["admin"]["school_id"]."' ");
                                        foreach($class as $cla){
                                            ?>
                                            <option <?php if($cla["class"]==$_GET["class"])echo 'selected';?> value="<?php echo $cla["class"] ?>">
                                                <?php echo $cla["class"] ?>
                                            </option>
                                            <?php
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                         <div class="control-group span3">
                            <!--<label class="control-label" for="class">Section</label>-->
                            <div class="controls span3">
                               <select name="section" id="section" >
                                    <option value="">Section</option>
                                    <?php 
                                        $class=$db->getRows("select * from section where school_id='".$_SESSION["admin"]["school_id"]."'");
                                        foreach($class as $cla){
                                            ?>
                                            <option <?php if($cla["name"]==$_GET["section"])echo 'selected';?> value="<?php echo $cla["name"] ?>">
                                                <?php echo $cla["name"] ?>
                                            </option>
                                            <?php
                                
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="control-group span3">
                            <div class="controls span3" id="criteriadiv">
                                <select name="criteria" id="criteria">
                                	<option value="">Select Criteria</option>
                                	
                                	<?php
                                    $type=$db->getRows("select * from criteria where class='$_GET[class]' and session='".$_SESSION["admin"]["session"]."' group by type");
                        			if(is_array($type) && count($type)>0){
                        				foreach($type as $t){
                        					?>
                        					<option <?php if($t["type"]==$_GET["criteria"])echo 'selected';?> value="<?php echo $t["type"] ?>"><?php echo $t["type"] ?></option><?php
                        				}
                        			}
                        			?>
                                	
                                </select>
                            </div>
                        </div>
                        
                        <div class="control-group span3">
                            <!--<label class="control-label" for="term">Term</label>-->
                            <div class="controls span3"> 
                                <select name="term" id="term">
                                    <option value="">Term</option>
                                    <?php 
                                        $class=$db->getRows("select * from examname ");
                                        foreach($class as $cla){
                                            ?> 
                                            <option <?php if($cla["name"]==$_GET["term"])echo 'selected';?> value="<?php echo $cla["name"] ?>">
                                                <?php echo $cla["name"] ?>
                                            </option>
                                            <?php
                                        } 
                                    ?>
                                </select>
                            </div>
                        </div>

                        <!--<div class="control-group span3">-->
                        <!--    <div class="controls span3">-->
                        <!--        <select name="term" id="term">-->
                        <!--            <option value="">Term</option>-->
                        <!--            <option <?php if("term1"==$_GET["term"])echo 'selected';?> value="term1">term1</option>-->
                        <!--            <option <?php if("term2"==$_GET["term"])echo 'selected';?> value="term2">term2</option>-->
                        <!--            <option <?php if("final"==$_GET["term"])echo 'selected';?> value="final">final</option>-->
                        <!--        </select>-->
                        <!--    </div>-->
                        <!--</div>-->
                    </div>
                    <br>
				</div>
				<!-- / Forms: Content -->
			</div>
    </div>
    </form>
	<?php
	$where=array();
	$qry="";
	if(isset($_GET["fee"])){
		?><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="fee" class="btn btn-primary"> Save Marks</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"]."' ";
		*/	
		if(isset($_GET["section"]) && $_GET["section"]!="")
			$whereC[]=" section='".$_GET["section"]."' ";
		$whereM="";
		if(is_array($whereC) && count($whereC)>0)
			$whereM=" where ".implode(" and ",$whereC);
		
		$allstudent=$db->getRows("select * from student $whereM and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and issue_TC=0 and status=1 order by name ");
		$allcriteria=$db->getRows("select * from criteria where type='".$_GET["criteria"]."' and class='".$_GET["class"]."' and session='".$_SESSION["admin"]["session"]."' and school_id='".$_SESSION["admin"]["school_id"]."'");
		//echo $db->getLastQuery();
		if(is_array($allstudent) && count($allstudent)>0){
			?><table class="table">
			<tr>
                <td>SNo.</td>
                <td>Student</td>
            <?php
			if(is_array($allcriteria) && count($allcriteria)>0){
				foreach($allcriteria as $allCrt){
					?><td><?php echo $allCrt["name"] ?></td><?php
				}
			}?>
            </tr>
			<?php $i=0;
			foreach($allstudent as $stu){ $i++;
				?>
				<tr>
                	<td><?php echo $i; ?></td>
                	<td><?php echo $stu["name"] ?></td>
                    <?php

					$gradeData=$db->getRows("select * from grade ");

					if(is_array($allcriteria) && count($allcriteria)>0){
						foreach($allcriteria as $sub){

							$grade=$db->getVal("select grade from examcriteriarecord where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$stu["idno"]." and subcriteria='".$sub["name"]."' and criteria='".$_GET["criteria"]."' and session='".$_SESSION["admin"]["session"]."' and term='".$_GET["term"]."'");

							?>
							<td>
                            <select name="student[]" style="width:100px">
                            	<option value=""> -- Select Grade --</option>
                            	<?php if(is_array($gradeData) && count($gradeData)>0){
									foreach($gradeData as $g){ ?>
										<option <?php if(trim($grade)==$g['grade']){echo 'selected="selected"';} ?> value="<?php echo $g["grade"] ?>"><?php echo $g["grade"] ?></option>
										<?php
									}
								}?>
                            </select>
                             <?php
							 echo $db->getErMsg();
							 $detail=array("idno"=>$stu["idno"],
							 				"criteria"=>$sub["name"]
											);
							$data=base64_encode(json_encode($detail));
							 ?>
                             <input type="hidden" name="detail[]" value="<?php echo $data ?>" />                            
                            </td><?php
						}
					}?>
                </tr>
				<?php
			}
			?></table><?php
		}
		?></div></div></div></div></form><?php
	}
	elseif(isset($_GET["create_file_grades"])){
		
		$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);
			
		if($_GET["term"]==""){
			echo "Please select term of exam..!";exit;
		}
				
		$allstudent=$db->getRows("select * from student $whereM and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and issue_TC=0 and status=1 order by name ");
		$allsubject=$db->getRows("select * from criteria where school_id='".$_SESSION["admin"]["school_id"]."' order by id");
		//echo $db->getLastQuery();
		if(is_array($allstudent) && count($allstudent)>0){
			define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
					/** Include PHPExcel */
			require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
			$objPHPExcel = new PHPExcel();
			$objPHPExcel->setActiveSheetIndex(0)
						->setCellValue('A1', 'idno')
						->setCellValue('B1', 'name')
						->setCellValue('C1', 'fname')
						->setCellValue('D1', 'class')
						->setCellValue('E1', 'section')
						->setCellValue('F1', 'term');
						
			$i=1;
			foreach($allstudent as $student){$i++;
				$objPHPExcel->setActiveSheetIndex(0)
							->setCellValue('A'.$i.'', ''.$student["idno"].'')
							->setCellValue('B'.$i.'', ''.$student["name"].'')
							->setCellValue('C'.$i.'', ''.$student["fname"].'')
							->setCellValue('D'.$i.'', ''.$student["class"].'')
							->setCellValue('E'.$i.'', ''.$student["section"].'')
							->setCellValue('F'.$i.'', ''.$_GET["term"].'');
							
			}$alpha="F";
			foreach($allsubject as $sub){$alpha++;
				$objPHPExcel->setActiveSheetIndex(0)
							->setCellValue(''.$alpha.'1', ''.$sub["name"].','.$sub["type"].'');
			}
			//$objPHPExcel->getActiveSheet()->setTitle('marks123');
			$objPHPExcel->setActiveSheetIndex(0);
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
			//echo $root=getcwd()."\\files\marks_".$_GET['class']."_".$_GET['section']."_".$_GET['term'].".xls";
			$root="D:\marks_excel\grade_indicator_".$_GET['class']."_".$_GET['section']."_".$_GET['term'].".xls";
			$objWriter->save($root);
			
			echo 'Files have been created in ' , $root , EOL;
			
			
		}
	}
	echo $db->getErMsg();
?>


<script>
    function getCriteria(cls){
    	$.post( "ExamCBSE/ajax.php", {type: "getCriteria", cls:cls	}, 
    		function( data ) {
    	    document.getElementById("criteriadiv").innerHTML = data;
    	});

    }
</script>


