Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/polls.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authordartcafe <github@dartcafe.de>2020-08-22 08:09:01 +0300
committerdartcafe <github@dartcafe.de>2020-08-22 08:09:01 +0300
commitb0b220ef34e65a3f4e4fb12603142c376e5573f5 (patch)
tree16a0096f81717ae2e6647dc5736c5b7f4e64e033 /lib
parent6b73deac12a2dabc4d94b92ce91eebd04620e3bb (diff)
move option sequence to backend #1058
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/OptionController.php13
-rw-r--r--lib/Service/OptionService.php43
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