<?php
require_once (PATH_TABLES . "admin/UserAbsentHistoryTable.php") ;
require_once (PATH_TABLES . "admin/UserTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class UserAbsentHistoryClass extends MasterBase {
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= UserAbsentHistoryTable::C_TABLE ;
		$this->fldid 	= UserAbsentHistoryTable::C_ID ;
	}
	function __destruct() {
	}
    function getAbsentHistoryByID($id){
        $sql = "SELECT * FROM " . $this->tbl . " WHERE " . UserAbsentHistoryTable::C_ID . " = " . $id;
        
        $rows = $this->db->getTable($sql);
        
        return $rows;
    }
    function getUserToBeReplaced($id){
        $sql = "select distinct u." . UserTable::C_ID . " " . 
                "from " . UserTable::C_TABLE . " u " .
	            "left outer join " . UserAbsentHistoryTable::C_TABLE . " a on (u." . UserTable::C_ID . " = a." . UserAbsentHistoryTable::C_ID . ") " .
                "where u." . UserTable::C_SUBTITUTE . " = " . $id . " " .
	            "and GETDATE() <= a." . UserAbsentHistoryTable::C_END_DATE . " " . 
	            "and GETDATE() >= a." . UserAbsentHistoryTable::C_START_DATE . " ";
        
        $rows = $this->db->getTable($sql);
        
        return $rows;
    }
    function getReplacementUser($id){
        $sql = "select distinct U. " . UserTable::C_SUBTITUTE  . " " . 
                "from " . UserAbsentHistoryTable::C_TABLE . " a " .
                "LEFT OUTER JOIN " . UserTable::C_TABLE . " U on (U." . UserTable::C_ID . " = a." . UserAbsentHistoryTable::C_ID . ")" .
                "where a." . UserTable::C_ID . " = " . $id . " " .
	            "and GETDATE() <= a." . UserAbsentHistoryTable::C_END_DATE . " " . 
	            "and GETDATE() >= a." . UserAbsentHistoryTable::C_START_DATE . " ";
        
        $rows = $this->db->getTable($sql);
        
        if (!is_null($rows) && count($rows) > 0){
            return $rows[0][UserTable::C_SUBTITUTE];
        }
        
        return $id;
    }
}
?>