<?php 
class Model_gelar_pendidikan extends CI_model{

//xxxxxxxxxxxxxxxxxxxxxxxxx
    function list_(){
        return $this->db->query("SELECT * from tb_gelar_pendidikan ORDER BY gelar_pendidikan_id DESC");
    }
	
    function tambah_baru(){
      
		$datadb = array('nama_gelar_pendidikan'=>$this->db->escape_str($this->input->post('a')),
						'keterangan'=>$this->db->escape_str($this->input->post('b')),
						'tanggal_input'=>date('Y-m-d H:i:s'),
						'username_input'=>$this->session->username);
		$this->db->insert('tb_gelar_pendidikan',$datadb);
		$title = "Tambah Data Gelar <br />dengan ID [id=".$this->input->post('b')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("INSERT",$title,$descrip,'tb_gelar_pendidikan');
	}
	function simpan_update(){
		$datadb = array('nama_gelar_pendidikan'=>$this->db->escape_str($this->input->post('a')),
						'keterangan'=>$this->db->escape_str($this->input->post('b')),
						'tanggal_perubahan'=>date('Y-m-d H:i:s'),
						'username_perubahan'=>$this->session->username);
		$this->db->where('gelar_pendidikan_id',$this->input->post('id'));
        $this->db->update('tb_gelar_pendidikan',$datadb);
		$title = "Update Data Gelar <br />dengan ID [id=".$this->input->post('b')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("UPDATE",$title,$descrip,'tb_gelar_pendidikan');
    }
	function list_edit($id){
        return $this->db->query("SELECT * from tb_gelar_pendidikan WHERE gelar_pendidikan_id='$id'");
    }
	 function hapus_data($id){
        return $this->db->query("DELETE FROM tb_gelar_pendidikan where gelar_pendidikan_id='$id'");
		$title = "Hapus Data Gelar <br />dengan ID [id=".$this->input->post('b')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("DELETE",$title,$descrip,'tb_gelar_pendidikan');
    }
	function list_detail($id){
        return $this->db->query("SELECT A.*,B.keterangan AS agama FROM tb_mahasiswa A LEFT JOIN tb_agama B ON A.agama_id=B.agama_id
								where A.mahasiswa_id='$id'")->result_array();
    }
	function fetch_description($title,$data){
		$descrip = '<br><table style="vertical-align:top" cellspacing="0" cellpadding="1" border="1">';
		$descrip .= '<tr><th>Nama Kolom</th><th>Nilai</th></tr>';
		foreach ($data as $key => $value) {
			$descrip .= '<tr>';
			$descrip .= '<td>'.$key.'</td>';
			$descrip .= '<td>'.$value.'</td>';
			$descrip .= '</tr>';
		}
		$descrip .= '</table>';
		return $descrip;
	}

	function add_audittrail($action,$title,$descrip,$tablename){
		try {
			$data = array(
				'datetime' => date("Y-m-d H:i:s"),
				'ipaddress' => $this->input->ip_address(),
				'username' => $this->session->userdata('username'),
				'tablename' => $tablename,
				'formname' => 'by komar',
				'action' => $action,
				'title' => $title,
				'description' => $descrip
			);
			$this->db->insert('sys_audittrail', $data);
		} catch (Exception $e) {
		}
	}

}