<?php
	require_once '../../autoload.php';

	$ins_id 	= -1;

	if ( isset($_GET['ins_id']) )
		$ins_id	= $_GET['ins_id'];

	$institution = ($ins_id == -1)? "" : " and institution.id=".$ins_id;
		
	$conn = get_dbconnection();
	
	$answer = "<departments>";
	
	if ( $conn )
	{
		// $sql ="select id,name, inst_id from department where removed=false;".$institution;
		$sql = "select institution.id,institution.name, department.id, department.name, department.removed from institution, department where department.removed=false and department.inst_id=institution.id".$institution." order by institution.id, department.name";
		
		if ( $query = pg_query($conn,$sql) )
		{
			while ( $row = pg_fetch_row($query) )
			{
				$answer .= "<department inst_id=\"".$row[0]."\" institution=\"".$row[1]."\" id=\"".$row[2]."\" department=\"".$row[3]."\" removed=\"".$row[4]."\" ></department>";
			}
		}
	}
	
	$answer .= "</departments>";

	header ('Content-type: text/xml' );
	
	echo $answer;
?>