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

	$survey_id = '-1';
	if(isset($_POST['survey_id']))
	{
		$survey_id = $_POST['survey_id'];
	}

	
	$conn = get_dbconnection();
	
	$sql = "select id, title, description from survey where id=".$survey_id;

	$query = pg_query($conn,$sql);

	$arr = array ();
	while ( $row = pg_fetch_row ($query) )
	{
		$arr ['id'] 			= $row [0];
		$arr ['title'] 			= $row [1];
		$arr ['description'] 	= $row [2];
	}

	$sql = "select id, content, type from survey_field where survey_id=".$survey_id." order by position asc";

	$query = pg_query($conn,$sql);

	$arr1 = array ();
	while ( $row = pg_fetch_row ($query) )
	{
		$res = array ();

		$res ['id'] 		= $row [0];
		$res ['content'] 	= $row [1];
		$res ['type'] 		= $row [2];
		
	    array_push ( $arr1, $res );
	}

	$arr['fields'] = $arr1;

	header('Content-type: application/json');
	
	echo json_encode($arr);
?>
