function saveSearch($phrase, $type, $numResults) { if (!isset($numResults)) { //This only happens if there is an error parsing the query. return; } $activeLibrary = Library::getActiveLibrary(); $libraryId = -1; if ($activeLibrary != null && $activeLibrary->useScope) { $libraryId = $activeLibrary->libraryId; } /** @var $locationSingleton Location */ global $locationSingleton; $locationId = -1; $activeLocation = $locationSingleton->getActiveLocation(); if ($activeLocation != null && $activeLocation->useScope) { $locationId = $activeLocation->locationId; } $searchStat = new SearchStat(); $searchStat->phrase = strtolower($phrase); $searchStat->type = $type; $searchStat->libraryId = $libraryId; $searchStat->locationId = $locationId; $searchStat->find(); $isNew = true; if ($searchStat->N > 0) { $searchStat->fetch(); $searchStat->numSearches++; $isNew = false; } else { $searchStat->numSearches = 1; } $searchStat->numResults = $numResults; $searchStat->lastSearch = time(); if ($isNew) { $searchStat->insert(); } else { $searchStat->update(); } }
/** * Retreive the top 20 search terms by popularity from the search_stats table * Enter description here ... */ function getTopSearches() { require_once ROOT_DIR . '/Drivers/marmot_inc/SearchStat.php'; $numSearchesToReturn = isset($_REQUEST['numResults']) ? $_REQUEST['numResults'] : 20; $searchStats = new SearchStat(); $searchStats->query("SELECT phrase, sum(numSearches) as numTotalSearches FROM `search_stats` where phrase != '' group by phrase order by numTotalSearches DESC LIMIT " . $numSearchesToReturn); $searches = array(); while ($searchStats->fetch()) { $searches[] = $searchStats->phrase; } return $searches; }