This commit is contained in:
106
lib/scoring.php
106
lib/scoring.php
@ -835,3 +835,109 @@ function qdb_set_coach_scoring_band(
|
||||
$row['profileID'] = $profileID;
|
||||
return qdb_scoring_review_row_to_api($row);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{profileID: string, name: string}>
|
||||
*/
|
||||
function qdb_active_scoring_profiles(PDO $pdo): array {
|
||||
$rows = $pdo->query(
|
||||
"SELECT profileID, name FROM scoring_profile WHERE isActive = 1 ORDER BY name"
|
||||
)->fetchAll(PDO::FETCH_ASSOC);
|
||||
return array_map(static fn(array $p) => [
|
||||
'profileID' => (string)$p['profileID'],
|
||||
'name' => (string)$p['name'],
|
||||
], $rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* CSV / results table columns for active scoring profiles.
|
||||
*
|
||||
* @param list<array{profileID: string, name: string}> $profiles
|
||||
* @return list<array{profileID: string, field: string, header: string}>
|
||||
*/
|
||||
function qdb_scoring_export_column_defs(array $profiles): array {
|
||||
$cols = [];
|
||||
foreach ($profiles as $p) {
|
||||
$name = trim((string)($p['name'] ?? ''));
|
||||
$prefix = $name !== '' ? $name : (string)($p['profileID'] ?? '');
|
||||
$profileID = (string)($p['profileID'] ?? '');
|
||||
if ($profileID === '') {
|
||||
continue;
|
||||
}
|
||||
$cols[] = ['profileID' => $profileID, 'field' => 'calculated', 'header' => "{$prefix} calculated category"];
|
||||
$cols[] = ['profileID' => $profileID, 'field' => 'counselor', 'header' => "{$prefix} counselor category"];
|
||||
$cols[] = ['profileID' => $profileID, 'field' => 'override_by', 'header' => "{$prefix} override by"];
|
||||
$cols[] = ['profileID' => $profileID, 'field' => 'override_at', 'header' => "{$prefix} override at"];
|
||||
}
|
||||
return $cols;
|
||||
}
|
||||
|
||||
function qdb_scoring_export_cell_value(array $profileResult, string $field): string {
|
||||
$calc = (string)($profileResult['band'] ?? '');
|
||||
$coach = trim((string)($profileResult['coachBand'] ?? ''));
|
||||
$differs = $coach !== '' && $coach !== $calc;
|
||||
return match ($field) {
|
||||
'calculated' => $calc,
|
||||
'counselor' => $coach,
|
||||
'override_by' => $differs ? (string)($profileResult['coachReviewedByUsername'] ?? '') : '',
|
||||
'override_at' => $differs && !empty($profileResult['coachReviewedAt'])
|
||||
? date('Y-m-d H:i', (int)$profileResult['coachReviewedAt']) : '',
|
||||
default => '',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{profileID: string, field: string, header: string}> $columnDefs
|
||||
* @param array<string, array<string, mixed>> $byProfile profileID => result row
|
||||
* @return array<string, string>
|
||||
*/
|
||||
function qdb_scoring_export_row_values(array $byProfile, array $columnDefs): array {
|
||||
$row = [];
|
||||
foreach ($columnDefs as $col) {
|
||||
$profileID = (string)($col['profileID'] ?? '');
|
||||
$result = $byProfile[$profileID] ?? null;
|
||||
$row[$col['header']] = is_array($result)
|
||||
? qdb_scoring_export_cell_value($result, (string)($col['field'] ?? ''))
|
||||
: '';
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scoring results for export, keyed by clientCode then profileID.
|
||||
*
|
||||
* @param list<string> $clientCodes
|
||||
* @return array<string, array<string, array<string, mixed>>>
|
||||
*/
|
||||
function qdb_scoring_export_results_map(PDO $pdo, array $clientCodes, array $tokenRec): array {
|
||||
if ($clientCodes === []) {
|
||||
return [];
|
||||
}
|
||||
[$rbacClause, $rbacParams] = rbac_client_filter($tokenRec, 'cl');
|
||||
$placeholders = implode(',', array_fill(0, count($clientCodes), '?'));
|
||||
$stmt = $pdo->prepare(
|
||||
"SELECT r.clientCode, r.profileID, r.band, r.coachBand, r.coachReviewedAt, r.coachReviewedByUserID,
|
||||
u.username AS coachReviewedByUsername
|
||||
FROM client_scoring_profile_result r
|
||||
INNER JOIN scoring_profile sp ON sp.profileID = r.profileID AND sp.isActive = 1
|
||||
INNER JOIN client cl ON cl.clientCode = r.clientCode
|
||||
LEFT JOIN users u ON u.userID = r.coachReviewedByUserID
|
||||
WHERE r.clientCode IN ($placeholders) AND ($rbacClause)"
|
||||
);
|
||||
$stmt->execute(array_merge(array_values($clientCodes), $rbacParams));
|
||||
|
||||
$map = [];
|
||||
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
||||
$cc = (string)$row['clientCode'];
|
||||
$map[$cc][(string)$row['profileID']] = $row;
|
||||
}
|
||||
return $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
function qdb_scoring_export_csv_headers(PDO $pdo): array {
|
||||
$profiles = qdb_active_scoring_profiles($pdo);
|
||||
return array_column(qdb_scoring_export_column_defs($profiles), 'header');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user