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 ;
2015-01-18 16:14:45 +01:00
use PDO ;
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-31 01:33:56 +01:00
/**
* @ return \PDO Connection to database
*/
function getPDO () {
return $this -> pdo ;
}
/**
* Find all tables in database .
*
* @ return array The array of table names
*/
function allTables () {
2014-12-21 23:48:22 +01:00
$result = $this -> pdo -> query ( 'SHOW TABLES' );
2014-12-03 21:08:08 +01:00
$schemas = $result -> fetchAll ( \PDO :: FETCH_COLUMN );
2014-12-29 21:54:07 +01:00
2014-12-31 01:33:56 +01:00
return $schemas ;
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 ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'SELECT * FROM `' . Utils :: table ( 'poll' ) . '` WHERE 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 ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'UPDATE `' . Utils :: table ( 'poll' ) . '` SET title=?, admin_name=?, admin_mail=?, description=?, end_date=FROM_UNIXTIME(?), active=?, editable=? WHERE id = ?' );
2014-12-18 13:57:25 +01:00
2015-01-08 22:03:44 +01:00
return $prepared -> execute ([ $poll -> title , $poll -> admin_name , $poll -> admin_mail , $poll -> description , $poll -> end_date , $poll -> active , $poll -> editable , $poll -> id ]);
2014-12-18 13:57:25 +01:00
}
2014-12-07 23:12:08 +01:00
function allCommentsByPollId ( $poll_id ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'SELECT * FROM `' . Utils :: table ( 'comment' ) . '` WHERE poll_id = ? ORDER BY id' );
2014-12-07 23:12:08 +01:00
$prepared -> execute ( array ( $poll_id ));
2014-12-29 21:54:07 +01:00
2014-12-07 23:12:08 +01:00
return $prepared -> fetchAll ();
}
2014-12-12 13:43:43 +01:00
function allUserVotesByPollId ( $poll_id ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'SELECT * FROM `' . Utils :: table ( 'vote' ) . '` WHERE poll_id = ? ORDER BY id' );
2014-12-07 23:12:08 +01:00
$prepared -> execute ( array ( $poll_id ));
2014-12-29 21:54:07 +01:00
2014-12-07 23:12:08 +01:00
return $prepared -> fetchAll ();
}
2014-12-12 13:43:43 +01:00
function allSlotsByPollId ( $poll_id ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'SELECT * FROM `' . Utils :: table ( 'slot' ) . '` WHERE poll_id = ? ORDER BY title' );
2014-12-07 16:47:35 +01:00
$prepared -> execute ( array ( $poll_id ));
2014-12-29 21:54:07 +01:00
2014-12-07 16:47:35 +01:00
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 ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'UPDATE `' . Utils :: table ( 'vote' ) . '` SET choices = CONCAT(SUBSTRING(choices, 1, ?), "0", SUBSTRING(choices, ?)) WHERE poll_id = ?' );
2014-12-29 21:54:07 +01:00
2014-12-23 00:30:05 +01:00
return $prepared -> execute ([ $insert_position , $insert_position + 1 , $poll_id ]);
}
2014-12-17 13:17:08 +01:00
function insertVote ( $poll_id , $name , $choices ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'INSERT INTO `' . Utils :: table ( 'vote' ) . '` (poll_id, name, choices) VALUES (?,?,?)' );
2014-12-17 13:17:08 +01:00
$prepared -> execute ([ $poll_id , $name , $choices ]);
2014-12-07 23:12:08 +01:00
$newVote = new \stdClass ();
2014-12-30 01:41:25 +01:00
$newVote -> poll_id = $poll_id ;
$newVote -> id = $this -> pdo -> lastInsertId ();
$newVote -> name = $name ;
$newVote -> choices = $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 ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'DELETE FROM `' . Utils :: table ( 'vote' ) . '` WHERE poll_id = ? AND id = ?' );
2014-12-29 21:54:07 +01:00
2014-12-21 00:25:00 +01:00
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 .
*/
2014-12-23 09:48:58 +01:00
function deleteVotesByPollId ( $poll_id ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'DELETE FROM `' . Utils :: table ( 'vote' ) . '` WHERE poll_id = ?' );
2014-12-29 21:54:07 +01:00
2014-12-19 00:36:09 +01:00
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 ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'UPDATE `' . Utils :: table ( 'vote' ) . '` SET choices = CONCAT(SUBSTR(choices, 1, ?), SUBSTR(choices, ?)) WHERE poll_id = ?' );
2014-12-29 21:54:07 +01:00
2014-12-21 23:48:22 +01:00
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 ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'SELECT * FROM `' . Utils :: table ( 'slot' ) . '` WHERE poll_id = ? AND SUBSTRING_INDEX(title, \'@\', 1) = ?' );
2014-12-22 14:18:33 +01:00
$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
2014-12-30 01:41:25 +01:00
* @ param $title mixed The title of the slot
* @ param $moments mixed | null The moments joined with " , "
2014-12-22 14:18:33 +01:00
* @ return bool true if action succeeded
*/
2014-12-30 01:41:25 +01:00
function insertSlot ( $poll_id , $title , $moments ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'INSERT INTO `' . Utils :: table ( 'slot' ) . '` (poll_id, title, moments) VALUES (?,?,?)' );
2014-12-29 21:54:07 +01:00
2014-12-30 01:41:25 +01:00
return $prepared -> execute ([ $poll_id , $title , $moments ]);
2014-12-22 14:18:33 +01:00
}
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
2014-12-30 01:41:25 +01:00
* @ param $newMoments mixed The new moments
2014-12-21 23:48:22 +01:00
* @ return bool | null true if action succeeded .
*/
2014-12-30 01:41:25 +01:00
function updateSlot ( $poll_id , $datetime , $newMoments ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'UPDATE `' . Utils :: table ( 'slot' ) . '` SET moments = ? WHERE poll_id = ? AND title = ?' );
2014-12-29 21:54:07 +01:00
2014-12-30 01:41:25 +01:00
return $prepared -> execute ([ $newMoments , $poll_id , $datetime ]);
2014-12-21 23:48:22 +01:00
}
/**
* 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 ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'DELETE FROM `' . Utils :: table ( 'slot' ) . '` WHERE poll_id = ? AND title = ?' );
2014-12-21 23:48:22 +01:00
$prepared -> execute ([ $poll_id , $datetime ]);
}
2014-12-23 09:48:58 +01:00
function deleteSlotsByPollId ( $poll_id ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'DELETE FROM `' . Utils :: table ( 'slot' ) . '` WHERE poll_id = ?' );
2014-12-30 01:41:25 +01:00
2014-12-29 21:54:07 +01:00
return $prepared -> execute ([ $poll_id ]);
2014-12-23 09:48:58 +01:00
}
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-23 09:48:58 +01:00
function deleteCommentsByPollId ( $poll_id ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'DELETE FROM `' . Utils :: table ( 'comment' ) . '` WHERE poll_id = ?' );
2014-12-29 21:54:07 +01:00
2014-12-19 00:36:09 +01:00
return $prepared -> execute ([ $poll_id ]);
2014-12-19 00:27:30 +01:00
}
2015-01-07 23:29:46 +01:00
function updateVote ( $poll_id , $vote_id , $name , $choices ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'UPDATE `' . Utils :: table ( 'vote' ) . '` SET choices = ?, name = ? WHERE poll_id = ? AND id = ?' );
2014-12-29 21:54:07 +01:00
2015-01-07 23:29:46 +01:00
return $prepared -> execute ([ $choices , $name , $poll_id , $vote_id ]);
2014-12-16 00:45:16 +01:00
}
2014-12-17 13:47:14 +01:00
function insertComment ( $poll_id , $name , $comment ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'INSERT INTO `' . Utils :: table ( 'comment' ) . '` (poll_id, name, comment) VALUES (?,?,?)' );
2014-12-29 21:54:07 +01:00
2014-12-17 13:47:14 +01:00
return $prepared -> execute ([ $poll_id , $name , $comment ]);
}
2014-12-19 00:13:21 +01:00
function deleteComment ( $poll_id , $comment_id ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'DELETE FROM `' . Utils :: table ( 'comment' ) . '` WHERE poll_id = ? AND id = ?' );
2014-12-29 21:54:07 +01:00
2014-12-19 00:13:21 +01:00
return $prepared -> execute ([ $poll_id , $comment_id ]);
}
2014-12-29 21:54:07 +01:00
function deletePollById ( $poll_id ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'DELETE FROM `' . Utils :: table ( 'poll' ) . '` WHERE id = ?' );
2014-12-30 01:41:25 +01:00
2014-12-29 21:54:07 +01:00
return $prepared -> execute ([ $poll_id ]);
}
/**
* Find old polls . Limit : 20.
*
* @ return array Array of old polls
*/
public function findOldPolls () {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'SELECT * FROM `' . Utils :: table ( 'poll' ) . '` WHERE end_date < NOW() AND end_date != 0 LIMIT 20' );
2014-12-29 21:54:07 +01:00
$prepared -> execute ([]);
return $prepared -> fetchAll ();
2014-12-23 09:48:58 +01:00
}
2015-01-18 16:14:45 +01:00
/**
* @ param $start int The index of the first poll to return
* @ param $limit int The limit size
* @ return array
*/
public function findAllPolls ( $start , $limit ) {
// Polls
2015-01-18 16:30:02 +01:00
$prepared = $this -> prepare ( '
SELECT p .* ,
2015-02-26 22:29:24 +01:00
( SELECT count ( 1 ) FROM `' . Utils::table('vote') . '` v WHERE p . id = v . poll_id ) votes
2015-01-18 16:30:02 +01:00
FROM ' . Utils::table(' poll ') . ' p
ORDER BY p . title ASC
LIMIT : start , : limit ' );
2015-01-18 16:14:45 +01:00
$prepared -> bindParam ( ':start' , $start , PDO :: PARAM_INT );
$prepared -> bindParam ( ':limit' , $limit , PDO :: PARAM_INT );
$prepared -> execute ();
$polls = $prepared -> fetchAll ();
// Total count
2015-02-26 22:29:24 +01:00
$stmt = $this -> query ( 'SELECT count(1) nb FROM `' . Utils :: table ( 'poll' ) . '`' );
$count = $stmt -> fetch ();
$stmt -> closeCursor ();
2015-01-06 23:52:52 +01:00
2015-01-18 16:14:45 +01:00
return [ 'polls' => $polls , 'count' => $count -> nb ];
2015-01-06 23:52:52 +01:00
}
public function countVotesByPollId ( $poll_id ) {
2015-02-26 22:29:24 +01:00
$prepared = $this -> prepare ( 'SELECT count(1) nb FROM `' . Utils :: table ( 'vote' ) . '` WHERE poll_id = ?' );
2015-01-06 23:52:52 +01:00
$prepared -> execute ([ $poll_id ]);
$result = $prepared -> fetch ();
$prepared -> closeCursor ();
return $result -> nb ;
}
2014-12-03 21:08:08 +01:00
}