From cae5f7cb7cc5bfb6b40fd9bec2dcea5bd624c00a Mon Sep 17 00:00:00 2001 From: Gilles Crettenand Date: Fri, 19 Jun 2015 13:32:22 +0200 Subject: [PATCH] add a specific method to speed up Dashboard display --- Lib/db/User.php | 22 ++++++++++++++++------ NetBiblio.php | 26 +++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/Lib/db/User.php b/Lib/db/User.php index f2bdfc1..2e8c2bc 100644 --- a/Lib/db/User.php +++ b/Lib/db/User.php @@ -139,12 +139,16 @@ class User extends DbMapping return Connection::execute($sql)->to_array(); } - private function _getLoans($table, $sort = "ItemNr ASC") + private function _getLoans($table, $count, $sort) { $circulations = $this->getLoansData($table, $sort); // getting the intval of the NoticeNr will remove any 'V' or 'T' and thus we will have no issues with // the virtual books that are used for Downloads and so. $codes = array_unique(array_map(function($c) { return intval(trim($c['NoticeNr'])); }, $circulations)); + if($count) { + return count($circulations); + } + $books = count($codes) > 0 ? BookSearch::GetBooks($codes) : array(); foreach($circulations as $c) { @@ -160,20 +164,22 @@ class User extends DbMapping /** * Loans (Circulations) as needed on the website + * @param boolean $count return only the count * @return array */ - public function GetLoans() + public function GetLoans($count = false) { - return $this->_getLoans('Circulations'); + return $this->_getLoans('Circulations', $count, "ItemNr ASC"); } /** * Old loans (OldCirculations) as needed on the website + * @param boolean $count return only the count * @return array */ - public function GetOldLoans() + public function GetOldLoans($count = false) { - return $this->_getLoans('OldCirculations', 'CheckOutDate DESC'); + return $this->_getLoans('OldCirculations', $count, 'CheckOutDate DESC'); } /** @@ -227,10 +233,11 @@ class User extends DbMapping /** * Wishes are all the books that this user want to read. + * @param boolean $count return only the count * @param int $limit * @return array */ - public function getWishes($limit = 50) + public function getWishes($count = false, $limit = 50) { $sql = sprintf("SELECT TOP $limit NoticeID @@ -240,6 +247,9 @@ class User extends DbMapping $result = Connection::execute($sql); $ids = array_map(function($r) { return $r['NoticeID']; }, $result->to_array()); + if($count) { + return count($ids); + } return BookSearch::GetBooks($ids, 'id'); } diff --git a/NetBiblio.php b/NetBiblio.php index 69946e5..a25fa76 100644 --- a/NetBiblio.php +++ b/NetBiblio.php @@ -13,7 +13,7 @@ use BSR\Lib\WebService; class NetBiblio extends WebService { /** @var string $version version number */ - public static $version = '1.1.3'; + public static $version = '1.1.4'; private $login = ''; private $client = 'website'; @@ -473,6 +473,30 @@ class NetBiblio extends WebService return array_values($this->AddBookData($circulations)); } + /** + * This method return information for the user dashboard : + * + * ° 'loans' : number of current loans + * ° 'oldLoans' : number of past loans + * ° 'wishes' : number of wishes + * ° 'novelties' : new books + * ° 'recommendations' : recommended books + * + * @return array + * @throws AuthenticationException + */ + public function GetDashboardData() + { + $user = $this->getUser(); + return array( + 'loans' => $user->GetLoans(true), + 'oldLoans' => $user->GetOldLoans(true), + 'wishes' => $user->getWishes(true), + 'novelties' => $this->LastBooksByType('', 15), + 'recommendations' => $this->MoreLikeLoans(), + ); + } + /** * This method adds a book to the currently authenticated user wishlist * based on its code.