<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
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
					/** Include PHPExcel */
				require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
				$objPHPExcel = new PHPExcel();
if($_POST){
	$mysqlImportFilename ='';
	if(isset($_FILES["photo"]["name"]) && !empty($_FILES["photo"]["name"])){
		$lfilename = basename($_FILES['photo']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		if(in_array($lext,array('xls'))||in_array($lext,array('xlsx')))
		{
			$lnewfile=md5(microtime()).".".$lext;
			if(move_uploaded_file($_FILES['photo']['tmp_name'],"media/".$lnewfile))
			{
				
				// Read in entire file
				$file = "media/".$lnewfile;$ij=0;
				// Loop through each line
				if (($handle = fopen($file, "r")) !== FALSE) {
					
					try { $objPHPExcel = PHPExcel_IOFactory::load($file); } 
					catch(Exception $e) { die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage()); }
					
						$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
					    $arrayCount = count($allDataInSheet); // Here get total count of row in that sheet
					 $allsubject=$db->getRows("select * from criteria where school_id='".$_SESSION["admin"]["school_id"]."' order by id");
					 //print_r($allsubject);exit;
					for($i=2;$i<=$arrayCount;$i++){
						$idno=trim($allDataInSheet[$i]["A"]);
						$term=trim($allDataInSheet[$i]["F"]);
						//$total=explode($allDataInSheet[1]["G"]);
						$newExam=array(
										"idno"=>trim($allDataInSheet[$i]["A"]),
										"term"=>trim($allDataInSheet[$i]["F"]),
										"school_id"=>$_SESSION["admin"]["school_id"],
										"added_by"=>$_SESSION["admin"]["uid"],
										"session"=>$_SESSION["admin"]["session"]
						);$alpha="G";
						foreach($allsubject as $sub){
							$total=explode(",",$allDataInSheet[1][''.$alpha.'']);
							$newExam['criteria']=$total[1];
							$newExam['subcriteria']=$total[0];
							
							$newExam['grade']=trim($allDataInSheet[$i][''.$alpha.'']);
							//print_r($newExam);echo  EOL;
							
							$db->delete("examcriteriarecord","where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$idno." and term='".$term."' and criteria='".$newExam["criteria"]."' and subcriteria='".$newExam['subcriteria']."' and session='".$_SESSION["admin"]["session"]."'");
							
							 if($newExam["grade"]==""){$newExam["grade"]="";}
								$db->insertAry("examcriteriarecord",$newExam);
							 
						 	  $alpha++;
						   }
					}
					fclose($handle);
				}else{
					echo "File error ".$handle ;
				}
				
				
				
				
				$_SESSION["msg"].="Grades imported successfully";
				//////////////////////////**************setting backup table***********////////////////////////////
				if($db->getErMsg()=="")
				redirect("?src=ExamCBSE&page_id=junior_import_grade");
				else
				$alert_err[] = $db->getErMsg();
				
			}
		}else{
			$alert_err[]="Invalid file extension";
		}
	}
	
}
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
}
?> 
<div class="row-fluid">
        <form method="post" action="" enctype="multipart/form-data">
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
                	
					<h3><i class="icon-cog"></i> Import File</h3>
                    
				</div>
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content -->
				<div class="well no-padding">

						<input type="hidden" name="ss" value="1">
                        <div class="control-group">
							<label class="control-label" for="msg">CSV File</label>
							<div class="controls">
                           		<input type="file" id="file" name="photo" class="input-block-level">
							</div>
						</div>
						<div class="form-actions">
							<button type="submit" class="btn-classic"> import</button>
							<!--<button type="reset" class="btn-classic"> Cancel</button>-->
						</div>
                        
				</div>
				<!-- / Forms: Content -->

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

        </form>
	</div>