2014-12-17 13:17:08 +01:00
|
|
|
<?php
|
2014-12-17 13:48:03 +01:00
|
|
|
/**
|
|
|
|
* This software is governed by the CeCILL-B license. If a copy of this license
|
|
|
|
* is not distributed with this file, you can obtain one at
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
|
|
|
|
*
|
|
|
|
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
|
2016-08-04 22:26:37 +02:00
|
|
|
* Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
|
2014-12-17 13:48:03 +01:00
|
|
|
*
|
|
|
|
* =============================
|
|
|
|
*
|
|
|
|
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
|
|
|
|
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
|
|
|
|
*
|
|
|
|
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
|
|
|
|
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
|
|
|
|
*/
|
2014-12-17 13:17:08 +01:00
|
|
|
namespace Framadate\Services;
|
|
|
|
|
2015-12-08 22:53:51 +01:00
|
|
|
use Framadate\Exception\AlreadyExistsException;
|
|
|
|
use Framadate\Exception\ConcurrentEditionException;
|
2018-02-20 16:47:10 +01:00
|
|
|
use Framadate\Exception\ConcurrentVoteException;
|
2014-12-25 00:55:52 +01:00
|
|
|
use Framadate\Form;
|
|
|
|
use Framadate\FramaDB;
|
2015-04-09 17:53:00 +02:00
|
|
|
use Framadate\Repositories\RepositoryFactory;
|
2015-12-05 16:30:49 +01:00
|
|
|
use Framadate\Security\Token;
|
2014-12-25 00:55:52 +01:00
|
|
|
|
2014-12-17 13:17:08 +01:00
|
|
|
class PollService {
|
|
|
|
private $connect;
|
2014-12-25 00:55:52 +01:00
|
|
|
private $logService;
|
2014-12-17 13:17:08 +01:00
|
|
|
|
2015-04-02 23:10:41 +02:00
|
|
|
private $pollRepository;
|
2015-04-02 23:23:34 +02:00
|
|
|
private $slotRepository;
|
2015-04-03 00:11:36 +02:00
|
|
|
private $voteRepository;
|
2015-04-02 23:32:24 +02:00
|
|
|
private $commentRepository;
|
2014-12-17 13:17:08 +01:00
|
|
|
|
2014-12-25 00:55:52 +01:00
|
|
|
function __construct(FramaDB $connect, LogService $logService) {
|
2014-12-17 13:17:08 +01:00
|
|
|
$this->connect = $connect;
|
2014-12-25 00:55:52 +01:00
|
|
|
$this->logService = $logService;
|
2015-04-02 23:10:41 +02:00
|
|
|
$this->pollRepository = RepositoryFactory::pollRepository();
|
2015-04-02 23:23:34 +02:00
|
|
|
$this->slotRepository = RepositoryFactory::slotRepository();
|
2015-04-03 00:11:36 +02:00
|
|
|
$this->voteRepository = RepositoryFactory::voteRepository();
|
2015-04-02 23:32:24 +02:00
|
|
|
$this->commentRepository = RepositoryFactory::commentRepository();
|
2014-12-17 13:17:08 +01:00
|
|
|
}
|
|
|
|
|
2014-12-24 22:42:50 +01:00
|
|
|
/**
|
|
|
|
* Find a poll from its ID.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @return \stdClass|null The found poll, or null
|
|
|
|
*/
|
2014-12-17 13:17:08 +01:00
|
|
|
function findById($poll_id) {
|
2015-12-05 16:35:32 +01:00
|
|
|
if (preg_match(POLL_REGEX, $poll_id)) {
|
2015-04-02 23:23:34 +02:00
|
|
|
return $this->pollRepository->findById($poll_id);
|
2014-12-17 13:17:08 +01:00
|
|
|
}
|
2015-10-28 22:11:00 +01:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findByAdminId($admin_poll_id) {
|
2015-12-05 16:35:32 +01:00
|
|
|
if (preg_match(ADMIN_POLL_REGEX, $admin_poll_id)) {
|
2015-10-28 22:11:00 +01:00
|
|
|
return $this->pollRepository->findByAdminId($admin_poll_id);
|
|
|
|
}
|
2014-12-17 13:17:08 +01:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function allCommentsByPollId($poll_id) {
|
2015-04-03 00:11:36 +02:00
|
|
|
return $this->commentRepository->findAllByPollId($poll_id);
|
2014-12-17 13:17:08 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 17:24:39 +01:00
|
|
|
function allVotesByPollId($poll_id) {
|
2015-04-03 00:11:36 +02:00
|
|
|
return $this->voteRepository->allUserVotesByPollId($poll_id);
|
2014-12-17 13:17:08 +01:00
|
|
|
}
|
|
|
|
|
2015-05-30 23:36:04 +02:00
|
|
|
function allSlotsByPoll($poll) {
|
|
|
|
$slots = $this->slotRepository->listByPollId($poll->id);
|
2018-02-19 00:18:43 +01:00
|
|
|
if ($poll->format === 'D') {
|
2016-03-02 23:55:12 +01:00
|
|
|
$this->sortSlorts($slots);
|
2015-05-30 23:36:04 +02:00
|
|
|
}
|
2015-10-12 21:42:59 +02:00
|
|
|
return $slots;
|
2014-12-17 13:17:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 16:47:10 +01:00
|
|
|
/**
|
|
|
|
* @param $poll_id
|
|
|
|
* @param $vote_id
|
|
|
|
* @param $name
|
|
|
|
* @param $choices
|
|
|
|
* @param $slots_hash
|
2018-04-06 21:16:28 +02:00
|
|
|
* @throws AlreadyExistsException
|
2018-02-20 16:47:10 +01:00
|
|
|
* @throws ConcurrentEditionException
|
|
|
|
* @throws ConcurrentVoteException
|
2018-02-20 16:50:14 +01:00
|
|
|
* @return bool
|
2018-02-20 16:47:10 +01:00
|
|
|
*/
|
2015-12-08 22:53:51 +01:00
|
|
|
public function updateVote($poll_id, $vote_id, $name, $choices, $slots_hash) {
|
2018-04-06 21:16:28 +02:00
|
|
|
$this->checkVoteConstraints($choices, $poll_id, $slots_hash, $name, $vote_id);
|
2018-04-06 14:00:20 +02:00
|
|
|
|
2015-12-08 22:53:51 +01:00
|
|
|
// Update vote
|
|
|
|
$choices = implode($choices);
|
2015-04-03 00:11:36 +02:00
|
|
|
return $this->voteRepository->update($poll_id, $vote_id, $name, $choices);
|
2014-12-17 13:17:08 +01:00
|
|
|
}
|
2018-04-06 21:16:28 +02:00
|
|
|
|
2018-02-20 16:47:10 +01:00
|
|
|
/**
|
|
|
|
* @param $poll_id
|
|
|
|
* @param $name
|
|
|
|
* @param $choices
|
|
|
|
* @param $slots_hash
|
|
|
|
* @throws AlreadyExistsException
|
|
|
|
* @throws ConcurrentEditionException
|
|
|
|
* @throws ConcurrentVoteException
|
2018-02-20 16:50:14 +01:00
|
|
|
* @return \stdClass
|
2018-02-20 16:47:10 +01:00
|
|
|
*/
|
2015-12-08 22:53:51 +01:00
|
|
|
function addVote($poll_id, $name, $choices, $slots_hash) {
|
2018-04-08 11:29:03 +02:00
|
|
|
$this->checkVoteConstraints($choices, $poll_id, $slots_hash, $name);
|
2018-04-06 21:16:28 +02:00
|
|
|
|
2015-12-08 22:53:51 +01:00
|
|
|
// Insert new vote
|
2014-12-17 13:17:08 +01:00
|
|
|
$choices = implode($choices);
|
2015-04-02 11:58:47 +02:00
|
|
|
$token = $this->random(16);
|
2015-04-07 23:17:26 +02:00
|
|
|
return $this->voteRepository->insert($poll_id, $name, $choices, $token);
|
2014-12-17 13:17:08 +01:00
|
|
|
}
|
|
|
|
|
2014-12-17 13:47:14 +01:00
|
|
|
function addComment($poll_id, $name, $comment) {
|
2015-04-02 23:32:24 +02:00
|
|
|
if ($this->commentRepository->exists($poll_id, $name, $comment)) {
|
|
|
|
return true;
|
2018-02-20 15:17:14 +01:00
|
|
|
}
|
2018-04-06 21:16:28 +02:00
|
|
|
|
|
|
|
return $this->commentRepository->insert($poll_id, $name, $comment);
|
2014-12-17 13:47:14 +01:00
|
|
|
}
|
|
|
|
|
2015-04-02 23:23:34 +02:00
|
|
|
/**
|
|
|
|
* @param Form $form
|
2018-02-20 19:29:56 +01:00
|
|
|
* @return array
|
2015-04-02 23:23:34 +02:00
|
|
|
*/
|
|
|
|
function createPoll(Form $form) {
|
|
|
|
// Generate poll IDs, loop while poll ID already exists
|
2015-12-05 16:30:49 +01:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2015-04-02 23:23:34 +02:00
|
|
|
|
|
|
|
// Insert poll + slots
|
|
|
|
$this->pollRepository->beginTransaction();
|
2015-04-09 18:18:05 +02:00
|
|
|
$this->pollRepository->insertPoll($poll_id, $admin_poll_id, $form);
|
2015-04-02 23:23:34 +02:00
|
|
|
$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);
|
|
|
|
|
2018-02-19 00:18:43 +01:00
|
|
|
return [$poll_id, $admin_poll_id];
|
2015-01-06 23:52:52 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 21:52:09 +02:00
|
|
|
public function findAllByAdminMail($mail) {
|
|
|
|
return $this->pollRepository->findAllByAdminMail($mail);
|
|
|
|
}
|
|
|
|
|
2018-04-06 09:40:53 +02:00
|
|
|
/**
|
|
|
|
* @param array $votes
|
|
|
|
* @param \stdClass $poll
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function computeBestChoices($votes, $poll) {
|
|
|
|
if (0 === count($votes)) {
|
|
|
|
return $this->computeEmptyBestChoices($poll);
|
|
|
|
}
|
|
|
|
$result = ['y' => [], 'inb' => []];
|
|
|
|
|
|
|
|
// if there are votes
|
|
|
|
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]++;
|
2014-12-17 13:17:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-25 00:55:52 +01:00
|
|
|
|
2014-12-17 13:17:08 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function splitSlots($slots) {
|
2018-02-19 00:18:43 +01:00
|
|
|
$splitted = [];
|
2014-12-17 13:17:08 +01:00
|
|
|
foreach ($slots as $slot) {
|
|
|
|
$obj = new \stdClass();
|
2014-12-30 01:41:25 +01:00
|
|
|
$obj->day = $slot->title;
|
|
|
|
$obj->moments = explode(',', $slot->moments);
|
2014-12-17 13:17:08 +01:00
|
|
|
|
|
|
|
$splitted[] = $obj;
|
|
|
|
}
|
2014-12-25 00:55:52 +01:00
|
|
|
|
2014-12-17 13:17:08 +01:00
|
|
|
return $splitted;
|
|
|
|
}
|
|
|
|
|
2015-12-08 22:53:51 +01:00
|
|
|
/**
|
|
|
|
* @param $slots array The slots to hash
|
|
|
|
* @return string The hash
|
|
|
|
*/
|
|
|
|
public function hashSlots($slots) {
|
|
|
|
return md5(array_reduce($slots, function($carry, $item) {
|
|
|
|
return $carry . $item->id . '@' . $item->moments . ';';
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2014-12-17 13:17:08 +01:00
|
|
|
function splitVotes($votes) {
|
2018-02-19 00:18:43 +01:00
|
|
|
$splitted = [];
|
2014-12-17 13:17:08 +01:00
|
|
|
foreach ($votes as $vote) {
|
|
|
|
$obj = new \stdClass();
|
2014-12-30 01:41:25 +01:00
|
|
|
$obj->id = $vote->id;
|
|
|
|
$obj->name = $vote->name;
|
2015-04-02 16:52:46 +02:00
|
|
|
$obj->uniqId = $vote->uniqId;
|
2014-12-30 01:41:25 +01:00
|
|
|
$obj->choices = str_split($vote->choices);
|
2014-12-17 13:17:08 +01:00
|
|
|
|
|
|
|
$splitted[] = $obj;
|
|
|
|
}
|
2014-12-25 00:55:52 +01:00
|
|
|
|
2014-12-17 13:17:08 +01:00
|
|
|
return $splitted;
|
|
|
|
}
|
2014-12-25 00:55:52 +01:00
|
|
|
|
2015-10-12 21:42:59 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2018-02-19 00:18:43 +01:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function sortSlorts(&$slots) {
|
|
|
|
uasort($slots, function ($a, $b) {
|
|
|
|
return $a->title > $b->title;
|
|
|
|
});
|
|
|
|
return $slots;
|
|
|
|
}
|
|
|
|
|
2018-04-06 09:43:32 +02:00
|
|
|
/**
|
|
|
|
* @param \stdClass $poll
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function computeEmptyBestChoices($poll)
|
|
|
|
{
|
|
|
|
$result = ['y' => [], 'inb' => []];
|
|
|
|
// if there is no votes, calculates the number of slot
|
|
|
|
|
|
|
|
$slots = $this->allSlotsByPoll($poll);
|
|
|
|
|
|
|
|
if ($poll->format === 'A') {
|
|
|
|
// poll format classic
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($slots); $i++) {
|
|
|
|
$result['y'][] = 0;
|
|
|
|
$result['inb'][] = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// poll format date
|
|
|
|
|
|
|
|
$slots = $this->splitSlots($slots);
|
|
|
|
|
|
|
|
foreach ($slots as $slot) {
|
|
|
|
for ($i = 0; $i < count($slot->moments); $i++) {
|
|
|
|
$result['y'][] = 0;
|
|
|
|
$result['inb'][] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-02-19 00:18:43 +01:00
|
|
|
private function random($length) {
|
|
|
|
return Token::getToken($length);
|
|
|
|
}
|
2018-04-06 21:16:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $choices
|
|
|
|
* @param $poll_id
|
|
|
|
* @param $slots_hash
|
|
|
|
* @param $name
|
2018-04-08 11:29:03 +02:00
|
|
|
* @param string $vote_id
|
2018-04-06 21:16:28 +02:00
|
|
|
* @throws AlreadyExistsException
|
|
|
|
* @throws ConcurrentVoteException
|
|
|
|
* @throws ConcurrentEditionException
|
|
|
|
*/
|
2018-04-08 11:29:03 +02:00
|
|
|
private function checkVoteConstraints($choices, $poll_id, $slots_hash, $name, $vote_id = FALSE) {
|
2018-04-06 21:16:28 +02:00
|
|
|
// Check if vote already exists with the same name
|
2018-04-08 11:29:03 +02:00
|
|
|
if (FALSE === $vote_id) {
|
2018-04-06 21:16:28 +02:00
|
|
|
$exists = $this->voteRepository->existsByPollIdAndName($poll_id, $name);
|
|
|
|
} else {
|
|
|
|
$exists = $this->voteRepository->existsByPollIdAndNameAndVoteId($poll_id, $name, $vote_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($exists) {
|
|
|
|
throw new AlreadyExistsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$poll = $this->findById($poll_id);
|
|
|
|
|
|
|
|
// Check that no-one voted in the meantime and it conflicts the maximum votes constraint
|
|
|
|
$this->checkMaxVotes($choices, $poll, $poll_id);
|
|
|
|
|
|
|
|
// Check if slots are still the same
|
|
|
|
$this->checkThatSlotsDidntChanged($poll, $slots_hash);
|
|
|
|
}
|
|
|
|
|
2015-12-08 22:53:51 +01:00
|
|
|
/**
|
|
|
|
* This method checks if the hash send by the user is the same as the computed hash.
|
|
|
|
*
|
2016-03-02 23:55:12 +01:00
|
|
|
* @param $poll /stdClass The poll
|
2015-12-08 22:53:51 +01:00
|
|
|
* @param $slots_hash string The hash sent by the user
|
|
|
|
* @throws ConcurrentEditionException Thrown when hashes are differents
|
|
|
|
*/
|
2016-03-02 23:55:12 +01:00
|
|
|
private function checkThatSlotsDidntChanged($poll, $slots_hash) {
|
|
|
|
$slots = $this->allSlotsByPoll($poll);
|
2015-12-08 22:53:51 +01:00
|
|
|
if ($slots_hash !== $this->hashSlots($slots)) {
|
|
|
|
throw new ConcurrentEditionException();
|
|
|
|
}
|
|
|
|
}
|
2018-02-20 16:47:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method checks if the votes doesn't conflicts the maximum votes constraint
|
|
|
|
*
|
|
|
|
* @param $user_choice
|
|
|
|
* @param \stdClass $poll
|
|
|
|
* @param string $poll_id
|
|
|
|
* @throws ConcurrentVoteException
|
|
|
|
*/
|
|
|
|
private function checkMaxVotes($user_choice, $poll, $poll_id) {
|
2018-02-20 19:29:56 +01:00
|
|
|
$votes = $this->allVotesByPollId($poll_id);
|
|
|
|
if (count($votes) <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-05 17:34:43 +02:00
|
|
|
$best_choices = $this->computeBestChoices($votes, $poll);
|
2018-02-20 16:47:10 +01:00
|
|
|
foreach ($best_choices['y'] as $i => $nb_choice) {
|
|
|
|
// if for this option we have reached maximum value and user wants to add itself too
|
2018-04-05 17:34:43 +02:00
|
|
|
if ($poll->ValueMax !== null && $nb_choice >= $poll->ValueMax && $user_choice[$i] === "2") {
|
2018-02-20 16:47:10 +01:00
|
|
|
throw new ConcurrentVoteException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-17 13:17:08 +01:00
|
|
|
}
|