From 2ab2a38753312dfc44081a2005c674966f200337 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Mon, 23 Nov 2026 08:32:49 +0100 Subject: [PATCH] fix(export): keep cells under their headers for empty grid and ranking answers The export writes one column per row of a grid, per row and column of a number grid and per option of a ranking. Three cases wrote fewer cells than the header has columns, so every later cell on the line, including the answers to all following questions, moved left: - a number grid row left completely empty wrote one cell instead of one per column, - a grid or ranking question left unanswered wrote one cell instead of one per export column. Also, a number grid cell answered with 0 was exported empty, because empty() treats 0 as missing. Fixes #3980 Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- lib/Service/SubmissionService.php | 17 ++- tests/Unit/Service/SubmissionServiceTest.php | 122 +++++++++++++++++++ 2 files changed, 136 insertions(+), 3 deletions(-) diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index 8a7609abd..bc027dbff 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -283,6 +283,8 @@ public function getSubmissionsData(Form $form, string $fileFormat, ?File $file = $gridColumnsPerQuestionId = []; /** @var array> $rankingOptionsPerQuestionId */ $rankingOptionsPerQuestionId = []; + /** @var array $exportColumnCountPerQuestionId */ + $exportColumnCountPerQuestionId = []; $optionPerOptionId = []; foreach ($questions as $question) { @@ -311,12 +313,16 @@ public function getSubmissionsData(Form $form, string $fileFormat, ?File $file = } } } + + $exportColumnCountPerQuestionId[$question->getId()] = count($gridRowsPerQuestionId[$question->getId()] ?? []) + * ($gridCellType === Constants::ANSWER_GRID_TYPE_NUMBER ? count($gridColumnsPerQuestionId[$question->getId()] ?? []) : 1); } elseif ($question->getType() === Constants::ANSWER_TYPE_RANKING) { $options = $optionsByQuestion[$question->getId()] ?? []; foreach ($options as $option) { $optionPerOptionId[$option->getId()] = $option; $rankingOptionsPerQuestionId[$question->getId()][] = $option->getId(); } + $exportColumnCountPerQuestionId[$question->getId()] = count($rankingOptionsPerQuestionId[$question->getId()] ?? []); foreach ($rankingOptionsPerQuestionId[$question->getId()] as $optionId) { $header[] = ['id' => 'question-id-' . $question->getId() . '-' . $optionId, 'title' => $question->getText() . ' (' . $optionPerOptionId[$optionId]->getText() . ')']; } @@ -373,7 +379,8 @@ function (array $carry, Answer $answer) use ($questionPerQuestionId, $gridRowsPe $columns = []; foreach ($gridRowsPerQuestionId[$questionId] as $row) { if (empty($answerText[$row])) { - $columns[] = ''; + $emptyCells = $gridCellType === Constants::ANSWER_GRID_TYPE_NUMBER ? count($gridColumnsPerQuestionId[$questionId] ?? []) : 1; + array_push($columns, ...array_fill(0, $emptyCells, '')); continue; } @@ -384,7 +391,7 @@ function (array $carry, Answer $answer) use ($questionPerQuestionId, $gridRowsPe } elseif ($gridCellType === Constants::ANSWER_GRID_TYPE_NUMBER) { // For number grids, we need to create a header for each cell in the grid foreach ($gridColumnsPerQuestionId[$questionId] as $column) { - if (empty($answerText[$row][$column])) { + if (!isset($answerText[$row][$column]) || $answerText[$row][$column] === '') { $columns[] = ''; continue; } @@ -414,7 +421,11 @@ function (array $carry, Answer $answer) use ($questionPerQuestionId, $gridRowsPe }, []); foreach ($questions as $question) { - $row[] = $answers[$question->getId()] ?? null; + $answer = $answers[$question->getId()] ?? null; + if ($answer === null && isset($exportColumnCountPerQuestionId[$question->getId()])) { + $answer = ['columns' => array_fill(0, $exportColumnCountPerQuestionId[$question->getId()], '')]; + } + $row[] = $answer; } $data[] = $row; diff --git a/tests/Unit/Service/SubmissionServiceTest.php b/tests/Unit/Service/SubmissionServiceTest.php index 261c136ca..9f108d0f5 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -449,6 +449,128 @@ public static function dataGetSubmissionsData() { "1","user1","User 1","1973-11-29T22:33:09+01:00","Option 55","Option 56" ' ], + 'grid-number-empty-row' => [ + // Questions + [ + ['id' => 1, 'type' => 'grid', 'text' => 'Question 1', 'extraSettings' => ['questionType' => 'number'], 'options' => [ + ['id' => 2, 'text' => 'A1', 'optionType' => 'row'], + ['id' => 3, 'text' => 'A2', 'optionType' => 'row'], + ['id' => 4, 'text' => 'A3', 'optionType' => 'row'], + ['id' => 55, 'text' => 'N0', 'optionType' => 'column'], + ['id' => 56, 'text' => 'N1', 'optionType' => 'column'], + ['id' => 57, 'text' => 'N2', 'optionType' => 'column'], + ]], + ['id' => 9, 'type' => 'short', 'text' => 'Question 2'], + ], + // Array of Submissions incl. Answers + [ + [ + 'id' => 1, + 'userId' => 'user1', + 'timestamp' => 123456789, + 'answers' => [ + ['questionId' => 1, 'text' => '{"2":{"55":2,"57":1},"4":{"56":4}}'], + ['questionId' => 9, 'text' => 'after'], + ] + ], + ], + // Expected CSV-Result + ' + "Submission ID","User ID","User display name","Timestamp","Question 1 (A1 - N0)","Question 1 (A1 - N1)","Question 1 (A1 - N2)","Question 1 (A2 - N0)","Question 1 (A2 - N1)","Question 1 (A2 - N2)","Question 1 (A3 - N0)","Question 1 (A3 - N1)","Question 1 (A3 - N2)","Question 2" + "submission_id","user_id","user_display_name","timestamp","question-id-1-2-55","question-id-1-2-56","question-id-1-2-57","question-id-1-3-55","question-id-1-3-56","question-id-1-3-57","question-id-1-4-55","question-id-1-4-56","question-id-1-4-57","question-id-9" + "1","user1","User 1","1973-11-29T22:33:09+01:00","2","","1","","","","","4","","after" + ' + ], + 'grid-number-zero' => [ + // Questions + [ + ['id' => 1, 'type' => 'grid', 'text' => 'Question 1', 'extraSettings' => ['questionType' => 'number'], 'options' => [ + ['id' => 2, 'text' => 'A1', 'optionType' => 'row'], + ['id' => 3, 'text' => 'A2', 'optionType' => 'row'], + ['id' => 4, 'text' => 'A3', 'optionType' => 'row'], + ['id' => 55, 'text' => 'N0', 'optionType' => 'column'], + ['id' => 56, 'text' => 'N1', 'optionType' => 'column'], + ['id' => 57, 'text' => 'N2', 'optionType' => 'column'], + ]], + ['id' => 9, 'type' => 'short', 'text' => 'Question 2'], + ], + // Array of Submissions incl. Answers + [ + [ + 'id' => 1, + 'userId' => 'user1', + 'timestamp' => 123456789, + 'answers' => [ + ['questionId' => 1, 'text' => '{"2":{"56":0},"3":{"55":5},"4":{"57":7}}'], + ['questionId' => 9, 'text' => 'after'], + ] + ], + ], + // Expected CSV-Result + ' + "Submission ID","User ID","User display name","Timestamp","Question 1 (A1 - N0)","Question 1 (A1 - N1)","Question 1 (A1 - N2)","Question 1 (A2 - N0)","Question 1 (A2 - N1)","Question 1 (A2 - N2)","Question 1 (A3 - N0)","Question 1 (A3 - N1)","Question 1 (A3 - N2)","Question 2" + "submission_id","user_id","user_display_name","timestamp","question-id-1-2-55","question-id-1-2-56","question-id-1-2-57","question-id-1-3-55","question-id-1-3-56","question-id-1-3-57","question-id-1-4-55","question-id-1-4-56","question-id-1-4-57","question-id-9" + "1","user1","User 1","1973-11-29T22:33:09+01:00","","0","","5","","","","","7","after" + ' + ], + 'grid-unanswered' => [ + // Questions + [ + ['id' => 1, 'type' => 'grid', 'text' => 'Question 1', 'extraSettings' => ['questionType' => 'radio'], 'options' => [ + ['id' => 2, 'text' => 'R1', 'optionType' => 'row'], + ['id' => 3, 'text' => 'R2', 'optionType' => 'row'], + ['id' => 4, 'text' => 'R3', 'optionType' => 'row'], + ['id' => 55, 'text' => 'C1', 'optionType' => 'column'], + ['id' => 56, 'text' => 'C2', 'optionType' => 'column'], + ]], + ['id' => 9, 'type' => 'short', 'text' => 'Question 2'], + ], + // Array of Submissions incl. Answers + [ + [ + 'id' => 1, + 'userId' => 'user1', + 'timestamp' => 123456789, + 'answers' => [ + ['questionId' => 9, 'text' => 'after'], + ] + ], + ], + // Expected CSV-Result + ' + "Submission ID","User ID","User display name","Timestamp","Question 1 (R1)","Question 1 (R2)","Question 1 (R3)","Question 2" + "submission_id","user_id","user_display_name","timestamp","question-id-1-2","question-id-1-3","question-id-1-4","question-id-9" + "1","user1","User 1","1973-11-29T22:33:09+01:00","","","","after" + ' + ], + 'ranking-unanswered-before-another-question' => [ + // Questions + [ + ['id' => 1, 'type' => 'ranking', 'text' => 'Question 1', 'options' => [ + ['id' => 2, 'text' => 'First'], + ['id' => 3, 'text' => 'Second'], + ['id' => 4, 'text' => 'Third'], + ]], + ['id' => 9, 'type' => 'short', 'text' => 'Question 2'], + ], + // Array of Submissions incl. Answers + [ + [ + 'id' => 1, + 'userId' => 'user1', + 'timestamp' => 123456789, + 'answers' => [ + ['questionId' => 9, 'text' => 'after'], + ] + ], + ], + // Expected CSV-Result + ' + "Submission ID","User ID","User display name","Timestamp","Question 1 (First)","Question 1 (Second)","Question 1 (Third)","Question 2" + "submission_id","user_id","user_display_name","timestamp","question-id-1-2","question-id-1-3","question-id-1-4","question-id-9" + "1","user1","User 1","1973-11-29T22:33:09+01:00","","","","after" + ' + ], 'file-multi-answers' => [ // Questions [