@ -1,56 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Activity / audit log helpers.
|
||||
*/
|
||||
|
||||
function log_activity(
|
||||
PDO $pdo,
|
||||
array $tokenRec,
|
||||
string $action,
|
||||
string $targetType,
|
||||
string $targetID,
|
||||
string $summary,
|
||||
array $details = [],
|
||||
?int $createdAt = null
|
||||
): void {
|
||||
$createdAt = $createdAt ?? (int)(microtime(true) * 1000);
|
||||
$eventID = 'evt_' . bin2hex(random_bytes(12));
|
||||
$userID = $tokenRec['userID'] ?? '';
|
||||
$username = '';
|
||||
if ($userID !== '') {
|
||||
$u = $pdo->prepare('SELECT username FROM users WHERE userID = :id');
|
||||
$u->execute([':id' => $userID]);
|
||||
$username = (string)($u->fetchColumn() ?: '');
|
||||
}
|
||||
$pdo->prepare("
|
||||
INSERT INTO activity_event (
|
||||
eventID, createdAt, actorUserID, actorUsername, actorRole, actorEntityID,
|
||||
action, targetType, targetID, summary, detailsJson
|
||||
) VALUES (
|
||||
:eid, :ca, :uid, :un, :role, :eid2,
|
||||
:act, :tt, :tid, :sum, :dj
|
||||
)
|
||||
")->execute([
|
||||
':eid' => $eventID,
|
||||
':ca' => $createdAt,
|
||||
':uid' => $userID,
|
||||
':un' => $username,
|
||||
':role' => $tokenRec['role'] ?? '',
|
||||
':eid2' => $tokenRec['entityID'] ?? '',
|
||||
':act' => $action,
|
||||
':tt' => $targetType,
|
||||
':tid' => $targetID,
|
||||
':sum' => $summary,
|
||||
':dj' => json_encode($details, JSON_UNESCAPED_UNICODE),
|
||||
]);
|
||||
}
|
||||
|
||||
function next_submission_version(PDO $pdo, string $clientCode, string $questionnaireID): int {
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT COALESCE(MAX(submissionVersion), 0) + 1
|
||||
FROM questionnaire_submission
|
||||
WHERE clientCode = :cc AND questionnaireID = :qn
|
||||
");
|
||||
$stmt->execute([':cc' => $clientCode, ':qn' => $questionnaireID]);
|
||||
return (int)$stmt->fetchColumn();
|
||||
}
|
||||
@ -1,81 +0,0 @@
|
||||
<?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