Move some code from FramaDB to Service

This commit is contained in:
Olivier PEREZ 2015-03-29 12:59:47 +02:00
parent a2ca03893a
commit 591c4dd1af
2 changed files with 20 additions and 7 deletions

View File

@ -277,12 +277,12 @@ class FramaDB {
} }
/** /**
* Search polls in databse.
*
* @param array $search Array of search : ['id'=>..., 'title'=>..., 'name'=>...] * @param array $search Array of search : ['id'=>..., 'title'=>..., 'name'=>...]
* @param $start int The index of the first poll to return * @return array The found polls
* @param $limit int The limit size
* @return array
*/ */
public function findAllPolls($search, $start, $limit) { public function findAllPolls($search) {
// Polls // Polls
$prepared = $this->prepare(' $prepared = $this->prepare('
SELECT p.*, SELECT p.*,
@ -301,14 +301,22 @@ SELECT p.*,
$prepared->bindParam(':title', $title, PDO::PARAM_STR); $prepared->bindParam(':title', $title, PDO::PARAM_STR);
$prepared->bindParam(':name', $name, PDO::PARAM_STR); $prepared->bindParam(':name', $name, PDO::PARAM_STR);
$prepared->execute(); $prepared->execute();
$polls = $prepared->fetchAll();
return $prepared->fetchAll();
}
/**
* Get the total number of polls in databse.
*
* @return int The number of polls
*/
public function countPolls() {
// Total count // Total count
$stmt = $this->query('SELECT count(1) nb FROM `' . Utils::table('poll') . '`'); $stmt = $this->query('SELECT count(1) nb FROM `' . Utils::table('poll') . '`');
$count = $stmt->fetch(); $count = $stmt->fetch();
$stmt->closeCursor(); $stmt->closeCursor();
return ['polls' => array_slice($polls,$start, $limit), 'count' => $prepared->rowCount(), 'total' => $count->nb]; return $count->nb;
} }
public function countVotesByPollId($poll_id) { public function countVotesByPollId($poll_id) {

View File

@ -25,7 +25,12 @@ class SuperAdminService {
* @return array ['polls' => The {$limit} polls, 'count' => Entries found by the query, 'total' => Total count] * @return array ['polls' => The {$limit} polls, 'count' => Entries found by the query, 'total' => Total count]
*/ */
public function findAllPolls($search, $page, $limit) { public function findAllPolls($search, $page, $limit) {
return $this->connect->findAllPolls($search, $page * $limit, $limit); $start = $page * $limit;
$polls = $this->connect->findAllPolls($search);
$total = $this->connect->countPolls();
return ['polls' => array_slice($polls, $start, $limit), 'count' => count($polls), 'total' => $total];
} }
} }