<?php 
class Model_tahun_akademik extends CI_model{

//xxxxxxxxxxxxxxxxxxxxxxxxx
    function list_(){
        return $this->db->query("SELECT * from tb_tahun_akademik ORDER BY batas_registrasi DESC");
    }
	
    function tambah_baru(){
		$exx = explode('-',$this->input->post('c'));
             $mulai = $exx[2].'-'.$exx[1].'-'.$exx[0];
           
			$datadb = array('tahun_akademik'=>$this->db->escape_str($this->input->post('a')),
						'semester'=>$this->db->escape_str($this->input->post('b')),
						'mulai_registrasi'=>$mulai,
						'batas_registrasi'=>$mulai,
						'keterangan'=>$this->input->post('d'),
						'status'=>$this->input->post('e'),
						'diinput_tanggal'=>date('Y-m-d H:i:s'),
						'diinput_oleh'=>$this->session->username);
		$this->db->insert('tb_tahun_akademik',$datadb);
			$title = "Tambah Data Tahun Akademik <br />dengan ID [id=".$this->input->post('a')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("INSERT",$title,$descrip,'tb_tahun_akademik');
    }
	function list_edit($id){
        return $this->db->query("SELECT * from tb_tahun_akademik WHERE tahun_akademik_id='$id'");
    }
	function simpan_edit(){
		$exy = explode('-',$this->input->post('c'));
            
            
            $selesai = $exy[2].'-'.$exy[1].'-'.$exy[0];
			$datadb = array('tahun_akademik'=>$this->db->escape_str($this->input->post('a')),
						'semester'=>$this->input->post('b'),
						'mulai_registrasi'=>$selesai,
						'batas_registrasi'=>$selesai,
						'keterangan'=>$this->input->post('d'),
						'status'=>$this->input->post('e'),
						'diperbaharui_tanggal'=>date('Y-m-d H:i:s'),
						'diperbaharui_oleh'=>$this->session->username);
		$this->db->where('tahun_akademik_id',$this->input->post('id'));
        $this->db->update('tb_tahun_akademik',$datadb);
	}
	 function hapus_data($id){
        return $this->db->query("DELETE FROM tb_tahun_akademik where tahun_akademik_id='$id'");
		
    }
	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) {
		}
	}

}