Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ public function getSubmissionsData(Form $form, string $fileFormat, ?File $file =
$gridColumnsPerQuestionId = [];
/** @var array<int, list<int>> $rankingOptionsPerQuestionId */
$rankingOptionsPerQuestionId = [];
/** @var array<int, int> $exportColumnCountPerQuestionId */
$exportColumnCountPerQuestionId = [];

$optionPerOptionId = [];
foreach ($questions as $question) {
Expand Down Expand Up @@ -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() . ')'];
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
122 changes: 122 additions & 0 deletions tests/Unit/Service/SubmissionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
[
Expand Down
Loading