From c54698f950cc51e5ba8a9c2bac506078bcc120bc Mon Sep 17 00:00:00 2001 From: Jonas Rittershofer Date: Mon, 11 May 2020 15:58:32 +0200 Subject: [PATCH 1/2] Clean loading results Signed-off-by: Jonas Rittershofer --- lib/Controller/ApiController.php | 12 ++---------- lib/Service/FormsService.php | 13 ++++++++++++- src/views/Results.vue | 23 ++++++++++------------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index a37f07f..638db4f 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -649,16 +649,8 @@ class ApiController extends Controller { $submissions[] = $submission; } - // Load question-texts, including deleted ones. - try { - $questionEntities = $this->questionMapper->findByForm($form->getId()); - } catch (DoesNotExistException $e) { - //handle silently - } - $questions = []; - foreach ($questionEntities as $questionEntity) { - $questions[] = $questionEntity->read(); - } + // Load currently active questions + $questions = $this->formsService->getQuestions($form->getId()); $response = [ 'submissions' => $submissions, diff --git a/lib/Service/FormsService.php b/lib/Service/FormsService.php index ee16c69..6eaaf12 100644 --- a/lib/Service/FormsService.php +++ b/lib/Service/FormsService.php @@ -78,7 +78,12 @@ class FormsService { $this->userSession = $userSession; } - + /** + * Load options corresponding to question + * + * @param integer $questionId + * @return array + */ public function getOptions(int $questionId): array { $optionList = []; try { @@ -93,6 +98,12 @@ class FormsService { } } + /** + * Load questions corresponding to form + * + * @param integer $formId + * @return array + */ public function getQuestions(int $formId): array { $questionList = []; try { diff --git a/src/views/Results.vue b/src/views/Results.vue index b95f36a..f020de2 100644 --- a/src/views/Results.vue +++ b/src/views/Results.vue @@ -71,10 +71,10 @@
@@ -117,14 +117,12 @@ export default { data() { return { loadingResults: true, - submissions: [], - questions: [], } }, computed: { noSubmissions() { - return this.submissions && this.submissions.length === 0 + return this.form.submissions?.length === 0 }, /** @@ -178,9 +176,8 @@ export default { const response = await axios.get(generateUrl('/apps/forms/api/v1/submissions/{hash}', { hash: this.form.hash, })) - this.submissions = response.data.submissions - this.questions = response.data.questions - console.debug(this.submissions) + this.form.submissions = response.data.submissions + this.form.questions = response.data.questions } catch (error) { console.error(error) showError(t('forms', 'There was an error while loading results')) @@ -194,8 +191,8 @@ export default { try { await axios.delete(generateUrl('/apps/forms/api/v1/submission/{id}', { id })) - const index = this.submissions.findIndex(search => search.id === id) - this.submissions.splice(index, 1) + const index = this.form.submissions.findIndex(search => search.id === id) + this.form.submissions.splice(index, 1) } catch (error) { console.error(error) showError(t('forms', 'There was an error while removing this response')) @@ -212,7 +209,7 @@ export default { this.loadingResults = true try { await axios.delete(generateUrl('/apps/forms/api/v1/submissions/{formId}', { formId: this.form.id })) - this.submissions = [] + this.form.submissions = [] } catch (error) { console.error(error) showError(t('forms', 'There was an error while removing responses')) @@ -229,14 +226,14 @@ export default { }) const formattedSubmissions = [] - this.submissions.forEach(submission => { + this.form.submissions.forEach(submission => { const formattedSubmission = { userDisplayName: submission.userDisplayName, timestamp: moment(submission.timestamp, 'X').format('L LT'), } submission.answers.forEach(answer => { - const questionText = this.questions.find(question => question.id === answer.questionId).text + const questionText = this.form.questions.find(question => question.id === answer.questionId).text if (questionText in formattedSubmission) { formattedSubmission[questionText] = formattedSubmission[questionText].concat('; ').concat(answer.text) } else { From 28a063946a7811dac479ab1f830dc7ceac024923 Mon Sep 17 00:00:00 2001 From: Jonas Rittershofer Date: Wed, 13 May 2020 15:31:58 +0200 Subject: [PATCH 2/2] Show Answers in current order Signed-off-by: Jonas Rittershofer --- src/components/Results/Answer.vue | 12 ++++---- src/components/Results/Submission.vue | 43 ++++++++++++++++----------- src/views/Results.vue | 13 ++++---- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/components/Results/Answer.vue b/src/components/Results/Answer.vue index 3352ac5..8977349 100644 --- a/src/components/Results/Answer.vue +++ b/src/components/Results/Answer.vue @@ -23,9 +23,9 @@ @@ -34,12 +34,12 @@ export default { name: 'Answer', props: { - answer: { - type: Object, + answerText: { + type: String, required: true, }, - question: { - type: Object, + questionText: { + type: String, required: true, }, }, diff --git a/src/components/Results/Submission.vue b/src/components/Results/Submission.vue index 72619d1..903253e 100644 --- a/src/components/Results/Submission.vue +++ b/src/components/Results/Submission.vue @@ -35,10 +35,10 @@

+ v-for="question in answeredQuestions" + :key="question.id" + :answer-text="question.squashedAnswers" + :question-text="question.text" /> @@ -70,30 +70,37 @@ export default { }, computed: { + // Format submission-timestamp to DateTime submissionDateTime() { return moment(this.submission.timestamp, 'X').format('LLLL') }, - squashedAnswers() { - const squashedArray = [] - this.submission.answers.forEach(answer => { - const index = squashedArray.findIndex(ansSq => ansSq.questionId === answer.questionId) - if (index > -1) { - squashedArray[index].text = squashedArray[index].text.concat('; ' + answer.text) - } else { - squashedArray.push(answer) + /** + * Join answered Questions with corresponding answers. + * Multiple answers to a question are squashed into one string. + * @returns {Array} + */ + answeredQuestions() { + const answeredQuestionsArray = [] + + this.questions.forEach(question => { + const answers = this.submission.answers.filter(answer => answer.questionId === question.id) + if (!answers.length) { + return // no answers, go to next question } - }) + const squashedAnswers = answers.map(answer => answer.text).join('; ') - return squashedArray + answeredQuestionsArray.push({ + 'id': question.id, + 'text': question.text, + 'squashedAnswers': squashedAnswers, + }) + }) + return answeredQuestionsArray }, }, methods: { - questionToAnswer(questionId) { - return this.questions.find(question => question.id === questionId) - }, - onDelete() { this.$emit('delete') }, diff --git a/src/views/Results.vue b/src/views/Results.vue index f020de2..987df56 100644 --- a/src/views/Results.vue +++ b/src/views/Results.vue @@ -232,13 +232,14 @@ export default { timestamp: moment(submission.timestamp, 'X').format('L LT'), } - submission.answers.forEach(answer => { - const questionText = this.form.questions.find(question => question.id === answer.questionId).text - if (questionText in formattedSubmission) { - formattedSubmission[questionText] = formattedSubmission[questionText].concat('; ').concat(answer.text) - } else { - formattedSubmission[questionText] = answer.text + this.form.questions.forEach(question => { + const questionText = question.text + const answers = submission.answers.filter(answer => answer.questionId === question.id) + if (!answers.length) { + return // no answers, go to next question } + const squashedAnswers = answers.map(answer => answer.text).join('; ') + formattedSubmission[questionText] = squashedAnswers }) formattedSubmissions.push(formattedSubmission) })