<?php defined('BASEPATH')or exit('No Direct Access is Allowed');?>
<?php
class Export extends MY_Controller{
	public function __construct(){
	
		 parent::__construct();
		 
		$this->load->helper(['form','url','html','security']);
		$this->load->library(['pagination','form_validation','csvimport']);
		 $this->load->helper('csv');
		$this->load->library('zip');
		
		ini_set('max_execution_time',0);
		define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
		date_default_timezone_set('Asia/Kolkata');
		error_reporting(0);


	}
	
	public function index(){
		$this->load->view('adminlte/panel/header',['userdata'=>$this->user_data,'title'=>'Csv Data Export Master List','menu_auth'=>$this->menu_auth]);
		$this->load->view('adminlte/panel/sidebar');
		$this->load->view('adminlte/master/export/export');
		$this->load->view('adminlte/panel/footer');
	}
	

	
	/*public function export_iadone($query, $filename =''){
	
		$this->load->dbutil();
        $this->load->helper('download');
        $delimiter = ",";
        $newline = "\r\n";
		$enclosure = '"';
		 
		 $query=$this->db->select(['iadone.project','iadone.circle','iadone.siteid','iadone.projectcode','iadone.wpid',
		 'iadone.packagename','iadone.itemid','iadone.workitem','iadone.subtype','iadone.vendor','iadone.ponumber'
		 ,'iadone.company','iadone.complete_date','iadone.ia_date','iadone.gdsrecipt_date','iadone.technology'])
		->from('iadone')->order_by('iadone.id','ASC')
		->get();
        $data = $this->dbutil->csv_from_result($query, $delimiter, $newline,$enclosure );
		$filename="IA_Done_".date('D-d-M-Y h:i:s:A').'.csv';
        force_download($filename, $data);
	
	}*/
	
	public function export_category(){
	
		$this->load->helper('csv');
        $delimiter 	= ",";
        $newline 	= "\r\n";
		$enclosure 	= '"';
		
		$filename='category_Export_'.date('D-d-M-Y h:i:s:A').'.csv';
		$header=array("Category","Description","Category Code");
		
		$get	=	array();
		array_push($get,$header);
		array_to_csv($get, $filename);
		
		$query=$this->db->select(['category','description','category_code'])
		->from('category')->order_by('category.id','ASC')->get();
		$g		=	$query->result();
		$get	=	array();
		foreach($g as $p){
				$csv10	=	array($p->category,$p->description,$p->category_code);
				array_push($get,$csv10);unset($csv10);
		}
		
		$filename='category_Export_'.date('D-d-M-Y h:i:s:A').'.csv';
		array_to_csv($get, $filename);	
	
	}
	
	public function export_class(){
	
		$this->load->helper('csv');
        $delimiter 	= ",";
        $newline 	= "\r\n";
		$enclosure 	= '"';
		
		$filename='export_Class_'.date('D-d-M-Y h:i:s:A').'.csv';
		$header=array("Class","Description","category Name","Class Code");
		
		$get	=	array();
		array_push($get,$header);
		array_to_csv($get, $filename);
		
		$query=$this->db->select(['class.*','Category.category','category.category_code'])
		->from('class')->join('category','class.category_id=category.id')->order_by('class.id','ASC')->get();
		$g		=	$query->result();
		
		$get	=	array();
		foreach($g as $p){
				$csv10	=	array(strtoupper($p->class),strtoupper($p->description),strtoupper($p->category),strtoupper($p->category_code.$p->class_code));
				array_push($get,$csv10);unset($csv10);
		}
		
		$filename='export_Class_'.date('D-d-M-Y h:i:s:A').'.csv';
		array_to_csv($get, $filename);	
	
	}
	
		public function export_subject(){
	
		$this->load->helper('csv');
        $delimiter 	= ",";
        $newline 	= "\r\n";
		$enclosure 	= '"';
		
		$filename='export_Subject_'.date('D-d-M-Y h:i:s:A').'.csv';
		$header=array("Subject","Description","Category Name","Class", "Subject Code");
		
		$get	=	array();
		array_push($get,$header);
		array_to_csv($get, $filename);
		
		$query=$this->db->select(['subject.*','category.category','category.category_code','class.class','class.class_code'])
		->from('subject')
		->join('category','subject.category_id=category.id')
		->join('class','subject.class_id=class.id')
		->order_by('subject.id','ASC')->get();
		$g		=	$query->result();
		
		$get	=	array();
		foreach($g as $p){
				$csv10	=	array(strtoupper($p->subject),strtoupper($p->description),strtoupper($p->category),strtoupper($p->class),strtoupper($p->category_code.$p->class_code.$p->subject_code));
				array_push($get,$csv10);unset($csv10);
		}
		
		$filename='export_Subject_'.date('D-d-M-Y h:i:s:A').'.csv';
		array_to_csv($get, $filename);	
	
	}
	public function export_faqulty(){
	
		$this->load->helper('csv');
        $delimiter 	= ",";
        $newline 	= "\r\n";
		$enclosure 	= '"';
		
		$filename='export_Faqulty'.date('D-d-M-Y h:i:s:A').'.csv';
		$header=array("Category Name","Class","Subject","Subject Code", "Description", "Faculty");
		
		$get	=	array();
		array_push($get,$header);
		array_to_csv($get, $filename);
		
		$query=$this->db->select(['v2_faculty.*','category.category','category.category_code','class.class','class.class_code',
		    'subject.subject','subject.subject_code','master_user.email'])
		->from('v2_faculty')
		->join('category','v2_faculty.category_id=category.id')
		->join('class','v2_faculty.class_id=class.id')
		->join('subject','v2_faculty.subject_id=subject.id')
		->join('master_user','v2_faculty.teacher_id=master_user.id')
		->order_by('subject.id','ASC')->get();
		$g		=	$query->result();
		
		$get	=	array();
		foreach($g as $p){
				$csv10	=	array(strtoupper($p->category),strtoupper($p->class),strtoupper($p->subject),strtoupper($p->category_code.$p->class_code.$p->subject_code),strtoupper($p->description),strtoupper($p->email));
				array_push($get,$csv10);unset($csv10);
		}
		
		$filename='export_Faqulty'.date('D-d-M-Y h:i:s:A').'.csv';
		array_to_csv($get, $filename);	
	
	}
	
	public function export_student_registration(){
	
		$this->load->helper('csv');
        $delimiter 	= ",";
        $newline 	= "\r\n";
		$enclosure 	= '"';
		
		$filename='student_Export_'.date('D-d-M-Y h:i:s:A').'.csv';
		$header=array("Attendee","Email","Mobile", "Gender");
		
		$get	=	array();
		array_push($get,$header);
		array_to_csv($get, $filename);
		
		$query=$this->db->select(['first_name','last_name','email','mobile','gender'])
		->from('master_user')->where('master_user.category','2')->order_by('master_user.id','ASC')->get();
		$g		=	$query->result();
		$get	=	array();
		foreach($g as $p){
				$csv10	=	array(strtoupper($p->first_name.$p->last_name),strtoupper($p->email),strtoupper($p->mobile),strtoupper($p->gender),);
				array_push($get,$csv10);unset($csv10);
		}
		
		$filename='student_Export_'.date('D-d-M-Y h:i:s:A').'.csv';
		array_to_csv($get, $filename);	
	
	}
	public function export_teacher_registration(){
	
		$this->load->helper('csv');
        $delimiter 	= ",";
        $newline 	= "\r\n";
		$enclosure 	= '"';
		
		$filename='teacher_Export_'.date('D-d-M-Y h:i:s:A').'.csv';
		$header=array("Attendee","Email","Mobile", "Gender");
		
		$get	=	array();
		array_push($get,$header);
		array_to_csv($get, $filename);
		
		$query=$this->db->select(['first_name','last_name','email','mobile','gender'])
		->from('master_user')->where('master_user.category','1')->order_by('master_user.id','ASC')->get();
		$g		=	$query->result();
		$get	=	array();
		foreach($g as $p){
				$csv10	=	array(strtoupper($p->first_name.$p->last_name),strtoupper($p->email),strtoupper($p->mobile),strtoupper($p->gender),);
				array_push($get,$csv10);unset($csv10);
		}
		
		$filename='teacher_Export_'.date('D-d-M-Y h:i:s:A').'.csv';
		array_to_csv($get, $filename);	
	
	}
	
	public function export_chapter(){
	
		$this->load->helper('csv');
        $delimiter 	= ",";
        $newline 	= "\r\n";
		$enclosure 	= '"';
		
		$filename='chapter_Export'.date('D-d-M-Y h:i:s:A').'.csv';
		$header=array("Category","Class","Subject", "Chapter", "Chapter Code", "Description");
		
		$get	=	array();
		array_push($get,$header);
		array_to_csv($get, $filename);
		
		$query  = $this->db->select(['chapter.*','category.category','category.category_code','class.class','class.class_code','subject.subject','subject.subject_code'])
	    ->from('chapter')
		->join('category','chapter.category_id=category.id')
		->join('class','chapter.class_id=class.id')
		->join('subject','chapter.subject_id=subject.id')
		->order_by('chapter.id','ASC')
		->get();
		$g		=	$query->result();
		$get	=	array();
		foreach($g as $p){
				$csv10	=	array(strtoupper($p->category),strtoupper($p->class),strtoupper($p->subject),
							strtoupper($p->chapter),strtoupper($p->category_code.$p->class_code.$p->subject_code.$p->chapter_code),strtoupper($p->description));
				array_push($get,$csv10);unset($csv10);
		}
		
		$filename='chapter_Export'.date('D-d-M-Y h:i:s:A').'.csv';
		array_to_csv($get, $filename);	
	
	}
	
	

	public function export_topic(){
	
		$this->load->helper('csv');
        $delimiter 	= ",";
        $newline 	= "\r\n";
		$enclosure 	= '"';
		
		$filename='topic_Export_'.date('D-d-M-Y h:i:s:A').'.csv';
		$header=array("Category","Class","Subject", "Chapter", "Topic", "Topic Code","Description");
		
		$get	=	array();
		array_push($get,$header);
		array_to_csv($get, $filename);
		
		$query  = $this->db->select(['topic.*','category.category','category.category_code','class.class','class.class_code',
		'subject.subject','subject.subject_code'])
	    ->from('topic')
		->join('category','topic.category_id=category.id')
		->join('class','topic.class_id=class.id')
		->join('subject','topic.subject_id=subject.id')
		->order_by('topic.id','ASC')
		->get();
		$g		=	$query->result();
		$get	=	array();
		foreach($g as $p){
				$csv10	=	array(strtoupper($p->category),strtoupper($p->class),strtoupper($p->subject),strtoupper($p->chapter),strtoupper($p->topic),
				strtoupper($p->category_code.$p->subject_code.$p->class_code.$p->topic_code),strtoupper($p->description));
				array_push($get,$csv10);unset($csv10);
		}
		
		$filename='topic_Export_'.date('D-d-M-Y h:i:s:A').'.csv';
		array_to_csv($get, $filename);	
	
	}
	
	public function export_student_query(){
	
		$this->load->helper('csv');
        $delimiter 	= ",";
        $newline 	= "\r\n";
		$enclosure 	= '"';
		
		$filename='student_Export_'.date('D-d-M-Y h:i:s:A').'.csv';
		$header=array("Category","Class","Subject", "Subject Code", "Month", "Year","Student", "Email");
		
		$get	=	array();
		array_push($get,$header);
		array_to_csv($get, $filename);
		$query        =   $this->db->select(['pr.*',
            'class.class','category.category','subject.subject','master_user.first_name','master_user.last_name','master_user.email',
             'master_user.mobile','master_user.gender'])
            ->from('v2_student_request_program as pr')
            ->join('class','pr.class_id=class.id')
            ->join('category','pr.category_id=category.id')
            ->join('subject','pr.subject_id=subject.id')
            ->join('master_user','master_user.id=pr.user_id')	
			->order_by('pr.id','ASC')
			->get();
		$g		=	$query->result();
		$get	=	array();
		foreach($g as $p){
				$csv10	=	array(strtoupper($p->category),strtoupper($p->class),strtoupper($p->subject),strtoupper($p->subject_code),
				strtoupper($p->month),strtoupper($p->year),strtoupper($p->first_name.$p->last_name),strtoupper($p->email));
				array_push($get,$csv10);unset($csv10);
		}
		
		$filename='student_Export_'.date('D-d-M-Y h:i:s:A').'.csv';
		array_to_csv($get, $filename);	
	
	}
	

}

?>