2026-03-09 07:12:13 +01:00

67 lines
2.1 KiB
PHP

<?php
// include database and object files
include_once '../config/init.php'; // contains $db
include_once '../config/error.php';
include_once '../objects/db_participates.php';
// prepare user object
$participation = new UserInGameSession($db);
$participation->userId = -1;
// read mandatory $_POST properties : ensure sessionId is passed in POST parameters
if (isset($_POST['sessionId']))
$participation->sessionId = $_POST['sessionId'];
else trigger_error( missing_parameter_error("sessionId") );
if (isset($_POST['userId']))
$participation->userId = $_POST['userId'];
$multiple = false;
// get overall objectives for all users in session
if ($participation->userId == -1)
{
$users = $participation->getAllUsersInSession();
$nbUsers = count($users);
if ($nbUsers > 1)
{
// multiple users for the session : get average of all users objectives
$multiple = true;
$firstElement = true;
foreach ($users as $oneUserId)
{
$part = new UserInGameSession($db);
$part->sessionId = $participation->sessionId;
$part->userId = (int)$oneUserId; // contains userId
if ($part->load())
{
// if this is the participation for the first user, simply update the participation variable
// otherwise, call add function to add this user's participation result to the overall one (participation variable)
if ($firstElement)
{
$participation->copy($part);
$firstElement = false;
}
else
$participation->add($part);
}
}
// once loop is completed, calculate average score, precision, reactionTime and objective completion
$participation->calculateAverages($nbUsers);
}
else if ($nbUsers == 1)
$participation->userId = (int)$users[0];
}
if (!$multiple && $participation->userId == -1)
trigger_error( missing_parameter_error("userId") );
// if we got participation for multiple users OR if we successfully load it for a single one, then everything is good
if ($multiple || (!$multiple && $participation->load()))
{
$participation_arr = $participation->getResultArray(true, "Results_Saved_OK");
print_r(json_encode($participation_arr)); // OK
}
else trigger_error("User_Results_Not_Found");
?>