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

class Mo_responden extends CI_Model {

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

	function getDataId($id)
	{
		$this->db->select('a.f_id, a.f_customer_id, f_customer_name,  b.f_event_id, b.f_event_no, f_name, f_age, f_gender, f_email, f_hp, f_jabatan, f_date, f_survey, f_ipaddres');
		$this->db->join('t_event b','b.f_event_id=a.f_event_id');
		$this->db->join('t_customers c','c.f_customer_id=a.f_customer_id');
		$sql = $this->db->get_where($this->table." a",array('a.f_id'=> $id))->row_array();
		//echo $this->db->last_query();
		return $sql;
	}

	function getDataCustomer($id,$event)
	{
		$this->db->select('f_customer_name, b.f_event_id, b.f_event_no');
		$this->db->where('b.f_event_id',$event->f_event_id);
		$this->db->where('a.f_customer_id',$id);
		$this->db->join('t_event b','b.f_customer_id=a.f_customer_id');
		$sql = $this->db->get('t_customers a')->row_array();
		//echo $this->db->last_query();
		return $sql;
	}

	function saveData()
	{
		$data = array(
				'f_id' => $this->input->post('f_id'),
				'f_event_id' => $this->input->post('f_event_id'),
				'f_customer_id' => $this->input->post('f_customer_id'),
				'f_name' => $this->input->post('f_name'),
				'f_age' => $this->input->post('f_age'),
				'f_gender' => $this->input->post('f_gender'),
				'f_date' => $this->input->post('f_date'),
				'f_email' => $this->input->post('f_email'),
				'f_hp' => $this->input->post('f_hp'),
				'f_survey' => $this->input->post('f_survey'),
				'f_ipaddres' => $this->input->post('f_ipaddres'),
		);
		$sql = $this->db->insert($this->table,$data);
		return $sql;
	}

	function updateData($id)
	{
		$data = array(
				//'f_id' => $this->input->post('f_id'),
				//'f_event_id' => $this->input->post('f_event_id'),
				//'f_customer_id' => $this->input->post('f_customer_id'),
				'f_name' => $this->input->post('f_name'),
				'f_age' => $this->input->post('f_age'),
				'f_gender' => $this->input->post('f_gender'),
				//'f_date' => $this->input->post('f_date'),
				'f_email' => $this->input->post('f_email'),
				'f_hp' => $this->input->post('f_hp'),
				//'f_survey' => $this->input->post('f_survey'),
				//'f_ipaddres' => $this->input->post('f_ipaddres'),
				'f_updated_on' => date('Y-m-d H:i:s'),
				'f_updated_by' => $this->session->userdata('sId'),
		);

		$this->db->where('f_id',$id);
		$sql = $this->db->update($this->table,$data);
		return $sql;		
	}

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

	function comboDataKategori()
	{
		$sql = $this->db->get('t_kategori');
		if($sql)
		{
			foreach($sql->result() as $a):
				$kategori[$a->f_kategori_id] = array(
						'name' => $a->f_kategori_name,
						'desc' => $a->f_kategori_desc,
					);
			endforeach;
		}
		return $kategori;
	}

	function comboDataSubKategori()
	{
		$sql = $this->db->get('t_subkategori');
		if($sql)
		{
			foreach($sql->result() as $a):
				$kategori[$a->f_kategori_id][$a->f_subkategori_id] = array(
						'name' => $a->f_subkategori_name,
						'desc' => $a->f_subkategori_desc,
					);
			endforeach;
		}
		return $kategori;
	}
	
	function comboDataSoal()
	{
		$sql = $this->db->get('t_soal');
		if($sql)
		{
			foreach($sql->result() as $a):
				$kategori[$a->f_soal_id] = $a->f_soal_name;
			endforeach;
		}
		return $kategori;
	}
	
	function getDataResponden($customer = null,$event = null)
	{
		if($event) $this->db->where('f_event_id',$event->f_event_id);
		if($customer) $this->db->where('f_customer_id',$customer);
		$sql = $this->db->get('trn_survey');
		if($sql->num_rows()>0)
		{
			$array = array();
			foreach($sql->result() as $a)
			{
				$bisnis_on = json_decode($a->f_bisnis_on,true);
				$bisnis_minat = json_decode($a->f_bisnis_minat,true);
				$sehat2 = json_decode($a->f_sehat2,true);
				$array[] = array(
					'f_id' => $a->f_id,
					'f_name' => $a->f_name,
					'f_age' => $a->f_age, 
					'f_gender' => $a->f_gender, 
					'f_date' => $a->f_date, 
					'f_email' => $a->f_email, 
					'f_alamat' => $a->f_alamat, 
					'f_hp' => $a->f_hp, 
					'f_bisnis' => $a->f_bisnis, 
					'f_survey' => unserialize($a->f_survey), 
					'f_bisnis_on' => $bisnis_on, 
					'f_bisnis_minat' => $bisnis_minat, 
					'f_sehat1' => $a->f_sehat1, 
					'f_sehat2' => $sehat2, 
					'f_ipaddres' => $a->f_ipaddres,
				); 
			}
			return $array; 
		}
		else
		{
			return false;
		}
		
	}

	function remoteComboData()
	{
		if($this->input->get('term')) $this->db->like('f_id',$this->input->get('term')); // pencarian name
		if($this->input->get('id')) $this->db->where('f_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_id, // value
				'text'	=> $row->f_id, // change display 
				);
		endforeach;
		return $data;
	}
	
	function last_event($id,$event = false)
	{
		if($event) $this->db->where('f_event_id',$event);
		$this->db->where('f_customer_id',$id);
		$this->db->order_by('f_event_id','desc');
		$sql = $this->db->get('t_event');
		if($sql->num_rows()>0)
		{
			$a = $sql->row();
			//return $a->f_event_id;
			return $a;
		}
		else
		{
			return FALSE;
		}
	}

	function getDataRespondenEvent($customer = null,$event = null)
	{
		if($event) $this->db->where('f_event_id',$event);
		if($customer) $this->db->where('f_customer_id',$customer);
		$sql = $this->db->get('trn_survey');
		//echo $this->db->last_query();
		if($sql->num_rows()>0)
		{
			$array = array();
			foreach($sql->result() as $a)
			{
				$bisnis_on = json_decode($a->f_bisnis_on,true);
				$bisnis_minat = json_decode($a->f_bisnis_minat,true);
				$sehat2 = json_decode($a->f_sehat2,true);
				$array[] = array(
					'f_id' => $a->f_id,
					'f_name' => $a->f_name,
					'f_age' => $a->f_age, 
					'f_gender' => $a->f_gender, 
					'f_date' => $a->f_date, 
					'f_email' => $a->f_email, 
					'f_alamat' => $a->f_alamat, 
					'f_hp' => $a->f_hp, 
					'f_bisnis' => $a->f_bisnis, 
					'f_survey' => unserialize($a->f_survey), 
					'f_bisnis_on' => $bisnis_on, 
					'f_bisnis_minat' => $bisnis_minat, 
					'f_sehat1' => $a->f_sehat1, 
					'f_sehat2' => $sehat2, 
					'f_ipaddres' => $a->f_ipaddres,
				); 
			}
			return $array; 
		}
		else
		{
			return false;
		}
		
	}

	function get_event_no($id,$event = false)
	{
		$this->db->select('f_customer_name, b.f_event_id, b.f_event_no');
		$this->db->where('a.f_customer_id',$id);
		$this->db->where('f_event_no',$event);
		$this->db->join('t_event b','b.f_customer_id=a.f_customer_id');
		$sql = $this->db->get('t_customers a');
		//echo $this->db->last_query()."<br/>";
		
		if($sql->num_rows()>0)
		{
			$a = $sql->row_array();
			return $a;
			//return $a->f_event_id;
		}
		else
		{
			return FALSE;
		}
	}
}

/* End of file Mo_responden.php */
/* Location: ./application/modules/Responden/models/Mo_responden.php */	

	


