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

	header("Content-Type: text/xml");


	$conn = get_dbconnection();
	//Cast pour éviter injections -> pas besoin de requète préparée
	$stu_id=(int) $_POST['stu_id'];
	$sql = "select 
			id as saw_id, 
			alert_name as alertname, 
			study.description_auto as description, 
			serie.description_auto as description_auto, 
			serie.instancenumber as instancenumber,
			nrd_valuetype,
			value  ||' '|| case when (nrd_valuetype='DAP') then 'mGy.cm²' when (nrd_valuetype='DLP') then 'mGy.cm' when (nrd_valuetype = 'DGM') then 'mGy' when (nrd_valuetype = 'CTDIvol') then 'mGy' when (nrd_valuetype = 'SCOPIE') then 's' when (nrd_valuetype = 'ACTIVITY') then 'MBq' when (nrd_valuetype = 'SACTIVITY') then 'MBq/Kg' else '' end as currentvalue, 
    nrd_value ||' '|| case when (nrd_valuetype='DAP') then 'mGy.cm²' when (nrd_valuetype='DLP') then 'mGy.cm' when (nrd_valuetype = 'DGM') then 'mGy' when (nrd_valuetype = 'CTDIvol') then 'mGy' when (nrd_valuetype = 'SCOPIE') then 's' when (nrd_valuetype = 'ACTIVITY') then 'MBq' when (nrd_valuetype = 'SACTIVITY') then 'MBq/Kg' else '' end as nrdvalue,
    round(value/nrd_value,2) as factor
    from alertwarning as saw left join study on saw.stu_id  = study.stu_id left join serie on saw.ser_id  = serie.ser_id
    where study.stu_id = {$stu_id} and removed = false order by factor desc,serie.instancenumber,serie.serorder,serie.seuid";


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

	$arr = array ();
	while ( $row = pg_fetch_row ($query) )
	{
		$res = array ( 
	        		"saw_id" 			=> $row [0],
	        		"alertname" 		=> $row [1],
	        		"description"		=> $row [2],
	        		"description_auto"	=> $row [3],
	        		"instancenumber"	=> $row [4],
	        		"valuetype"			=> $row [5],
	        		"currentvalue"		=> $row [6],
	        		"nrdvalue"			=> $row [7],
	        		"factor"			=> $row [8]
	        	);

	    array_push ( $arr, $res );
	}

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