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