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

class Mod_authentication extends CI_Model {

    function login($username, $password)
	{
        $this->db->select('*');
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $this->db->from('tbluser');
        $data=$this->db->get();

        if($data->num_rows()>0)
		{
			$userData = $data->row_array();		
			$this->session->set_userdata('admin', TRUE);
			$this->session->set_userdata('role',$this->getrole($userData['role_id']));
			$this->session->set_userdata('userId', $userData['userid']);
			$this->session->set_userdata('userName', $userData['username']);
			return TRUE;
        }
		else
		{
            return FALSE;
        }

    }
	
	function getrole($username)
	{
		$this->db->SELECT("*");
		$this->db->where('id', $username);
		$this->db->from("roles");
		$result = $this->db->get();
		$result = $result->result_array();
		if($result){
			return $result[0]['name'];
		}else{
			return 'User';	
		}	
	}
	
	

}

