Merge pull request #386 from nextcloud/fix/result_order

This commit is contained in:
John Molakvoæ 2020-06-09 10:31:46 +02:00 committed by GitHub
commit af898d3ea4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 53 deletions

View file

@ -648,16 +648,8 @@ class ApiController extends Controller {
$submissions[] = $submission; $submissions[] = $submission;
} }
// Load question-texts, including deleted ones. // Load currently active questions
try { $questions = $this->formsService->getQuestions($form->getId());
$questionEntities = $this->questionMapper->findByForm($form->getId());
} catch (DoesNotExistException $e) {
//handle silently
}
$questions = [];
foreach ($questionEntities as $questionEntity) {
$questions[] = $questionEntity->read();
}
$response = [ $response = [
'submissions' => $submissions, 'submissions' => $submissions,

View file

@ -85,7 +85,12 @@ class FormsService {
$this->currentUser = $userSession->getUser(); $this->currentUser = $userSession->getUser();
} }
/**
* Load options corresponding to question
*
* @param integer $questionId
* @return array
*/
public function getOptions(int $questionId): array { public function getOptions(int $questionId): array {
$optionList = []; $optionList = [];
try { try {
@ -100,6 +105,12 @@ class FormsService {
} }
} }
/**
* Load questions corresponding to form
*
* @param integer $formId
* @return array
*/
public function getQuestions(int $formId): array { public function getQuestions(int $formId): array {
$questionList = []; $questionList = [];
try { try {

View file

@ -23,9 +23,9 @@
<template> <template>
<div class="answer"> <div class="answer">
<h4 class="question-text"> <h4 class="question-text">
{{ question.text }} {{ questionText }}
</h4> </h4>
<p>{{ answer.text }}</p> <p>{{ answerText }}</p>
</div> </div>
</template> </template>
@ -34,12 +34,12 @@ export default {
name: 'Answer', name: 'Answer',
props: { props: {
answer: { answerText: {
type: Object, type: String,
required: true, required: true,
}, },
question: { questionText: {
type: Object, type: String,
required: true, required: true,
}, },
}, },

View file

@ -35,10 +35,10 @@
</p> </p>
<Answer <Answer
v-for="answer in squashedAnswers" v-for="question in answeredQuestions"
:key="answer.questionId" :key="question.id"
:answer="answer" :answer-text="question.squashedAnswers"
:question="questionToAnswer(answer.questionId)" /> :question-text="question.text" />
</div> </div>
</template> </template>
@ -70,30 +70,37 @@ export default {
}, },
computed: { computed: {
// Format submission-timestamp to DateTime
submissionDateTime() { submissionDateTime() {
return moment(this.submission.timestamp, 'X').format('LLLL') 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) * Join answered Questions with corresponding answers.
if (index > -1) { * Multiple answers to a question are squashed into one string.
squashedArray[index].text = squashedArray[index].text.concat('; ' + answer.text) * @returns {Array}
} else { */
squashedArray.push(answer) 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: { methods: {
questionToAnswer(questionId) {
return this.questions.find(question => question.id === questionId)
},
onDelete() { onDelete() {
this.$emit('delete') this.$emit('delete')
}, },

View file

@ -71,10 +71,10 @@
<section v-else> <section v-else>
<Submission <Submission
v-for="submission in submissions" v-for="submission in form.submissions"
:key="submission.id" :key="submission.id"
:submission="submission" :submission="submission"
:questions="questions" :questions="form.questions"
@delete="deleteSubmission(submission.id)" /> @delete="deleteSubmission(submission.id)" />
</section> </section>
</AppContent> </AppContent>
@ -117,14 +117,12 @@ export default {
data() { data() {
return { return {
loadingResults: true, loadingResults: true,
submissions: [],
questions: [],
} }
}, },
computed: { computed: {
noSubmissions() { noSubmissions() {
return this.submissions && this.submissions.length === 0 return this.form.submissions?.length === 0
}, },
/** /**
@ -180,9 +178,8 @@ export default {
const response = await axios.get(generateUrl('/apps/forms/api/v1/submissions/{hash}', { const response = await axios.get(generateUrl('/apps/forms/api/v1/submissions/{hash}', {
hash: this.form.hash, hash: this.form.hash,
})) }))
this.submissions = response.data.submissions this.form.submissions = response.data.submissions
this.questions = response.data.questions this.form.questions = response.data.questions
console.debug(this.submissions)
} catch (error) { } catch (error) {
console.error(error) console.error(error)
showError(t('forms', 'There was an error while loading results')) showError(t('forms', 'There was an error while loading results'))
@ -196,8 +193,8 @@ export default {
try { try {
await axios.delete(generateUrl('/apps/forms/api/v1/submission/{id}', { id })) await axios.delete(generateUrl('/apps/forms/api/v1/submission/{id}', { id }))
const index = this.submissions.findIndex(search => search.id === id) const index = this.form.submissions.findIndex(search => search.id === id)
this.submissions.splice(index, 1) this.form.submissions.splice(index, 1)
} catch (error) { } catch (error) {
console.error(error) console.error(error)
showError(t('forms', 'There was an error while removing this response')) showError(t('forms', 'There was an error while removing this response'))
@ -214,7 +211,7 @@ export default {
this.loadingResults = true this.loadingResults = true
try { try {
await axios.delete(generateUrl('/apps/forms/api/v1/submissions/{formId}', { formId: this.form.id })) await axios.delete(generateUrl('/apps/forms/api/v1/submissions/{formId}', { formId: this.form.id }))
this.submissions = [] this.form.submissions = []
} catch (error) { } catch (error) {
console.error(error) console.error(error)
showError(t('forms', 'There was an error while removing responses')) showError(t('forms', 'There was an error while removing responses'))
@ -231,19 +228,20 @@ export default {
}) })
const formattedSubmissions = [] const formattedSubmissions = []
this.submissions.forEach(submission => { this.form.submissions.forEach(submission => {
const formattedSubmission = { const formattedSubmission = {
userDisplayName: submission.userDisplayName, userDisplayName: submission.userDisplayName,
timestamp: moment(submission.timestamp, 'X').format('L LT'), timestamp: moment(submission.timestamp, 'X').format('L LT'),
} }
submission.answers.forEach(answer => { this.form.questions.forEach(question => {
const questionText = this.questions.find(question => question.id === answer.questionId).text const questionText = question.text
if (questionText in formattedSubmission) { const answers = submission.answers.filter(answer => answer.questionId === question.id)
formattedSubmission[questionText] = formattedSubmission[questionText].concat('; ').concat(answer.text) if (!answers.length) {
} else { return // no answers, go to next question
formattedSubmission[questionText] = answer.text
} }
const squashedAnswers = answers.map(answer => answer.text).join('; ')
formattedSubmission[questionText] = squashedAnswers
}) })
formattedSubmissions.push(formattedSubmission) formattedSubmissions.push(formattedSubmission)
}) })