Skip to content

Commit

Permalink
if AllowEdit is enabled, allow editing and pass the original submissi…
Browse files Browse the repository at this point in the history
…on to the client
  • Loading branch information
tpokorra committed Aug 18, 2023
1 parent 1c64f7b commit 7449027
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use OCA\Forms\Activity\ActivityManager;
use OCA\Forms\Constants;
use OCA\Forms\Db\AnswerMapper;
use OCA\Forms\Db\Form;
use OCA\Forms\Db\FormMapper;
use OCA\Forms\Db\OptionMapper;
Expand Down Expand Up @@ -69,6 +70,9 @@ class FormsService {
/** @var SubmissionMapper */
private $submissionMapper;

/** @var AnswerMapper */
private $answerMapper;

/** @var ConfigService */
private $configService;

Expand All @@ -93,6 +97,7 @@ public function __construct(ActivityManager $activityManager,
QuestionMapper $questionMapper,
ShareMapper $shareMapper,
SubmissionMapper $submissionMapper,
AnswerMapper $answerMapper,
ConfigService $configService,
IGroupManager $groupManager,
LoggerInterface $logger,
Expand All @@ -105,6 +110,7 @@ public function __construct(ActivityManager $activityManager,
$this->questionMapper = $questionMapper;
$this->shareMapper = $shareMapper;
$this->submissionMapper = $submissionMapper;
$this->answerMapper = $answerMapper;
$this->configService = $configService;
$this->groupManager = $groupManager;
$this->logger = $logger;
Expand Down Expand Up @@ -144,19 +150,51 @@ public function getOptions(int $questionId): array {
}
}

private function getAnswers(int $formId, string $userId): array {

$submissionList = [];
try {
$submissionEntities = $this->submissionMapper->findByForm($formId);
foreach ($submissionEntities as $submissionEntity) {
$submission = $submissionEntity->read();
if ($submission['userId'] == $userId) {
$answerEntities = $this->answerMapper->findBySubmission($submission['id']);
return $answerEntities;
}
}
} catch (DoesNotExistException $e) {
// Just ignore, if no Data
}

return array();
}

/**
* Load questions corresponding to form
*
* @param integer $formId
* @return array
*/
public function getQuestions(int $formId): array {

$answerEntities = NULL;
if ($this->currentUser->getUID()) {
$answerEntities = $this->getAnswers($formId, $this->currentUser->getUID());
}
$questionList = [];
try {
$questionEntities = $this->questionMapper->findByForm($formId);
foreach ($questionEntities as $questionEntity) {
$question = $questionEntity->read();
$question['options'] = $this->getOptions($question['id']);
if ($answerEntities) {
foreach ($answerEntities as $answerEntity) {
$answer = $answerEntity->read();
if ($answer['questionId'] == $question['id']) {
$question['value'] = $answer['text'];
}
}
}
$questionList[] = $question;
}
} catch (DoesNotExistException $e) {
Expand Down Expand Up @@ -321,8 +359,8 @@ public function canSubmit(int $formId): bool {
return true;
}

// Refuse access, if SubmitMultiple is not set and user already has taken part.
if (!$form->getSubmitMultiple()) {
// Refuse access, if SubmitMultiple is not set and AllowEdit is not set and user already has taken part.
if (!$form->getSubmitMultiple() && !$form->getAllowEdit()) {
$participants = $this->submissionMapper->findParticipantsByForm($form->getId());
foreach ($participants as $participant) {
if ($participant === $this->currentUser->getUID()) {
Expand Down

0 comments on commit 7449027

Please sign in to comment.