<?php 
class Model_silabus extends CI_model{
   function list_semester(){
        return $this->db->query("SELECT * from tb_semester ORDER BY id_semester ASC");
    }
   function list_(){
        return $this->db->query("SELECT A.*,B.nama_lengkap,C.nama_mata_kuliah,C.sks ,
								D.keterangan AS nama_semester,A.id_semester
								FROM tb_silabus A
								LEFT JOIN tb_dosen B ON A.dosen_id=B.dosen_id 
								LEFT JOIN tb_mata_kuliah C ON A.mata_kuliah_id=C.mata_kuliah_id
								LEFT JOIN tb_semester D ON A.id_semester=D.id_semester
								WHERE A.jenis_file = 1 ORDER BY A.silabus_id DESC");
    }
	function list_rps(){
        return $this->db->query("SELECT A.*,B.nama_lengkap,C.nama_mata_kuliah,C.sks ,
								D.keterangan AS nama_semester,A.id_semester
								FROM tb_silabus A
								LEFT JOIN tb_dosen B ON A.dosen_id=B.dosen_id 
								LEFT JOIN tb_mata_kuliah C ON A.mata_kuliah_id=C.mata_kuliah_id
								LEFT JOIN tb_semester D ON A.id_semester=D.id_semester
								WHERE A.jenis_file = 2 ORDER BY A.silabus_id DESC");
    }
    function updatehits($file){
        return $this->db->query("UPDATE download set hits=hits+1 where nama_file='".$this->db->escape_str($file)."'");
    }

    function hitungdownload(){
        return $this->db->query("SELECT * FROM download");
    }


	
    function download_tambah(){
        $config['upload_path'] = 'asset/dokumen/';
        $config['allowed_types'] = 'gif|jpg|png|zip|rar|pdf|doc|docx|ppt|pptx|xls|xlsx|txt|rtf';
        $config['max_size'] = '35000'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('b');
        $hasil=$this->upload->data();
            if ($hasil['file_name']==''){
                    $datadb = array('judul'=>$this->db->escape_str($this->input->post('a')),
									'url_eksternal'=>$this->db->escape_str($this->input->post('url')),
                                    'tgl_posting'=>date('Y-m-d'),
                                    'hits'=>'0');
            }else{
            		$datadb = array('judul'=>$this->db->escape_str($this->input->post('a')),
									'url_eksternal'=>$this->db->escape_str($this->input->post('url')),
                                    'nama_file'=>$hasil['file_name'],
                                    'tgl_posting'=>date('Y-m-d'),
                                    'hits'=>'0');
            }
        $this->db->insert('download',$datadb);
    }
	 function silabus_tambah(){
        $config['upload_path'] = 'asset/silabus/';
        $config['allowed_types'] = 'gif|jpg|png|zip|rar|pdf|doc|docx|ppt|pptx|xls|xlsx|txt|rtf';
        $config['max_size'] = '35000'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('b');
        $hasil=$this->upload->data();
            if ($hasil['file_name']==''){
                    $datadb = array('judul'=>$this->db->escape_str($this->input->post('a')),
									'dosen_id'=>$this->db->escape_str($this->input->post('dosen_id')),
									'mata_kuliah_id'=>$this->db->escape_str($this->input->post('mata_kuliah_id')),
									'url_eksternal'=>$this->db->escape_str($this->input->post('url')),
                                    'tgl_posting'=>date('Y-m-d'),
                                    'hits'=>'0',
									'diinput_tanggal'=>date('Y-m-d H:i:s'),
                                    'diinput_oleh'=>$this->session->username);
            }else{
            		$datadb = array('judul'=>$this->db->escape_str($this->input->post('a')),
									'dosen_id'=>$this->db->escape_str($this->input->post('dosen_id')),
									'mata_kuliah_id'=>$this->db->escape_str($this->input->post('mata_kuliah_id')),
									'url_eksternal'=>$this->db->escape_str($this->input->post('url')),
                                    'nama_file'=>$hasil['file_name'],
                                    'tgl_posting'=>date('Y-m-d'),
                                    'hits'=>'0',
									'diinput_tanggal'=>date('Y-m-d H:i:s'),
                                    'diinput_oleh'=>$this->session->username);
            }
        $this->db->insert('tb_silabus',$datadb);
    }
	function rps_tambah(){
        $config['upload_path'] = 'asset/silabus/';
        $config['allowed_types'] = 'gif|jpg|png|zip|rar|pdf|doc|docx|ppt|pptx|xls|xlsx|txt|rtf';
        $config['max_size'] = '350000'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('b');
        $hasil=$this->upload->data();
            if ($hasil['file_name']==''){
                    $datadb = array('judul'=>$this->db->escape_str($this->input->post('a')),
									'dosen_id'=>$this->db->escape_str($this->input->post('dosen_id')),
									'mata_kuliah_id'=>$this->db->escape_str($this->input->post('mata_kuliah_id')),
									'id_semester'=>$this->db->escape_str($this->input->post('semester')),
									'jenis_file'=>2,
									'url_eksternal'=>$this->db->escape_str($this->input->post('url')),
                                    'tgl_posting'=>date('Y-m-d'),
                                    'hits'=>'0',
									'diinput_tanggal'=>date('Y-m-d H:i:s'),
                                    'diinput_oleh'=>$this->session->username);
            }else{
            		$datadb = array('judul'=>$this->db->escape_str($this->input->post('a')),
									'dosen_id'=>$this->db->escape_str($this->input->post('dosen_id')),
									'mata_kuliah_id'=>$this->db->escape_str($this->input->post('mata_kuliah_id')),
									'id_semester'=>$this->db->escape_str($this->input->post('semester')),
									'url_eksternal'=>$this->db->escape_str($this->input->post('url')),
                                    'nama_file'=>$hasil['file_name'],
									'jenis_file'=>2,
                                    'tgl_posting'=>date('Y-m-d'),
                                    'hits'=>'0',
									'diinput_tanggal'=>date('Y-m-d H:i:s'),
                                    'diinput_oleh'=>$this->session->username);
            }
        $this->db->insert('tb_silabus',$datadb);
		$title = "Tambah Data RPS Dosen <br />dengan ID [id=".$this->input->post('dosen_id')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("UPDATE",$title,$descrip,'tb_silabus');
    }
    function download_edit($id){
        return $this->db->query("SELECT * FROM download where id_download='$id'");
    }

    function download_update(){
        $config['upload_path'] = 'asset/dokumen/';
        $config['allowed_types'] = 'gif|jpg|png|zip|rar|pdf|doc|docx|ppt|pptx|xls|xlsx|txt|rtf';
        $config['max_size'] = '25000'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('b');
        $hasil=$this->upload->data();
            if ($hasil['file_name']==''){
                    $datadb = array('judul'=>$this->db->escape_str($this->input->post('a')),
									'url_eksternal'=>$this->db->escape_str($this->input->post('url')),
                                    'tgl_posting'=>date('Y-m-d'),
                                    'hits'=>'0');
            }else{
            		$datadb = array('judul'=>$this->db->escape_str($this->input->post('a')),
									'url_eksternal'=>$this->db->escape_str($this->input->post('url')),
                                    'nama_file'=>$hasil['file_name'],
                                    'tgl_posting'=>date('Y-m-d'),
                                    'hits'=>'0');
            }
        $this->db->where('id_download',$this->input->post('id'));
        $this->db->update('download',$datadb);
    }

    function download_delete($id){
        return $this->db->query("DELETE FROM download where id_download='$id'");
    }
	function hitung_download(){
        return $this->db->query("SELECT * FROM download");
    }
	function semua_download($start, $limit){
        return $this->db->query("SELECT * FROM download ORDER BY id_download DESC LIMIT $start,$limit");
    }
	function hapus_data($id){
        return $this->db->query("DELETE FROM tb_silabus where silabus_id='$id'");
		$title = "Hapus Data Materi <br />dengan ID [id=".$this->input->post('id')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("DELETE",$title,$descrip,'tb_silabus');
    }
	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) {
		}
	}
}