connect = $connect; $this->logService = $logService; $this->pollRepository = RepositoryFactory::pollRepository(); $this->slotRepository = RepositoryFactory::slotRepository(); $this->voteRepository = RepositoryFactory::voteRepository(); $this->commentRepository = RepositoryFactory::commentRepository(); } /** * Find a poll from its ID. * * @param $poll_id int The ID of the poll * @return \stdClass|null The found poll, or null */ function findById($poll_id) { if (preg_match('/^[\w\d]{16}$/i', $poll_id)) { return $this->pollRepository->findById($poll_id); } return null; } public function findByAdminId($admin_poll_id) { if (preg_match('/^[\w\d]{24}$/i', $admin_poll_id)) { return $this->pollRepository->findByAdminId($admin_poll_id); } return null; } function allCommentsByPollId($poll_id) { return $this->commentRepository->findAllByPollId($poll_id); } function allVotesByPollId($poll_id) { return $this->voteRepository->allUserVotesByPollId($poll_id); } function allSlotsByPoll($poll) { $slots = $this->slotRepository->listByPollId($poll->id); if ($poll->format == 'D') { uasort($slots, function ($a, $b) { return $a->title > $b->title; }); } return $slots; } public function updateVote($poll_id, $vote_id, $name, $choices) { $choices = implode($choices); return $this->voteRepository->update($poll_id, $vote_id, $name, $choices); } function addVote($poll_id, $name, $choices) { if ($this->voteRepository->existsByPollIdAndName($poll_id, $name)) { return false; } $choices = implode($choices); $token = $this->random(16); return $this->voteRepository->insert($poll_id, $name, $choices, $token); } function addComment($poll_id, $name, $comment) { if ($this->commentRepository->exists($poll_id, $name, $comment)) { return true; } else { return $this->commentRepository->insert($poll_id, $name, $comment); } } /** * @param Form $form * @return string */ function createPoll(Form $form) { // Generate poll IDs, loop while poll ID already exists if (empty($form->id)) { // User want us to generate an id for him do { $poll_id = $this->random(16); } while ($this->pollRepository->existsById($poll_id)); $admin_poll_id = $poll_id . $this->random(8); } else { // User have choosen the poll id $poll_id = $form->id; do { $admin_poll_id = $this->random(24); } while ($this->pollRepository->existsByAdminId($admin_poll_id)); } // Insert poll + slots $this->pollRepository->beginTransaction(); $this->pollRepository->insertPoll($poll_id, $admin_poll_id, $form); $this->slotRepository->insertSlots($poll_id, $form->getChoices()); $this->pollRepository->commit(); $this->logService->log('CREATE_POLL', 'id:' . $poll_id . ', title: ' . $form->title . ', format:' . $form->format . ', admin:' . $form->admin_name . ', mail:' . $form->admin_mail); return array($poll_id, $admin_poll_id); } public function findAllByAdminMail($mail) { return $this->pollRepository->findAllByAdminMail($mail); } function computeBestChoices($votes) { $result = ['y' => [0], 'inb' => [0]]; foreach ($votes as $vote) { $choices = str_split($vote->choices); foreach ($choices as $i => $choice) { if (!isset($result['y'][$i])) { $result['inb'][$i] = 0; $result['y'][$i] = 0; } if ($choice == 1) { $result['inb'][$i]++; } if ($choice == 2) { $result['y'][$i]++; } } } return $result; } function splitSlots($slots) { $splitted = array(); foreach ($slots as $slot) { $obj = new \stdClass(); $obj->day = $slot->title; $obj->moments = explode(',', $slot->moments); $splitted[] = $obj; } return $splitted; } function splitVotes($votes) { $splitted = array(); foreach ($votes as $vote) { $obj = new \stdClass(); $obj->id = $vote->id; $obj->name = $vote->name; $obj->uniqId = $vote->uniqId; $obj->choices = str_split($vote->choices); $splitted[] = $obj; } return $splitted; } private function random($length) { return Token::getToken($length); } /** * @return int The max timestamp allowed for expiry date */ public function maxExpiryDate() { global $config; return time() + (86400 * $config['default_poll_duration']); } /** * @return int The min timestamp allowed for expiry date */ public function minExpiryDate() { return time() + 86400; } }