<?php 
class Model_ruangan extends CI_model{

//xxxxxxxxxxxxxxxxxxxxxxxxx
    function list_(){
        return $this->db->query("SELECT * from tb_ruangan ORDER BY nama_ruangan ASC");
    }
	
    function tambah_baru(){
      
		$datadb = array('nama_gedung'=>$this->db->escape_str($this->input->post('a')),
						'nama_ruangan'=>$this->db->escape_str($this->input->post('b')),
						'kapasitas'=>$this->input->post('c'),
						'keterangan'=>$this->db->escape_str($this->input->post('d')),
						'kondisi'=>$this->input->post('e'),
						'tanggal_input'=>date('Y-m-d H:i:s'),
						'username_input'=>$this->session->username);
		$this->db->insert('tb_ruangan',$datadb);
			$title = "Tambah Data Master Kelas <br />dengan ID [id=".$this->input->post('b')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("TAMBAH",$title,$descrip,'tb_ruangan');
    }
	function list_update(){
      
		$datadb = array('nama_gedung'=>$this->db->escape_str($this->input->post('a')),
						'nama_ruangan'=>$this->db->escape_str($this->input->post('b')),
						'kapasitas'=>$this->input->post('c'),
						'keterangan'=>$this->db->escape_str($this->input->post('d')),
						'kondisi'=>$this->input->post('e'),
						'tanggal_input'=>date('Y-m-d H:i:s'),
						'username_input'=>$this->session->username);
		$this->db->where('ruangan_id',$this->input->post('id'));
			$this->db->update('tb_ruangan',$datadb);
			$title = "Ubah Data Master Kelas <br />dengan ID [id=".$this->input->post('b')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("UPDATE",$title,$descrip,'tb_ruangan');
    }
	function list_edit($id){
        return $this->db->query("SELECT * from tb_ruangan WHERE ruangan_id='$id'");
    }
	 function hapus_data($id){
        return $this->db->query("DELETE FROM tb_ruangan where ruangan_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) {
		}
	}

}