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

class Mo_soal extends CI_Model {

	public $table = 't_soal';
	
    function fnGetData()
    {
        $this->db->order_by('f_soal_id', 'DESC');
        return $this->db->get($this->table)->result();		
	}

	function getDataId($id)
	{
		$this->db->select('f_soal_id, a.f_kategori_id, f_kategori_name, a.f_subkategori_id, f_subkategori_name, f_soal_name, f_soal_type, f_soal_array, f_soal_active, a.f_created_on, (select f_user_name from t_user where f_user_id=a.f_created_by) as f_created_by, a.f_updated_on, (select f_user_name from t_user where f_user_id=a.f_updated_by) as f_updated_by');
		$this->db->join('t_kategori b','b.f_kategori_id=a.f_kategori_id');
		$this->db->join('t_subkategori c','c.f_subkategori_id=a.f_subkategori_id');
		$sql = $this->db->get_where($this->table.' a',array('f_soal_id'=> $id))->row_array();
		$data = array(
			'f_soal_id' => $sql['f_soal_id'],
			'f_kategori_id' => $sql['f_kategori_id'],
			'f_kategori_name' => $sql['f_kategori_name'],
			'f_subkategori_id' => $sql['f_subkategori_id'],
			'f_subkategori_name' => $sql['f_subkategori_name'],
			'f_soal_name' => $sql['f_soal_name'],
			'f_soal_type' => $sql['f_soal_type'],
			'f_soal_array' => unserialize($sql['f_soal_array']),
			'f_soal_active' => $sql['f_soal_active'],
			'f_created_on' => $sql['f_created_on'],
			'f_created_by' => $sql['f_created_by'],
			'f_updated_on' => $sql['f_updated_on'],
			'f_updated_by' => $sql['f_updated_by'],
		);
		return $data;
	}

	function saveData()
	{
		$data = array(
				//'f_soal_id' => $this->input->post('f_soal_id'),
				'f_kategori_id' => $this->input->post('f_kategori_id'),
				'f_subkategori_id' => $this->input->post('f_subkategori_id'),
				'f_soal_name' => $this->input->post('f_soal_name'),
				'f_soal_type' => $this->input->post('f_soal_type'),
				//'f_soal_array' => $this->input->post('f_soal_array'),
				'f_soal_active' => $this->input->post('f_soal_active'),
				'f_created_on' => date('Y-m-d H:i:s'),
				'f_created_by' => $this->session->userdata('sId'),
		);
		
		if($this->input->post('f_soal_type') == 'checkbox' || $this->input->post('f_soal_type') == 'radio')
		{
			$pilihan = array();
			$value	= $this->input->post('value');
			$ket	= $this->input->post('keterangan');
			for($i=0;$i<count($value);$i++)
			{
				$pilihan[] = array(
					'value' => $value[$i],
					'keterangan' => $ket[$i],
				);
			}
			$data['f_soal_array'] = serialize($pilihan);
		}
		
		$sql = $this->db->insert($this->table,$data);
		return $sql;
	}

	function updateData($id)
	{
		$data = array(
				//'f_soal_id' => $this->input->post('f_soal_id'),
				'f_kategori_id' => $this->input->post('f_kategori_id'),
				'f_subkategori_id' => $this->input->post('f_subkategori_id'),
				'f_soal_name' => $this->input->post('f_soal_name'),
				'f_soal_type' => $this->input->post('f_soal_type'),
				//'f_soal_array' => $this->input->post('f_soal_array'),
				'f_soal_active' => $this->input->post('f_soal_active'),
				'f_updated_on' => date('Y-m-d H:i:s'),
				'f_updated_by' => $this->session->userdata('sId'),
		);

		if($this->input->post('f_soal_type') == 'checkbox' || $this->input->post('f_soal_type') == 'radio')
		{
			$pilihan = array();
			$value	= $this->input->post('value');
			$ket	= $this->input->post('keterangan');
			for($i=0;$i<count($value);$i++)
			{
				$pilihan[] = array(
					'value' => $value[$i],
					'keterangan' => $ket[$i],
				);
			}
			$data['f_soal_array'] = serialize($pilihan);
		}
		
		$this->db->where('f_soal_id',$id);
		$sql = $this->db->update($this->table,$data);
		return $sql;		
	}

	function deleteData($id)
	{
		$this->db->where('f_soal_id',$id);
		$sql = $this->db->delete($this->table);
		return $sql;		
	}
	
	function comboData()
	{
		$sql = $this->db->get('t_soal');
		if($sql)
		{
			foreach($sql->result() as $r):
				$list[$r->f_soal_id] = $r->f_soal_id;
			endforeach;
		}
		return $list;
	}

	function remoteComboData()
	{
		if($this->input->get('term')) $this->db->like('f_soal_id',$this->input->get('term')); // pencarian name
		if($this->input->get('id')) $this->db->where('f_soal_id',$this->input->get('id')); // saat pencarian update
		
		$this->db->limit('10');
		$result = $this->db->get($this->table)->result();
		$data = array();
		foreach($result as $row):
			$data[] = array(
				'id'	=> $row->f_soal_id, // value
				'text'	=> $row->f_soal_id, // change display 
				);
		endforeach;
		return $data;
	}
}

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

	


