57 lines
1.7 KiB
PHP
57 lines
1.7 KiB
PHP
<?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();
|
|
}
|