<?php
session_start();
// ==================  
include("../../asset/theme/css/myfunc.php");           
include("../../php_crud/my_class.php");
$obj = new my_class();
extract($_REQUEST);
$branchid = $_SESSION['LOGIN_BRANCH'];
$shift_id;
$adm_dtl;
$class_id;
$medium_id;
$current_session=$_SESSION['ADMISSION_SESSION'];

//$sc_id;
//$ct_date;
//$ct_id;
//$subject_id;
//$session;
//$exam_term;
//$dat = $ct_date?date("Y-m-d",strtotime($ct_date)):NULL;
//echo $sc_id."---".$quiz_date."---".$quiz_type_id."---".$subject_id."---".$session;
/* $subject_det = $obj->Details_By_Cond("_int_subject","_id='$subject_id'");
$subject_type = $subject_det['_subject_type'];
$combined_id = $subject_det['_combined_sub_id']; */
// date_default_timezone_set('Asia/Dhaka');

$date_time   = date("Y-m-d h:m:s");
// $date   = date("Y-m-d");

$uniq_code = 1000 * strtotime($date_time);

    if ( 0 < $_FILES['file']['error'] ) {
        echo 'Error: ' . $_FILES['file']['error'] . '<br>';
    }
    else {
          
        //$new_file_name = date('Ymdhis') . rand(1000, 999999) . $_FILES['file']['name'];
			//$new_file_name = "jahid";
        $new_file_name = date('Ymdhis'). $_FILES['file']['name'];
        $sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
        $targetPath = "../../module/admission/form_excel/" . $new_file_name; // Target path where file is to be stored
        move_uploaded_file($sourcePath, $targetPath); // Moving Uploaded file

     }
///==================================================

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

require_once('../../asset/custom/PHPExcel_class/PHPExcel.php');
require_once('../../asset/custom/PHPExcel_class/PHPExcel/Calculation.php');
require_once('../../asset/custom/PHPExcel_class/PHPExcel/Cell.php');


$inputFileType = 'Excel5';
// $inputFileName = 'attendance.xls'; 
$inputFileName = $targetPath;

//  Read your Excel workbook
try {
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
    die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}


//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
/* $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
echo $objPHPExcel->setActiveSheetIndex(0)->calculateWorksheetDimension();
 */
 $highestRow = $sheet->getHighestRow();

$high = $highestRow - 1;
//$abc = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow(); 
//$abc = $objPHPExcel->sheets[0]["cells"]; 
//$abc = count($objPHPExcel->getActiveSheet());
 //$total_row = count($sheet->sheets[0]["cells"]);
$maxCell = $sheet->getHighestRowAndColumn();
$data = $sheet->rangeToArray('A1:' . $maxCell['column'] . $maxCell['row']);
$data = array_map('array_filter', $data);
$data = array_filter($data);
$abc = count($data);


$highestColumn = 'G';

function super_unique($array, $key) {   // For duplicat array removed
    $temp_array = array();
    foreach ($array as &$v) {
        if (!isset($temp_array[$v[$key]]))
            $temp_array[$v[$key]] = & $v;
    }
    $array = array_values($temp_array);
    return $array;
}

//====================================
$flag=0;

$data = array();
for ($row = 7; $row <= $abc; $row++) {
//for ($row = 2; $row <= $total_row; $row++) {   
$row_data = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
	 $sl = $row_data[0][0];
	 //$uniq_id = $row_data[0][1];
	 $std_name = $row_data[0][1];
	 $form_no = $row_data[0][2];
	 $mobile_no = $row_data[0][3];
	 $father_name = $row_data[0][4];
	 $attendence_status =  $row_data[0][5];
     $mark = $row_data[0][6];
	 
//$table_name="_students_admission_form f LEFT JOIN _students_admission_mark_entry e ON f.admission_id = e.admission_id";
//$column="f.admission_id,f._candidate_name,f.admission_form_id,f._mobile_number,f._father_name,e.mark_entry_id,e.exam_mark,e.attendence_status,e.branch_id";
//$where_cond="f._session='".$current_session."' and f._class=".$selected_admission_details." ORDER BY f.admission_form_id ASC";

$table_name="_students_admission_form";
//$column="*";
$where_cond="admission_form_id='$form_no'";
$can_list=$obj->Details_By_Cond($table_name,$where_cond);
//echo $can_list['admission_id'];
//exit;

 $form_data = array(
						'exam_mark'=>$mark,
						//'_status'=>'A',
						'attendence_status'=>$attendence_status,
						'last_update_by'=>$_SESSION['LOGIN_USERID'],
						'last_update_date'=>date("Y:m:d h:i:s")
						);
						
$upresult =$obj->Update_Data("_students_admission_mark_entry", $form_data,"admission_id='".$can_list['admission_id']."'");
if($upresult!=NULL){
	$flag=1;
}
}
//echo $flag;
if($flag==1){
	$flag= 'ok';
}else{
	$flag='Error Uploading Data';
}
echo $flag;
?>
