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

class Nilai extends CI_Controller {

	//load model
	public function __construct()
	{
		parent::__construct();
		$this->load->model('nilai_model');
		//proteksi halaman
		$this->simple_login->check_login();
		//proteksi admin dan penguji
		if($this->session->userdata('level') !='Administrator') 
		{
			if($this->session->userdata('level') !='Penguji') 
			{
			//kalau bukan adamin, lempar ke login
			$this->session->set_flashdata('warning', 'Hak akses anda tidak diijinkan mengakses menu BTQ & Wawancara');
			redirect(base_url('login'),'refresh');		
			}
		}
	}

	//Data nilai
	public function index()
	{
		$nilai = $this->nilai_model->listing();
		$total = $this->nilai_model->total();

		//validasi input
		$valid = $this->form_validation;
		//check nama
		$valid->set_rules('nama','Nama Lengkap','required',
			array( 'required'		=> '%s harus diisi'));
		//check email
		$valid->set_rules('email','Email','required|valid_email',
			array(  'required'		=> '%s harus diisi',
					'valid_email'	=> '%s tidak valid. Masukan email yang benar'));
		//check nilainame
		$valid->set_rules('nilaiid','Nilaiid','required|is_unique[nilai2.nilaiid]',
			array(  'required'		=> '%s harus diisi',
					'is_unique'		=> '%s sudah ada. Buat nilaiid baru'));
		//check password
		$valid->set_rules('password','Password','required|min_length[6]|max_length[32]',
			array(  'required'		=> '%s harus diisi',
					'min_length'	=> '%s minimal 6 karakter',
					'max_length'	=> '%s maksimal 32 karakter'));
		//jika sudah dicek dan error
		if($valid->run()===FALSE) {
		//end validasi

			$data = array(	'title'		=> 'Jumlah Pendaftar Yang Sudah Tes BTQ & Wawancara [ '.$total->total.' ]',
						  	'nilai'		=> $nilai,
						  	'content' 	=> 'nilai/index'
						);
			$this->load->view('layout/wrapper', $data, FALSE);	
		//jika validasi oke, masuk database
		}else{
			$inp  = $this->input;
			$data = array(	'nama'			=> $inp->post('nama'),
							'email'			=> $inp->post('email'),
							'nilaiid'		=> $inp->post('nilaiid'),
							'password'		=> MD5($inp->post('password')),
							'level'			=> $inp->post('level'),
							'gambar'		=> $inp->post('gambar'),
						);
			//proses oleh model
			$this->nilai_model->tambah($data);
			//notifikasi dan redirect
			$this->session->set_flashdata('sukses', 'Data nilai telah ditambah');
			redirect(base_url('nilai'),'refresh');
		}
		//end masuk database
	}

	//Edit nilai
	public function edit($no_pendaftaran)
	{
		//panggil data nilai yang akan diedit
		$nilai = $this->nilai_model->detail($no_pendaftaran);
		
		//validasi input
		$valid = $this->form_validation;
		//check nama
		$valid->set_rules('btq_membaca','Nilai membaca','required',
				array( 'required'		=> '%s harus diisi'));

		//jika sudah dicek dan error
		if($valid->run()===FALSE) {
		//end validasi
		//end validasi
			$data = array(	'title'		=> 'Edit Data Nilai : '.$nilai->no_pendaftaran.' -- '.$nilai->nama_lengkap.' -- '.$nilai->sekolah_asal.' -- '.$nilai->kd_kompetensi,
						  	'nilai'		=> $nilai,
						  	'content' 	=> 'nilai/edit'
						);
			$this->load->view('layout/wrapper', $data, FALSE);	
			
		//jika validasi oke, masuk database
		//jika validasi oke, masuk database
		}else{
		
			$inp  = $this->input;
			$data = array(	    
								'no_pendaftaran'	=> $no_pendaftaran,
								'btq_membaca'		=> $inp->post('btq_membaca'),
								'btq_makhrojul'		=> $inp->post('btq_makhrojul'),	
								'btq_tajwid'		=> $inp->post('btq_tajwid'),
								'btq_huruftunggal'	=> $inp->post('btq_huruftunggal'),
								'btq_merangkaihuruf'=> $inp->post('btq_merangkaihuruf'),	
								'btq_kerapihan'		=> $inp->post('btq_kerapihan'),	
								'btq_akhlak '		=> $inp->post('btq_akhlak'),	
								'w2n_alasan'		=> $inp->post('w2n_alasan'),	
								'w2n_kepercayaan'	=> $inp->post('w2n_kepercayaan'),	
								'w2n_penanganan'	=> $inp->post('w2n_penanganan'),	
								'w2n_kebiasaan'		=> $inp->post('w2n_kebiasaan'),	
								'w2n_psikologi'		=> $inp->post('w2n_psikologi'),	
								'w2n_sholat'		=> $inp->post('w2n_sholat'),	
								'w2n_keikutsertaan'	=> $inp->post('w2n_keikutsertaan'),	
								'w2n_kesediaan'		=> $inp->post('w2n_kesediaan'),	
								'catatan'			=> $inp->post('catatan'),
								'penguji'           => $this->session->userdata('nama'),
                      			'tanggal'           => date('Y-m-d H:i:s')									
								
						);


			//proses oleh model
			$this->nilai_model->edit($data);
			//notifikasi dan redirect
			$this->session->set_flashdata('sukses', 'Data nilai '.$nilai->nama_lengkap.' telah diedit');
			redirect(base_url('nilai'),'refresh');
		}
		//end masuk database
	}

	//Delete nilai
	public function delete($no_pendaftaran)
	{
		$data = array('no_pendaftaran' => $no_pendaftaran);
		//proses hapus
		$this->nilai_model->delete($data);
		//notifikasi
		$this->session->set_flashdata('sukses', 'Data nilai telah dihapus');
		redirect(base_url('nilai'),'refresh');
	}



	//EXPORT NILAI KE EXCEL
	public function export(){
    // Load plugin PHPExcel nya
    include APPPATH.'third_party/PHPExcel/PHPExcel.php';
    
    // Panggil class PHPExcel nya
    $excel = new PHPExcel();
    // Settingan awal fil excel
    $excel->getProperties()->setCreator('By : 415 DIL')
                 ->setLastModifiedBy('By : 415 DIL')
                 ->setTitle("Data Nilai")
                 ->setSubject("Nilai")
                 ->setDescription("Laporan Data Nilai BTQ & Wawancara")
                 ->setKeywords("Data Nilai");
    // Buat sebuah variabel untuk menampung pengaturan style dari header tabel
    $style_col = array(
      'font' => array('bold' => true), // Set font nya jadi bold
      'alignment' => array(
        'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, // Set text jadi ditengah secara horizontal (center)
        'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER // Set text jadi di tengah secara vertical (middle)
      ),
      'borders' => array(
        'top' => array('style'  => PHPExcel_Style_Border::BORDER_THIN), // Set border top dengan garis tipis
        'right' => array('style'  => PHPExcel_Style_Border::BORDER_THIN),  // Set border right dengan garis tipis
        'bottom' => array('style'  => PHPExcel_Style_Border::BORDER_THIN), // Set border bottom dengan garis tipis
        'left' => array('style'  => PHPExcel_Style_Border::BORDER_THIN) // Set border left dengan garis tipis
      )
    );
    // Buat sebuah variabel untuk menampung pengaturan style dari isi tabel
    $style_row = array(
      'alignment' => array(
        'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER // Set text jadi di tengah secara vertical (middle)
      ),
      'borders' => array(
        'top' => array('style'  => PHPExcel_Style_Border::BORDER_THIN), // Set border top dengan garis tipis
        'right' => array('style'  => PHPExcel_Style_Border::BORDER_THIN),  // Set border right dengan garis tipis
        'bottom' => array('style'  => PHPExcel_Style_Border::BORDER_THIN), // Set border bottom dengan garis tipis
        'left' => array('style'  => PHPExcel_Style_Border::BORDER_THIN) // Set border left dengan garis tipis
      )
    );
    $excel->setActiveSheetIndex(0)->setCellValue('A1', "DATA NILAI BTQ & WAWANCARA PPDB SMK AL-MUHTADIN DEPOK"); // Set kolom A1 dengan tulisan "DATA SISWA"
    $excel->getActiveSheet()->mergeCells('A1:Y1'); // Set Merge Cell pada kolom A1 sampai E1
    $excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(TRUE); // Set bold kolom A1
    $excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(15); // Set font size 15 untuk kolom A1
    $excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // Set text center untuk kolom A1
    // Buat header tabel nya pada baris ke 3
    $excel->setActiveSheetIndex(0)->setCellValue('A3', "NO."); // Set kolom A3 dengan tulisan "NO"
    $excel->setActiveSheetIndex(0)->setCellValue('B3', "NO. PEND"); // Set kolom B3 dengan tulisan "NO.PEND"
    $excel->setActiveSheetIndex(0)->setCellValue('C3', "NAMA LENGKAP"); // Set kolom C3 dengan tulisan "NAMA"
    $excel->setActiveSheetIndex(0)->setCellValue('D3', "JENIS KELAMIN"); // Set kolom D3 dengan tulisan "JENIS KELAMIN"
    $excel->setActiveSheetIndex(0)->setCellValue('E3', "SEKOLAH ASAL"); // Set kolom E3 dengan tulisan "SEKOLAH"
    $excel->setActiveSheetIndex(0)->setCellValue('F3', "KOMPETENSI");
    $excel->setActiveSheetIndex(0)->setCellValue('G3', "GELOMBANG");
    $excel->setActiveSheetIndex(0)->setCellValue('H3', "1B-MEMBACA");
    $excel->setActiveSheetIndex(0)->setCellValue('I3', "2B-MAKHROJUL");
    $excel->setActiveSheetIndex(0)->setCellValue('J3', "3B-TAJWID");
    $excel->setActiveSheetIndex(0)->setCellValue('K3', "4B-HURUF TUNGGAL");
    $excel->setActiveSheetIndex(0)->setCellValue('L3', "5B-MERANGKAI HURUF");
    $excel->setActiveSheetIndex(0)->setCellValue('M3', "6B-KERAPIHAN");
    $excel->setActiveSheetIndex(0)->setCellValue('N3', "7B-AKHLAK");   
    $excel->setActiveSheetIndex(0)->setCellValue('O3', "1W-ALASAN");
    $excel->setActiveSheetIndex(0)->setCellValue('P3', "2W-KEPERCAYAAN");
    $excel->setActiveSheetIndex(0)->setCellValue('Q3', "3W-PENANGANAN");
    $excel->setActiveSheetIndex(0)->setCellValue('R3', "4W-KEBIASAAN");
    $excel->setActiveSheetIndex(0)->setCellValue('S3', "5W-PSIKOLOGI");
    $excel->setActiveSheetIndex(0)->setCellValue('T3', "6W-SHOLAT");
    $excel->setActiveSheetIndex(0)->setCellValue('U3', "7W-KEIKUTSERTAAN");
    $excel->setActiveSheetIndex(0)->setCellValue('V3', "8W-KESEDIAAN");
    $excel->setActiveSheetIndex(0)->setCellValue('W3', "CATATAN");
    $excel->setActiveSheetIndex(0)->setCellValue('X3', "PENGUJI");
    $excel->setActiveSheetIndex(0)->setCellValue('Y3', "TANGGAL TES");
   
    // Apply style header yang telah kita buat tadi ke masing-masing kolom header
    $excel->getActiveSheet()->getStyle('A3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('B3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('C3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('D3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('E3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('F3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('G3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('H3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('I3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('J3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('K3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('L3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('M3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('N3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('O3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('P3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('Q3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('R3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('S3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('T3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('U3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('V3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('W3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('X3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('Y3')->applyFromArray($style_col);
    // Panggil function view yang ada di nilai_model untuk menampilkan semua data nilainya
    $nilai = $this->nilai_model->ViewUrut();
    $no = 1; // Untuk penomoran tabel, di awal set dengan 1
    $numrow = 4; // Set baris pertama untuk isi tabel adalah baris ke 4
    foreach($nilai as $data){ // Lakukan looping pada variabel nilai
      $excel->setActiveSheetIndex(0)->setCellValue('A'.$numrow, $no);
      $excel->setActiveSheetIndex(0)->setCellValue('B'.$numrow, $data->no_pendaftaran);
      $excel->setActiveSheetIndex(0)->setCellValue('C'.$numrow, $data->nama_lengkap);
      $excel->setActiveSheetIndex(0)->setCellValue('D'.$numrow, $data->jkelamin);
      $excel->setActiveSheetIndex(0)->setCellValue('E'.$numrow, $data->sekolah_asal);
      $excel->setActiveSheetIndex(0)->setCellValue('F'.$numrow, $data->kd_kompetensi);
      $excel->setActiveSheetIndex(0)->setCellValue('G'.$numrow, $data->gelombang);
      $excel->setActiveSheetIndex(0)->setCellValue('H'.$numrow, $data->btq_membaca);
      $excel->setActiveSheetIndex(0)->setCellValue('I'.$numrow, $data->btq_makhrojul);
      $excel->setActiveSheetIndex(0)->setCellValue('J'.$numrow, $data->btq_tajwid);
      $excel->setActiveSheetIndex(0)->setCellValue('K'.$numrow, $data->btq_huruftunggal);
      $excel->setActiveSheetIndex(0)->setCellValue('L'.$numrow, $data->btq_merangkaihuruf);
      $excel->setActiveSheetIndex(0)->setCellValue('M'.$numrow, $data->btq_kerapihan);
      $excel->setActiveSheetIndex(0)->setCellValue('N'.$numrow, $data->btq_akhlak);
      $excel->setActiveSheetIndex(0)->setCellValue('O'.$numrow, $data->w2n_alasan);
      $excel->setActiveSheetIndex(0)->setCellValue('P'.$numrow, $data->w2n_kepercayaan);
      $excel->setActiveSheetIndex(0)->setCellValue('Q'.$numrow, $data->w2n_penanganan);
      $excel->setActiveSheetIndex(0)->setCellValue('R'.$numrow, $data->w2n_kebiasaan);
      $excel->setActiveSheetIndex(0)->setCellValue('S'.$numrow, $data->w2n_psikologi);
      $excel->setActiveSheetIndex(0)->setCellValue('T'.$numrow, $data->w2n_sholat);
      $excel->setActiveSheetIndex(0)->setCellValue('U'.$numrow, $data->w2n_keikutsertaan);
      $excel->setActiveSheetIndex(0)->setCellValue('V'.$numrow, $data->w2n_kesediaan);
      $excel->setActiveSheetIndex(0)->setCellValue('W'.$numrow, $data->catatan);
      $excel->setActiveSheetIndex(0)->setCellValue('X'.$numrow, $data->penguji);
      $excel->setActiveSheetIndex(0)->setCellValue('Y'.$numrow, $data->tanggal);
      
      // Apply style row yang telah kita buat tadi ke masing-masing baris (isi tabel)
      $excel->getActiveSheet()->getStyle('A'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('B'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('C'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('D'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('E'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('F'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('G'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('H'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('I'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('J'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('K'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('L'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('M'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('N'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('O'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('P'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('Q'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('R'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('S'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('T'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('U'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('V'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('W'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('X'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('Y'.$numrow)->applyFromArray($style_row);
      
      $no++; // Tambah 1 setiap kali looping
      $numrow++; // Tambah 1 setiap kali looping
    }
    // Set width kolom
    $excel->getActiveSheet()->getColumnDimension('A')->setWidth(5); // Set width kolom A
    $excel->getActiveSheet()->getColumnDimension('B')->setWidth(12); // Set width kolom B
    $excel->getActiveSheet()->getColumnDimension('C')->setWidth(35); // Set width kolom C
    $excel->getActiveSheet()->getColumnDimension('D')->setWidth(15); // Set width kolom D
    $excel->getActiveSheet()->getColumnDimension('E')->setWidth(35); // Set width kolom E
    $excel->getActiveSheet()->getColumnDimension('F')->setWidth(13);
    $excel->getActiveSheet()->getColumnDimension('G')->setWidth(13);
    $excel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
    $excel->getActiveSheet()->getColumnDimension('I')->setWidth(15);
    $excel->getActiveSheet()->getColumnDimension('J')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('K')->setWidth(19);
    $excel->getActiveSheet()->getColumnDimension('L')->setWidth(21);
    $excel->getActiveSheet()->getColumnDimension('M')->setWidth(15);
    $excel->getActiveSheet()->getColumnDimension('N')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('O')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('P')->setWidth(18);
    $excel->getActiveSheet()->getColumnDimension('Q')->setWidth(18);
    $excel->getActiveSheet()->getColumnDimension('R')->setWidth(15);
    $excel->getActiveSheet()->getColumnDimension('S')->setWidth(15);
    $excel->getActiveSheet()->getColumnDimension('T')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('U')->setWidth(19);
    $excel->getActiveSheet()->getColumnDimension('V')->setWidth(15);
    $excel->getActiveSheet()->getColumnDimension('W')->setWidth(30);
    $excel->getActiveSheet()->getColumnDimension('X')->setWidth(25);
    $excel->getActiveSheet()->getColumnDimension('Y')->setWidth(20);
    
    // Set height semua kolom menjadi auto (mengikuti height isi dari kolommnya, jadi otomatis)
    $excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(-1);
    // Set orientasi kertas jadi LANDSCAPE
    $excel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
    // Set judul file excel nya
    $excel->getActiveSheet(0)->setTitle("Data Nilai BTQ & Wawancara");
    $excel->setActiveSheetIndex(0);
    // Proses file excel
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment; filename="Data Nilai BTQ & Wawancara.xlsx"'); // Set nama file excel nya
    header('Cache-Control: max-age=0');
    $write = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
    $write->save('php://output');
  }

}

/* End of file Nilai.php */
/* Location: ./application/controllers/Nilai.php */