<?php
	include_once("db.php");

	//make result object
	$result = array();
	$result['ok'] = false;

	//get post data
	$postdata = file_get_contents("php://input");
	$post = json_decode($postdata, true);

	$type = $post['type'];
	$idrace = $post['idrace'];

	if($type == 'now') {
		$addmeasurement= pg_prepare($dbconn, "addmeasurement", "insert into measurement (idrace, manual) values ($1, true)");
		$addmeasurement= pg_execute($dbconn, "addmeasurement", array($idrace));
		if($addmeasurement===false) {
			$result['ok']	= false;
			$result['error'] = pg_last_error($dbconn);
		} else {
			$result['ok'] = true;
		}
	} else if($type == 'relative')  {
		$timestamp = $post['timestamp'];
		$addmeasurement= pg_prepare($dbconn, "addmeasurement", "insert into measurement (idrace, timestamp, manual) values ($1, $2, true)");
		$addmeasurement= pg_execute($dbconn, "addmeasurement", array($idrace, $timestamp));
		if($addmeasurement===false) {
			$result['ok']	= false;
			$result['error'] = pg_last_error($dbconn);
		} else {
			$result['ok'] = true;
		}
	}

	//print message
	$resultjson =  json_encode($result);

	echo $resultjson;
?>
