updated api
This commit is contained in:
81
lib/migrate_v4.php
Normal file
81
lib/migrate_v4.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* One-time migration to v4: submission history, uploadedAt, activity tables.
|
||||
*/
|
||||
|
||||
function qdb_migrate_v4(PDO $pdo): void {
|
||||
$cols = $pdo->query("PRAGMA table_info(completed_questionnaire)")->fetchAll(PDO::FETCH_ASSOC);
|
||||
$hasUploaded = false;
|
||||
foreach ($cols as $c) {
|
||||
if (($c['name'] ?? '') === 'uploadedAt') {
|
||||
$hasUploaded = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$hasUploaded) {
|
||||
$pdo->exec('ALTER TABLE completed_questionnaire ADD COLUMN uploadedAt INTEGER');
|
||||
}
|
||||
|
||||
$subCount = (int)$pdo->query('SELECT COUNT(*) FROM questionnaire_submission')->fetchColumn();
|
||||
if ($subCount > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$completions = $pdo->query('SELECT * FROM completed_questionnaire')->fetchAll(PDO::FETCH_ASSOC);
|
||||
if (empty($completions)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$insertSub = $pdo->prepare("
|
||||
INSERT INTO questionnaire_submission (
|
||||
submissionID, clientCode, questionnaireID, assignedByCoach, status,
|
||||
startedAt, completedAt, uploadedAt, sumPoints, submissionVersion,
|
||||
submittedByUserID, submittedByRole
|
||||
) VALUES (
|
||||
:sid, :cc, :qn, :abc, :st, :sa, :ca, :ua, :sp, 1, NULL, NULL
|
||||
)
|
||||
");
|
||||
$insertAns = $pdo->prepare("
|
||||
INSERT INTO submission_answer (submissionID, questionID, answerOptionID, freeTextValue, numericValue, answeredAt)
|
||||
VALUES (:sid, :qid, :aoid, :ftv, :nv, :at)
|
||||
");
|
||||
$updateCq = $pdo->prepare('UPDATE completed_questionnaire SET uploadedAt = :ua WHERE clientCode = :cc AND questionnaireID = :qn');
|
||||
|
||||
foreach ($completions as $cq) {
|
||||
$cc = $cq['clientCode'];
|
||||
$qn = $cq['questionnaireID'];
|
||||
$uploadedAt = $cq['completedAt'] !== null ? (int)$cq['completedAt'] : (int)(microtime(true) * 1000);
|
||||
$submissionID = 'mig_' . bin2hex(random_bytes(12));
|
||||
|
||||
$insertSub->execute([
|
||||
':sid' => $submissionID,
|
||||
':cc' => $cc,
|
||||
':qn' => $qn,
|
||||
':abc' => $cq['assignedByCoach'] ?? null,
|
||||
':st' => $cq['status'] ?: 'completed',
|
||||
':sa' => $cq['startedAt'],
|
||||
':ca' => $cq['completedAt'],
|
||||
':ua' => $uploadedAt,
|
||||
':sp' => $cq['sumPoints'],
|
||||
]);
|
||||
|
||||
$ansStmt = $pdo->prepare("
|
||||
SELECT questionID, answerOptionID, freeTextValue, numericValue, answeredAt
|
||||
FROM client_answer WHERE clientCode = :cc
|
||||
AND questionID IN (SELECT questionID FROM question WHERE questionnaireID = :qn)
|
||||
");
|
||||
$ansStmt->execute([':cc' => $cc, ':qn' => $qn]);
|
||||
foreach ($ansStmt->fetchAll(PDO::FETCH_ASSOC) as $a) {
|
||||
$insertAns->execute([
|
||||
':sid' => $submissionID,
|
||||
':qid' => $a['questionID'],
|
||||
':aoid' => $a['answerOptionID'],
|
||||
':ftv' => $a['freeTextValue'],
|
||||
':nv' => $a['numericValue'],
|
||||
':at' => $a['answeredAt'],
|
||||
]);
|
||||
}
|
||||
|
||||
$updateCq->execute([':ua' => $uploadedAt, ':cc' => $cc, ':qn' => $qn]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user