adding process completion logic
Some checks failed
PHPUnit / test (push) Has been cancelled

This commit is contained in:
2026-07-01 12:07:41 +02:00
parent 9bd9d9653c
commit 339091e13f
10 changed files with 353 additions and 10 deletions

View File

@ -473,7 +473,7 @@ function qdb_list_scoring_profiles(PDO $pdo): array {
$stmt = $pdo->query(
'SELECT profileID, name, description, isActive,
greenMin, greenMax, yellowMin, yellowMax, redMin,
createdAt, updatedAt
processCompleteBands, createdAt, updatedAt
FROM scoring_profile ORDER BY name'
);
$profiles = [];
@ -481,6 +481,7 @@ function qdb_list_scoring_profiles(PDO $pdo): array {
$row['isActive'] = (int)$row['isActive'];
$bands = qdb_normalize_scoring_bands($row);
$row = array_merge($row, $bands);
$row['processCompleteBands'] = qdb_row_process_complete_bands($row);
$row['questionnaires'] = qdb_scoring_profile_members($pdo, $row['profileID']);
$profiles[] = $row;
}
@ -491,7 +492,7 @@ function qdb_get_scoring_profile(PDO $pdo, string $profileID): ?array {
$stmt = $pdo->prepare(
'SELECT profileID, name, description, isActive,
greenMin, greenMax, yellowMin, yellowMax, redMin,
createdAt, updatedAt
processCompleteBands, createdAt, updatedAt
FROM scoring_profile WHERE profileID = :pid'
);
$stmt->execute([':pid' => $profileID]);
@ -501,6 +502,7 @@ function qdb_get_scoring_profile(PDO $pdo, string $profileID): ?array {
}
$row['isActive'] = (int)$row['isActive'];
$row = array_merge($row, qdb_normalize_scoring_bands($row));
$row['processCompleteBands'] = qdb_row_process_complete_bands($row);
$row['questionnaires'] = qdb_scoring_profile_members($pdo, $profileID);
return $row;
}
@ -598,12 +600,13 @@ function qdb_seed_default_rhs_scoring_profile(PDO $pdo): ?string {
$now = time();
$pdo->prepare(
'INSERT INTO scoring_profile (profileID, name, description, isActive,
greenMin, greenMax, yellowMin, yellowMax, redMin, createdAt, updatedAt)
VALUES (:id, :name, :desc, 1, 0, 12, 13, 36, 37, :ca, :ua)'
greenMin, greenMax, yellowMin, yellowMax, redMin, processCompleteBands, createdAt, updatedAt)
VALUES (:id, :name, :desc, 1, 0, 12, 13, 36, 37, :pcb, :ca, :ua)'
)->execute([
':id' => $profileID,
':name' => 'RHS Index',
':desc' => 'Legacy RHS thresholds (green 012, yellow 1336, red 37+)',
':pcb' => '[]',
':ca' => $now,
':ua' => $now,
]);
@ -618,6 +621,56 @@ function qdb_is_valid_scoring_band(?string $band): bool {
return in_array($band, ['green', 'yellow', 'red'], true);
}
/**
* @param mixed $raw
* @return list<string>
*/
function qdb_parse_process_complete_bands($raw): array {
if (is_string($raw)) {
$decoded = json_decode($raw, true);
$raw = is_array($decoded) ? $decoded : [];
}
if (!is_array($raw)) {
return [];
}
$out = [];
foreach ($raw as $band) {
$b = strtolower(trim((string)$band));
if (qdb_is_valid_scoring_band($b)) {
$out[] = $b;
}
}
return array_values(array_unique($out));
}
/**
* @return list<string>
*/
function qdb_decode_process_complete_bands(?string $json): array {
if ($json === null || trim($json) === '') {
return [];
}
return qdb_parse_process_complete_bands(json_decode($json, true));
}
/**
* @param list<string> $bands
*/
function qdb_encode_process_complete_bands(array $bands): string {
return json_encode(qdb_parse_process_complete_bands($bands), JSON_UNESCAPED_UNICODE);
}
/**
* @param array<string, mixed> $row
* @return list<string>
*/
function qdb_row_process_complete_bands(array $row): array {
if (array_key_exists('processCompleteBands', $row) && is_array($row['processCompleteBands'])) {
return qdb_parse_process_complete_bands($row['processCompleteBands']);
}
return qdb_decode_process_complete_bands((string)($row['processCompleteBands'] ?? '[]'));
}
/**
* Coach decision when set; otherwise the server-computed band.
*/