Update api/answer.php
This commit is contained in:
+71
-23
@@ -1,49 +1,97 @@
|
||||
<?php
|
||||
|
||||
require_once '../includes/config.php';
|
||||
require_once '../includes/functions.php';
|
||||
require_once '../includes/jsondb.php';
|
||||
require_once '../includes/srs.php';
|
||||
require_once '../includes/auth.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'];
|
||||
|
||||
function normalize(string $s): string
|
||||
{
|
||||
$s = strtolower(trim($s));
|
||||
$s = preg_replace('/\s+/', ' ', $s);
|
||||
return $s;
|
||||
$inputRaw = file_get_contents('php://input');
|
||||
$input = json_decode($inputRaw, true);
|
||||
|
||||
if (!is_array($input)) {
|
||||
echo json_encode([
|
||||
'error' => 'invalid_json'
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$lang = $input['lang'];
|
||||
$list = $input['list'];
|
||||
$wordId = (int)$input['wordId'];
|
||||
$userAnswer = trim($input['answer']);
|
||||
$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) {
|
||||
foreach ($data['words'] as &$word) {
|
||||
|
||||
if ($word['id'] === $wordId) {
|
||||
if ((int)$word['id'] === $wordId) {
|
||||
|
||||
$correct =
|
||||
normalize($word['answer']) === normalize($userAnswer);
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
JsonDB::write($file, $data);
|
||||
// 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
|
||||
'correct' => $correct,
|
||||
'stats' => [
|
||||
'wordId' => $wordId,
|
||||
'correct' => $correct ? 1 : 0
|
||||
]
|
||||
]);
|
||||
Reference in New Issue
Block a user