<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Manage_test extends CI_Controller {	 
	public function __construct() {
        parent::__construct();
		if($this->session->userdata('mapmyadmin')['Logged_In']){
			$this->load->model('webadmin/Custom_model','model');
			 
		}else{
			redirect(ADMIN_URL);
		}			
    }
	
	public function index(){ 	
		$data['lists'] = $this->model->get_test_list($this->session->userdata('mapmyadmin')['AdminId']);
		$this->load->view('webadmin/includes/mapmycareer_header',$data);
		$this->load->view('webadmin/manage_test_list_view');
		$this->load->view('webadmin/includes/mapmycareer_footer');		
	}
	
	function exports_student(){
		
		$data['lists'] = $this->model->get_test_list($this->session->userdata('mapmyadmin')['AdminId']);
		$this->load->view('webadmin/includes/mapmycareer_header',$data);
		$this->load->view('webadmin/create_student_login_view');
		$this->load->view('webadmin/includes/mapmycareer_footer');
	}
	
	
	//this function used only internal purpose
	function exports(){
		var_dump($this->input->get());die;
		if($this->input->get()){
			 $test_id=$this->input->get('test_id');
			$limit=$this->input->get('limit');
			$code=$this->input->get('code');
			$sku='SVSU';
			//$code=601;
			$export_array=array();
			$export_array[] = array('ID','StudentCode', 'Email','Password');
			for($i=1;$i<=$limit;$i++){
				
				$sku_code=$sku .''.$code;
				$email=strtolower($sku_code.'@mmc.in');
				$password = strtolower($sku_code);
				$export_array_object=array(
					'ID'=>$i,
					'StudentCode'=>$sku_code,
					'Email'=>$email,					
					'Password'=>$password
				);
				$export_array[] = $export_array_object;
				 $ins_student=array(
					'ParticipateTestId' => $test_id,
					'StudentCode'=>$sku_code,
					'EmailId'=>$email,
					'Password'=>md5($password)
				);
				$this->db->insert('tblstudents',$ins_student);
				
				$last_id = $this->db->insert_id();
				 
				$ins_status=array(
					'ParticipateTestId' => $test_id,
					'StudentId'=>$last_id
				);
				$this->db->insert('tblstudents_test_status',$ins_status);
				
				$code++;
				
			}
			
			$this->create_excel($export_array); 
			
			
			//redirect(ADMIN_URL);
		}else{
			redirect(ADMIN_URL);
		}
	}
	
	function create_excel($exceldata){
		//var_dump($Contacts[0]);die;
		$this->load->library('PHPExcel');
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->setActiveSheetIndex(0);
		
		 
		$maxWidth = 20;
		//var_dump($exceldata);die;

		$objPHPExcel->getActiveSheet()->getStyle('A1:I1')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(false)->setWidth($maxWidth); //id
		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(false)->setWidth($maxWidth); //name
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(false)->setWidth($maxWidth); //cat  
		
		$objPHPExcel->getActiveSheet()->fromArray($exceldata, null, 'A1');
       
		$filename='student_500.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		ob_end_clean();
		
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		$objWriter->save('php://output');

	}
	 
}