<?php
	require_once '../../autoload.php';
	require( '../GetDataFromDB.php' );
	
	RightsCheck ('VIEW_EXAMLINK');	

	
	$toProcess = true;
	
	$draw			= $_GET ['draw'];
	$start			= $_GET ['start'];
	$length			= $_GET ['length'];
	
	$columns 		= $_GET ['columns'];
	$order			= $_GET ['order'];
	$orderCol		= $order [0]['column'];
	$orderDir		= $order [0]['dir'];
	$orderColName	= $columns [$orderCol]['data'];
	$orderby		= " ORDER BY ".$orderColName." ".$orderDir;
        
	$limit = '';

	if ( isset($_GET['start']) && $_GET['length'] != -1 ) 
	{
		$limit = " OFFSET ".intval($_GET['start'])." LIMIT ".intval($_GET['length']);
	}

	$exam_id 			= -1;
	$name 				= "";
	$description 	= "";

	if(isset($_GET['id']))      	$id 			= $_GET['id'];
	if(isset($_GET['name']))      	$name 			= $_GET['name'];
	if(isset($_GET['description'])) $description 	= $_GET['description'];

	$req = " ";
	if ( empty ($name) == false )
		$req.= " and name ilike '%".$name."%'";
	if ( empty ($description) == false )
		$req.= " and description ilike '%".$description."%'";
    
   	$psql = "select id, name, description from examident where removed=false and examident.id not in(select examident_id from examident_link where exam_id='{$id}'){$req}";

	$recordsTotal 		= 1000;
	$recordsFiltered	= 0;
	
	$data				= Array ();
	
	if ( $toProcess == true )
		$data = GetDataFromDB ( $psql, $recordsFiltered, $limit, $orderby );
	
	$arr = array (
			"draw"            => intval( $draw ),
			"recordsTotal"    => intval( $recordsTotal ),
			"recordsFiltered" => intval( $recordsFiltered ),
			"data"            => $data,
			"sql"			  => $psql.$orderby.$limit
	);

	$result = json_encode ( $arr );

	echo $result;
	
 
?>