<?php
require_once (PATH_TABLES . "activity/TaskPlanParticipantTable.php") ;
require_once (PATH_MODELS . "activity/TaskPlanClass.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class TaskPlanParticipantClass extends MasterBase {
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= TaskPlanParticipantTable::C_TABLE ;
		$this->fldid 	= TaskPlanParticipantTable::C_ID ;
	}
	function __destruct() {
	}
    
    function getTaskPlanParticipant($taskID){
        $sql = "SELECT * FROM " . TaskPlanParticipantTable::C_TABLE . " ";
        $filter = "WHERE " . TaskPlanParticipantTable::C_ID . " = " . $taskID . " ";
        
        $rows = $this->db->getTable($sql . $filter);
        
        return $rows;
    }
	
    function setTaskPlanParticipant($taskID, $doUser, $updateUsers, $monitorUsers){
		try {
			$sql = "INSERT INTO " . TaskPlanParticipantTable::C_TABLE . " VALUES ";
			$values = "";
			
			$values .= "( " . $taskID . ", 0, 0, " . $doUser . ")";
				
			if (count($updateUsers) > 0){
				foreach($updateUsers as $updateUser){
					if($values != "") $values .= ", ";
					$values .= "( " . $taskID . ", 1, " . $updateUser["USER_ID"] . ", '" . $updateUser["USER_NAME"]  . "', '" . $updateUser["USER_EMAIL"] . "')";
				}
			}
			
			if (count($monitorUsers) > 0){
				foreach($monitorUsers as $monitorUser){
					if($values != "") $values .= ", ";
					$values .= "( " . $taskID . ", 2, " . $monitorUser["USER_ID"] . ", '" . $monitorUser["USER_NAME"]  . "', '" . $monitorUser["USER_EMAIL"] . "')";
				}
			}
			
			$this->db->executeSql($sql);
			
		} catch (Exception $e) {
			Log::write('[TaskPlanParticipant]' . $e->getMessage());
			return false;
		}
        
        return true;
    }
	
    function deleteTaskPlanParticipant($taskID){
		try {
			$sql = "DELETE FROM " . TaskPlanParticipantTable::C_TABLE . " ";
			$filter = "WHERE " . TaskPlanParticipantTable::C_ID . " = " . $taskID . " ";
			
			$this->db->executeSql($sql . $filter);
			
		} catch (Exception $e) {
			Log::write('[TaskPlanParticipant]' . $e->getMessage());
			return false;
		}
        
        return true;
    }
}
?>