Files
2026-06-15 12:47:13 +02:00

107 lines
2.2 KiB
PHP

<?php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/jsondb.php';
require_once __DIR__ . '/../includes/srs.php';
require_once __DIR__ . '/../includes/auth.php';
requireLogin();
header('Content-Type: application/json; charset=utf-8');
$user = $_SESSION['user'];
$input = json_decode(file_get_contents('php://input'), true);
if (!is_array($input)) {
echo json_encode(['error' => 'invalid_json']);
exit;
}
$lang = $input['lang'] ?? '';
$list = $input['list'] ?? '';
$wordId = (int)($input['wordId'] ?? 0);
$userAnswer = trim($input['answer'] ?? '');
$file = listPath($user, $lang, $list);
$data = JsonDB::read($file);
if (!isset($data['words']) || !is_array($data['words'])) {
echo json_encode(['error' => 'list_not_found']);
exit;
}
$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) {
// normalize
$expected = strtolower(trim($data['words'][$i]['answer']));
$given = strtolower(trim($userAnswer));
$correct = ($expected === $given);
/**
* ❗ SESSIE STATS (DIT IS WAT JE WIL)
*/
if ($correct) {
$_SESSION['session_stats']['correct']++;
} else {
$_SESSION['session_stats']['wrong']++;
}
/**
* SRS UPDATE (blijft op woordniveau)
*/
updateWordStats($data['words'][$i], $correct);
$updatedWord = $data['words'][$i];
break;
}
}
if ($updatedWord === null) {
echo json_encode([
'error' => 'word_not_found',
'wordId' => $wordId
]);
exit;
}
$ok = JsonDB::write($file, $data);
if (!$ok) {
echo json_encode(['error' => 'write_failed']);
exit;
}
echo json_encode([
'correct' => $correct,
/**
* UI FEEDBACK (SESSIE)
*/
'session' => $_SESSION['session_stats'],
/**
* DEBUG (optioneel)
*/
'debug' => [
'wordId' => $wordId
]
]);