<?php
	require_once '../../autoload.php';
	
	$whereclause2='';
  
  	if(isset($_GET['hasactivejob']))
  	{
	  	if($_GET['hasactivejob']=='false')
			$whereclause2 = "where jobactif!=".$_GET['hasactivejob']."::integer";
  	}
		
	$conn = get_dbconnection();
	
	$answer = "<results>";
	
	if ( $conn )
	{
		$sql = "with temp as(select staff_member.id as id, lastname ||' '|| firstname as fullname, string_agg(distinct staff_role.name,',') as job, bool_or(tag_link.tag_id = any ('{{$_SESSION['alltagidallow']}}'::integer[]) and staff_job.enddate::date >now()::date)::integer as jobactif
from staff_member, staff_job, staff_role,v_staff_member_historyjobdeptagidlist,tag_link
where staff_job.removed = false and  staff_member.id = staff_job.staff_id and staff_role.id = staff_job.staff_role_id
and tag_link.type ='department' and tag_link.ext_id = staff_job.dep_id
and v_staff_member_historyjobdeptagidlist.staff_id = staff_member.id and v_staff_member_historyjobdeptagidlist.deptagidlist && '{{$_SESSION['alltagidallow']}}'::integer[]
and staff_member.removed = false group by staff_member.id order by lastname ||' '|| firstname asc)
select * from temp {$whereclause2}
";

		if ( $query = pg_query($conn,$sql) )
		{
			while ( $row = pg_fetch_row($query) )
			{
				$answer .= "<user id=\"".$row[0]."\" name=\"".$row[1]."\"></user>";
			}
		}
	}
	
	$answer .= "</results>";
	
	header ('Content-type: text/xml' );
	
	echo $answer;
?>