diff --git a/Lib/Search/BookSearch.php b/Lib/Search/BookSearch.php index 082235b..9b70e45 100644 --- a/Lib/Search/BookSearch.php +++ b/Lib/Search/BookSearch.php @@ -64,6 +64,22 @@ class BookSearch $this->query->addSortField($field, $order); } + public function addFacetField($field) + { + $this->query->addFacetField($field); + } + + public function setFacetLimits($limit = null, $count = null) + { + if(! is_null($limit)) { + $this->query->setFacetLimit($limit); + } + + if(! is_null($count)) { + $this->query->setFacetMinCount($count); + } + } + /** * @param int $start * @param int $count diff --git a/Lib/db/DbHelper.php b/Lib/db/DbHelper.php index 6c15e43..2207e0b 100644 --- a/Lib/db/DbHelper.php +++ b/Lib/db/DbHelper.php @@ -4,33 +4,6 @@ namespace BSR\Lib\db; class DbHelper { - /** - * Retrieve the list of all readers (volunteers) having read at least 4 books (2 notices per book). - * Returns an associative array containing $lastname and $firstname - */ - public static function ListOfReaders() - { - $sql = "SELECT - count(*), - ContentShortPart AS name - FROM NoticeFields - WHERE Tag=901 - GROUP BY ContentShortPart - HAVING count(*) > 6 - ORDER BY SUBSTRING(ContentShortPart, CHARINDEX(' ', ContentShortPart)+1, 15);"; - - $results = Connection::execute($sql); - return array_map(function($row) { - $fullname = str_replace("*", "", $row['name']); - $parts = explode(" ", $fullname); - $firstname = array_shift($parts); - $lastname = implode(" ", $parts); - return array( - 'lastname' => $lastname, - 'firstname' => $firstname); - }, $results->to_array()); - } - /** * Retrieve the list of all type available in the database. * @param boolean $withJeunesse add 'Jeunesse' to the list diff --git a/NetBiblio.php b/NetBiblio.php index 46c5e54..9c4b306 100644 --- a/NetBiblio.php +++ b/NetBiblio.php @@ -662,7 +662,30 @@ class NetBiblio extends WebService */ public function ListOfReaders() { - return DBHelper::ListOfReaders(); + $s = new BookSearch(); + $s->addQuery(1, 'visible'); + $s->addFacetField('reader'); + $s->setFacetLimits(2000, 10); + $results = $s->getResults(0, 0); + + $readers = array(); + foreach($results['facets']['facets']['reader'] as $name => $count) { + $parts = explode(" ", $name); + $firstname = array_shift($parts); + $lastname = implode(" ", $parts); + + $fullname = trim($lastname.' '.$firstname); + $readers[$fullname] = array( + 'lastname' => $lastname, + 'firstname' => $firstname, + 'count' => $count, + ); + } + + // sort readers by lastname + ksort($readers); + + return array_values($readers); } /**