101 lines
2.2 KiB
PHP
101 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'];
|
|
|
|
$inputRaw = file_get_contents('php://input');
|
|
$input = json_decode($inputRaw, 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;
|
|
$updated = false;
|
|
|
|
foreach ($data['words'] as &$word) {
|
|
|
|
if ((int)$word['id'] === $wordId) {
|
|
|
|
// init stats veilig
|
|
if (!isset($word['correct'])) $word['correct'] = 0;
|
|
if (!isset($word['wrong'])) $word['wrong'] = 0;
|
|
|
|
// normalize compare
|
|
$expected = strtolower(trim($word['answer']));
|
|
$given = strtolower(trim($userAnswer));
|
|
|
|
$correct = ($expected === $given);
|
|
|
|
// update stats
|
|
if ($correct) {
|
|
$word['correct']++;
|
|
} else {
|
|
$word['wrong']++;
|
|
}
|
|
|
|
// SRS update (nextReview + interval)
|
|
updateWordStats($word, $correct);
|
|
|
|
$updated = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// als woord niet gevonden is
|
|
if (!$updated) {
|
|
echo json_encode([
|
|
'error' => 'word_not_found',
|
|
'wordId' => $wordId
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
// write back
|
|
$ok = JsonDB::write($file, $data);
|
|
|
|
if (!$ok) {
|
|
echo json_encode([
|
|
'error' => 'write_failed'
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode([
|
|
'correct' => $correct,
|
|
'stats' => [
|
|
'wordId' => $wordId,
|
|
'correct' => $correct ? 1 : 0
|
|
],
|
|
'debug' => [
|
|
'updatedCorrect' => $data['words'][$i]['correct'] ?? null,
|
|
'updatedWrong' => $data['words'][$i]['wrong'] ?? null
|
|
]
|
|
]); |