diff options
author | dartcafe <github@dartcafe.de> | 2020-08-22 08:09:01 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2020-08-22 08:09:01 +0300 |
commit | b0b220ef34e65a3f4e4fb12603142c376e5573f5 (patch) | |
tree | 16a0096f81717ae2e6647dc5736c5b7f4e64e033 /lib | |
parent | 6b73deac12a2dabc4d94b92ce91eebd04620e3bb (diff) |
move option sequence to backend #1058
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/OptionController.php | 13 | ||||
-rw-r--r-- | lib/Service/OptionService.php | 43 |
2 files changed, 56 insertions, 0 deletions
diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php index 466902ee..f7a36ce6 100644 --- a/lib/Controller/OptionController.php +++ b/lib/Controller/OptionController.php @@ -118,4 +118,17 @@ class OptionController extends Controller { public function reorder($pollId, $options) { return new DataResponse(['options' => $this->optionService->reorder($pollId, $options)], Http::STATUS_OK); } + + /** + * Reorder options + * @NoAdminRequired + * @param int $optionId + * @param int $step + * @param string $unit + * @param int $amount + * @return DataResponse + */ + public function sequence($optionId, $step, $unit, $amount) { + return new DataResponse(['options' => $this->optionService->sequence($optionId, $step, $unit, $amount)], Http::STATUS_OK); + } } diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php index 14d6e8a1..25242c66 100644 --- a/lib/Service/OptionService.php +++ b/lib/Service/OptionService.php @@ -23,6 +23,7 @@ namespace OCA\Polls\Service; +use DateTime; use Exception; use OCP\AppFramework\Db\DoesNotExistException; use OCA\Polls\Exceptions\NotAuthorizedException; @@ -201,6 +202,48 @@ class OptionService { } /** + * Make a sequence of date poll options + * @NoAdminRequired + * @param int $optionId + * @param int $step + * @param string $unit + * @param int $amount + * @return array Array of Option objects + * @throws NotAuthorizedException + */ + public function sequence($optionId, $step, $unit, $amount) { + + $baseDate = new DateTime; + $origin = $this->optionMapper->find($optionId); + + if (!$this->acl->set($origin->getPollId())->getAllowEdit()) { + throw new NotAuthorizedException; + } + + if ($step === 0) { + return $this->optionMapper->findByPoll($origin->getPollId()); + } + + $baseDate->setTimestamp($origin->getTimestamp()); + + for ($i=0; $i < $amount; $i++) { + + $this->option = new Option(); + $this->option->setPollId($origin->getPollId()); + $this->option->setConfirmed(0); + $this->option->setTimestamp($baseDate->modify($step . ' ' . $unit)->getTimestamp()); + $this->option->setPollOptionText($baseDate->format('c')); + $this->option->setOrder($baseDate->getTimestamp()); + try { + $this->optionMapper->insert($this->option); + } catch (UniqueConstraintViolationException $e) { + \OC::$server->getLogger()->warning('skip adding ' . $baseDate->format('c') . 'for pollId' . $origin->getPollId() . '. Option alredy exists.'); + } + } + return $this->optionMapper->findByPoll($origin->getPollId()); + } + + /** * Copy options from $fromPoll to $toPoll * @NoAdminRequired * @param int $fromPollId |