function getRatingData($permanentId = null) { if (is_null($permanentId) && isset($_REQUEST['id'])) { $permanentId = $_REQUEST['id']; } global $user; //Set default rating data $ratingData = array('average' => 0, 'count' => 0, 'user' => 0, 'num1star' => 0, 'num2star' => 0, 'num3star' => 0, 'num4star' => 0, 'num5star' => 0); //Somehow we didn't get an id (work no longer exists in the index) if (is_null($permanentId)) { return $ratingData; } require_once ROOT_DIR . '/sys/LocalEnrichment/UserWorkReview.php'; $reviewData = new UserWorkReview(); $reviewData->groupedRecordPermanentId = $permanentId; $reviewData->find(); $totalRating = 0; while ($reviewData->fetch()) { if ($reviewData->rating > 0) { $totalRating += $reviewData->rating; $ratingData['count']++; if ($user && $reviewData->userId == $user->id) { $ratingData['user'] = $reviewData->rating; } if ($reviewData->rating == 1) { $ratingData['num1star']++; } elseif ($reviewData->rating == 2) { $ratingData['num2star']++; } elseif ($reviewData->rating == 3) { $ratingData['num3star']++; } elseif ($reviewData->rating == 4) { $ratingData['num4star']++; } elseif ($reviewData->rating == 5) { $ratingData['num5star']++; } } } if ($ratingData['count'] > 0) { $ratingData['average'] = $totalRating / $ratingData['count']; $ratingData['barWidth5Star'] = 100 * $ratingData['num5star'] / $ratingData['count']; $ratingData['barWidth4Star'] = 100 * $ratingData['num4star'] / $ratingData['count']; $ratingData['barWidth3Star'] = 100 * $ratingData['num3star'] / $ratingData['count']; $ratingData['barWidth2Star'] = 100 * $ratingData['num2star'] / $ratingData['count']; $ratingData['barWidth1Star'] = 100 * $ratingData['num1star'] / $ratingData['count']; } else { $ratingData['barWidth5Star'] = 0; $ratingData['barWidth4Star'] = 0; $ratingData['barWidth3Star'] = 0; $ratingData['barWidth2Star'] = 0; $ratingData['barWidth1Star'] = 0; } return $ratingData; }
public function launch() { global $interface; global $user; //Load user ratings require_once ROOT_DIR . '/sys/LocalEnrichment/UserWorkReview.php'; require_once ROOT_DIR . '/sys/Grouping/GroupedWork.php'; require_once ROOT_DIR . '/RecordDrivers/GroupedWorkDriver.php'; $rating = new UserWorkReview(); $rating->userId = $user->id; $rating->find(); $ratings = array(); while ($rating->fetch()) { $groupedWorkDriver = new GroupedWorkDriver($rating->groupedRecordPermanentId); if ($groupedWorkDriver->isValid) { $ratings[] = array('id' => $rating->id, 'groupedWorkId' => $rating->groupedRecordPermanentId, 'title' => $groupedWorkDriver->getTitle(), 'author' => $groupedWorkDriver->getPrimaryAuthor(), 'rating' => $rating->rating, 'review' => $rating->review, 'link' => $groupedWorkDriver->getLinkUrl(), 'dateRated' => $rating->dateRated, 'ratingData' => $groupedWorkDriver->getRatingData()); } } asort($ratings); //Load titles the user is not interested in $notInterested = array(); require_once ROOT_DIR . '/sys/LocalEnrichment/NotInterested.php'; $notInterestedObj = new NotInterested(); $notInterestedObj->userId = $user->id; $notInterestedObj->find(); while ($notInterestedObj->fetch()) { $groupedWorkId = $notInterestedObj->groupedRecordPermanentId; $groupedWorkDriver = new GroupedWorkDriver($groupedWorkId); if ($groupedWorkDriver->isValid) { $notInterested[] = array('id' => $notInterestedObj->id, 'title' => $groupedWorkDriver->getTitle(), 'author' => $groupedWorkDriver->getPrimaryAuthor(), 'dateMarked' => $notInterestedObj->dateMarked, 'link' => $groupedWorkDriver->getLinkUrl()); } } $interface->assign('ratings', $ratings); $interface->assign('notInterested', $notInterested); $interface->assign('showNotInterested', false); $interface->setPageTitle('My Ratings'); $interface->assign('sidebar', 'MyAccount/account-sidebar.tpl'); $interface->setTemplate('myRatings.tpl'); $interface->display('layout.tpl'); }
static function getSuggestions($userId = -1) { global $configArray; //Configuration for suggestions $doNovelistRecommendations = true; $numTitlesToLoadNovelistRecommendationsFor = 10; $doMetadataRecommendations = true; $doSimilarlyRatedRecommendations = false; $maxRecommendations = 30; if ($userId == -1) { global $user; $userId = $user->id; } //Load all titles the user is not interested in $notInterestedTitles = array(); $notInterested = new NotInterested(); $notInterested->userId = $userId; $notInterested->find(); while ($notInterested->fetch()) { $notInterestedTitles[$notInterested->groupedRecordPermanentId] = $notInterested->groupedRecordPermanentId; } //Load all titles the user has rated $allRatedTitles = array(); $allLikedRatedTitles = array(); require_once ROOT_DIR . '/sys/LocalEnrichment/UserWorkReview.php'; $ratings = new UserWorkReview(); $ratings->userId = $userId; $ratings->find(); while ($ratings->fetch()) { $allRatedTitles[$ratings->groupedRecordPermanentId] = $ratings->groupedRecordPermanentId; if ($ratings->rating >= 4) { $allLikedRatedTitles[] = $ratings->groupedRecordPermanentId; } } // Setup Search Engine Connection $class = $configArray['Index']['engine']; $url = $configArray['Index']['url']; $db = new $class($url); $suggestions = array(); if ($doNovelistRecommendations) { //Get a list of all titles the user has rated (3 star and above) $ratings = new UserWorkReview(); $ratings->whereAdd("userId = {$userId}", 'AND'); $ratings->whereAdd('rating >= 3', 'AND'); $ratings->orderBy('rating DESC, dateRated DESC, id DESC'); //Use just recent ratings to make real-time recommendations faster $ratings->limit(0, $numTitlesToLoadNovelistRecommendationsFor); $ratings->find(); //echo("User has rated {$ratings->N} titles<br/>"); require_once ROOT_DIR . '/services/API/WorkAPI.php'; $workApi = new WorkAPI(); if ($ratings->N > 0) { while ($ratings->fetch()) { $groupedWorkId = $ratings->groupedRecordPermanentId; //echo("Found resource for $resourceId - {$resource->title}<br/>"); $ratedTitles[$ratings->groupedRecordPermanentId] = clone $ratings; $isbns = $workApi->getIsbnsForWork($groupedWorkId); Suggestions::getNovelistRecommendations($ratings, $groupedWorkId, $isbns, $allRatedTitles, $suggestions, $notInterestedTitles); /*if (count($suggestions) >= $maxRecommendations){ break; }*/ } } } if ($doSimilarlyRatedRecommendations && count($suggestions) < $maxRecommendations) { //Get a list of all titles the user has rated (3 star and above) $ratings = new UserWorkReview(); $ratings->whereAdd("userId = {$userId}", 'AND'); $ratings->whereAdd('rating >= 3', 'AND'); $ratings->orderBy('rating DESC, dateRated DESC, id DESC'); //Use just recent ratings to make real-time recommendations faster $ratings->limit(0, $numTitlesToLoadNovelistRecommendationsFor); $ratings->find(); //echo("User has rated {$ratings->N} titles<br/>"); require_once ROOT_DIR . '/services/API/WorkAPI.php'; $workApi = new WorkAPI(); if ($ratings->N > 0) { while ($ratings->fetch()) { Suggestions::getSimilarlyRatedTitles($workApi, $db, $ratings, $userId, $allRatedTitles, $suggestions, $notInterestedTitles); } } } //Get metadata recommendations if enabled, we have ratings, and we don't have enough suggestions yet if ($doMetadataRecommendations && count($allLikedRatedTitles) > 0 && count($suggestions) < $maxRecommendations) { //Get recommendations based on everything I've rated using more like this functionality $class = $configArray['Index']['engine']; $url = $configArray['Index']['url']; /** @var Solr $db */ $db = new $class($url); //$db->debug = true; $moreLikeTheseSuggestions = $db->getMoreLikeThese($allLikedRatedTitles, $notInterestedTitles); foreach ($moreLikeTheseSuggestions['response']['docs'] as $suggestion) { if (!array_key_exists($suggestion['id'], $allRatedTitles) && !array_key_exists($suggestion['id'], $notInterestedTitles)) { $suggestions[$suggestion['id']] = array('rating' => $suggestion['rating'] - 2.5, 'titleInfo' => $suggestion, 'basedOn' => 'MetaData for all titles rated'); } if (count($suggestions) == $maxRecommendations) { break; } } } //sort suggestions based on score from ascending to descending uasort($suggestions, 'Suggestions::compareSuggestions'); //Only return up to $maxRecommendations suggestions to make the page size reasonable $suggestions = array_slice($suggestions, 0, $maxRecommendations, true); //Return suggestions for use in the user interface. return $suggestions; }
function saveReview() { $result = array(); global $user; if ($user === false) { $result['result'] = false; $result['message'] = 'Please login before adding a review.'; } else { require_once ROOT_DIR . '/sys/LocalEnrichment/UserWorkReview.php'; $result['result'] = true; $id = $_REQUEST['id']; $rating = $_REQUEST['rating']; $comment = $_REQUEST['comment']; $groupedWorkReview = new UserWorkReview(); $groupedWorkReview->userId = $user->id; $groupedWorkReview->groupedRecordPermanentId = $id; $newReview = true; if ($groupedWorkReview->find(true)) { $newReview = false; } $result['newReview'] = $newReview; $groupedWorkReview->rating = $rating; $groupedWorkReview->review = $comment; if ($newReview) { $groupedWorkReview->dateRated = time(); $groupedWorkReview->insert(); } else { $groupedWorkReview->update(); } $result['reviewId'] = $groupedWorkReview->id; global $interface; $interface->assign('review', $groupedWorkReview); $result['reviewHtml'] = $interface->fetch('GroupedWork/view-user-review.tpl'); } return json_encode($result); }
public function getUserReviews() { $reviews = array(); require_once ROOT_DIR . '/sys/LocalEnrichment/UserWorkReview.php'; $userReview = new UserWorkReview(); $userReview->groupedRecordPermanentId = $this->getUniqueID(); $joinUser = new User(); $userReview->joinAdd($joinUser); $userReview->find(); while ($userReview->fetch()) { if (!$userReview->displayName) { if (strlen(trim($userReview->firstname)) >= 1) { $userReview->displayName = substr($userReview->firstname, 0, 1) . '. ' . $userReview->lastname; } else { $userReview->displayName = $userReview->lastname; } } //TODO: Clean the review text $reviews[] = clone $userReview; } return $reviews; }