Skip to content

Commit

Permalink
return selected values as a separate array
Browse files Browse the repository at this point in the history
calculate the right id for the dropdown and radio buttons and checkboxes
load answers into the UI
  • Loading branch information
tpokorra committed Aug 23, 2023
1 parent 3f69f71 commit 0f99455
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
49 changes: 30 additions & 19 deletions lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,39 @@ public function getOptions(int $questionId): array {

private function getAnswers(int $formId, string $userId): array {

$submissionList = [];
try {
$submissionEntities = $this->submissionMapper->findByForm($formId);
$submissionEntities = $this->submissionMapper->findByFormAndUser($formId, $userId);
// we will only use the first submissionEntity
foreach ($submissionEntities as $submissionEntity) {
$answerList = [];
$submission = $submissionEntity->read();
if ($submission['userId'] == $userId) {
$answerEntities = $this->answerMapper->findBySubmission($submission['id']);
return $answerEntities;
$answerEntities = $this->answerMapper->findBySubmission($submission['id']);
foreach ($answerEntities as $answerEntity) {
$answer = $answerEntity->read();
$questionId = $answer['questionId'];
if (!array_key_exists($questionId, $answerList)) {
$answerList[$questionId] = array();
}
$options = $this->getOptions($answer['questionId']);
if (!empty($options)) {
error_log(print_r($options,true));
// match option text to option index
foreach ($options as $option) {
if ($option['text'] == $answer['text']) {
$answerList[$questionId][] = $option['id'];
}
}
} else {
// copy the text
$answerList[$questionId][] = $answer['text'];
}
}
return $answerList;
}
} catch (DoesNotExistException $e) {
// Just ignore, if no Data
}

return array();
return [];
}

/**
Expand All @@ -177,24 +195,12 @@ private function getAnswers(int $formId, string $userId): array {
*/
public function getQuestions(int $formId): array {

$answerEntities = NULL;
if ($this->currentUser->getUID()) {
$answerEntities = $this->getAnswers($formId, $this->currentUser->getUID());
}
$questionList = [];
try {
$questionEntities = $this->questionMapper->findByForm($formId);
foreach ($questionEntities as $questionEntity) {
$question = $questionEntity->read();
$question['options'] = $this->getOptions($question['id']);
if ($answerEntities) {
foreach ($answerEntities as $answerEntity) {
$answer = $answerEntity->read();
if ($answer['questionId'] == $question['id']) {
$question['value'] = $answer['text'];
}
}
}
$questionList[] = $question;
}
} catch (DoesNotExistException $e) {
Expand Down Expand Up @@ -234,6 +240,11 @@ public function getForm(int $id): array {
$form = $this->formMapper->findById($id);
$result = $form->read();
$result['questions'] = $this->getQuestions($id);

if ($this->currentUser->getUID()) {
$result['answers'] = $this->getAnswers($id, $this->currentUser->getUID());
}

$result['shares'] = $this->getShares($id);

// Append permissions for current user.
Expand Down
3 changes: 3 additions & 0 deletions src/views/Submit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ export default {
computed: {
validQuestions() {
// load answers from the server into this form
this.answers = this.form.answers
return this.form.questions.filter(question => {
// All questions must have a valid title
if (question.text?.trim() === '') {
Expand Down

0 comments on commit 0f99455

Please sign in to comment.