diff options
author | dartcafe <github@dartcafe.de> | 2020-06-20 12:38:34 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2020-06-20 12:38:34 +0300 |
commit | d3b8c7c7028b6c112c230df6c010263dfb1a1130 (patch) | |
tree | 29e63adc08d02a4fc1b6eb32c01c72046444fc5b /lib/Service | |
parent | b1c1a707d1b5523868d992b96a4ccddd1086ef2b (diff) |
Completed API with poll
Diffstat (limited to 'lib/Service')
-rw-r--r-- | lib/Service/OptionService.php | 17 | ||||
-rw-r--r-- | lib/Service/PollService.php | 408 |
2 files changed, 425 insertions, 0 deletions
diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php index 0d0e7be4..e7f485aa 100644 --- a/lib/Service/OptionService.php +++ b/lib/Service/OptionService.php @@ -214,4 +214,21 @@ class OptionService { return $this->get($pollId); } + + /** + * Set order by order of the given array + * @NoAdminRequired + * @param array $options + * @return array Array of Option objects + */ + public function clone($fromPollId, $toPollId) { + + foreach ($this->optionMapper->findByPoll($fromPollId) as $option) { + $option->setPollId($toPollId); + $this->optionMapper->insert($option); + } + + return $this->optionMapper->findByPoll($toPollId); + + } } diff --git a/lib/Service/PollService.php b/lib/Service/PollService.php new file mode 100644 index 00000000..ecf35b18 --- /dev/null +++ b/lib/Service/PollService.php @@ -0,0 +1,408 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author René Gieling <github@dartcafe.de> + * + * @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\Polls\Service; + + use Exception; + use OCP\AppFramework\Db\DoesNotExistException; + use OCA\Polls\Exceptions\EmptyTitleException; + use OCA\Polls\Exceptions\InvalidAccessException; + use OCA\Polls\Exceptions\InvalidShowResultsException; + use OCA\Polls\Exceptions\InvalidPollTypeException; + use OCA\Polls\Exceptions\NotAuthorizedException; + + use OCP\ILogger; + + use OCA\Polls\Db\Poll; + use OCA\Polls\Db\PollMapper; + use OCA\Polls\Service\CommentService; + use OCA\Polls\Service\OptionService; + use OCA\Polls\Service\ShareService; + use OCA\Polls\Service\VoteService; + use OCA\Polls\Service\LogService; + use OCA\Polls\Model\Acl; + + class PollService { + + private $logger; + private $userid; + private $pollMapper; + private $poll; + private $logService; + private $commentService; + private $optionService; + private $shareService; + private $voteService; + private $acl; + + /** + * PollController constructor. + * @param string $appName + * @param $userId + * @param PollMapper $pollMapper + * @param LogService $logService + * @param CommentService $commentService + * @param OptionService $optionService + * @param ShareService $shareService + * @param VoteService $voteService + * @param Acl $acl + */ + + public function __construct( + string $appName, + $userId, + ILogger $logger, + PollMapper $pollMapper, + Poll $poll, + LogService $logService, + CommentService $commentService, + OptionService $optionService, + ShareService $shareService, + VoteService $voteService, + Acl $acl + ) { + $this->userId = $userId; + $this->logger = $logger; + $this->pollMapper = $pollMapper; + $this->poll = $poll; + $this->logService = $logService; + $this->commentService = $commentService; + $this->optionService = $optionService; + $this->shareService = $shareService; + $this->voteService = $voteService; + $this->acl = $acl; + } + + + /** + * list + * @NoAdminRequired + * @return DataResponse + */ + + public function list() { + if (!\OC::$server->getUserSession()->isLoggedIn()) { + throw new NotAuthorizedException; + } + + $polls = $this->pollMapper->findAll(); + // TODO: Not the elegant way. Improvement neccessary + foreach ($polls as $poll) { + $combinedPoll = (object) array_merge( + (array) json_decode(json_encode($poll)), (array) json_decode(json_encode($this->acl->setPollId($poll->getId())))); + if ($combinedPoll->allowView) { + $pollList[] = $combinedPoll; + } + } + + return $pollList; + } + + /** + * get + * @NoAdminRequired + * @param integer $pollId + * @return array + */ + public function get($pollId = 0, $token = '') { + $this->poll = $this->pollMapper->find($pollId); + if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowView()) { + throw new NotAuthorizedException; + } + + return [ + 'acl' => $this->acl, + 'poll' => $this->poll, + 'comments' => $this->commentService->list($pollId, $token), + 'options' => $this->optionService->list($pollId, $token), + 'shares' => $this->shareService->list($pollId, $token), + 'votes' => $this->voteService->list($pollId, $token) + ]; + + } + + /** + * delete + * @NoAdminRequired + * @NoCSRFRequired + * @param integer $pollId + * @return Poll + */ + + public function delete($pollId) { + $this->poll = $this->pollMapper->find($pollId); + + if (!$this->acl->setPollId($pollId)->getAllowEdit()) { + throw new NotAuthorizedException; + } + if ($this->poll->getDeleted()) { + $this->poll->setDeleted(0); + } else { + $this->poll->setDeleted(time()); + } + + $this->poll = $this->pollMapper->update($this->poll); + $this->logService->setLog($this->poll->getId(), 'deletePoll'); + return $this->poll; + } + + /** + * deletePermanently + * @NoAdminRequired + * @NoCSRFRequired + * @param integer $pollId + * @return Poll + */ + + public function deletePermanently($pollId) { + $this->poll = $this->pollMapper->find($pollId); + + if (!$this->acl->setPollId($pollId)->getAllowEdit() || !$this->poll->getDeleted()) { + throw new NotAuthorizedException; + } + return $this->pollMapper->delete($this->poll); + // return $this->poll; + } + + /** + * write + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function add($type, $title) { + if (!\OC::$server->getUserSession()->isLoggedIn()) { + throw new NotAuthorizedException; + } + $this->logger->alert(json_encode($type)); + $this->logger->alert(json_encode($title)); + + // Validate valuess + if (!in_array($type, $this->getValidPollType())) { + throw new InvalidPollTypeException('Invalid poll type'); + } + + if (!$title) { + throw new EmptyTitleException('Title must not be empty'); + } + + $this->poll = new Poll(); + $this->poll->setType($type); + $this->poll->setCreated(time()); + $this->poll->setOwner($this->userId); + $this->poll->setTitle($title); + $this->poll->setDescription(''); + $this->poll->setAccess('hidden'); + $this->poll->setExpire(0); + $this->poll->setAnonymous(0); + $this->poll->setFullAnonymous(0); + $this->poll->setAllowMaybe(0); + $this->poll->setVoteLimit(0); + $this->poll->setSettings(''); + $this->poll->setOptions(''); + $this->poll->setShowResults('always'); + $this->poll->setDeleted(0); + $this->poll->setAdminAccess(0); + $this->poll = $this->pollMapper->insert($this->poll); + + $this->logService->setLog($this->poll->getId(), 'addPoll'); + $this->logger->alert(json_encode($this->poll)); + + return $this->poll; + } + + /** + * write + * @NoAdminRequired + * @NoCSRFRequired + * @depricated + * @param Array $poll + * @return DataResponse + */ + + public function write($poll, $pollId = null) { + + if (!$pollId) { + $pollId = $poll['id']; + } + + // Validate valuess + if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) { + throw new InvalidShowResultsException('Invalid value for prop showResults'); + } + + if (isset($poll['access']) && !in_array($poll['access'], $this->getValidShowResults())) { + throw new InvalidAccessException('Invalid value for prop access'); + } + + if (isset($poll['title']) && !$poll['title']) { + throw new EmptyTitleException('Title must not be empty'); + } + + try { + // find pollId + $this->poll = $this->pollMapper->find($pollId); + $this->logService->setLog($this->poll->getId(), 'updatePoll'); + + + } catch (DoesNotExistException $e) { + // if not found create a new poll + + // Validate valuess + if (!in_array($poll['type'], $this->getValidPollType())) { + throw new InvalidPollTypeException('Invalid poll type'); + } + + if (!$poll['title']) { + throw new EmptyTitleException('Title must not be empty'); + } + + + $this->poll = new Poll(); + $this->poll->setType($poll['type']); + $this->poll->setOwner($this->userId); + $this->poll->setTitle($poll['title']); + $this->poll->setCreated(time()); + $this->poll = $this->pollMapper->insert($this->poll); + + $this->logService->setLog($this->poll->getId(), 'addPoll'); + } + + if (!$this->acl->setPollId($this->poll->getId())->getAllowEdit()) { + throw new NotAuthorizedException; + } + + $this->poll->setTitle(isset($poll['title']) ? $poll['title'] : $this->poll->getTitle()); + $this->poll->setDescription(isset($poll['description']) ? $poll['description'] : $this->poll->getDescription()); + $this->poll->setAccess(isset($poll['access']) ? $poll['access'] : $this->poll->getAccess()); + $this->poll->setExpire(isset($poll['expire']) ? $poll['expire'] : $this->poll->getExpire()); + $this->poll->setAnonymous(isset($poll['anonymous']) ? $poll['anonymous'] : $this->poll->getAnonymous()); + $this->poll->setAllowMaybe(isset($poll['allowMaybe']) ? $poll['allowMaybe'] : $this->poll->getAllowMaybe()); + $this->poll->setVoteLimit(isset($poll['voteLimit']) ? $poll['voteLimit'] : $this->poll->getVoteLimit()); + $this->poll->setShowResults(isset($poll['showResults']) ? $poll['showResults'] : $this->poll->getShowResults()); + $this->poll->setDeleted(isset($poll['deleted']) ? $poll['deleted'] : $this->poll->getDeleted()); + $this->poll->setAdminAccess(isset($poll['adminAccess']) ? $poll['adminAccess'] : $this->poll->getAdminAccess()); + + $this->poll->setFullAnonymous(0); + $this->poll->setVoteLimit(0); + $this->poll->setSettings(''); + $this->poll->setOptions(''); + + $this->pollMapper->update($this->poll); + + return $this->poll; + } + + /** + * write + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function update($pollId, $poll) { + + $this->poll = $this->pollMapper->find($pollId); + + if (!$this->acl->setPollId($this->poll->getId())->getAllowEdit()) { + throw new NotAuthorizedException; + } + + // Validate valuess + if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) { + throw new InvalidShowResultsException('Invalid value for prop showResults'); + } + + if (isset($poll['access']) && !in_array($poll['access'], $this->getValidAccess())) { + throw new InvalidAccessException('Invalid value for prop access '. $poll['access']); + } + + if (isset($poll['title']) && !$poll['title']) { + throw new EmptyTitleException('Title must not be empty'); + } + + $this->poll->setTitle($poll['title'] ? $poll['title'] : $this->poll->getTitle()); + $this->poll->setDescription(isset($poll['description']) ? $poll['description'] : $this->poll->getDescription()); + $this->poll->setAccess(isset($poll['access']) ? $poll['access'] : $this->poll->getAccess()); + $this->poll->setExpire(isset($poll['expire']) ? $poll['expire'] : $this->poll->getExpire()); + $this->poll->setAnonymous(isset($poll['anonymous']) ? $poll['anonymous'] : $this->poll->getAnonymous()); + $this->poll->setAllowMaybe(isset($poll['allowMaybe']) ? $poll['allowMaybe'] : $this->poll->getAllowMaybe()); + $this->poll->setVoteLimit(isset($poll['voteLimit']) ? $poll['voteLimit'] : $this->poll->getVoteLimit()); + $this->poll->setShowResults(isset($poll['showResults']) ? $poll['showResults'] : $this->poll->getShowResults()); + $this->poll->setDeleted(isset($poll['deleted']) ? $poll['deleted'] : $this->poll->getDeleted()); + $this->poll->setAdminAccess(isset($poll['adminAccess']) ? $poll['adminAccess'] : $this->poll->getAdminAccess()); + + $this->pollMapper->update($this->poll); + $this->logService->setLog($this->poll->getId(), 'updatePoll'); + + return $this->poll; + } + + /** + * clone + * @NoAdminRequired + * @NoCSRFRequired + * @param integer $pollId + * @return DataResponse + */ + public function clone($pollId) { + $this->poll = $this->pollMapper->find($pollId); + + $this->poll->setCreated(time()); + $this->poll->setOwner($this->userId); + $this->poll->setTitle('Clone of ' . $this->poll->getTitle()); + $this->poll->setDeleted(0); + $this->poll->setId(0); + + $this->poll = $this->pollMapper->insert($this->poll); + $this->logService->setLog($clonePoll->getId(), 'addPoll'); + + $this->optionService->clone($pollId, $this->poll->getId()); + + return $this->poll; + + } + + public function getValidEnum() { + return [ + 'pollType' => $this->getValidPollType(), + 'access' => $this->getValidAccess(), + 'showResults' => $this->getValidShowResults() + ]; + } + + private function getValidPollType() { + return ['datePoll', 'textPoll']; + } + + private function getValidAccess() { + return ['hidden', 'public']; + } + + private function getValidShowResults() { + return ['always', 'expired', 'never']; + } +} |