/** * Load titles that have been rated by other users which are similar to this. * * @param SearchObject_Solr|SearchObject_Base $db * @param UserRating $ratedTitle * @param integer $userId * @param array $ratedTitles * @param array $suggestions * @param integer[] $notInterestedTitles * @return int The number of suggestions for this title */ static function getSimilarlyRatedTitles($db, $ratedTitle, $userId, $ratedTitles, &$suggestions, $notInterestedTitles) { $numRecommendations = 0; //If there is no ISBN, can we come up with an alternative algorithm? //Possibly using common ratings with other patrons? //Get a list of other patrons that have rated this title and that like it as much or more than the active user.. $otherRaters = new UserRating(); //Query the database to get items that other users who rated this liked. $sqlStatement = "SELECT resourceid, record_id, " . " sum(case rating when 5 then 10 when 4 then 6 end) as rating " . " FROM `user_rating` inner join resource on resource.id = user_rating.resourceid WHERE userId in " . " (select userId from user_rating where resourceId = " . $ratedTitle->resourceid . " and rating >= 4 " . " and userid != " . $userId . ") " . " and rating >= 4 " . " and resourceId != " . $ratedTitle->resourceid . " and deleted = 0 " . " group by resourceid order by rating desc limit 10"; //Sort so the highest titles are on top and limit to 10 suggestions. $otherRaters->query($sqlStatement); if ($otherRaters->N > 0) { //Other users have also rated this title. while ($otherRaters->fetch()) { //Process the title disableErrorHandler(); if (!($ownedRecord = $db->getRecord($otherRaters->record_id))) { //Old record which has been removed? Ignore for purposes of suggestions. continue; } enableErrorHandler(); //get the title from the Solr Index if (isset($ownedRecord['isbn'])) { if (strpos($ownedRecord['isbn'][0], ' ') > 0) { $isbnInfo = explode(' ', $ownedRecord['isbn'][0]); $isbn = $isbnInfo[0]; } else { $isbn = $ownedRecord['isbn'][0]; } $isbn13 = strlen($isbn) == 13 ? $isbn : ISBNConverter::convertISBN10to13($isbn); $isbn10 = strlen($isbn) == 10 ? $isbn : ISBNConverter::convertISBN13to10($isbn); } else { $isbn13 = ''; $isbn10 = ''; } //See if we can get the series title from the record if (isset($ownedRecord['series'])) { $series = $ownedRecord['series'][0]; } else { $series = ''; } $similarTitle = array('title' => $ownedRecord['title'], 'title_short' => $ownedRecord['title_short'], 'author' => isset($ownedRecord['author']) ? $ownedRecord['author'] : '', 'publicationDate' => $ownedRecord['publishDate'], 'isbn' => $isbn13, 'isbn10' => $isbn10, 'upc' => isset($ownedRecord['upc']) ? $ownedRecord['upc'][0] : '', 'recordId' => $ownedRecord['id'], 'id' => $ownedRecord['id'], 'libraryOwned' => true, 'isCurrent' => false, 'shortId' => substr($ownedRecord['id'], 1), 'format_category' => isset($ownedRecord['format_category']) ? $ownedRecord['format_category'] : '', 'format' => $ownedRecord['format'], 'recordtype' => $ownedRecord['recordtype'], 'series' => $series, 'grouping_term' => $ownedRecord['grouping_term']); $numRecommendations++; Suggestions::addTitleToSuggestions($ratedTitle, $similarTitle['title'], $similarTitle['recordId'], $similarTitle, $ratedTitles, $suggestions, $notInterestedTitles); } } return $numRecommendations; }