Skip to content

Commit

Permalink
Switch to ListItem and add counter/second line
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hartmann <chris-hartmann@gmx.de>
  • Loading branch information
Chartman123 committed Jul 31, 2022
1 parent 6566670 commit 1b55934
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 15 deletions.
17 changes: 17 additions & 0 deletions lib/Db/SubmissionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ public function findParticipantsByForm(int $formId): array {
return $userIds;
}

/**
* @throws DBException
*/
public function countSubmissions(int $formId): int {
$qb = $this->db->getQueryBuilder();

$qb->select($qb->func()->count('*', 'num_submissions'))
->from($this->getTableName())
->where($qb->expr()->eq('form_id', $qb->createNamedParameter($formId, IQueryBuilder::PARAM_INT)));

$result = $qb->executeQuery();
$row = $result->fetch();
$result->closeCursor();

return (int) ($row['num_submissions' ?? 0]);
}

/**
* Delete the Submission, including answers.
* @param int $id of the submission to delete
Expand Down
14 changes: 13 additions & 1 deletion lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ public function getForm(int $id): array {
// Append canSubmit, to be able to show proper EmptyContent on internal view.
$result['canSubmit'] = $this->canSubmit($form->getId());

// Append submissionCount if currentUser is owner
if ($this->currentUser && $form->getOwnerId() === $this->currentUser->getUID()) {
$result['submissionCount'] = $this->submissionMapper->countSubmissions($id);
}

return $result;
}

Expand All @@ -199,14 +204,21 @@ public function getForm(int $id): array {
public function getPartialFormArray(int $id): array {
$form = $this->formMapper->findById($id);

return [
$result = [
'id' => $form->getId(),
'hash' => $form->getHash(),
'title' => $form->getTitle(),
'expires' => $form->getExpires(),
'permissions' => $this->getPermissions($form->getId()),
'partial' => true
];

// Append submissionCount if currentUser is owner
if ($this->currentUser && $form->getOwnerId() === $this->currentUser->getUID()) {
$result['submissionCount'] = $this->submissionMapper->countSubmissions($id);
}

return $result;
}

/**
Expand Down
56 changes: 50 additions & 6 deletions src/components/AppNavigationForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,22 @@
-->

<template>
<AppNavigationItem ref="navigationItem"
:icon="icon"
<ListItem ref="navigationItem"
:title="formTitle"
:to="{
name: routerTarget,
params: { hash: form.hash }
}"
:counter-number="form.submissionCount"
:active="isActive"
:compact="true"
@click="mobileCloseNavigation">
<template #icon>
<div :class="icon" />
</template>
<template v-if="hasSubtitle" #subtitle>
{{ formSubtitle }}
</template>
<template v-if="!loading && !readOnly" #actions>
<ActionButton :close-after-click="true" icon="icon-share" @click="onShareForm">
{{ t('forms', 'Share form') }}
Expand All @@ -48,7 +56,7 @@
{{ t('forms', 'Delete form') }}
</ActionButton>
</template>
</AppNavigationItem>
</ListItem>
</template>

<script>
Expand All @@ -57,7 +65,7 @@ import { showError } from '@nextcloud/dialogs'
import ActionButton from '@nextcloud/vue/dist/Components/ActionButton'
import ActionRouter from '@nextcloud/vue/dist/Components/ActionRouter'
import ActionSeparator from '@nextcloud/vue/dist/Components/ActionSeparator'
import AppNavigationItem from '@nextcloud/vue/dist/Components/AppNavigationItem'
import ListItem from '@nextcloud/vue/dist/Components/ListItem'
import axios from '@nextcloud/axios'
import moment from '@nextcloud/moment'
Expand All @@ -67,7 +75,7 @@ export default {
name: 'AppNavigationForm',
components: {
AppNavigationItem,
ListItem,
ActionButton,
ActionRouter,
ActionSeparator,
Expand Down Expand Up @@ -101,6 +109,16 @@ export default {
return 'icon-forms'
},
/**
* Check if form is current form and set active
*/
isActive() {
return this.form.hash === this.$route.params.hash
},
/**
* Check if form is expired
*/
isExpired() {
return this.form.expires && moment().unix() > this.form.expires
},
Expand All @@ -117,6 +135,27 @@ export default {
return t('forms', 'New form')
},
/**
* Return expiration details for subtitle
*/
formSubtitle() {
if (this.form.expires) {
const relativeDate = moment(this.form.expires, 'X').fromNow()
if (this.isExpired) {
return t('forms', 'Expired {relativeDate}', { relativeDate })
}
return t('forms', 'Expires {relativeDate}', { relativeDate })
}
return ''
},
/**
* Return, if form has Subtitle
*/
hasSubtitle() {
return this.formSubtitle !== ''
},
/**
* Route to use, depending on readOnly
*
Expand Down Expand Up @@ -164,6 +203,11 @@ export default {
}
},
},
}
</script>

<style lang="scss" scoped>
.icon-forms {
background-size: 16px;
}
</style>
30 changes: 22 additions & 8 deletions tests/Unit/Service/FormsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function dataGetForm() {
'hash' => 'abcdefg',
'title' => 'Form 1',
'description' => 'Description Text',
'ownerId' => 'someUser',
'ownerId' => 'currentUser',
'created' => 123456789,
'access' => [
'permitAllUsers' => false,
Expand All @@ -140,6 +140,7 @@ public function dataGetForm() {
'isAnonymous' => false,
'submitMultiple' => true,
'canSubmit' => true,
'submissionCount' => 123,
'questions' => [
[
'id' => 1,
Expand Down Expand Up @@ -178,11 +179,13 @@ public function dataGetForm() {
'id' => 1,
'formId' => 42,
'shareType' => 0,
'shareWith' => 'currentUser',
'displayName' => 'Current User'
'shareWith' => 'someUser',
'displayName' => 'Some User'
]
],
'permissions' => [
'edit',
'results',
'submit'
]
]]
Expand All @@ -201,7 +204,7 @@ public function testGetForm(array $expected) {
$form->setHash('abcdefg');
$form->setTitle('Form 1');
$form->setDescription('Description Text');
$form->setOwnerId('someUser');
$form->setOwnerId('currentUser');
$form->setCreated(123456789);
$form->setAccess([
'permitAllUsers' => false,
Expand All @@ -220,10 +223,10 @@ public function testGetForm(array $expected) {
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getDisplayName')
->willReturn('Current User');
->willReturn('Some User');
$this->userManager->expects($this->once())
->method('get')
->with('currentUser')
->with('someUser')
->willReturn($user);

// Questions
Expand Down Expand Up @@ -266,13 +269,18 @@ public function testGetForm(array $expected) {
$share->setId(1);
$share->setFormId(42);
$share->setShareType(0);
$share->setShareWith('currentUser');
$share->setShareWith('someUser');

$this->shareMapper->expects($this->exactly(3))
$this->shareMapper->expects($this->any())
->method('findByForm')
->with(42)
->willReturn([$share]);

$this->submissionMapper->expects($this->once())
->method('countSubmissions')
->with(42)
->willReturn(123);

// Run the test
$this->assertEquals($expected, $this->formsService->getForm(42));
}
Expand All @@ -285,6 +293,7 @@ public function dataGetPartialForm() {
'title' => 'Form 1',
'expires' => 0,
'permissions' => ['edit', 'results', 'submit'],
'submissionCount' => 123,
'partial' => true
]]
];
Expand All @@ -307,6 +316,11 @@ public function testGetPartialForm(array $expected) {
->with(42)
->willReturn($form);

$this->submissionMapper->expects($this->once())
->method('countSubmissions')
->with(42)
->willReturn(123);

// Run the test
$this->assertEquals($expected, $this->formsService->getPartialFormArray(42));
}
Expand Down

0 comments on commit 1b55934

Please sign in to comment.