<?php
// include database and object files
include_once '../config/init.php';	// contains $db
include_once '../config/error.php';
include_once '../objects/stats_object.php';

$sessionId = -1;

// read mandatory $_POST properties : ensure sessionId is passed in $_POST parameters
if (isset($_POST['sessionId']))
	$sessionId = $_POST['sessionId'];
else trigger_error( missing_parameter_error("sessionId") );

$userId = isset($_POST['userId']) ? $_POST['userId'] : -1;

// check sessionId is > 0
//if ($sessionId > 0)
//{
	$cacheKey = "debrief_" . intval($userId);
	$cached = cache_get($sessionId, $cacheKey);
	if ($cached !== null)
	{
		print_r($cached);
	}
	else
	{
		$stats = new StatsObject($db);
		$stats->getStatsForSession($sessionId, $userId);

		$stats_arr = $stats->getResultArray(true, "Stats_Collected_OK");
		$json = json_encode($stats_arr);
		cache_put($sessionId, $cacheKey, $json);
		print_r($json);	// OK
	}
//}
//else trigger_error("Invalid session id");
?>