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

class Soal extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('mo_soal');
		$config = array('module' => 'soal','roleid'=> $this->session->userdata('sRoleId'));
		$this->load->library('rolemodule',$config);
    }

	public function index()
    {
		$this->rolemodule->view_page();
		$data = array(
			'app_title'=>'Data Soal',
			'app_desc' =>'it all Soal'
		);
		
        $data['content']	= 'vw_soal';
        $this->load->view('webadmin/main', $data);
    }

	public function fnSoalDataJson()
	{
		$button = '';
		if($this->rolemodule->check_button('edit'))
		{
			$button .= '&nbsp;&nbsp;<a href="#" onclick="fnEdit($1)" class="tip" title="Edit"><i class="glyphicon glyphicon-edit"></i></a>&nbsp;&nbsp;';
		}

		if($this->rolemodule->check_button('delete'))
		{
			$button .= '&nbsp;&nbsp;<a href="#" onclick="fnDelete($1)" class="tip" title="Hapus"><i class="text-red glyphicon glyphicon-trash"></i></a>&nbsp;&nbsp;';
		}

		$this->load->library('datatables');
		$this->datatables->select('f_soal_id,f_kategori_name,f_subkategori_name,f_soal_name,f_soal_type,f_soal_array,f_soal_active');
		$this->datatables->join('t_kategori b','b.f_kategori_id=a.f_kategori_id');
		$this->datatables->join('t_subkategori c','c.f_subkategori_id=a.f_subkategori_id');
		$this->datatables->from('t_soal a');
		$this->datatables->add_column('Aksi',$button,'f_soal_id');
		
		echo $this->datatables->generate();		
	}

	public function fnSoalDataId()
	{
		$id 	= $this->input->get_post('id');
		$data	= $this->mo_soal->getDataId($id);
		echo json_encode($data);		
	}

	//Untuk Form Terpisah
	public function fnSoalAdd()
    {
		$this->rolemodule->check_function('add');
		$data = array(
			'app_title'=>'Form Soal',
			'app_desc' =>'it all Form Soal',
			'form_header'	=>'<i class="fa fa-table"></i> Add&nbsp;&nbsp;',
			'fAct'			=>'Add',
		);
		
        $data['content']	= 'vw_soal_add';
        $this->load->view('webadmin/main', $data);
    }

	public function fnSoalSave()
	{
		$this->rolemodule->check_function('add');
		if($this->mo_soal->saveData())
		{
			$this->session->set_userdata('SUCMSG','Sukses Tambah !!!');
			echo json_encode(array('msg'=>true));
		}
		else
		{
			echo json_encode(array('msg'=>false));
		}		
	}

	//Untuk Form Terpisah
	public function fnSoalEdit($id)
    {
		$this->rolemodule->check_function('edit');
		$data = array(
			'app_title'=>'Form Soal',
			'app_desc' =>'it all Form Soal',
			'form_header'	=>'<i class="fa fa-edit"></i> Edit&nbsp;&nbsp;',
			'fAct'			=>'Edit',
			'fId'			=>$id,
		);
		
        $data['content']	= 'vw_soal_add';
        $this->load->view('webadmin/main', $data);
    }

	public function fnSoalUpdate()
	{
		$this->rolemodule->check_function('edit');
		$id 	= $this->input->get_post('id');
		if($this->mo_soal->updateData($id))
		{
			$this->session->set_userdata('SUCMSG','Sukses Edit !!!');
			echo json_encode(array('msg'=>true));
		}
		else
		{
			echo json_encode(array('msg'=>false));
		}		
	}

	public function fnSoalDelete()
	{
		$this->rolemodule->check_function('delete');
		$id 	= $this->input->get_post('id');
		if($this->mo_soal->deleteData($id))
		{
			echo 'TRUE';
		}
		else
		{
			echo 'FALSE';
		}		
	}

	public function fnSoalComboData()
	{
		$json['result'] = $this->mo_soal->remoteComboData();
		echo json_encode($json);
	}

	public function fnSoalExcel()
    {
        $this->load->helper('exportexcel');
        $namaFile = "t_soal.xls";
        $judul = "t_soal";
        $tablehead = 0;
        $tablebody = 1;
        $nourut = 1;
        //penulisan header
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Disposition: attachment;filename=" . $namaFile . "");
        header("Content-Transfer-Encoding: binary ");

        xlsBOF();

        $kolomhead = 0;
        xlsWriteLabel($tablehead, $kolomhead++, "No");
		xlsWriteLabel($tablehead, $kolomhead++, "F Soal Id");
		xlsWriteLabel($tablehead, $kolomhead++, "F Kategori Id");
		xlsWriteLabel($tablehead, $kolomhead++, "F Subkategori Id");
		xlsWriteLabel($tablehead, $kolomhead++, "F Soal Name");
		xlsWriteLabel($tablehead, $kolomhead++, "F Soal Type");
		xlsWriteLabel($tablehead, $kolomhead++, "F Soal Array");
		xlsWriteLabel($tablehead, $kolomhead++, "F Soal Active");
		xlsWriteLabel($tablehead, $kolomhead++, "F Created On");
		xlsWriteLabel($tablehead, $kolomhead++, "F Created By");
		xlsWriteLabel($tablehead, $kolomhead++, "F Updated On");
		xlsWriteLabel($tablehead, $kolomhead++, "F Updated By");

		foreach ($this->mo_soal->fnGetData() as $data) {
            $kolombody = 0;

            //ubah xlsWriteLabel menjadi xlsWriteNumber untuk kolom numeric
            xlsWriteNumber($tablebody, $kolombody++, $nourut);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_soal_id);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_kategori_id);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_subkategori_id);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_soal_name);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_soal_type);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_soal_array);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_soal_active);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_created_on);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_created_by);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_updated_on);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_updated_by);

			$tablebody++;
			$nourut++;
        }

        xlsEOF();
        exit();
	}

	public function fnSoalWord()
    {
        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition: attachment;Filename=t_soal.doc");

        $data = array(
            't_soal_data' => $this->mo_soal->fnGetData(),
            'start' => 0
        );
        
        $this->load->view('vw_soal_doc',$data);
    }

}

/* End of file Soal.php */
/* Location: ./application/modules/Soal/controllers/Soal.php */ 
