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
|
|
|
|
* Authors of Framadate/OpenSondate: Framasoft (https://github.com/framasoft)
|
|
|
|
*
|
|
|
|
* =============================
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2014-12-25 00:55:52 +01:00
|
|
|
use Framadate\Form;
|
|
|
|
use Framadate\FramaDB;
|
|
|
|
use Framadate\Utils;
|
2015-04-02 11:58:47 +02:00
|
|
|
use Framadate\Security\Token;
|
2015-04-02 23:10:41 +02:00
|
|
|
use Framadate\Repositories\RepositoryFactory;
|
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) {
|
|
|
|
if (preg_match('/^[\w\d]{16}$/i', $poll_id)) {
|
2015-04-02 23:23:34 +02:00
|
|
|
return $this->pollRepository->findById($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
|
|
|
}
|
|
|
|
|
|
|
|
function allSlotsByPollId($poll_id) {
|
2015-04-02 23:23:34 +02:00
|
|
|
return $this->slotRepository->listByPollId($poll_id);
|
2014-12-17 13:17:08 +01:00
|
|
|
}
|
|
|
|
|
2015-01-07 23:29:46 +01:00
|
|
|
public function updateVote($poll_id, $vote_id, $name, $choices) {
|
2014-12-17 13:17:08 +01:00
|
|
|
$choices = implode($choices);
|
2014-12-25 00:55:52 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function addVote($poll_id, $name, $choices) {
|
|
|
|
$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;
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function createPoll(Form $form) {
|
|
|
|
|
|
|
|
// Generate poll IDs, loop while poll ID already exists
|
|
|
|
do {
|
|
|
|
$poll_id = $this->random(16);
|
|
|
|
} while ($this->pollRepository->existsById($poll_id));
|
|
|
|
$admin_poll_id = $poll_id . $this->random(8);
|
|
|
|
|
|
|
|
// 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);
|
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);
|
|
|
|
}
|
|
|
|
|
2014-12-27 00:19:48 +01:00
|
|
|
function computeBestChoices($votes) {
|
2014-12-17 13:17:08 +01:00
|
|
|
$result = [];
|
|
|
|
foreach ($votes as $vote) {
|
2014-12-30 01:41:25 +01:00
|
|
|
$choices = str_split($vote->choices);
|
2014-12-25 00:55:52 +01:00
|
|
|
foreach ($choices as $i => $choice) {
|
2014-12-17 13:17:08 +01:00
|
|
|
if (empty($result[$i])) {
|
|
|
|
$result[$i] = 0;
|
|
|
|
}
|
|
|
|
if ($choice == 2) {
|
|
|
|
$result[$i]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-25 00:55:52 +01:00
|
|
|
|
2014-12-17 13:17:08 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function splitSlots($slots) {
|
|
|
|
$splitted = array();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function splitVotes($votes) {
|
|
|
|
$splitted = array();
|
|
|
|
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-04-02 11:58:47 +02:00
|
|
|
private function random($length) {
|
|
|
|
return Token::getToken($length);
|
2014-12-25 00:55:52 +01:00
|
|
|
}
|
2015-04-02 11:58:47 +02:00
|
|
|
|
2014-12-17 13:17:08 +01:00
|
|
|
}
|