enhanced submission handling with deccision trees
Some checks failed
PHPUnit / test (push) Has been cancelled

This commit is contained in:
2026-06-05 11:57:28 +02:00
parent 6883654d7c
commit c37940c9a9
4 changed files with 157 additions and 13 deletions

View File

@ -128,6 +128,7 @@ if ($method === 'POST') {
$sumPoints = 0;
$glassByParent = [];
$submittedQuestionIds = [];
$answerInsert = $pdo->prepare("
INSERT INTO client_answer (clientCode, questionID, answerOptionID, freeTextValue, numericValue, answeredAt)
VALUES (:cc, :qid, :aoid, :ftv, :nv, :at)
@ -137,6 +138,9 @@ if ($method === 'POST') {
numericValue = excluded.numericValue,
answeredAt = excluded.answeredAt
");
$answerDelete = $pdo->prepare(
'DELETE FROM client_answer WHERE clientCode = :cc AND questionID = :qid'
);
require_once __DIR__ . '/../lib/app_answers.php';
$answers = qdb_group_app_submit_answers($answers, $shortIdToType, $symptomParentMap);
@ -173,6 +177,8 @@ if ($method === 'POST') {
$maxLen = $freeTextMaxLen[$fullQID] ?? 2000;
$freeTextValue = sanitize_free_text($freeTextValue, $maxLen);
if ($freeTextValue === '') {
$answerDelete->execute([':cc' => $clientCode, ':qid' => $fullQID]);
$submittedQuestionIds[$fullQID] = true;
continue;
}
}
@ -192,6 +198,15 @@ if ($method === 'POST') {
}
}
$hasValue = $answerOptionID !== null
|| ($freeTextValue !== null && $freeTextValue !== '')
|| $numericValue !== null;
if (!$hasValue) {
$answerDelete->execute([':cc' => $clientCode, ':qid' => $fullQID]);
$submittedQuestionIds[$fullQID] = true;
continue;
}
$answerInsert->execute([
':cc' => $clientCode,
':qid' => $fullQID,
@ -200,23 +215,35 @@ if ($method === 'POST') {
':nv' => $numericValue,
':at' => $answeredAt,
]);
$submittedQuestionIds[$fullQID] = true;
}
foreach ($glassByParent as $parentQID => $symptomLabels) {
$existing = $pdo->prepare(
'SELECT freeTextValue FROM client_answer WHERE clientCode = :cc AND questionID = :qid'
$json = qdb_build_glass_symptom_json($symptomLabels);
if ($json === null) {
$answerDelete->execute([':cc' => $clientCode, ':qid' => $parentQID]);
} else {
$answerInsert->execute([
':cc' => $clientCode,
':qid' => $parentQID,
':aoid' => null,
':ftv' => $json,
':nv' => null,
':at' => $completedAt ?? $startedAt,
]);
}
$submittedQuestionIds[$parentQID] = true;
}
// Re-edit uploads omit answers pruned on the device (e.g. branching changes).
// Drop live rows for questions not present in this payload.
$staleQuestionIds = array_diff(array_values($shortIdMap), array_keys($submittedQuestionIds));
if ($staleQuestionIds !== []) {
$placeholders = implode(',', array_fill(0, count($staleQuestionIds), '?'));
$staleDelete = $pdo->prepare(
"DELETE FROM client_answer WHERE clientCode = ? AND questionID IN ($placeholders)"
);
$existing->execute([':cc' => $clientCode, ':qid' => $parentQID]);
$prevJson = $existing->fetchColumn();
$merged = qdb_merge_glass_symptom_json($prevJson !== false ? (string)$prevJson : null, $symptomLabels);
$answerInsert->execute([
':cc' => $clientCode,
':qid' => $parentQID,
':aoid' => null,
':ftv' => $merged,
':nv' => null,
':at' => $completedAt ?? $startedAt,
]);
$staleDelete->execute(array_merge([$clientCode], array_values($staleQuestionIds)));
}
// Upsert completed_questionnaire record