<?php
error_reporting(0);//(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj=new my_class();
	
extract($_GET);
$section_id;
$ct_id;
//$subject_id;
$session;
$exam_term;
//echo "section:".$section_id." quiz:".$quiz_type_id." subject:".$subject_id." session:".$session." exam_term".$exam_term;
$cl_sec = $obj->Details_By_Cond("_int_institute_setup_vw","section_id='$section_id'");
extract($cl_sec);
$cal = $cl_sec['class'];
$class_id = $cl_sec['class_id'];
$dep = $cl_sec['department'];
$department_id = $cl_sec['department_id'];
$s_t = $cl_sec['student_type'];
$section = $cl_sec['section'];


$ct_info = $obj->Details_By_Cond("_ct_class_test_add","_id='$ct_id'");
extract($ct_info);
$ct_name = $ct_info['_ct_name'];	
//$sub_det = $obj->Details_By_Cond("_int_subject","_id='$subject_id'");
//extract($sub_det);
//$subject_short_name = $sub_det['_subject_short_name'];
//$sub_name = $sub_det['_subject_full_name'];
$sublist = $obj->View_colmn_By_Cond("_int_subject","_id,_subject_full_name,_subject_short_name,_subject_code,_subject_type,_subject_catagori","_dept_id='$department_id' AND _status='A' ORDER BY _order_no");

if(file_exists('../../asset/custom/upload/ct_empty_all_sub_'.$ct_id.'_'.$exam_term.'Class_'.$class_id.'_'.$section_id.'_'.$session .'.xlsx')) {
    unlink('../../asset/custom/upload/ct_empty_all_sub_'.$ct_id.'_'.$exam_term.'Class_'.$class_id.'_'.$section_id.'_'.$session .'.xlsx');
}
	
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');
	
	
$filename = '../../asset/custom/upload/ct_empty_sample_mark_sheet_'.$class_id.'_'.$section_id.'_'.$ct_id.'_'.$exam_term.'_'.$session .'.xlsx';
		
		try {
			
			
			
		$objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("MokNathal")
                                     ->setLastModifiedBy("MokNathal")
                                     ->setTitle("STAFF REPORT")
                                     ->setSubject("STAFF REPORT")
                                     ->setDescription("STAFF REPORT")
                                     ->setKeywords("STAFF REPORT")
                                     ->setCategory("STAFF REPORT");

 

        $objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
        $objPHPExcel->setActiveSheetIndex(0)
                    ->getCell('A1')->setValue("Class:". $cal);
					
        $objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getAlignment()->setWrapText(true);       

        $objPHPExcel->getActiveSheet()->mergeCells('A2:F2');
        $objPHPExcel->setActiveSheetIndex(0)
                    ->getCell('A2')->setValue( "Student Type:". $student_type);
        $objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getAlignment()->setWrapText(true);       

        $objPHPExcel->getActiveSheet()->mergeCells('A3:F3');
        $objPHPExcel->setActiveSheetIndex(0)
                    ->getCell('A3')->setValue( "Section:". $section);
        $objPHPExcel->getActiveSheet()->getStyle('A3:F3')->getAlignment()->setWrapText(true);           

        $objPHPExcel->getActiveSheet()->mergeCells('A4:F4');
        $objPHPExcel->setActiveSheetIndex(0)
                    ->getCell('A4')->setValue("Department:".$dep);
        $objPHPExcel->getActiveSheet()->getStyle('A4:F4')->getAlignment()->setWrapText(true);  

		
		//$objPHPExcel->getActiveSheet()->mergeCells('A5:F5');
        //$objPHPExcel->setActiveSheetIndex(0)
                    //->getCell('A5')->setValue("Subject:". $sub_name);
        //$objPHPExcel->getActiveSheet()->getStyle('A4:F4')->getAlignment()->setWrapText(true);  

		

        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A3')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A4')->getFont()->setBold(true);
        //$objPHPExcel->getActiveSheet()->getStyle('A5')->getFont()->setBold(true);
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A5', 'SL')
                     ->setCellValue('B5', 'Student ID')
                     ->setCellValue('C5', 'Full Name')
                     ->setCellValue('D5', 'Roll');
		$col=4;
		foreach($sublist as $index=>$value){
			//echo '<th>'.$value['_subject_short_name'].'</th>';
			//$subject_id=$value['_id'];
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 5, $value['_subject_short_name']);
			//$objPHPExcel->getActiveSheet()->getStyle($col)->getFont()->setBold(true);
			$col++;	
		}
                     // ->setCellValue('E6', 'Mark');
                       //->setCellValue('F6', 'Objective')
					   //->setCellValue('G6', 'Class Test')
					   //->setCellValue('H6', 'Practical')
					   //->setCellValue('I6', 'Spot Test');

        $objPHPExcel->getActiveSheet()->getStyle('A5:E5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objPHPExcel->getActiveSheet()->getStyle('A5')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('B5')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('C5')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('D5')->getFont()->setBold(true);
       // $objPHPExcel->getActiveSheet()->getStyle('E6')->getFont()->setBold(true);
        //$objPHPExcel->getActiveSheet()->getStyle('F6')->getFont()->setBold(true);
		//$objPHPExcel->getActiveSheet()->getStyle('G6')->getFont()->setBold(true);
		//$objPHPExcel->getActiveSheet()->getStyle('H6')->getFont()->setBold(true);
		//$objPHPExcel->getActiveSheet()->getStyle('I6')->getFont()->setBold(true);
	
		$row = 6;
		
		$table="_pf_std_personal_info a LEFT JOIN _pf_std_basic_info b ON a._id=b._pid";
			$column="a._id as std_id,a._uniq_id,a._full_name,a._section_roll";
			$where="b._session=$session AND b._section_id=$section_id AND a._status='A' ORDER BY a._section_roll";
		
		foreach($obj->View_all_data_row_column_By_Cond($table,$column,$where) as $v1){
        
			//extract($value);	 
			
			$slno = $row-5;
			$std_id = $v1['std_id'];
			$uniq_id =  $v1['_uniq_id']; 
			$fullname = $v1['_full_name']; 
			/*if(!empty($value['_nick_name']))
			{
				
			$fullname .= ' ('.$value['_nick_name'].')'; 
			} 	*/	 
			$section_roll =  $v1['_section_roll'];  
			$objPHPExcel->setActiveSheetIndex(0)
                         ->setCellValue('A'.$row, $slno)
                         ->setCellValue('B'.$row, $uniq_id)
                         ->setCellValue('C'.$row, $fullname)
						 ->setCellValue('D'.$row, $section_roll);
			$col1=4;
			foreach($obj->View_all_data_row_column_By_Cond("_ct_mark_sheet ct LEFT JOIN _int_subject ins ON ct._subject_id=ins._id","ct._subject_id","_ct_id=$ct_id AND _section_id=$section_id AND _std_id=$std_id AND _exam_term='$exam_term' AND ins._status='A' ORDER BY ins._order_no") as $v2){
				$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col1, $row, '');
				$col1++;
			}
						 //->setCellValue('E'.$i, '')
						 //->setCellValue('F'.$i, '')
						 //->setCellValue('G'.$i, '')
						 //->setCellValue('H'.$i, '')
						  //->setCellValue('I'.$i, '');
						  
						  

             $row++;
			// echo $slno."-".$uniq_id."-".$fullname."-".$section_roll;
			// echo "<br/>";
			 
		

        }
        foreach(range('A','E') as $columnID) {
            $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
                ->setAutoSize(true);
				
        }


	
        $objPHPExcel->getActiveSheet()->setTitle('Trasaction List');
        $objPHPExcel->setActiveSheetIndex(0);
	ob_clean();
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
	
	
   /*if(file_exist('../../asset/custom/upload/'.$subject_short_name.'_Class_'.$cal.'_mark_sheet_'.$class_id.'_Section_'.$subject_id .'_'.$section_id.'_'.$exam_term_id.'_'.$exam_session .'.xlsx')) 
	{
    unlink('../../asset/custom/upload/'.$subject_short_name.'_Class_'.$cal.'_mark_sheet_'.$class_id.'_Section_'.$subject_id .'_'.$section_id.'_'.$exam_term_id.'_'.$exam_session .'.xlsx');
    }
	*/
	
	//echo '../../asset/custom/upload/'.$subject_short_name.'_Class_'.$cal.'_mark_sheet_'.$class_id.'_Section_'.$subject_id .'_'.$section_id.'_'.$exam_term_id.'_'.$exam_session .'.xlsx';
	//$objWriter->delete('../../asset/custom/upload/'.$subject_short_name.'_Class_'.$cal.'_mark_sheet_'.$class_id.'_Section_'.$subject_id .'_'.$section_id.'_'.$exam_term_id.'_'.$exam_session .'.xlsx');
    
	//ob_end_clean();
	$objWriter->save('../../asset/custom/upload/ct_all_sub_empty_mark'.'_Class_'.$cal.'_mark_sheet_'.$class_id.'_Section_'.$section_id.'_'.$ct_id.'_'.$exam_term.'_'.$session .'.xlsx');
    header("Location: ../../asset/custom/upload/ct_all_sub_empty_mark".'_Class_'.$cal.'_mark_sheet_'.$class_id.'_Section_'.$section_id.'_'.$ct_id.'_'.$exam_term.'_'.$session .'.xlsx');
	
	//$objWriter->save('/tmp/'.$subject_short_name.'_Class_'.$cal.'_mark_sheet_'.$class_id.'_Section_'.$section.'_'.$subject_id .'_'.$section_id.'_'.$exam_term_id.'_'.$exam_session .'.xlsx'");
    //header("Location: /tmp/".$subject_short_name.'_Class_'.$cal.'_mark_sheet_'.$class_id.'_Section_'.$section.'_'.$subject_id .'_'.$section_id.'_'.$exam_term_id.'_'.$exam_session .'.xlsx');
	
	//ob_clean();
	}
	catch (Exception $e)
	{
		

    echo 'Caught exception: ',  $e->getMessage(), "\n";
	}
?>