diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 6e8c494..16d1fe7 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -553,6 +553,25 @@ class ApiController extends Controller { return new Http\JSONResponse($id); } + /** + * @NoAdminRequired + */ + private function getAnswers(int $submissionId): array { + try { + $answerEntities = $this->answerMapper->findBySubmission($submissionId); + } catch (DoesNotExistException $e) { + //Just ignore, if no Data. Returns empty Answers-Array + } + + // Load Answer-Data + $answers = []; + foreach ($answerEntities as $answerEntity) { + $answers[] = $answerEntity->read(); + } + + return $answers; + } + /** * @NoAdminRequired */ @@ -569,24 +588,39 @@ class ApiController extends Controller { return new Http\JSONResponse([], Http::STATUS_FORBIDDEN); } - $result = []; - $submissionList = $this->submissionMapper->findByForm($form->getId()); - foreach ($submissionList as $submissionEntity) { - $answerList = $this->answerMapper->findBySubmission($submissionEntity->id); - foreach ($answerList as $answerEntity) { - $answer = $answerEntity->read(); - //Temporary Adapt Data to be usable by old Results-View - $answer['userId'] = $submissionEntity->getUserId(); - - $question = $this->questionMapper->findById($answer['questionId']); - $answer['questionText'] = $question->getText(); - $answer['questionType'] = $question->getType(); - - $result[] = $answer; - } + try { + $submissionEntities = $this->submissionMapper->findByForm($form->getId()); + } catch (DoesNotExistException $e) { + //Just ignore, if no Data. Returns empty Submissions-Array } - return new Http\JSONResponse($result); + $submissions = []; + foreach ($submissionEntities as $submissionEntity) { + // Load Submission-Data & corresponding Answers + $submission = $submissionEntity->read(); + $submission['answers'] = $this->getAnswers($submission['id']); + + // Add to returned List of Submissions + $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(); + } + + $response = [ + 'submissions' => $submissions, + 'questions' => $questions, + ]; + + return new Http\JSONResponse($response); } /** diff --git a/lib/Db/SubmissionMapper.php b/lib/Db/SubmissionMapper.php index a8131b4..729ce42 100644 --- a/lib/Db/SubmissionMapper.php +++ b/lib/Db/SubmissionMapper.php @@ -57,7 +57,9 @@ class SubmissionMapper extends QBMapper { ->from($this->getTableName()) ->where( $qb->expr()->eq('form_id', $qb->createNamedParameter($formId, IQueryBuilder::PARAM_INT)) - ); + ) + //Newest submissions first + ->orderBy('timestamp', 'DESC'); return $this->findEntities($qb); } diff --git a/src/components/Results/Answer.vue b/src/components/Results/Answer.vue new file mode 100644 index 0000000..3aa5820 --- /dev/null +++ b/src/components/Results/Answer.vue @@ -0,0 +1,62 @@ + + + + + + + diff --git a/src/components/Results/Submission.vue b/src/components/Results/Submission.vue new file mode 100644 index 0000000..aa6141d --- /dev/null +++ b/src/components/Results/Submission.vue @@ -0,0 +1,123 @@ + + + + + + + diff --git a/src/views/Results.vue b/src/views/Results.vue index e40f52f..95485e0 100644 --- a/src/views/Results.vue +++ b/src/views/Results.vue @@ -39,9 +39,9 @@

{{ t('forms', 'Responses for {title}', { title: form.title }) }}

-
+
@@ -56,11 +56,13 @@
- - + +
@@ -81,10 +83,11 @@ import { generateUrl } from '@nextcloud/router' import { showError } from '@nextcloud/dialogs' import AppContent from '@nextcloud/vue/dist/Components/AppContent' import axios from '@nextcloud/axios' -import json2csvParser from 'json2csv' +// import json2csvParser from 'json2csv' import EmptyContent from '../components/EmptyContent' -import ResultItem from '../components/resultItem' +// import ResultItem from '../components/resultItem' +import Submission from '../components/Results/Submission' import TopBar from '../components/TopBar' import ViewsMixin from '../mixins/ViewsMixin' @@ -94,7 +97,7 @@ export default { components: { AppContent, EmptyContent, - ResultItem, + Submission, TopBar, }, @@ -103,45 +106,14 @@ export default { data() { return { loadingResults: true, - answers: [], + submissions: [], + questions: [], } }, computed: { - stats() { - const sums = [] - - if (this.answers != null) { - const uniqueAns = [] - const uniqueQs = [] - const ansToQ = new Map() - for (let i = 0; i < this.answers.length; i++) { - if (this.answers[i].questionType === 'radiogroup' || this.answers[i].questionType === 'dropdown') { - if (uniqueAns.includes(this.answers[i].text) === false) { - uniqueAns.push(this.answers[i].text) - ansToQ.set(this.answers[i].text, this.answers[i].questionId) - } - if (uniqueQs.includes(this.answers[i].questionId) === false) { - uniqueQs.push(this.answers[i].questionId) - } - } - } - for (let i = 0; i < uniqueAns.length; i++) { - sums[i] = 0 - } - for (let i = 0; i < this.answers.length; i++) { - sums[uniqueAns.indexOf(this.answers[i].text)]++ - } - for (let i = 0; i < sums.length; i++) { - sums[i] = 'Question ' + ansToQ.get(uniqueAns[i]) + ': ' + (sums[i] / ((this.answers.length / uniqueQs.length)) * 100).toFixed(2) + '%' + ' of respondents voted for answer choice: ' + uniqueAns[i] - } - } - - return sums.sort() - }, - noSubmissions() { - return this.answers && this.answers.length === 0 + return this.submissions && this.submissions.length === 0 }, }, @@ -150,7 +122,6 @@ export default { }, methods: { - showEdit() { this.$router.push({ name: 'edit', @@ -168,8 +139,9 @@ export default { const response = await axios.get(generateUrl('/apps/forms/api/v1/submissions/{hash}', { hash: this.form.hash, })) - this.answers = response.data - console.debug(this.answers) + this.submissions = response.data.submissions + this.questions = response.data.questions + console.debug(this.submissions) } catch (error) { console.error(error) showError(t('forms', 'There was an error while loading results')) @@ -178,9 +150,9 @@ export default { } }, - download() { +/* download() { this.loading = true - axios.get(generateUrl('apps/forms/get/form/' + this.$route.params.hash)) + axios.get(OC.generateUrl('apps/forms/get/form/' + this.$route.params.hash)) .then((response) => { this.json2csvParser = ['userId', 'questionId', 'questionText', 'Answer'] // TODO Is this one necessary?? const formattedAns = [] @@ -202,26 +174,11 @@ export default { document.body.removeChild(element) this.loading = false }, (error) => { - /* eslint-disable-next-line no-console */ + /* eslint-disable-next-line no-console * console.log(error.response) this.loading = false }) - }, + }, */ }, } - -