2014-12-03 21:08: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-03 21:08:08 +01:00
|
|
|
namespace Framadate;
|
|
|
|
|
2014-12-21 23:48:22 +01:00
|
|
|
class FramaDB {
|
2014-12-03 21:08:08 +01:00
|
|
|
/**
|
|
|
|
* PDO Object, connection to database.
|
|
|
|
*/
|
|
|
|
private $pdo = null;
|
|
|
|
|
2014-12-21 23:48:22 +01:00
|
|
|
function __construct($connection_string, $user, $password) {
|
2014-12-03 21:08:08 +01:00
|
|
|
$this->pdo = new \PDO($connection_string, $user, $password);
|
2014-12-05 01:08:38 +01:00
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ);
|
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
2014-12-03 21:08:08 +01:00
|
|
|
}
|
|
|
|
|
2014-12-21 23:48:22 +01:00
|
|
|
function areTablesCreated() {
|
|
|
|
$result = $this->pdo->query('SHOW TABLES');
|
2014-12-03 21:08:08 +01:00
|
|
|
$schemas = $result->fetchAll(\PDO::FETCH_COLUMN);
|
2014-12-22 14:18:33 +01:00
|
|
|
return 0 != count(array_diff($schemas, ['comments', 'sondage', 'sujet_studs', 'user_studs']));
|
2014-12-03 21:08:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function prepare($sql) {
|
|
|
|
return $this->pdo->prepare($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
function beginTransaction() {
|
|
|
|
$this->pdo->beginTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
function commit() {
|
|
|
|
$this->pdo->commit();
|
|
|
|
}
|
|
|
|
|
2014-12-23 00:30:05 +01:00
|
|
|
function rollback() {
|
|
|
|
$this->pdo->rollback();
|
|
|
|
}
|
|
|
|
|
|
|
|
function errorCode() {
|
|
|
|
return $this->pdo->errorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
function errorInfo() {
|
|
|
|
return $this->pdo->errorInfo();
|
|
|
|
}
|
|
|
|
|
2014-12-03 21:08:08 +01:00
|
|
|
function query($sql) {
|
|
|
|
return $this->pdo->query($sql);
|
|
|
|
}
|
2014-12-07 23:12:08 +01:00
|
|
|
|
2014-12-16 00:02:01 +01:00
|
|
|
function findPollById($poll_id) {
|
2014-12-17 13:17:08 +01:00
|
|
|
$prepared = $this->prepare('SELECT * FROM sondage WHERE sondage.poll_id = ?');
|
2014-12-07 23:12:08 +01:00
|
|
|
|
2014-12-17 13:17:08 +01:00
|
|
|
$prepared->execute([$poll_id]);
|
|
|
|
$poll = $prepared->fetch();
|
|
|
|
$prepared->closeCursor();
|
2014-12-07 23:12:08 +01:00
|
|
|
|
2014-12-17 13:17:08 +01:00
|
|
|
return $poll;
|
2014-12-07 23:12:08 +01:00
|
|
|
}
|
|
|
|
|
2014-12-18 13:57:25 +01:00
|
|
|
function updatePoll($poll) {
|
2014-12-23 00:30:05 +01:00
|
|
|
$prepared = $this->prepare('UPDATE sondage SET title=?, admin_mail=?, comment=?, active=?, editable=? WHERE poll_id = ?');
|
2014-12-18 13:57:25 +01:00
|
|
|
|
|
|
|
return $prepared->execute([$poll->title, $poll->admin_mail, $poll->comment, $poll->active, $poll->editable, $poll->poll_id]);
|
|
|
|
}
|
|
|
|
|
2014-12-07 23:12:08 +01:00
|
|
|
function allCommentsByPollId($poll_id) {
|
|
|
|
$prepared = $this->prepare('SELECT * FROM comments WHERE id_sondage = ? ORDER BY id_comment');
|
|
|
|
$prepared->execute(array($poll_id));
|
|
|
|
return $prepared->fetchAll();
|
|
|
|
}
|
|
|
|
|
2014-12-12 13:43:43 +01:00
|
|
|
function allUserVotesByPollId($poll_id) {
|
2014-12-07 23:12:08 +01:00
|
|
|
$prepared = $this->prepare('SELECT * FROM user_studs WHERE id_sondage = ? ORDER BY id_users');
|
|
|
|
$prepared->execute(array($poll_id));
|
|
|
|
return $prepared->fetchAll();
|
|
|
|
}
|
|
|
|
|
2014-12-12 13:43:43 +01:00
|
|
|
function allSlotsByPollId($poll_id) {
|
2014-12-07 23:12:08 +01:00
|
|
|
$prepared = $this->prepare('SELECT * FROM sujet_studs WHERE id_sondage = ? ORDER BY sujet');
|
2014-12-07 16:47:35 +01:00
|
|
|
$prepared->execute(array($poll_id));
|
|
|
|
return $prepared->fetchAll();
|
|
|
|
}
|
2014-12-03 21:08:08 +01:00
|
|
|
|
2014-12-23 00:30:05 +01:00
|
|
|
function insertDefaultVote($poll_id, $insert_position) {
|
|
|
|
$prepared = $this->prepare('UPDATE user_studs SET reponses = CONCAT(SUBSTRING(reponses, 1, ?), "0", SUBSTRING(reponses, ?)) WHERE id_sondage = ?');
|
|
|
|
return $prepared->execute([$insert_position, $insert_position + 1, $poll_id]);
|
|
|
|
}
|
|
|
|
|
2014-12-17 13:17:08 +01:00
|
|
|
function insertVote($poll_id, $name, $choices) {
|
|
|
|
$prepared = $this->prepare('INSERT INTO user_studs (id_sondage,nom,reponses) VALUES (?,?,?)');
|
|
|
|
$prepared->execute([$poll_id, $name, $choices]);
|
2014-12-07 23:12:08 +01:00
|
|
|
|
|
|
|
$newVote = new \stdClass();
|
|
|
|
$newVote->id_sondage = $poll_id;
|
|
|
|
$newVote->id_users = $this->pdo->lastInsertId();
|
|
|
|
$newVote->nom = $name;
|
2014-12-17 13:17:08 +01:00
|
|
|
$newVote->reponse = $choices;
|
2014-12-07 23:12:08 +01:00
|
|
|
|
|
|
|
return $newVote;
|
|
|
|
}
|
|
|
|
|
2014-12-21 00:25:00 +01:00
|
|
|
function deleteVote($poll_id, $vote_id) {
|
|
|
|
$prepared = $this->prepare('DELETE FROM user_studs WHERE id_sondage = ? AND id_users = ?');
|
|
|
|
return $prepared->execute([$poll_id, $vote_id]);
|
|
|
|
}
|
|
|
|
|
2014-12-19 00:36:09 +01:00
|
|
|
/**
|
|
|
|
* Delete all votes of a given poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the given poll.
|
|
|
|
* @return bool|null true if action succeeded.
|
|
|
|
*/
|
|
|
|
function deleteVotesByAdminPollId($poll_id) {
|
|
|
|
$prepared = $this->prepare('DELETE FROM user_studs WHERE id_sondage = ?');
|
|
|
|
return $prepared->execute([$poll_id]);
|
|
|
|
}
|
2014-12-19 00:27:30 +01:00
|
|
|
|
2014-12-21 23:48:22 +01: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
|
|
|
|
* @return bool|null true if action succeeded.
|
|
|
|
*/
|
|
|
|
function deleteVotesByIndex($poll_id, $index) {
|
|
|
|
$prepared = $this->prepare('UPDATE user_studs SET reponses = CONCAT(SUBSTR(reponses, 1, ?), SUBSTR(reponses, ?)) WHERE id_sondage = ?');
|
|
|
|
return $prepared->execute([$index, $index + 2, $poll_id]);
|
|
|
|
}
|
|
|
|
|
2014-12-22 14:18:33 +01:00
|
|
|
/**
|
|
|
|
* Find the slot into poll for a given datetime.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @param $datetime int The datetime of the slot
|
|
|
|
* @return mixed Object The slot found, or null
|
|
|
|
*/
|
|
|
|
function findSlotByPollIdAndDatetime($poll_id, $datetime) {
|
|
|
|
$prepared = $this->prepare('SELECT * FROM sujet_studs WHERE id_sondage = ? AND SUBSTRING_INDEX(sujet, \'@\', 1) = ?');
|
|
|
|
|
|
|
|
$prepared->execute([$poll_id, $datetime]);
|
|
|
|
$slot = $prepared->fetch();
|
|
|
|
$prepared->closeCursor();
|
|
|
|
|
|
|
|
return $slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new slot into a given poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @param $slot mixed The value of the slot
|
|
|
|
* @return bool true if action succeeded
|
|
|
|
*/
|
|
|
|
function insertSlot($poll_id, $slot) {
|
|
|
|
$prepared = $this->prepare('INSERT INTO sujet_studs (id_sondage, sujet) VALUES (?,?)');
|
|
|
|
return $prepared->execute([$poll_id, $slot]);
|
|
|
|
}
|
|
|
|
|
2014-12-21 23:48:22 +01:00
|
|
|
/**
|
|
|
|
* Update a slot into a poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @param $datetime int The datetime of the slot to update
|
|
|
|
* @param $newValue mixed The new value of the entire slot
|
|
|
|
* @return bool|null true if action succeeded.
|
|
|
|
*/
|
|
|
|
function updateSlot($poll_id, $datetime, $newValue) {
|
|
|
|
$prepared = $this->prepare('UPDATE sujet_studs SET sujet = ? WHERE id_sondage = ? AND SUBSTRING_INDEX(sujet, \'@\', 1) = ?');
|
|
|
|
return $prepared->execute([$newValue, $poll_id, $datetime]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a entire slot from a poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @param $datetime mixed The datetime of the slot
|
|
|
|
*/
|
|
|
|
function deleteSlot($poll_id, $datetime) {
|
|
|
|
$prepared = $this->prepare('DELETE FROM sujet_studs WHERE id_sondage = ? AND SUBSTRING_INDEX(sujet, \'@\', 1) = ?');
|
|
|
|
$prepared->execute([$poll_id, $datetime]);
|
|
|
|
}
|
|
|
|
|
2014-12-19 00:36:09 +01:00
|
|
|
/**
|
|
|
|
* Delete all comments of a given poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the given poll.
|
|
|
|
* @return bool|null true if action succeeded.
|
|
|
|
*/
|
2014-12-21 00:25:00 +01:00
|
|
|
function deleteCommentsByAdminPollId($poll_id) {
|
2014-12-19 00:36:09 +01:00
|
|
|
$prepared = $this->prepare('DELETE FROM comments WHERE id_sondage = ?');
|
|
|
|
return $prepared->execute([$poll_id]);
|
2014-12-19 00:27:30 +01:00
|
|
|
}
|
|
|
|
|
2014-12-16 00:45:16 +01:00
|
|
|
function updateVote($poll_id, $vote_id, $choices) {
|
|
|
|
$prepared = $this->prepare('UPDATE user_studs SET reponses = ? WHERE id_sondage = ? AND id_users = ?');
|
|
|
|
return $prepared->execute([$choices, $poll_id, $vote_id]);
|
|
|
|
}
|
|
|
|
|
2014-12-17 13:47:14 +01:00
|
|
|
function insertComment($poll_id, $name, $comment) {
|
|
|
|
$prepared = $this->prepare('INSERT INTO comments (id_sondage, usercomment, comment) VALUES (?,?,?)');
|
|
|
|
return $prepared->execute([$poll_id, $name, $comment]);
|
|
|
|
}
|
|
|
|
|
2014-12-19 00:13:21 +01:00
|
|
|
function deleteComment($poll_id, $comment_id) {
|
|
|
|
$prepared = $this->prepare('DELETE FROM comments WHERE id_sondage = ? AND id_comment = ?');
|
|
|
|
return $prepared->execute([$poll_id, $comment_id]);
|
|
|
|
}
|
|
|
|
|
2014-12-03 21:08:08 +01:00
|
|
|
}
|