<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User_model extends CI_Model {

    public function user_all() {
       	$this->db->select('a.*');
		$this->db->select('b.ID as groupID, b.groupName');
		$this->db->where('a.is_delete', 0);
		$this->db->from('user a');
		$this->db->join('user_group b', 'a.groupID = b.ID', 'LEFT');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function insertUser($tname, $data){
		$sql = $this->db->insert_string($tname, $data);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	
	public function checkAdminPassword($ID, $adminPassword)
	{
		
		$this->db->where('ID', $ID);
		$this->db->where('password', $adminPassword);
		$this->db->from('user');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return TRUE;
        } else {
            return FALSE;
        }
		
    }
	
	public function updateUser($tname, $ID, $data) {
        $this->db->where('ID', $ID);
        $query = $this->db->update($tname, $data);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	
	public function get_userbyID($userID) {
       	$this->db->select('a.*');
		$this->db->select('b.ID as groupID, b.groupName');
		$this->db->where('a.ID', $userID);
		$this->db->from('user a');
		$this->db->join('user_group b', 'a.groupID = b.ID', 'LEFT');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function get_userGroup(){
		
		$this->db->from('user_group');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
		
	}
	

}
