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

class Survey extends MX_Controller {

	var $data;
	function __construct()
	{
		parent::__construct();
		$this->load->model('mo_survey','ms');
		$this->load->helper('survey');
	}
	
	public function index($short = null)
	{
		echo $short;
		//$data['soal'] = $this->ms->getSoal();
		if($short)
		{
			$this->load->view('form_survey',$data);
		}
		else
		{
			show_404();
		}
	}
	
	function saveInfoData()
	{
		$data = array(
			'DATA_NAMA' => $this->input->post('nama'),
			'DATA_GEN' => $this->input->post('gender'),
			'DATA_HP' => $this->input->post('hp'),
			'DATA_EMAIL' => $this->input->post('email'),
			'DATA_SEKO' => $this->input->post('sekolah'),
		);
		$sql = "INSERT IGNORE INTO t_data_diri(f_nama, f_gender, f_hp, f_email, f_sekolah) VALUES ('".$this->input->post('nama')."','".$this->input->post('gender')."','".$this->input->post('hp')."','".$this->input->post('email')."','".$this->input->post('sekolah')."')";
		$this->db->query($sql);
		$this->session->set_userdata($data);
		
		echo json_encode(array('status' => 'ok'));
	}
	
	function saveData()
	{
		$this->db->trans_start();
		//echo "<pre>data ";echo print_r($_POST)."</pre><br/>";

		$this->data = array(
			'f_name' 		=> $this->input->post('nama'),
			'f_age' 		=> $this->input->post('usia'),
			'f_gender' 		=> $this->input->post('gender'),
			'f_email'		=> $this->input->post('email'),
			'f_hp' 			=> $this->input->post('hp'),
			'f_ipaddres' 	=> $_SERVER['REMOTE_ADDR'],
			'f_date'  		=> date('Y-m-d'),
			'f_event_id'  	=> date('Y-m-d'),
			'f_customer_id'	=> date('Y-m-d'),
		);

		$this->db->where('f_soal_active',1);		
		$soal = $this->db->get('t_soal a')->result();
		$dataSoal = array();
		foreach($soal as $a)
		{
			$namepost = "no".$a->f_soal_id;
			$jawab = explode('_',$this->input->post($namepost));
			$dataSoal['kategori'][$a->f_kategori_id][$a->f_soal_id] = array('urut'=>$jawab[0],'jawab'=>$jawab[1]); 
			$dataSoal['subkategori'][$a->f_subkategori_id][$a->f_soal_id] = array('urut'=>$jawab[0],'jawab'=>$jawab[1]); 
		}		

		$this->data['f_survey'] = serialize($dataSoal);

		$this->db->insert('trn_survey',$this->data);
		
		$this->db->trans_complete();
		if($this->db->trans_status())
		{
			echo json_encode(array('status'=>'ok'));
		}
		else
		{
			echo json_encode(array('status'=>'no'));
		}
	}

	public function fnRespondenResult($id)
	{
		$this->load->model('admin/mo_responden');
		$data = $this->mo_responden->assessment_result($id);
		$view = $this->load->view('vw_responden_result',$data,true);
		echo json_encode(array('statys'=>'ok','view'=> $view));
	}
		

}
