From f7bbcb2c08e4f680c771992c24390a2fbd624f41 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Fri, 3 Jan 2025 11:29:27 +0100 Subject: [PATCH] hmmm... Signed-off-by: dartcafe --- lib/Db/Vote.php | 34 -------------- src/components/Export/ExportPoll.vue | 70 ++++++++++++++++++---------- src/stores/votes.ts | 2 - tests/Unit/Db/VoteMapperTest.php | 4 +- 4 files changed, 48 insertions(+), 62 deletions(-) diff --git a/lib/Db/Vote.php b/lib/Db/Vote.php index 70629b02e..5ce56387c 100644 --- a/lib/Db/Vote.php +++ b/lib/Db/Vote.php @@ -9,11 +9,6 @@ namespace OCA\Polls\Db; use JsonSerializable; -use OCA\Polls\AppConstants; -use OCA\Polls\Helper\Container; -use OCA\Polls\UserSession; -use OCP\IL10N; -use OCP\L10N\IFactory; /** * @psalm-suppress UnusedProperty @@ -43,10 +38,6 @@ class Vote extends EntityWithUser implements JsonSerializable { public const VOTE_NO = 'no'; public const VOTE_EVENTUALLY = 'maybe'; - protected IL10N $l10n; - protected UserSession $userSession; - protected IFactory $transFactory; - // schema columns public $id = null; protected int $pollId = 0; @@ -62,18 +53,6 @@ class Vote extends EntityWithUser implements JsonSerializable { public function __construct( ) { - $this->userSession = Container::queryClass(UserSession::class); - $this->transFactory = Container::queryClass(IFactory::class); - $this->userSession->getUser()->getLocaleCode(); - - $languageCode = $this->userSession->getUser()->getLanguageCode() !== '' ? $this->userSession->getUser()->getLanguageCode() : $this->transFactory->findGenericLanguage(); - - $this->l10n = $this->transFactory->get( - AppConstants::APP_ID, - $languageCode, - $this->userSession->getUser()->getLocaleCode() - ); - $this->addType('id', 'integer'); $this->addType('pollId', 'integer'); $this->addType('voteOptionId', 'integer'); @@ -93,18 +72,6 @@ private function getAnswerSymbol(): string { } } - private function getAnswerTranslated(): string { - switch ($this->getVoteAnswer()) { - case self::VOTE_YES: - return $this->l10n->t('Yes'); - case self::VOTE_NO: - return $this->l10n->t('No'); - case self::VOTE_EVENTUALLY: - return $this->l10n->t('Maybe'); - default: - return ''; - } - } /** * @return array * @@ -120,7 +87,6 @@ public function jsonSerialize(): array { 'optionId' => $this->getOptionId(), 'user' => $this->getUser(), 'answerSymbol' => $this->getAnswerSymbol(), - 'answerTranslated' => $this->getAnswerTranslated(), ]; } } diff --git a/src/components/Export/ExportPoll.vue b/src/components/Export/ExportPoll.vue index cb54acff7..8061385ec 100644 --- a/src/components/Export/ExportPoll.vue +++ b/src/components/Export/ExportPoll.vue @@ -23,9 +23,21 @@ import { PollsAPI } from '../../Api/index.js' import { usePollStore, PollType } from '../../stores/poll.ts' - import { useVotesStore } from '../../stores/votes.ts' + import { Answer, AnswerSymbol, useVotesStore } from '../../stores/votes.ts' import { useOptionsStore } from '../../stores/options.ts' + enum ArrayStyle { + Symbols = 'symbols', + Raw = 'raw', + Generic = 'generic', + } + + enum ExportFormat { + Html = 'html', + Xlsx = 'xlsx', + Ods = 'ods', + Csv = 'csv', + } const route = useRoute() const pollStore = usePollStore() @@ -50,11 +62,21 @@ return buf } + function getAnswerTranslated(answer: Answer) { + switch (answer) { + case Answer.Yes: + return t('polls', 'Yes') + case Answer.Maybe: + return t('polls', 'Maybe') + default: + return t('polls', 'No') + } + } /** * - * @param exportType - export type + * @param exportFormat - export type */ - async function exportFile(exportType) { + async function exportFile(exportFormat: ExportFormat) { const participantsHeader = [t('polls', 'Participants')] const fromHeader = [t('polls', 'From')] const toHeader = [t('polls', 'To')] @@ -62,7 +84,7 @@ workBook.value.SheetNames.push(sheetName.value) sheetData.value = [] - if (['html', 'xlsx', 'ods'].includes(exportType)) { + if ([ExportFormat.Html, ExportFormat.Xlsx, ExportFormat.Ods].includes(exportFormat)) { sheetData.value.push( [DOMPurify.sanitize(pollStore.configuration.title)], [DOMPurify.sanitize(pollStore.configuration.description)], @@ -82,7 +104,7 @@ } if (pollStore.type === PollType.Text) { - if (['html'].includes(exportType)) { + if ([ExportFormat.Html].includes(exportFormat)) { sheetData.value.push([ ...participantsHeader, ...optionsStore.list.map((item) => DOMPurify.sanitize(item.text)), @@ -94,13 +116,13 @@ ]) } - } else if (['csv'].includes(exportType)) { + } else if ([ExportFormat.Csv].includes(exportFormat)) { sheetData.value.push([ ...participantsHeader, ...optionsStore.list.map((option) => optionsStore.explodeDates(option).iso), ]) - } else if (['html'].includes(exportType)) { + } else if ([ExportFormat.Html].includes(exportFormat)) { sheetData.value.push([ ...participantsHeader, ...optionsStore.list.map((option) => optionsStore.explodeDates(option).raw), @@ -117,16 +139,16 @@ ]) } - if (['html', 'ods', 'xlsx'].includes(exportType)) { - addVotesArray('symbols') - } else if (['csv'].includes(exportType)) { - addVotesArray('raw') + if ([ExportFormat.Html, ExportFormat.Ods, ExportFormat.Xlsx].includes(exportFormat)) { + addVotesArray(ArrayStyle.Symbols) + } else if ([ExportFormat.Csv].includes(exportFormat)) { + addVotesArray(ArrayStyle.Raw) } else { - addVotesArray('generic') + addVotesArray(ArrayStyle.Generic) } try { - const workBookOutput = xlsxWrite(workBook.value, { bookType: exportType, type: 'binary' }) - saveAs(new Blob([s2ab(workBookOutput)], { type: 'application/octet-stream' }), `pollStore.${exportType}`) + const workBookOutput = xlsxWrite(workBook.value, { bookType: exportFormat, type: 'binary' }) + saveAs(new Blob([s2ab(workBookOutput)], { type: 'application/octet-stream' }), `pollStore.${exportFormat}`) } catch (error) { console.error(error) showError(t('polls', 'Error exporting file.')) @@ -137,7 +159,7 @@ * * @param style - style */ - function addVotesArray(style: 'symbols' | 'raw' | 'generic') { + function addVotesArray(style: ArrayStyle) { pollStore.participants.forEach((participant) => { const votesLine = [participant.displayName] try { @@ -146,18 +168,18 @@ } optionsStore.list.forEach((option) => { - if (style === 'symbols') { - votesLine.push(votesStore.getVote({ user: participant, option }).answerSymbol ?? '❌') - } else if (style === 'raw') { + if (style === ArrayStyle.Symbols) { + votesLine.push(votesStore.getVote({ user: participant, option }).answerSymbol ?? AnswerSymbol.No) + } else if (style === ArrayStyle.Raw) { votesLine.push(votesStore.getVote({ user: participant, option }).answer) } else { - votesLine.push(votesStore.getVote({ user: participant, option }).answerTranslated ?? t('polls', 'No')) + votesLine.push(getAnswerTranslated(votesStore.getVote({ user: participant, option }).answer)) } }) sheetData.value.push(votesLine) } catch (error) { - // just skip this participant + // just skip this participant } }) @@ -174,7 +196,7 @@ + @click="exportFile(ExportFormat.Xlsx)"> @@ -183,7 +205,7 @@ + @click="exportFile(ExportFormat.Ods)"> @@ -192,7 +214,7 @@ + @click="exportFile(ExportFormat.Csv)"> @@ -201,7 +223,7 @@ + @click="exportFile(ExportFormat.Html)"> diff --git a/src/stores/votes.ts b/src/stores/votes.ts index 92547aaad..626e163ac 100644 --- a/src/stores/votes.ts +++ b/src/stores/votes.ts @@ -30,7 +30,6 @@ export type Vote = { optionText: string answer: Answer answerSymbol: AnswerSymbol - answerTranslated: string deleted: number optionId: number user: User @@ -63,7 +62,6 @@ export const useVotesStore = defineStore('votes', { optionText: payload.option.text, user: payload.user, answerSymbol: AnswerSymbol.None, - answerTranslated: '', deleted: 0, id: 0, optionId: payload.option.id, diff --git a/tests/Unit/Db/VoteMapperTest.php b/tests/Unit/Db/VoteMapperTest.php index bc3410c63..501d79c74 100644 --- a/tests/Unit/Db/VoteMapperTest.php +++ b/tests/Unit/Db/VoteMapperTest.php @@ -115,9 +115,9 @@ public function testUpdate() { /** * testDeleteByPollAndUser */ - public function testDeleteByPollAndUserId() { + public function testDeleteByPollAndUserId(): void { foreach ($this->polls as $poll) { - $this->assertNull($this->voteMapper->deleteByPollAndUserId($poll->getId(), 'voter')); + $this->voteMapper->deleteByPollAndUserId($poll->getId(), 'voter'); } }