2015-04-03 00:11:36 +02:00
|
|
|
<?php
|
|
|
|
namespace Framadate\Repositories;
|
|
|
|
|
|
|
|
use Framadate\FramaDB;
|
|
|
|
use Framadate\Utils;
|
|
|
|
|
|
|
|
class VoteRepository extends AbstractRepository {
|
2018-04-18 16:16:22 +02:00
|
|
|
/**
|
|
|
|
* @param $poll_id
|
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function allUserVotesByPollId($poll_id)
|
|
|
|
{
|
|
|
|
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('vote') . ' WHERE poll_id = ? ORDER BY id');
|
2018-02-19 00:18:43 +01:00
|
|
|
$prepared->execute([$poll_id]);
|
2015-04-03 00:11:36 +02:00
|
|
|
|
|
|
|
return $prepared->fetchAll();
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
/**
|
|
|
|
* @param $poll_id
|
|
|
|
* @param $insert_position
|
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function insertDefault($poll_id, $insert_position)
|
|
|
|
{
|
|
|
|
# TODO : Handle this on PHP's side
|
|
|
|
$prepared = $this->prepare('UPDATE ' . Utils::table('vote') . ' SET choices = CONCAT(SUBSTRING(choices, 1, ?), " ", SUBSTRING(choices, ?)) WHERE poll_id = ?'); //#51 : default value for unselected vote
|
2015-04-03 00:11:36 +02:00
|
|
|
|
|
|
|
return $prepared->execute([$insert_position, $insert_position + 1, $poll_id]);
|
|
|
|
}
|
|
|
|
|
2018-03-18 10:40:38 +01:00
|
|
|
function insert($poll_id, $name, $choices, $token, $mail) {
|
2018-04-18 16:16:22 +02:00
|
|
|
$this->connect->insert(Utils::table('vote'), ['poll_id' => $poll_id, 'name' => $name, 'choices' => $choices, 'uniqId' => $token, 'mail' => $mail]);
|
2015-04-03 00:11:36 +02:00
|
|
|
|
|
|
|
$newVote = new \stdClass();
|
|
|
|
$newVote->poll_id = $poll_id;
|
|
|
|
$newVote->id = $this->lastInsertId();
|
|
|
|
$newVote->name = $name;
|
|
|
|
$newVote->choices = $choices;
|
2015-04-07 23:17:26 +02:00
|
|
|
$newVote->uniqId = $token;
|
2018-04-18 16:16:22 +02:00
|
|
|
$newVote->mail=$mail;
|
2015-04-03 00:11:36 +02:00
|
|
|
|
|
|
|
return $newVote;
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
/**
|
|
|
|
* @param $poll_id
|
|
|
|
* @param $vote_id
|
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function deleteById($poll_id, $vote_id)
|
|
|
|
{
|
|
|
|
return $this->connect->delete(Utils::table('vote'), ['poll_id' => $poll_id, 'id' => $vote_id]) > 0;
|
2015-04-03 00:11:36 +02:00
|
|
|
}
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2018-04-13 21:24:43 +02:00
|
|
|
public function deleteOldVotesByPollId($poll_id, $votesToDelete) {
|
|
|
|
$prepared = $this->prepare('DELETE FROM `' . Utils::table('vote') . '` WHERE poll_id = ? ORDER BY `poll_id` ASC LIMIT ' . $votesToDelete);
|
2015-04-03 00:11:36 +02:00
|
|
|
|
2018-04-13 21:24:43 +02:00
|
|
|
return $prepared->execute([$poll_id]);
|
|
|
|
}
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2015-04-03 00:11:36 +02:00
|
|
|
/**
|
|
|
|
* Delete all votes of a given poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the given poll.
|
2018-04-18 16:16:22 +02:00
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
2015-04-03 00:11:36 +02:00
|
|
|
* @return bool|null true if action succeeded.
|
|
|
|
*/
|
2018-04-18 16:16:22 +02:00
|
|
|
public function deleteByPollId($poll_id)
|
|
|
|
{
|
|
|
|
return $this->connect->delete(Utils::table('vote'), ['poll_id' => $poll_id]) > 0;
|
2015-04-03 00:11:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all votes made on given moment index.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @param $index int The index of the vote into the poll
|
2018-04-18 16:16:22 +02:00
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
2015-04-03 00:11:36 +02:00
|
|
|
* @return bool|null true if action succeeded.
|
|
|
|
*/
|
2018-04-18 16:16:22 +02:00
|
|
|
public function deleteByIndex($poll_id, $index)
|
|
|
|
{
|
|
|
|
$prepared = $this->prepare('UPDATE ' . Utils::table('vote') . ' SET choices = CONCAT(SUBSTR(choices, 1, ?), SUBSTR(choices, ?)) WHERE poll_id = ?');
|
2015-04-03 00:11:36 +02:00
|
|
|
|
|
|
|
return $prepared->execute([$index, $index + 2, $poll_id]);
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
/**
|
|
|
|
* @param $poll_id
|
|
|
|
* @param $vote_id
|
|
|
|
* @param $name
|
|
|
|
* @param $choices
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function update($poll_id, $vote_id, $name, $choices, $mail)
|
|
|
|
{
|
|
|
|
return $this->connect->update(Utils::table('vote'), [
|
|
|
|
'choices' => $choices,
|
|
|
|
'name' => $name,
|
|
|
|
'mail' => $mail,
|
|
|
|
], [
|
|
|
|
'poll_id' => $poll_id,
|
|
|
|
'id' => $vote_id,
|
|
|
|
]) > 0;
|
2015-04-03 00:11:36 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 00:29:10 +02:00
|
|
|
/**
|
|
|
|
* Check if name is already used for the given poll.
|
|
|
|
*
|
|
|
|
* @param int $poll_id ID of the poll
|
|
|
|
* @param string $name Name of the vote
|
2018-04-18 16:16:22 +02:00
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
2015-04-24 00:29:10 +02:00
|
|
|
* @return bool true if vote already exists
|
|
|
|
*/
|
|
|
|
public function existsByPollIdAndName($poll_id, $name) {
|
2018-04-18 16:16:22 +02:00
|
|
|
$prepared = $this->prepare('SELECT 1 FROM ' . Utils::table('vote') . ' WHERE poll_id = ? AND name = ?');
|
2018-02-19 00:18:43 +01:00
|
|
|
$prepared->execute([$poll_id, $name]);
|
2015-04-24 00:29:10 +02:00
|
|
|
return $prepared->rowCount() > 0;
|
|
|
|
}
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2018-04-06 14:00:20 +02:00
|
|
|
/**
|
|
|
|
* Check if name is already used for the given poll and another vote.
|
|
|
|
*
|
|
|
|
* @param int $poll_id ID of the poll
|
|
|
|
* @param string $name Name of the vote
|
|
|
|
* @param int $vote_id ID of the current vote
|
2018-04-18 16:16:22 +02:00
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
2018-04-06 14:00:20 +02:00
|
|
|
* @return bool true if vote already exists
|
|
|
|
*/
|
|
|
|
public function existsByPollIdAndNameAndVoteId($poll_id, $name, $vote_id) {
|
2018-04-18 16:16:22 +02:00
|
|
|
$prepared = $this->prepare('SELECT 1 FROM ' . Utils::table('vote') . ' WHERE poll_id = ? AND name = ? AND id != ?');
|
2018-04-06 14:00:20 +02:00
|
|
|
$prepared->execute([$poll_id, $name, $vote_id]);
|
|
|
|
return $prepared->rowCount() > 0;
|
|
|
|
}
|
2015-04-03 00:11:36 +02:00
|
|
|
}
|
2018-04-06 14:04:07 +02:00
|
|
|
|