<?php

class Log_mdl extends Base_Model {

    function __construct() {

        parent::__construct();
		$this->table = 'log_subkon';
		$this->table2 = 'user_subkon';
		$this->pk_field = 'no';
    }

	function getAll($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,no AS id FROM ".$this->table." a
			  INNER JOIN ".$this->table2." b ON b.nama_user=a.nama_user
			  WHERE a.nama_user LIKE '%$key%'
			  ORDER BY tgl DESC";
		$rstot=$this->db->query($q)->result();
	
		$q .= " LIMIT $offset,$rows";
		$rs=$this->db->query($q)->result();
		
		$result["total"] = count($rstot);
		$result["rows"] = $rs;
		
		return $result;
	}
	
	function add($data){
		$run = $this->db->insert($this->table, $data); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($data['nama_user'],"Tambah User Activity ".$data['nama_user']);	
		} else {
			$rs=false;
		}
		
		return $rs;
	}
	
	function edit($data,$id){
		$run = $this->db->update($this->table, $data, array($this->pk_field => $id)); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($id,"Ubah User Activity ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;
	}
	
	function del($id){
		$this->db->delete($this->table, array($this->pk_field => $id)); 		
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($id,"Hapus User Activity ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;		
	}
}