<?php

require_once '../../autoload.php';

header('Content-Type: application/json; charset=utf-8');

$conn = get_dbconnection();
$stu_id=pg_escape_string($conn,$_GET['stuid']);
$out = array();
if($conn)
{


	$sql = "with t as(
	select alertwarning.id,nrd_id,nrd_type,nrd_value,value,nrd_valuetype,alert_name,ackalert.id as ackid,ackalert.removed as ackremoved,ackalert.comment,ackalert.lastmodif,ackalert.user_id, pat_id,alertwarning.stu_id from alertwarning left join ackalert on alertwarning.ack_id =ackalert.id where alertwarning.stu_id={$stu_id} and alertwarning.removed=false
	)
	select id,nrd_id,nrd_type,nrd_value,value,nrd_valuetype,case when pat_id = -1 then alert_name else '<b>&#9888; <span style=''color:red;''>'||alert_name||'</span></b>' end as alert_name,coalesce(ackid,-1) as ack_id,ackremoved,
	coalesce(comment,'') as comment,
	coalesce((select array_to_string(array_agg(distinct(val->>'146')),'<br>')
	from serie where stu_id={$stu_id} and val->>'146' is not null),'') as teccomment,
	lastmodif,user_id,account.firstname,account.lastname from t left join account on t.user_id=account.acc_id";

	  if($query=pg_query($conn,$sql))
	  {
		$out = pg_fetch_all($query,PGSQL_ASSOC);
		if($out==false)
		{
			$out=array();
		}
	  }
}

echo json_encode($out);

?>


