2015-04-03 00:11:36 +02:00
|
|
|
<?php
|
|
|
|
namespace Framadate\Repositories;
|
|
|
|
|
|
|
|
use Framadate\FramaDB;
|
|
|
|
use Framadate\Utils;
|
|
|
|
|
|
|
|
class VoteRepository extends AbstractRepository {
|
2021-12-20 17:46:50 +01:00
|
|
|
/**
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function allUserVotesByPollId(string $poll_id) {
|
2015-04-03 00:11:36 +02:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function insertDefault(string $poll_id, int $insert_position): bool
|
|
|
|
{
|
2016-03-13 16:20:38 +01:00
|
|
|
$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]);
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function insert(string $poll_id, string $name, string $choices, string $token): \stdClass {
|
2015-04-07 23:17:26 +02:00
|
|
|
$prepared = $this->prepare('INSERT INTO `' . Utils::table('vote') . '` (poll_id, name, choices, uniqId) VALUES (?,?,?,?)');
|
|
|
|
$prepared->execute([$poll_id, $name, $choices, $token]);
|
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;
|
2015-04-03 00:11:36 +02:00
|
|
|
|
|
|
|
return $newVote;
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function deleteById(string $poll_id, int $vote_id): bool
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
$prepared = $this->prepare('DELETE FROM `' . Utils::table('vote') . '` WHERE poll_id = ? AND id = ?');
|
|
|
|
|
|
|
|
return $prepared->execute([$poll_id, $vote_id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all votes of a given poll.
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $poll_id The ID of the given poll.
|
2015-04-03 00:11:36 +02:00
|
|
|
* @return bool|null true if action succeeded.
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function deleteByPollId(string $poll_id): ?bool
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
$prepared = $this->prepare('DELETE FROM `' . Utils::table('vote') . '` WHERE poll_id = ?');
|
|
|
|
|
|
|
|
return $prepared->execute([$poll_id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all votes made on given moment index.
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $poll_id The ID of the poll
|
2015-04-03 00:11:36 +02:00
|
|
|
* @param $index int The index of the vote into the poll
|
|
|
|
* @return bool|null true if action succeeded.
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function deleteByIndex(string $poll_id, int $index): ?bool
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
$prepared = $this->prepare('UPDATE `' . Utils::table('vote') . '` SET choices = CONCAT(SUBSTR(choices, 1, ?), SUBSTR(choices, ?)) WHERE poll_id = ?');
|
|
|
|
|
|
|
|
return $prepared->execute([$index, $index + 2, $poll_id]);
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function update(string $poll_id, string $vote_id, string $name, $choices): bool
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
$prepared = $this->prepare('UPDATE `' . Utils::table('vote') . '` SET choices = ?, name = ? WHERE poll_id = ? AND id = ?');
|
|
|
|
|
|
|
|
return $prepared->execute([$choices, $name, $poll_id, $vote_id]);
|
|
|
|
}
|
|
|
|
|
2015-04-24 00:29:10 +02:00
|
|
|
/**
|
|
|
|
* Check if name is already used for the given poll.
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $poll_id ID of the poll
|
2015-04-24 00:29:10 +02:00
|
|
|
* @param string $name Name of the vote
|
|
|
|
* @return bool true if vote already exists
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function existsByPollIdAndName(string $poll_id, string $name): bool
|
|
|
|
{
|
2015-04-24 00:29:10 +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;
|
|
|
|
}
|
2021-12-20 17:46:50 +01:00
|
|
|
|
2018-04-06 14:00:20 +02:00
|
|
|
/**
|
|
|
|
* Check if name is already used for the given poll and another vote.
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $poll_id ID of the poll
|
2018-04-06 14:00:20 +02:00
|
|
|
* @param string $name Name of the vote
|
|
|
|
* @param int $vote_id ID of the current vote
|
|
|
|
* @return bool true if vote already exists
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function existsByPollIdAndNameAndVoteId(string $poll_id, string $name, int $vote_id): bool
|
|
|
|
{
|
2018-04-06 14:00:20 +02:00
|
|
|
$prepared = $this->prepare('SELECT 1 FROM `' . Utils::table('vote') . '` WHERE poll_id = ? AND name = ? AND id != ?');
|
|
|
|
$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
|
|
|
|