Update api/answer.php

This commit is contained in:
2026-06-15 12:47:13 +02:00
parent 46e0321093
commit 1f3e208e36
+29 -22
View File
@@ -35,33 +35,38 @@ if (!isset($data['words']) || !is_array($data['words'])) {
$correct = false;
$updatedWord = null;
/**
* SESSION STATS INIT
*/
if (!isset($_SESSION['session_stats'])) {
$_SESSION['session_stats'] = [
'correct' => 0,
'wrong' => 0
];
}
foreach ($data['words'] as $i => $word) {
if ((int)$word['id'] === $wordId) {
// init stats
if (!isset($data['words'][$i]['correct'])) {
$data['words'][$i]['correct'] = 0;
}
if (!isset($data['words'][$i]['wrong'])) {
$data['words'][$i]['wrong'] = 0;
}
// normalize
$expected = strtolower(trim($data['words'][$i]['answer']));
$given = strtolower(trim($userAnswer));
$correct = ($expected === $given);
// update stats
/**
* ❗ SESSIE STATS (DIT IS WAT JE WIL)
*/
if ($correct) {
$data['words'][$i]['correct']++;
$_SESSION['session_stats']['correct']++;
} else {
$data['words'][$i]['wrong']++;
$_SESSION['session_stats']['wrong']++;
}
// SRS update (nextReview etc.)
/**
* SRS UPDATE (blijft op woordniveau)
*/
updateWordStats($data['words'][$i], $correct);
$updatedWord = $data['words'][$i];
@@ -78,7 +83,6 @@ if ($updatedWord === null) {
exit;
}
// save JSON
$ok = JsonDB::write($file, $data);
if (!$ok) {
@@ -88,13 +92,16 @@ if (!$ok) {
echo json_encode([
'correct' => $correct,
/**
* UI FEEDBACK (SESSIE)
*/
'session' => $_SESSION['session_stats'],
/**
* DEBUG (optioneel)
*/
'debug' => [
'wordId' => $wordId,
'updatedCorrect' => $updatedWord['correct'] ?? null,
'updatedWrong' => $updatedWord['wrong'] ?? null
],
'stats' => [
'wordId' => $wordId,
'correct' => $correct ? 1 : 0
],
'wordId' => $wordId
]
]);