'lu_CurrentRating', 'vote_title' => 'lu_VoteTitle', 'vote_count' => 'lu_VoteCount', 'invalid_rating' => 'lu_InvalidRating', 'already_voted' => 'lu_AlreadyVoted', 'thanks_for_voting' => 'lu_ThanksForVoting', ); /** * Draws rating bar for a given category item * * @param kDBItem $object * @param bool $show_div * @param string $additional_msg * @param string $additional_style * @return string * @access public */ public function ratingBar(&$object, $show_div = true, $additional_msg = '', $additional_style = '') { $perm_prefix = $this->Application->getUnitOption($object->Prefix, 'PermItemPrefix'); $static = !$this->Application->CheckPermission($perm_prefix . '.RATE', 0, $object->GetDBField('CategoryId')); $total_votes = $object->GetDBField('CachedVotesQty'); $total_rating = $object->GetDBField('CachedRating') * $total_votes; $spam_helper =& $this->Application->recallObject('SpamHelper'); /* @var $spam_helper SpamHelper */ $config_mapping = $this->Application->getUnitOption($object->Prefix, 'ConfigMapping'); $review_settings = $config_mapping['RatingDelayValue'] . ':' . $config_mapping['RatingDelayInterval']; $spam_helper->InitHelper($object->GetDBField('ResourceId'), 'Rating', $review_settings, $object->GetCol('ResourceId')); $user_voted = $spam_helper->InSpamControl(); // now draw the rating bar $unit_selected_width = $additional_style ? $this->ratingSmallUnitWidth : $this->ratingUnitWidth; $rating_width = $total_votes ? @number_format($total_rating / $total_votes, 2) * $unit_selected_width : 0; $rating1 = $total_votes ? @number_format($total_rating / $total_votes, 1) : 0; $rating2 = $total_votes ? @number_format($total_rating / $total_votes, 2) : 0; $rater = ' '; // this part is disabled for now, will be addressed once properly review // $rater .= '

' . $this->_replaceInPhrase('vote_title', Array ('' . $rating1 . '', $this->ratingMaximal)) . ' (' . $this->_replaceInPhrase('vote_count', Array ($total_votes)) . ')

'; $rater .= ' ' . $additional_msg . ''; if ( $show_div ) { // adds div around rating stars (when drawing rating first time) $rater = '
' . $rater . '
'; } return $rater; } /** * Saves user's vote, when allowed * * @param kDBItem $object * @return string */ function makeVote(&$object) { $spam_helper =& $this->Application->recallObject('SpamHelper'); /* @var $spam_helper SpamHelper */ $config_mapping = $this->Application->getUnitOption($object->Prefix, 'ConfigMapping'); $review_settings = $config_mapping['RatingDelayValue'].':'.$config_mapping['RatingDelayInterval']; $spam_helper->InitHelper($object->GetDBField('ResourceId'), 'Rating', $review_settings, $object->GetCol('ResourceId')); if (!$object->isLoaded() || $spam_helper->InSpamControl()) { return '@err:' . $this->_replaceInPhrase('already_voted'); } $perm_prefix = $this->Application->getUnitOption($object->Prefix, 'PermItemPrefix'); $can_rate = $this->Application->CheckPermission($perm_prefix . '.RATE', 0, $object->GetDBField('CategoryId')); $rating = (int)$this->Application->GetVar('rating'); // not numeric rating is from GoogleBot :( $additional_style = $this->Application->GetVar('size'); if (($rating <= 0) || ($rating > $this->ratingMaximal) || !$can_rate) { return '@err:' . $this->_replaceInPhrase('invalid_rating'); } // save current rating $fields_hash = Array ( 'ItemId' => $object->GetID(), 'RatingValue' => $rating, 'IPAddress' => $_SERVER['REMOTE_ADDR'], 'CreatedOn' => adodb_mktime(), ); $this->Conn->doInsert($fields_hash, TABLE_PREFIX.'ItemRating'); // recalculate average rating $votes_count = $object->GetDBField('CachedVotesQty'); $avg_rating = $object->GetDBField('CachedRating'); $avg_rating = round((($votes_count * $avg_rating) + $rating) / ($votes_count + 1), 2); $object->SetDBField('CachedRating', "$avg_rating"); $object->Update(); $sql = 'UPDATE '.$object->TableName.' SET CachedVotesQty = CachedVotesQty + 1 WHERE '.$object->IDField.' = '.$object->GetID(); $this->Conn->Query($sql); $object->SetDBField('CachedVotesQty', $object->GetDBField('CachedVotesQty') + 1); // for using in template // prevent user from voting too quickly $spam_helper->AddToSpamControl(); return $this->ratingBar($object, false, '' . $this->_replaceInPhrase('thanks_for_voting') . '', $additional_style); } /*function purgeVotes() { $expired = adodb_mktime() - 86400 * $this->Application->ConfigValue('Timeout_Rating'); // 3600 $sql = 'DELETE FROM ' . TABLE_PREFIX . 'ItemRating WHERE CreatedOn < ' . $expired; $this->Conn->Query($sql); }*/ /** * Performs sprintf on phrase translation using given variables * * @param string $phrase * @param Array $arguments * @return string */ function _replaceInPhrase($phrase, $arguments = Array ()) { $value = $this->Application->Phrase($this->_phrases[$phrase]); if ($arguments) { return vsprintf($value, $arguments); } return $value; } }