Skip to content

Commit

Permalink
Add lastUpdated property to Form
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hartmann <[email protected]>
  • Loading branch information
Chartman123 committed Feb 2, 2023
1 parent 742c347 commit 95b339f
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 7 deletions.
28 changes: 26 additions & 2 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public function newForm(): DataResponse {
$form->setShowExpiration(false);
$form->setExpires(0);
$form->setIsAnonymous(false);
$form->setLastUpdated(time());

$this->formMapper->insert($form);

Expand Down Expand Up @@ -315,6 +316,7 @@ public function cloneForm(int $id): DataResponse {
$formData = $oldForm->read();
unset($formData['id']);
$formData['created'] = time();
$formData['lastUpdated'] = time();
$formData['hash'] = $this->formsService->generateFormHash();
// TRANSLATORS Appendix to the form Title of a duplicated/copied form.
$formData['title'] .= ' - ' . $this->l10n->t('Copy');
Expand Down Expand Up @@ -384,9 +386,10 @@ public function updateForm(int $id, array $keyValuePairs): DataResponse {
throw new OCSForbiddenException();
}

// Don't allow to change params id, hash, ownerId, created
// Don't allow to change params id, hash, ownerId, created, lastUpdated
if (key_exists('id', $keyValuePairs) || key_exists('hash', $keyValuePairs) ||
key_exists('ownerId', $keyValuePairs) || key_exists('created', $keyValuePairs)) {
key_exists('ownerId', $keyValuePairs) || key_exists('created', $keyValuePairs) ||
key_exists('lastUpdated', $keyValuePairs)) {
$this->logger->info('Not allowed to update id, hash, ownerId or created');
throw new OCSForbiddenException();
}
Expand All @@ -397,6 +400,7 @@ public function updateForm(int $id, array $keyValuePairs): DataResponse {

// Update changed Columns in Db.
$this->formMapper->update($form);
$this->formsService->setLastUpdatedTimestamp($id);

return new DataResponse($form->getId());
}
Expand Down Expand Up @@ -501,6 +505,8 @@ public function newQuestion(int $formId, string $type, string $text = ''): DataR
$response = $question->read();
$response['options'] = [];

$this->formsService->setLastUpdatedTimestamp($id);

return new DataResponse($response);
}

Expand Down Expand Up @@ -594,6 +600,8 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
];
}

$this->formsService->setLastUpdatedTimestamp($id);

return new DataResponse($response);
}

Expand Down Expand Up @@ -654,6 +662,8 @@ public function updateQuestion(int $id, array $keyValuePairs): DataResponse {
// Update changed Columns in Db.
$this->questionMapper->update($question);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($question->getId());
}

Expand Down Expand Up @@ -703,6 +713,8 @@ public function deleteQuestion(int $id): DataResponse {
}
}

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($id);
}

Expand Down Expand Up @@ -744,6 +756,8 @@ public function newOption(int $questionId, string $text): DataResponse {

$option = $this->optionMapper->insert($option);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($option->read());
}

Expand Down Expand Up @@ -798,6 +812,8 @@ public function updateOption(int $id, array $keyValuePairs): DataResponse {
// Update changed Columns in Db.
$this->optionMapper->update($option);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($option->getId());
}

Expand Down Expand Up @@ -833,6 +849,8 @@ public function deleteOption(int $id): DataResponse {

$this->optionMapper->delete($option);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($id);
}

Expand Down Expand Up @@ -1013,6 +1031,8 @@ public function insertSubmission(int $formId, array $answers, string $shareHash
}
}

$this->formsService->setLastUpdatedTimestamp($formId);

//Create Activity
$this->activityManager->publishNewSubmission($form, $submission->getUserId());

Expand Down Expand Up @@ -1051,6 +1071,8 @@ public function deleteSubmission(int $id): DataResponse {
// Delete submission (incl. Answers)
$this->submissionMapper->deleteById($id);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($id);
}

Expand Down Expand Up @@ -1085,6 +1107,8 @@ public function deleteAllSubmissions(int $formId): DataResponse {
// Delete all submissions (incl. Answers)
$this->submissionMapper->deleteByForm($formId);

$this->formsService->setLastUpdatedTimestamp($formId);

return new DataResponse($formId);
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Controller/ShareApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ public function newShare(int $formId, int $shareType, string $shareWith = '', ar

// Create share-notifications (activity)
$this->formsService->notifyNewShares($form, $share);

$this->formsService->setLastUpdatedTimestamp($formId);

// Append displayName for Frontend
$shareData = $share->read();
Expand Down Expand Up @@ -250,6 +252,8 @@ public function deleteShare(int $id): DataResponse {

$this->shareMapper->deleteById($id);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($id);
}

Expand Down Expand Up @@ -303,6 +307,8 @@ public function updateShare(int $id, array $keyValuePairs): DataResponse {
$share->setPermissions($keyValuePairs['permissions']);
$share = $this->shareMapper->update($share);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($share->getId());
}

Expand Down
7 changes: 6 additions & 1 deletion lib/Db/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
* @method void setSubmitMultiple(bool $value)
* @method integer getShowExpiration()
* @method void setShowExpiration(bool $value)
* @method integer getLastUpdated()
* @method void setLastUpdated(integer $value)
*/
class Form extends Entity {
protected $hash;
Expand All @@ -62,6 +64,7 @@ class Form extends Entity {
protected $isAnonymous;
protected $submitMultiple;
protected $showExpiration;
protected $lastUpdated;

/**
* Form constructor.
Expand All @@ -72,6 +75,7 @@ public function __construct() {
$this->addType('isAnonymous', 'bool');
$this->addType('submitMultiple', 'bool');
$this->addType('showExpiration', 'bool');
$this->addType('lastUpdated', 'integer');
}

// JSON-Decoding of access-column.
Expand All @@ -97,7 +101,8 @@ public function read() {
'expires' => (int)$this->getExpires(),
'isAnonymous' => (bool)$this->getIsAnonymous(),
'submitMultiple' => (bool)$this->getSubmitMultiple(),
'showExpiration' => (bool)$this->getShowExpiration()
'showExpiration' => (bool)$this->getShowExpiration(),
'lastUpdated' => (int)$this->getLastUpdated()
];
}
}
10 changes: 6 additions & 4 deletions lib/Db/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ public function findAll(): array {

$qb->select('*')
->from($this->getTableName())
//Newest forms first
->orderBy('created', 'DESC');
//Last updated forms first, then newest forms first
->addOrderBy('last_updated', 'DESC')
->addOrderBy('created', 'DESC');

return $this->findEntities($qb);
}
Expand All @@ -116,8 +117,9 @@ public function findAllByOwnerId(string $ownerId): array {
->where(
$qb->expr()->eq('owner_id', $qb->createNamedParameter($ownerId))
)
//Newest forms first
->orderBy('created', 'DESC');
//Last updated forms first, then newest forms first
->addOrderBy('last_updated', 'DESC')
->addOrderBy('created', 'DESC');

return $this->findEntities($qb);
}
Expand Down
63 changes: 63 additions & 0 deletions lib/Migration/Version030100Date20230202175747.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Your name <[email protected]>
*
* @author Your name <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Forms\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version030100Date20230202175747 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$table = $schema->getTable('forms_v2_forms');

if (!$table->hasColumn('last_updated')) {
$table->addColumn('last_updated', Types::INTEGER, [
'notnull' => false,
'default' => 0,
'comment' => 'unix-timestamp',
]);

return $schema;
}

return null;
}
}
12 changes: 12 additions & 0 deletions lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public function getPartialFormArray(int $id): array {
'hash' => $form->getHash(),
'title' => $form->getTitle(),
'expires' => $form->getExpires(),
'lastUpdated' => $form->getLastUpdated(),
'permissions' => $this->getPermissions($form->getId()),
'partial' => true
];
Expand Down Expand Up @@ -523,4 +524,15 @@ protected function getSharesWithUser(int $formId, string $userId): array {
}
});
}

/**
* Update lastUpdated timestamp for the given form
*
* @param int $formId The form to update
*/
public function setLastUpdatedTimestamp(int $formId): void {
$form = $this->formMapper->findById($formId);
$form->setLastUpdated(time());
$this->formMapper->update($form);
}
}
8 changes: 8 additions & 0 deletions tests/Unit/Service/FormsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public function dataGetForm() {
'isAnonymous' => false,
'submitMultiple' => true,
'showExpiration' => false,
'lastUpdated' => 123456789,
'canSubmit' => true,
'submissionCount' => 123,
'questions' => [
Expand Down Expand Up @@ -231,6 +232,7 @@ public function testGetForm(array $expected) {
$form->setIsAnonymous(false);
$form->setSubmitMultiple(true);
$form->setShowExpiration(false);
$form->setLastUpdated(123456789);

$this->formMapper->expects($this->any())
->method('findById')
Expand Down Expand Up @@ -315,6 +317,7 @@ public function dataGetPartialForm() {
'hash' => 'abcdefg',
'title' => 'Form 1',
'expires' => 0,
'lastUpdated' => 123456789,
'permissions' => Constants::PERMISSION_ALL,
'submissionCount' => 123,
'partial' => true
Expand All @@ -333,6 +336,7 @@ public function testGetPartialForm(array $expected) {
$form->setTitle('Form 1');
$form->setOwnerId('currentUser');
$form->setExpires(0);
$form->setLastUpdated(123456789);

$this->formMapper->expects($this->exactly(2))
->method('findById')
Expand All @@ -355,6 +359,7 @@ public function dataGetPartialFormShared() {
'hash' => 'abcdefg',
'title' => 'Form 1',
'expires' => 0,
'lastUpdated' => 123456789,
'permissions' => ['results', 'submit'],
'submissionCount' => 123,
'partial' => true
Expand All @@ -374,6 +379,7 @@ public function testGetPartialFormShared(array $expected) {
$form->setTitle('Form 1');
$form->setOwnerId('otherUser');
$form->setExpires(0);
$form->setLastUpdated(123456789);

$share = new Share();
$share->setFormId(42);
Expand Down Expand Up @@ -410,6 +416,7 @@ public function dataGetPublicForm() {
'description' => 'Description Text',
'created' => 123456789,
'expires' => 0,
'lastUpdated' => 123456789,
'isAnonymous' => false,
'submitMultiple' => true,
'showExpiration' => false,
Expand Down Expand Up @@ -440,6 +447,7 @@ public function testGetPublicForm(array $expected) {
'showToAllUsers' => false,
]);
$form->setExpires(0);
$form->setLastUpdated(123456789);
$form->setIsAnonymous(false);
$form->setSubmitMultiple(true);
$form->setShowExpiration(false);
Expand Down

0 comments on commit 95b339f

Please sign in to comment.