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'); }
/** * Load titles that have been rated by other users which are similar to this. * * @param WorkAPI $workApi * @param SearchObject_Solr|SearchObject_Base $db * @param UserWorkReview $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($workApi, $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 UserWorkReview(); //Query the database to get items that other users who rated this liked. $sqlStatement = "SELECT groupedRecordPermanentId, " . " sum(case rating when 5 then 10 when 4 then 6 end) as rating " . " FROM `user_work_review` WHERE userId in " . " (select userId from user_work_review where groupedRecordPermanentId = " . $ratedTitle->groupedRecordPermanentId . " and rating >= 4 " . " and userid != " . $userId . ") " . " and rating >= 4 " . " and groupedRecordPermanentId != " . $ratedTitle->groupedRecordPermanentId . " 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->groupedRecordPermanentId))) { //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); $numRecommendations++; Suggestions::addTitleToSuggestions($ratedTitle, $similarTitle['title'], $similarTitle['recordId'], $similarTitle, $ratedTitles, $suggestions, $notInterestedTitles); } } return $numRecommendations; }
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; }