<?php 
class Model_matakuliah extends CI_model{

//xxxxxxxxxxxxxxxxxxxxxxxxx
    function list_($awal,$akhir){
        return $this->db->query("SELECT * from tb_mata_kuliah ORDER BY mata_kuliah_id ASC LIMIT $awal, $akhir");
    }
	function list_semester(){
        return $this->db->query("SELECT * from tb_semester ORDER BY id_semester ASC");
    }
    function tambah_(){
      
		$datadb = array('kode_kelompok'=>$this->db->escape_str($this->input->post('a')),
						'nama'=>$this->db->escape_str($this->input->post('b')),
						'diinput_tanggal'=>date('Y-m-d H:i:s'),
						'diinput_oleh'=>$this->session->username);
		$this->db->insert('tb_konsentrasi_mata_kuliah',$datadb);
			$title = "Tambah Data Master Kelompok Mata Kuliah <br />dengan ID [id=".$this->input->post('b')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("TAMBAH",$title,$descrip,'tb_konsentrasi_mata_kuliah');
    }
	function list_edit($id){
        return $this->db->query("SELECT * from tb_konsentrasi_mata_kuliah WHERE kelompok_id='$id'");
    }
	
	function list_cari($cari){
        return $this->db->query("SELECT * FROM tb_konsentrasi_mata_kuliah 
								 WHERE nama LIKE '%$cari%' 
								 ORDER BY nama ASC");
    }
	function getCari($cari){
		try {
			$this->db->where('kode_kelompok',$cari);
			$this->db->where('nama',$cari);
			$this->db->select('*');
			return $this->db->get('tb_konsentrasi_mata_kuliah');
		} catch (Exception $e) {
			return '';
		}
	}
	 function hapus_data($id){
        return $this->db->query("DELETE FROM tb_master_akun where akun_id='$id'");
		
    }
	function deleteData($table,$data){
		$data['enc_act'] = $this->uri->segment(4);
		$id = $this->encrypt->decode(base64_decode($data['enc_act']));
			$title = "Hapus Data Master Kelompok Mata Kuliah <br />dengan ID [id=".$id."]";
			$descrip = $this->fetch_description($title,$data);
			$this->add_audittrail("DELETE",$title,$descrip,'tb_konsentrasi_mata_kuliah');
		$this->db->delete($table,$data);
	}
	function list_update(){
		
		$datadb = array('kode_kelompok'=>$this->db->escape_str($this->input->post('a')),
						'nama'=>$this->db->escape_str($this->input->post('b')),
						'diperbaharui_tanggal'=>date('Y-m-d H:i:s'),
						'diperbaharui_oleh'=>$this->session->admin_user);
		
        $this->db->where('kelompok_id',$this->input->post('id'));
        $this->db->update('tb_konsentrasi_mata_kuliah',$datadb);
			$title = "Update Data Master Kelompok Mata Kuliah <br />dengan ID [id=".$this->input->post('id')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("UPDATE",$title,$descrip,'tb_konsentrasi_mata_kuliah');
    }
	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) {
		}
	}

}