Skip to content

Commit

Permalink
hmmm...
Browse files Browse the repository at this point in the history
Signed-off-by: dartcafe <github@dartcafe.de>
  • Loading branch information
dartcafe committed Jan 3, 2025
1 parent c5d8ac7 commit f7bbcb2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 62 deletions.
34 changes: 0 additions & 34 deletions lib/Db/Vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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');
Expand All @@ -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
*
Expand All @@ -120,7 +87,6 @@ public function jsonSerialize(): array {
'optionId' => $this->getOptionId(),
'user' => $this->getUser(),
'answerSymbol' => $this->getAnswerSymbol(),
'answerTranslated' => $this->getAnswerTranslated(),
];
}
}
70 changes: 46 additions & 24 deletions src/components/Export/ExportPoll.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -50,19 +62,29 @@
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')]
workBook.value = xlsxUtils.book_new()
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)],
Expand All @@ -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)),
Expand All @@ -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),
Expand All @@ -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.'))
Expand All @@ -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 {
Expand All @@ -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
}
})

Expand All @@ -174,7 +196,7 @@
<NcActionButton close-after-click
:name="t('polls', 'Download Excel spreadsheet')"
:aria-label="t('polls', 'Download Excel spreadsheet')"
@click="exportFile('xlsx')">
@click="exportFile(ExportFormat.Xlsx)">
<template #icon>
<ExcelIcon />
</template>
Expand All @@ -183,7 +205,7 @@
<NcActionButton close-after-click
:name="t('polls', 'Download Open Document spreadsheet')"
:aria-label="t('polls', 'Download Open Document spreadsheet')"
@click="exportFile('ods')">
@click="exportFile(ExportFormat.Ods)">
<template #icon>
<FileTableIcon />
</template>
Expand All @@ -192,7 +214,7 @@
<NcActionButton close-after-click
:name="t('polls', 'Download CSV file')"
::aria-label="t('polls', 'Download CSV file')"
@click="exportFile('csv')">
@click="exportFile(ExportFormat.Csv)">
<template #icon>
<CsvIcon />
</template>
Expand All @@ -201,7 +223,7 @@
<NcActionButton close-after-click
:name="t('polls', 'Download HTML file')"
:aria-label="t('polls', 'Download HTML file')"
@click="exportFile('html')">
@click="exportFile(ExportFormat.Html)">
<template #icon>
<XmlIcon />
</template>
Expand Down
2 changes: 0 additions & 2 deletions src/stores/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export type Vote = {
optionText: string
answer: Answer
answerSymbol: AnswerSymbol
answerTranslated: string
deleted: number
optionId: number
user: User
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Db/VoteMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand Down

0 comments on commit f7bbcb2

Please sign in to comment.