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; $correct = false;
$updatedWord = null; $updatedWord = null;
/**
* SESSION STATS INIT
*/
if (!isset($_SESSION['session_stats'])) {
$_SESSION['session_stats'] = [
'correct' => 0,
'wrong' => 0
];
}
foreach ($data['words'] as $i => $word) { foreach ($data['words'] as $i => $word) {
if ((int)$word['id'] === $wordId) { 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 // normalize
$expected = strtolower(trim($data['words'][$i]['answer'])); $expected = strtolower(trim($data['words'][$i]['answer']));
$given = strtolower(trim($userAnswer)); $given = strtolower(trim($userAnswer));
$correct = ($expected === $given); $correct = ($expected === $given);
// update stats /**
* ❗ SESSIE STATS (DIT IS WAT JE WIL)
*/
if ($correct) { if ($correct) {
$data['words'][$i]['correct']++; $_SESSION['session_stats']['correct']++;
} else { } else {
$data['words'][$i]['wrong']++; $_SESSION['session_stats']['wrong']++;
} }
// SRS update (nextReview etc.) /**
* SRS UPDATE (blijft op woordniveau)
*/
updateWordStats($data['words'][$i], $correct); updateWordStats($data['words'][$i], $correct);
$updatedWord = $data['words'][$i]; $updatedWord = $data['words'][$i];
@@ -78,7 +83,6 @@ if ($updatedWord === null) {
exit; exit;
} }
// save JSON
$ok = JsonDB::write($file, $data); $ok = JsonDB::write($file, $data);
if (!$ok) { if (!$ok) {
@@ -88,13 +92,16 @@ if (!$ok) {
echo json_encode([ echo json_encode([
'correct' => $correct, 'correct' => $correct,
/**
* UI FEEDBACK (SESSIE)
*/
'session' => $_SESSION['session_stats'],
/**
* DEBUG (optioneel)
*/
'debug' => [ 'debug' => [
'wordId' => $wordId, 'wordId' => $wordId
'updatedCorrect' => $updatedWord['correct'] ?? null, ]
'updatedWrong' => $updatedWord['wrong'] ?? null
],
'stats' => [
'wordId' => $wordId,
'correct' => $correct ? 1 : 0
],
]); ]);