<?php 
class Model_publikasi extends CI_model{

//xxxxxxxxxxxxxxxxxxxxxxxxx
    function list_(){
        return $this->db->query("SELECT A.dosen_id,
									A.nidn,
									A.gelar_pendidikan_id,
									A.nama_lengkap,
									A.nip,
									A.jenis_kelamin,
									A.status_kawin,
									A.alamat,
									A.hp,
									A.email,
									A.photo,
									A.aktif,A.nama_file,
									F.publikasi_id,
									F.judul_penelitian,
									F.abstrak,
									F.waktu_pelaksanaan_penelitian,
									F.lokasi_penelitian,
									B.keterangan AS keterangan_pendidikan,B.nama_gelar_pendidikan,
									D.nama_gelar_pendidikan AS gelar_belakang
								FROM tb_publikasi_dosen F
								LEFT JOIN tb_dosen A ON A.dosen_id=F.dosen_id
								LEFT JOIN tb_gelar_pendidikan B ON A.gelar_pendidikan_id=B.gelar_pendidikan_id
								LEFT JOIN tb_gelar_pendidikan D ON D.gelar_pendidikan_id=A.gelar_belakang_pendidikan_id
									ORDER BY A.nama_lengkap ASC, A.nidn DESC");
    }
	function list_gelar(){
        return $this->db->query("SELECT * FROM tb_gelar_pendidikan ORDER BY nama_gelar_pendidikan DESC");
    }
	function list_agama(){
        return $this->db->query("SELECT * FROM tb_agama ORDER BY keterangan DESC");
    }
    
    function tambah_baru(){
		$exx = explode('-',$this->input->post('tanggal_penelitian'));
			$tanggal_penelitian = $exx[2].'-'.$exx[1].'-'.$exx[0];
			$datadb = array('dosen_id'=>$this->input->post('dosen_id'),
							'judul_penelitian'=>$this->input->post('judul'),
							'kata_kunci'=>$this->db->escape_str($this->input->post('kata_kunci')),
							'waktu_pelaksanaan_penelitian'=>$tanggal_penelitian,
							'lokasi_penelitian'=>$this->db->escape_str($this->input->post('lokasi')),
							'abstrak'=>$this->input->post('abstrak'),
							'tanggal_input'=>date('Y-m-d H:i:s'),
							'username_input'=>$this->session->username);
            
        $this->db->insert('tb_publikasi_dosen',$datadb);
		$title = "Tambah Data Penelitian Dosen <br />dengan ID [id=".$this->input->post('dosen_id')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("INSERT",$title,$descrip,'tb_publikasi_dosen');
    }
	function insert_pengguna(){
      		$datadb = array('username'=>$this->db->escape_str($this->input->post('username')),
							'user_id'=>$this->db->escape_str($this->input->post('id')),
							'pengguna_id'=>$this->db->escape_str($this->input->post('id')),
							'password'=>hash("sha512", md5('dosen')),
							'level'=>'6',
							'blokir'=>'N',
							'tanggal_perubahan'=>date('Y-m-d H:i:s'),
							'username_perubahan'=>$this->session->username,
							'id_session'=>md5($this->input->post('a')));
			$this->db->insert('tb_pengguna',$datadb);
    }
	function update_pengguna(){
		if ($this->input->post('username') =='' AND $this->input->post('password') ==''){
      		$datadb = array('level'=>'6',
							'blokir'=>'N',
							'tanggal_perubahan'=>date('Y-m-d H:i:s'),
							'username_perubahan'=>$this->session->username);
		}elseif ($this->input->post('username') !='' AND $this->input->post('password') !=''){
			$datadb = array('username'=>$this->db->escape_str($this->input->post('username')),
							'pengguna_id'=>$this->db->escape_str($this->input->post('id')),
							'password'=>hash("sha512", md5($this->input->post('password'))),
							'level'=>'6',
							'blokir'=>'N',
							'tanggal_perubahan'=>date('Y-m-d H:i:s'),
							'username_perubahan'=>$this->session->username,
							'id_session'=>md5($this->input->post('username')));
		}elseif ($this->input->post('username') !='' AND $this->input->post('password') ==''){
			$datadb = array('username'=>$this->db->escape_str($this->input->post('username')),
							'pengguna_id'=>$this->db->escape_str($this->input->post('id')),
							'level'=>'6',
							'blokir'=>'N',
							'tanggal_perubahan'=>date('Y-m-d H:i:s'),
							'username_perubahan'=>$this->session->username,
							'id_session'=>md5($this->input->post('username')));
		}elseif ($this->input->post('username') =='' AND $this->input->post('password') !=''){
			$datadb = array('password'=>hash("sha512", md5($this->input->post('password'))),
							'pengguna_id'=>$this->db->escape_str($this->input->post('id')),
							'level'=>'6',
							'blokir'=>'N',
							'tanggal_perubahan'=>date('Y-m-d H:i:s'),
							'username_perubahan'=>$this->session->username);
		}
			$this->db->where('user_id',$this->input->post('id'));
			$this->db->update('tb_pengguna',$datadb);
			
    }
	function list_edit($id){
        return $this->db->query("SELECT A.*,D.nama_lengkap,D.nidn 
								from tb_publikasi_dosen A 
								LEFT JOIN tb_dosen D ON A.dosen_id=D.dosen_id
								WHERE A.publikasi_id='$id'");
    }
	 function hapus_data($id){
		 $title = "Hapus Data Publikasi Dosen <br />dengan ID [id=".$id."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("DELETE",$title,$descrip,'tb_publikasi_dosen');
        return $this->db->query("DELETE FROM tb_publikasi_dosen where publikasi_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 list_update(){
        $exx = explode('-',$this->input->post('tanggal_penelitian'));
			$tanggal_penelitian = $exx[2].'-'.$exx[1].'-'.$exx[0];
			$datadb = array('dosen_id'=>$this->input->post('dosen_id'),
							'judul_penelitian'=>$this->input->post('judul'),
							'kata_kunci'=>$this->db->escape_str($this->input->post('kata_kunci')),
							'waktu_pelaksanaan_penelitian'=>$tanggal_penelitian,
							'lokasi_penelitian'=>$this->db->escape_str($this->input->post('lokasi')),
							'abstrak'=>$this->input->post('abstrak'),
							'tanggal_input'=>date('Y-m-d H:i:s'),
							'username_input'=>$this->session->username);
        $this->db->where('publikasi_id',$this->input->post('id'));
        $this->db->update('tb_publikasi_dosen',$datadb);
			$title = "Update Data Publikasi Dosen <br />dengan ID [id=".$this->input->post('id')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("UPDATE",$title,$descrip,'tb_publikasi_dosen');
    }
	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) {
		}
	}

}