<?php 

	include '../../autoload.php';

	function setConditionalOKKO(&$variable,$isok)
	{
	
		if ($variable=="KOK")  return;
		if($isok==true)
		{
			if($variable=="KO")
				$variable="KOK";
			else $variable="OK";
			return;
		}
		else
		{
			if($variable=="OK")
				$variable="KOK";
			else $variable="KO";
			return;
		}
	}

	if(!isset($_SESSION['language']))
		$_SESSION['language']="FR";
	$language=$_SESSION['language'];
	
	$getonlyonepoint="";
	$getdata=false;
	if(isset($_REQUEST['id']))
	{
		$getonlyonepoint=" and quaad_point.id = ".$_REQUEST['id']." ";
		$getdata=true;
	}
	$xml="<rulecheck>";
	
	$getonlyonetag="";
	if(isset($_REQUEST['tag_id']))
	{
		if($_REQUEST['tag_id']>-1)
		$getonlyonetag=" and quaadpoint_tag.id= ".$_REQUEST['tag_id']." ";
	}
	
	$ins_id=-1;
	$dep_id=-1;
	$beg_date="19000101";
	$end_date="29991231";
	$audit_type=1;
	
	
	$conn = get_dbconnection();
	
	if(isset($_REQUEST['audit_id']))
	{
			
		$sqlv = "select type,inst_id,dep_id,coalesce(to_char(start_date,'YYYY-MM-DD 00:00:00'),'1900-01-01 00:00:00'),coalesce(to_char(end_date,'YYYY-MM-DD 00:00:00'),'2999-12-31 00:00:00') from audit where id=".$_REQUEST['audit_id'];
		if($queryv=pg_query($conn,$sqlv))
		{
			if($rowv=pg_fetch_row($queryv))
			{
			  $audit_type=$rowv[0];
			  $ins_id=$rowv[1];
			  $dep_id=$rowv[2];
			  $beg_date=$rowv[3];
			  $end_date=$rowv[4];
			}
		}
	}


	$sql="select quaad_point.id,quaadpoint_tag_name.name, quaadpoint_tag.procedure_needed, quaadpoint_tag.documents_needed,quaadpoint_tag.scope,period,additional_param,quaadpoint_tag.id as tagid,quaadpoint_tag.scope2,quaadpoint_tag.where_notagid,isglobalscope,document_scope from quaad_point,quaadpoint_tag, link_quaad_tag,quaadpoint_tag_name  where quaad_point.quaadversion=".$audit_type." and quaad_point.id=link_quaad_tag.id_quaadpoint and link_quaad_tag.id_tag=quaadpoint_tag.id and quaadpoint_tag.id=quaadpoint_tag_name.id_quaadpoint_tag and quaadpoint_tag_name.language='".$language."' ".$getonlyonepoint." ".$getonlyonetag.";";

	// error_log ( "sql : ".$sql );

	$query=pg_query($conn,$sql);
	//$xml.=$sql;
	
	if($query)
	{
		$isok=array();
		$validateds=array();
		if(isset($_REQUEST['audit_id']))
		{
			
			$sqlv = "select distinct quaad_point_id from audit_record where validated=true and audit_id=".$_REQUEST['audit_id'];
			if($queryv=pg_query($conn,$sqlv))
			{
				while($rowv=pg_fetch_row($queryv))
				{
				   array_push($validateds,$rowv[0]);
				}
			}
		}
		while($row = pg_fetch_row($query))
		{
			$id=$row[0];
			$tag=$row[1];
			$procedure=$row[2];
			$nbrdoc=$row[3];
			$scope=$row[4];
			$period=$row[5];
			$additionalparam=$row[6];
			$tagid=$row[7];
			$scope2=$row[8];
			$where_notagid=$row[9];
			$isglobalscope=$row[10];
			$document_scope=$row[11];

			error_log ( "+++++++++++++++++++++++++++++++++++++++++++++++++++++" );
			error_log ( " id : " .$id );
			error_log ( " tagid : " .$tagid );
			error_log ( " tag : " .$tag );
			// error_log ( " dep_id : " .$dep_id );
			// error_log ( " additionalparam : " .$additionalparam );
			// error_log ( " scope : " .$scope );
			// error_log ( " scope2 : " .$scope2 );
			// error_log ( " document_scope : " .$document_scope );
			// error_log ( " procedure : " .$procedure );
			// error_log ( " period : " .$period );

	
			$additionalparam=str_replace("___ins_id___",$ins_id,$additionalparam);
			$additionalparam=str_replace("___dep_id___",$dep_id,$additionalparam);
			$additionalparam=str_replace("___start_date___",$beg_date,$additionalparam);
			$additionalparam=str_replace("___end_date___",$end_date,$additionalparam);
			
		
			$d1 = strtotime($beg_date);
			$d2 = strtotime($end_date);
			$totalSecondsDiff = abs($d1-$d2);
			$months_audit  = round($totalSecondsDiff/60/60/24/30);
			if($period >0)
			{
				$nbrdoc=floor(($months_audit/$period)*$nbrdoc);
			}
			error_log ( " nbrdoc : " .$nbrdoc );
			
			//$additionalparam=preg_replace('/___(.*?)___/',"{\$$1}",$additionalparam);
			if($getdata==true)
			{
				//$xml.="<expected procedure=\"".$procedure."\" nbrdocs=\"".$nbrdoc."\" />";
				$xml.="<expected procedure=\"".$tag."\" tag_id=\"".$tagid."\" nbrdocs=\"".$nbrdoc."\" />";
			}
			if(!isset($isok[$id]))
				$isok[$id]='U';
			
			if($procedure>0)
			{
				//// ISRECORDPROCEDURE
				$isrecord=false;
				if(isset($_REQUEST['audit_id']))
				{
					$audit_id=$_REQUEST['audit_id'];
					$sqlrec="select count(*) from audit_recordprocedure,audit_record where audit_recordprocedure.audit_record_id=audit_record.id and  audit_id=".$audit_id." and audit_record.quaad_point_id=".$id.";";
					
					if($queryrec=pg_query($conn,$sqlrec))
					{
						if($rowrec=pg_fetch_row($queryrec))
						{
							if($rowrec[0] != 0)
							{
								$isrecord=true;
							}
						}
					}
				}
				//ODOT
				//$sql2="select folder,file,realfilename,document_link.doc_id,procedure.id,'' as remark, label from document_link,document, document_master, procedure where procedure.id=document_link.ext_id and document_link.ext_id in(select id from procedure where type=".$procedure." and removed=false) and document_link.type='procedure' and document.id=document_link.id and document.doc_master_id=document_master.id and document_master.removed=false;";
				$sql2="select  folder,file,realfilename,dl.doc_id,procedure.id,'' as remark, label,name_fr from procedure,document_link dl ,document_master dm ,document,quaadtag_doc_link qdl where document.doc_master_id =dm.id and dl.doc_id =document.id and qdl.id_doclink =dl.id and qdl.id_quaadtag =".$tagid." and procedure.id=dl.ext_id and dl.type='procedure'";
				
				
				if($isrecord==true)
				{				
					$sql2="select folder,file,realfilename,document_link.doc_id,audit_recordprocedure.procedure_id,remark,label, '' from audit_record,audit_recordprocedure, document_link,document where
					audit_record.audit_id=".$_REQUEST['audit_id']." and audit_record.quaad_point_id=".$id." and audit_recordprocedure.audit_record_id = audit_record.id and
					document_link.doc_id=document.id and document_link.doc_id =audit_recordprocedure.doc_id";
				}
				
				error_log ( " sql2 : " .$sql2 );
				$query2 = pg_query($conn,$sql2);
				$count=0;
				while($roww=pg_fetch_row($query2))
				{
					$count++;
					if($getdata==true)
					{
						$folder	= htmlspecialchars($roww[0], ENT_XML1, 'UTF-8');
						$file = htmlspecialchars($roww[1], ENT_XML1, 'UTF-8');
						$realfilename = htmlspecialchars($roww[2], ENT_XML1, 'UTF-8');
						$docid = $roww[3];
						$procid	= $roww[4];
						$remark	= htmlspecialchars($roww[5], ENT_XML1, 'UTF-8');
						$label = htmlspecialchars($roww[6], ENT_XML1, 'UTF-8');
						error_log ( " name : " . $roww[7] );

						$name = htmlspecialchars($roww[7], ENT_XML1, 'UTF-8');

						error_log ( " name : " . $name );
						$xml.="<procedure pointid=\"".$id."\" folder=\"".$folder."\" file=\"".$file."\" fname=\"".$realfilename."\" docid=\"".$docid."\" procid=\"".$procid."\" remark=\"".$remark."\" label=\"".$label."\"  name=\"".$name."\" />";
					}
				}
				$isrecord=false;
				
				if($count>0)
				{
					setConditionalOKKO($isok[$id],true);
				}
				else 
				{
					setConditionalOKKO($isok[$id],false);
				}
			}
			if($nbrdoc>=0)
			{
					$nameinscope="id";
					if($getdata==true)
					{
						$sqlns = "select name from quaad_nameinscope where scope_name='".$scope."'";
						
						$queryns=pg_query($conn,$sqlns);
						if($queryns)
						{
							if($rowns=pg_fetch_row($queryns))
							{
								$nameinscope=$rowns[0];
							}
						}
					}
				
				
				$getonlyonescopeid="";
				if(isset($_REQUEST['scope_id']))
				{
					$getonlyonescopeid=" and id= ".$_REQUEST['scope_id']." ";
				}

				//	$sql3="select distinct(id),".$nameinscope." from ".$scope." where 1=1 and removed=false ".$getonlyonescopeid." ".$additionalparam.";";
				$sql3="select distinct(id),".$nameinscope." from ".$scope." where 1=1 ".$getonlyonescopeid." ".$additionalparam.";";
				
				$sql_scopeview="select view from link_scope_view where scope='".$document_scope."';";
				
				$queryscopev=pg_query($conn,$sql_scopeview);
				$scope_view="";
				if($queryscopev)
				{
					if($rowsvi=pg_fetch_row($queryscopev))
					{
						$scope_view=$rowsvi[0];
					}
				}
				
				
				if($isglobalscope=="t")
				{
					$sql3= "select generate_series(1,".$nbrdoc."),'Document'; ";
				}

				error_log ( " sql3 : " .$sql3 );
				$query3=pg_query($conn,$sql3);

				if($query3)
				{
					
					while($row3=pg_fetch_row($query3))
					{
						$scope_id=$row3[0];
						$scope_name=htmlspecialchars($row3[1], ENT_XML1, 'UTF-8');
						error_log (" -- scope_id : " .$scope_id );
						error_log (" -- scope_name : " .$scope_name );
						if($getdata==true)
						{
							
							$xml.="<entity ent_id=\"".$scope_id."\" type_view=\"".$scope_view."\" type=\"".$scope."\" name=\"".$scope_name."\" tagid=\"".$tagid."\" tagname=\"".$tag."\">";
						}
						if(strlen((string)$scope2)>0)
						{
							$isrecord=false;
							
							
							$getonlyonescope2id="";
							$sqlrec= "select count(*) from audit_subrecord,audit_record where  audit_subrecord.audit_record_id=audit_record.id and  audit_id=".$audit_id." and audit_record.quaad_point_id=".$id;
							if($qrec=pg_query($conn,$sqlrec))
							{
								if($rowrec=pg_fetch_row($qrec))
								{
									if($rowrec[0]>0) $isrecord=true;
								}
							}
							
							if(isset($_REQUEST['scope2_id']) && ($_REQUEST['scope2_id']!=-1))
							{
								$getonlyonescope2id=" and id= ".$_REQUEST['scope2_id']." ";
								
								$sqlrec="select count(*) from audit_subrecord where scope_id=".$scope_id." and quaadpoint_tag_id=".$tagid." and scope2_id=".$_REQUEST['scope2_id'];
								if($queryrec=pg_query($conn,$sqlrec))
								{
									if($rowrec=pg_fetch_row($queryrec))
									{
										if($rowrec[0] != 0)
										{
											$isrecord=true;
											
										}
									}
								}
							}
							else 
							{
								if($where_notagid=="false")
								{
									$sqlscope2="select count(*) from ".$scope2.",".$scope.",document_link,v_last_documentstatus vld ,quaadtag_doc_link where ".$scope."_id=".$scope_id." and ".$scope2.".".$scope."_id =".$scope.".id  and document_link.ext_id=".$scope2.".id and document_link.type='".$document_scope."' and vld.status_id=14 and vld.doc_id = document_link.doc_id and quaadtag_doc_link.id_doclink=document_link.id and quaadtag_doc_link.id_quaadtag=".$tagid." ".$getonlyonescope2id;
								}
								else
								{
									//$sqlscope2="select count(*) from ".$scope2.",".$scope.",document_link,v_last_documentstatus vld  where ".$scope."_id=".$scope_id."  and document_link.ext_id=".$scope2.".id and document_link.type='".$document_scope."' and vld.status_id=1 and vld.doc_id = document_link.doc_id ".$getonlyonescope2id;
									//$sqlscope2="
									$sqlscope2="select count(*) from ".$scope2." where ".$scope."_id=".$scope_id." ".$getonlyonescope2id;
									if($period>0)
									{
										$sqlscope2.=" and tocheck_date>'".$beg_date."' and tocheck_date < '".$end_date."';";
										
									}
								}
							}

							//TOCHECK
							if($isrecord==true)
							{
								if($where_notagid=="false") $sql2="select count(*) from document_link,v_last_documentstatus,quaadtag_doc_link where type='".$document_scope."' ".$ext_id_sql." and v_last_documentstatus.doc_id=document_link.doc_id and v_last_documentstatus.status_id =14 and quaadtag_doc_link.id_quaadtag=".$tagid." and quaadtag_doc_link.id_doclink=document_link.id";			
									else $sql2="select count(*) from document_link,v_last_documentstatus where type='".$document_scope."' ".$ext_id_sql." and v_last_documentstatus.doc_id=document_link.doc_id and v_last_documentstatus.status_id =14";
							
								$sqldetectchange="with t as( select count(*) from audit_subrecord,audit_record where audit_record_id=audit_record.id and  scope_id=".$scope_id." and scope2_id=".$_REQUEST['scope2_id']." and quaadpoint_tag_id=".$tagid." and doc_id>-1 and validated=false union ".$sql2.") select count(*) from t";
								
								$querydetectchange=pg_query($conn,$sqldetectchange);
								$isrecord=false;
								if($querydetectchange)
								{
									if($rowdc = pg_fetch_row($querydetectchange))
									{
										if($rowdc[0]==1)
										$sql2="select count(*) from audit_subrecord where scope_id=".$scope_id." and quaadpoint_tag_id=".$tagid." and force_result!='KO' and scope2_id=".$_REQUEST['scope2_id'];
										$isrecord=true;
									}
								}
							}


							$queryscope2=pg_query($conn,$sqlscope2);
							if($queryscope2)
							{
								if($rowsc2=pg_fetch_row($queryscope2))
								{
									$countsc2=$rowsc2[0];
									$sql_period=" ";
									
									if($period >0)
									{
									
										
										$sql_period="with t as (select id,doc_id,force_result,force_doctext,force_docid,date from ".$scope2." where ".$scope."_id=".$scope_id." ".$getonlyonescope2id." and date > ".$end_date." - ".$period." month::interval order by date desc limit 1)
										select id,doc_id,force_result,force_doctext,force_docid,date   from ".$scope2." where ".$scope."_id=".$scope_id." ".$getonlyonescope2id." and date > coalesce((select date from t limit 1)- '";
										/*with t as(
select * from v_bquaad_deviceinspection vbd  where device_id=10 and date > now() - '12 month'::interval order by date desc limit 1
)
select * from v_bquaad_deviceinspection where device_id=10 and date > coalesce((select date from t limit 1)- '24 month'::interval,now()-'24 month'::interval) and date < coalesce((select date from t limit 1),now())
union select * from t*/
										//$sql_period="and date > now()- '".$period." month'::interval ";
										
									
									}
									
									error_log ( " countsc2 : " .$countsc2. "   nbrdoc : ".$nbrdoc );
									if(intval($countsc2) >= intval($nbrdoc))
									{
										setConditionalOKKO($isok[$id],true);
									}
									else 
									{
										setConditionalOKKO($isok[$id],false);
									}
									if($countsc2>0)
									{
										if($where_notagid=="true") //--> on considère que si un tag n'est pas nécessaire, une vue fait office de lien pour déterminer le bon document
										{
											
											$sqlscope2="select id,doc_id,force_result,force_doctext,force_docid from ".$scope2." where ".$scope."_id=".$scope_id." ".$getonlyonescope2id;
											$queryscope2 = pg_query($conn,$sqlscope2);
											//($sqlscope2);
											if($queryscope2)
											{
												
												while ($rowsc2 = pg_fetch_row($queryscope2))
												{
													if($rowsc2[2]=="KO")
													{
														setConditionalOKKO($isok[$id],false);
													}
													if($getdata==true)
													{
													
														$xml.="<subentity subent_id=\"".$rowsc2[0]."\" >";
													
														// $xml.="<document realfname=\"Inspection\" file=\"\" docid=\"".$rowsc2[1]."\" force_doc_id=\"".$rowsc2[4]."\" force_result=\"".$rowsc2[2]."\">".htmlspecialchars($rowsc2[3], ENT_XML1, 'UTF-8')."</document>";
														$xml.="<document realfname=\"Inspection\" file=\"\" docid=\"".$rowsc2[1]."\" force_doc_id=\"".$rowsc2[4]."\" force_result=\"".$rowsc2[2]."\" label=\"Inspection\" remark=\"\">".htmlspecialchars($rowsc2[3], ENT_XML1, 'UTF-8')."</document>";
														$xml.="</subentity>";
													}
												}
											}
										}
										else{ // --> si le tag est bien fourni --> fonctionnement classique
											$sqlsc2_doc="select document.file ,folder,realfilename,document.id,'' as remark, label,".$scope2.".id, name_fr from document_link,v_last_documentstatus,document,document_master,quaadtag_doc_link,".$scope2." where document_link.type='".$document_scope."'  and ext_id =".$scope2.".id and v_last_documentstatus.doc_id=document_link.doc_id and v_last_documentstatus.status_id =14  and document.id=document_link.doc_id and document_master.id=document.doc_master_id and document_master.removed=false and quaadtag_doc_link.id_doclink=document_link.id and quaadtag_doc_link.id_quaadtag=".$tagid.";";
											if($querysc2_doc=pg_query($conn,$sqlsc2_doc))
											{
												
												while ($row_doc=pg_fetch_row($querysc2_doc))
												{
												
													$doc_file = htmlspecialchars($row_doc[0], ENT_XML1, 'UTF-8');
													$folder	= htmlspecialchars($row_doc[1], ENT_XML1, 'UTF-8');
													$doc_realfilename = htmlspecialchars($row_doc[2], ENT_XML1, 'UTF-8');
													$docid = $row_doc[3];
													$remark	= htmlspecialchars($row_doc[4], ENT_XML1, 'UTF-8');
													$label = htmlspecialchars($row_doc[5], ENT_XML1, 'UTF-8');
													$sc2id= $row_doc[6];
													$name_fr = htmlspecialchars($row_doc[7], ENT_XML1, 'UTF-8');
													$xml.="<subentity subent_id=\"".$sc2id."\">";
													$xml.="<document file=\"".$doc_file."\" realfname=\"".$doc_realfilename."\" docid=\"".$docid."\" tagid=\"".$tagid."\" remark=\"".$remark."\" label=\"".$label."\" name_fr=\"".$name_fr."\" scope2=\"".$sc2id."\"/>";
												$xml.="</subentity>";
												}
												
											}
										}
									}
								}
							}
						}
						else
						{
							$isrecord=false;
							$sqlrec = "select count(*) from audit_subrecord where scope_id=".$scope_id." and quaadpoint_tag_id=".$tagid;
							
							if($queryrec=pg_query($conn,$sqlrec))
							{
								if($rowrec=pg_fetch_row($queryrec))
								{
									if($rowrec[0] != 0)
									{
										$isrecord=true;
									}
								}
							}
							
							/*$sql2="select count(*) from document_link,".$scope.",document where document_link.type='".$scope."' and document_link.ext_id =".$scope.".id and document_link.doc_id =document.id and document.removed=false;";*/
							$ext_id_sql=" and ext_id =".$scope_id." ";
							if($isglobalscope=="t") $ext_id_sql="";
						/*	$sql2="select count(*) from document_link,v_last_documentstatus,quaadtag_doc_link where type='".$scope."' ".$ext_id_sql." and v_last_documentstatus.doc_id=document_link.doc_id and v_last_documentstatus.status_id =1 and quaadtag_doc_link.id_quaadtag=".$tagid;				
							if($where_notagid=="false")  $sql2.=" and quaadtag_doc_link.id_doclink=document_link.id ";*/
							$sql2="";
							if($where_notagid=="false") $sql2="select count(*) from document_link,v_last_documentstatus,quaadtag_doc_link where type='".$document_scope."' ".$ext_id_sql." and v_last_documentstatus.doc_id=document_link.doc_id and v_last_documentstatus.status_id =14 and quaadtag_doc_link.id_quaadtag=".$tagid." and quaadtag_doc_link.id_doclink=document_link.id";			
							else $sql2="select count(*) from document_link,v_last_documentstatus where type='".$document_scope."' ".$ext_id_sql." and v_last_documentstatus.doc_id=document_link.doc_id and v_last_documentstatus.status_id =14";
							
							if($isrecord==true)
							{
								
								$sqldetectchange="with t as( select count(*) from audit_subrecord,audit_record where audit_record_id=audit_record.id and  scope_id=".$scope_id." and quaadpoint_tag_id=".$tagid." and doc_id>-1 and validated=false union ".$sql2.") select count(*) from t";
								$querydetectchange=pg_query($conn,$sqldetectchange);
								$isrecord=false;
								if($querydetectchange)
								{
									if($rowdc = pg_fetch_row($querydetectchange))
									{
										if($rowdc[0]==1)
										$sql2="select count(*) from audit_subrecord where scope_id=".$scope_id." and quaadpoint_tag_id=".$tagid." and doc_id>-1";
										$isrecord=true;
									}
								}
							}

							$query2=pg_query($conn,$sql2);
							$count=0;
							if($row2=pg_fetch_row($query2))
							{
								$count=$row2[0];
								if($count>0 && $getdata==true)
								{
								/*	$sql_doc="select document.file ,folder,realfilename,document.id,'' as remark, label from document_link,v_last_documentstatus,quaadtag_doc_link,document,document_master where document_link.type='".$scope."' ".$ext_id_sql." and v_last_documentstatus.doc_id=document_link.doc_id and v_last_documentstatus.status_id =1 and quaadtag_doc_link.id_quaadtag=".$tagid."  and document.id=document_link.doc_id and document_master.id=document.doc_master_id and document_master.removed=false";
									if($where_notagid=="false") $sql_doc .=" and quaadtag_doc_link.id_doclink=document_link.id";
									*/
									$sql_doc="";
									if($where_notagid=="false"){
										$sql_doc="select document.file ,folder,realfilename,document.id,'' as remark, label, document_master.name from document_link,v_last_documentstatus,quaadtag_doc_link,document,document_master where document_link.type='".$document_scope."' ".$ext_id_sql." and v_last_documentstatus.doc_id=document_link.doc_id and v_last_documentstatus.status_id =14 and quaadtag_doc_link.id_quaadtag=".$tagid."  and document.id=document_link.doc_id and document_master.id=document.doc_master_id and document_master.removed=false and quaadtag_doc_link.id_doclink=document_link.id";
									}
									else{
										$sql_doc="select document.file ,folder,realfilename,document.id,'' as remark, label, document_master.name from document_link,v_last_documentstatus,document,document_master where document_link.type='".$document_scope."' ".$ext_id_sql." and v_last_documentstatus.doc_id=document_link.doc_id and v_last_documentstatus.status_id =14  and document.id=document_link.doc_id and document_master.id=document.doc_master_id and document_master.removed=false";
									}
									if($query_doc=pg_query($conn,$sql_doc))
									{
										while ($row_doc=pg_fetch_row($query_doc))
										{
										
											$doc_file 			= htmlspecialchars($row_doc[0], ENT_XML1, 'UTF-8');
											$folder				= htmlspecialchars($row_doc[1], ENT_XML1, 'UTF-8');
											$doc_realfilename 	= htmlspecialchars($row_doc[2], ENT_XML1, 'UTF-8');
											$docid 				= $row_doc[3];
											$remark				= htmlspecialchars($row_doc[4], ENT_XML1, 'UTF-8');
											$label 				= htmlspecialchars($row_doc[5], ENT_XML1, 'UTF-8');
											$name 				= htmlspecialchars($row_doc[6], ENT_XML1, 'UTF-8');
											
											if($isrecord==true)
											{
												$sql_doc2="select document.file,folder,realfilename,document.id,remark,label from document,audit_subrecord where audit_subrecord.scope_id=".$scope_id." and quaadpoint_tag_id=".$tagid." and audit_subrecord.doc_id=document.id and doc_id=".$docid;
												
												if($query_doc2=pg_query($conn,$sql_doc2))
												{
													if($rowdoc2=pg_fetch_row($query_doc2))
													{
														$remark	= htmlspecialchars($rowdoc2[4], ENT_XML1, 'UTF-8');
														$label = htmlspecialchars($rowdoc2[5], ENT_XML1, 'UTF-8');
													}
												}
										
											}
											
											$xml.="<document file=\"".$doc_file."\" realfname=\"".$doc_realfilename."\" docid=\"".$docid."\" tagid=\"".$tagid."\" remark=\"".$remark."\" label=\"".$label."\" name=\"".$name."\" />";
										}									
									}
																	
								}
								$forcestatustrue=false;
								
									if(isset($_REQUEST['audit_id']))
									{
										$sql_docmis="select remark,forcedstatus from audit_subrecord, audit_record where audit_record.id=audit_subrecord.audit_record_id and quaadpoint_tag_id=".$tagid." and audit_id=".$_REQUEST['audit_id']." and scope_id=".$scope_id." and doc_id=-1;";
										if($query_docmis=pg_query($conn,$sql_docmis))
										{
											if($rowdocmis=pg_fetch_row($query_docmis))
											{
												$remark=htmlspecialchars($rowdocmis[0], ENT_XML1, 'UTF-8');
												$fs = $rowdocmis[1];
												if($fs=="t") $forcestatustrue=true;
												if($getdata==true)
												{
													$xml.="<documentmis tagid=\"".$tagid."\" remark=\"".$remark."\" forcestatus=\"".$fs."\"/>";
												}
											}
										}						
									}		
								
								if((intval($count) >= intval($nbrdoc)) || $forcestatustrue==true)
								{
									setConditionalOKKO($isok[$id],true);
								}
								else {
									
									setConditionalOKKO($isok[$id],false);
									
								}
							}														
						}
						if($getdata==true)
							$xml.="</entity>";
					}
				}
			}
		}
		foreach ($isok as $key => $value) 
		{
			$validated="false";
			if(in_array($key,$validateds))
			{
				$validated="true";
			}
			$xml.="<check id=\"".$key."\" value=\"".$value."\" validated=\"".$validated."\"/>";
		}
	}
	$xml.="</rulecheck>";
	header('Content-Type: text/xml');
	echo $xml;
	
?>