Update api/answer.php
This commit is contained in:
+27
-32
@@ -12,13 +12,10 @@ header('Content-Type: application/json; charset=utf-8');
|
|||||||
|
|
||||||
$user = $_SESSION['user'];
|
$user = $_SESSION['user'];
|
||||||
|
|
||||||
$inputRaw = file_get_contents('php://input');
|
$input = json_decode(file_get_contents('php://input'), true);
|
||||||
$input = json_decode($inputRaw, true);
|
|
||||||
|
|
||||||
if (!is_array($input)) {
|
if (!is_array($input)) {
|
||||||
echo json_encode([
|
echo json_encode(['error' => 'invalid_json']);
|
||||||
'error' => 'invalid_json'
|
|
||||||
]);
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,46 +28,49 @@ $file = listPath($user, $lang, $list);
|
|||||||
$data = JsonDB::read($file);
|
$data = JsonDB::read($file);
|
||||||
|
|
||||||
if (!isset($data['words']) || !is_array($data['words'])) {
|
if (!isset($data['words']) || !is_array($data['words'])) {
|
||||||
echo json_encode([
|
echo json_encode(['error' => 'list_not_found']);
|
||||||
'error' => 'list_not_found'
|
|
||||||
]);
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$correct = false;
|
$correct = false;
|
||||||
$updated = false;
|
$updatedWord = null;
|
||||||
|
|
||||||
foreach ($data['words'] as &$word) {
|
foreach ($data['words'] as $i => $word) {
|
||||||
|
|
||||||
if ((int)$word['id'] === $wordId) {
|
if ((int)$word['id'] === $wordId) {
|
||||||
|
|
||||||
// init stats veilig
|
// init stats
|
||||||
if (!isset($word['correct'])) $word['correct'] = 0;
|
if (!isset($data['words'][$i]['correct'])) {
|
||||||
if (!isset($word['wrong'])) $word['wrong'] = 0;
|
$data['words'][$i]['correct'] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// normalize compare
|
if (!isset($data['words'][$i]['wrong'])) {
|
||||||
$expected = strtolower(trim($word['answer']));
|
$data['words'][$i]['wrong'] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalize
|
||||||
|
$expected = strtolower(trim($data['words'][$i]['answer']));
|
||||||
$given = strtolower(trim($userAnswer));
|
$given = strtolower(trim($userAnswer));
|
||||||
|
|
||||||
$correct = ($expected === $given);
|
$correct = ($expected === $given);
|
||||||
|
|
||||||
// update stats
|
// update stats
|
||||||
if ($correct) {
|
if ($correct) {
|
||||||
$word['correct']++;
|
$data['words'][$i]['correct']++;
|
||||||
} else {
|
} else {
|
||||||
$word['wrong']++;
|
$data['words'][$i]['wrong']++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// SRS update (nextReview + interval)
|
// SRS update (nextReview etc.)
|
||||||
updateWordStats($word, $correct);
|
updateWordStats($data['words'][$i], $correct);
|
||||||
|
|
||||||
|
$updatedWord = $data['words'][$i];
|
||||||
|
|
||||||
$updated = true;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// als woord niet gevonden is
|
if ($updatedWord === null) {
|
||||||
if (!$updated) {
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'error' => 'word_not_found',
|
'error' => 'word_not_found',
|
||||||
'wordId' => $wordId
|
'wordId' => $wordId
|
||||||
@@ -78,24 +78,19 @@ if (!$updated) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// write back
|
// save JSON
|
||||||
$ok = JsonDB::write($file, $data);
|
$ok = JsonDB::write($file, $data);
|
||||||
|
|
||||||
if (!$ok) {
|
if (!$ok) {
|
||||||
echo json_encode([
|
echo json_encode(['error' => 'write_failed']);
|
||||||
'error' => 'write_failed'
|
|
||||||
]);
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'correct' => $correct,
|
'correct' => $correct,
|
||||||
'stats' => [
|
|
||||||
'wordId' => $wordId,
|
|
||||||
'correct' => $correct ? 1 : 0
|
|
||||||
],
|
|
||||||
'debug' => [
|
'debug' => [
|
||||||
'updatedCorrect' => $data['words'][$i]['correct'] ?? null,
|
'wordId' => $wordId,
|
||||||
'updatedWrong' => $data['words'][$i]['wrong'] ?? null
|
'updatedCorrect' => $updatedWord['correct'] ?? null,
|
||||||
|
'updatedWrong' => $updatedWord['wrong'] ?? null
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
Reference in New Issue
Block a user