diff --git a/docs/android-questionnaire-api.md b/docs/android-questionnaire-api.md index f5ed61f..0abb77f 100644 --- a/docs/android-questionnaire-api.md +++ b/docs/android-questionnaire-api.md @@ -176,8 +176,20 @@ Success: "ok": true, "data": { "clients": [ - "CLIENT-001", - "CLIENT-002" + { + "clientCode": "CLIENT-001", + "completedQuestionnaires": [ + { + "questionnaireID": "questionnaire_1_demographic_information", + "sumPoints": 42, + "completedAt": 1714471800 + } + ] + }, + { + "clientCode": "CLIENT-002", + "completedQuestionnaires": [] + } ] } } @@ -185,7 +197,12 @@ Success: Fields: -- `clients`: array of client code strings assigned to the coach, ordered alphabetically. +- `clients`: array of client objects assigned to the coach, ordered alphabetically by `clientCode`. +- `clients[].clientCode`: the client's code string. +- `clients[].completedQuestionnaires`: array of questionnaires already completed by this client. Empty array if none. +- `clients[].completedQuestionnaires[].questionnaireID`: the questionnaire ID, matching the IDs from the questionnaire list endpoint. +- `clients[].completedQuestionnaires[].sumPoints`: total points from the last upload for this questionnaire. +- `clients[].completedQuestionnaires[].completedAt`: Unix timestamp (seconds) of when the questionnaire was completed, or `null` if not recorded. Common failures: @@ -411,7 +428,18 @@ data class SubmitQuestionnaireResponse( ) data class ClientsResponse( - val clients: List + val clients: List +) + +data class ClientInfo( + val clientCode: String, + val completedQuestionnaires: List = emptyList() +) + +data class CompletedQuestionnaire( + val questionnaireID: String, + val sumPoints: Int, + val completedAt: Long? ) ``` @@ -453,4 +481,4 @@ interface QuestionnaireApi { } ``` -Pass the token as `Bearer $token`. +Pass the token as `Bearer $token`. \ No newline at end of file diff --git a/handlers/app_questionnaires.php b/handlers/app_questionnaires.php index 0720a44..9dbc26b 100644 --- a/handlers/app_questionnaires.php +++ b/handlers/app_questionnaires.php @@ -175,7 +175,32 @@ if ($fetchClients) { ORDER BY clientCode "); $stmt->execute([':cid' => $coachID]); - $clients = $stmt->fetchAll(PDO::FETCH_COLUMN); + $clientCodes = $stmt->fetchAll(PDO::FETCH_COLUMN); + + $completions = []; + if (!empty($clientCodes)) { + $placeholders = implode(',', array_fill(0, count($clientCodes), '?')); + $stmt2 = $pdo->prepare(" + SELECT clientCode, questionnaireID, sumPoints, completedAt + FROM completed_questionnaire + WHERE clientCode IN ($placeholders) + "); + $stmt2->execute($clientCodes); + foreach ($stmt2->fetchAll(PDO::FETCH_ASSOC) as $row) { + $completions[$row['clientCode']][] = [ + 'questionnaireID' => $row['questionnaireID'], + 'sumPoints' => (int) $row['sumPoints'], + 'completedAt' => $row['completedAt'] !== null ? (int) $row['completedAt'] : null, + ]; + } + } + + $clients = array_map(function ($code) use ($completions) { + return [ + 'clientCode' => $code, + 'completedQuestionnaires' => $completions[$code] ?? [], + ]; + }, $clientCodes); qdb_discard($tmpDb, $lockFp); json_success(['coachID' => $coachID, 'clients' => $clients]);