<?php 

class tgi_permissions
{
	protected $db;
	
	protected $TGIinterviewerID;
	public $TGIinterviewer;
	
	
	public function __construct($db)
	{
		$this->db=$db;
	}


	public function getPermissions($idUser,$typeUser,$idTGIproject) {
		$permission = array();
		
		$sql = "SELECT TGI_project from TGI_projects_permissions WHERE id_user=$idUser AND type_user=$typeUser AND TGI_project='$idTGIproject'";
		$this->db->LogWrite($sql,true,"SQL get TGI permissions");
		$res = $this->db->SQLexecute($sql);
		if ($this->db->NumRows($res)>0) {
			$row = $this->db->fetchArray($res);
			$permission['project'][$idTGIproject] = true;
			$this->db->LogWrite("true",true,"permissions for user $idUser for TGI_project $idTGIproject");
		} else {
			$permission['project'][$idTGIproject] = false;
			$this->db->LogWrite("false",true,"permissions for user $idUser for TGI_project $idTGIproject");
		}
		
		return $permission;
		
	}
	
	
	/**
	 * Get user info from pool interviewer table
	 *
	 * @param string $user
	 * @param string $pass
	 * @return boolean
	 */
	public function checkUser($user,$pass) {
		$sql = "SELECT `email`, `TGIuser`, `TGIpass`, `Firstname`, `Surname` FROM _Interviewer WHERE TGIuser='$user' AND TGIpass='$pass'";
		//echo "$sql<br>";
		$this->db->LogWrite($sql,true,"SQL check TGI pool user");
		$res = $this->db->SQLexecute($sql);
		if ($this->db->NumRows($res)>0) {
			$user_array = $this->db->fetchAssoc($res);
			//echo ;
			$this->db->LogWrite(print_r($user_array,1),true,"print user info");
			$this->set_TGIuser($user_array);
			return true;
		} else {
			$this->db->LogWrite("I did not find user inside _Interviewer table",true,"check user");
			return false;
		}
	}
	
	
	protected function set_TGIuser($user) {
		$this->TGIinterviewer = $user;
	}
	

	public function get_TGIuser() {
		return $this->TGIinterviewer;
	}
	
	
	protected function set_TGIuserID($userID) {
		$this->TGIinterviewerID = $userID;
	}
	
	public function get_TGIuserID() {
		return $this->TGIinterviewerID;
	}
	
	
	public function addInterviwer2Warpit($TGIuser,$idLanguage = 0) {
		$sql = "SELECT id FROM _Interviewer 
				WHERE `username`='".$TGIuser['TGIuser']."' AND `password`='".$TGIuser['TGIpass']."'";
		//echo "$sql<br>";
		$this->db->LogWrite($sql,true,"SQL check if warpit interviewer exist");
		$res = $this->db->SQLexecute($sql);
		if ($this->db->NumRows($res)) {
			$row = $this->db->fetchAssoc($res);
			$interviwerID = $row['id'];
			$this->db->LogWrite("ID exist, my ID = ".$interviwerID,true,"addInterviwer2Warpit"); 
		} else {
			$sql = "INSERT INTO _Interviewer SET 
						`username`='".$TGIuser['TGIuser']."', 
						`password`='".$TGIuser['TGIpass']."', 
						`Firstname`='".$TGIuser['Firstname']."', 
						`Surname`='".$TGIuser['Surname']."', 
						`email`='".$TGIuser['email']."', 
						`language`='".$idLanguage."'";
			//echo "$sql<br>";
			$this->db->LogWrite($sql,true,"SQL insert TGI pool user to warpit interviewer");
			if ($this->db->SQLexecute($sql)) {
				$interviwerID = $this->db->GetLastInsertId();
				$this->db->LogWrite("ID inserted, my ID = ".$interviwerID,true,"addInterviwer2Warpit");
			} else {
				return false;
			}
		}
		
		$this->set_TGIuserID($interviwerID);
		return true;
	}
	
	
	public function setTGIinterviwer() {
		$userID = $this->get_TGIuserID();
		$TGI_project = $this->getActiveProject();
		
		$sql = "INSERT IGNORE INTO TGI_projects_permissions SET 
			`id_user` = '".$userID."', 
			`type_user` = '0', 
			`TGI_project` = '".$TGI_project."'";
		//echo "$sql<br>";
		$this->db->LogWrite($sql,true,"SQL set TGI project permissions for pool user");
		
		if ($this->db->SQLexecute($sql)) {
			return true;
		} else {
			return false;
		}
	}
	
	
	
	public function getActiveProject() {
		$row = $this->db->get("TGI_projects","active=1","id");
		$this->activeProject = $row[0];
		
		$this->db->LogWrite($this->activeProject,true,"get active project inside tgi class");
		
		return $this->activeProject;
	}
	
}
	
?>