Index: branches/5.2.x/core/units/reviews/reviews_tag_processor.php =================================================================== diff -u -N -r14769 -r14778 --- branches/5.2.x/core/units/reviews/reviews_tag_processor.php (.../reviews_tag_processor.php) (revision 14769) +++ branches/5.2.x/core/units/reviews/reviews_tag_processor.php (.../reviews_tag_processor.php) (revision 14778) @@ -1,6 +1,6 @@ GetRecordsCount(false) != $object->GetRecordsCount() ? $object->GetRecordsCount().' / '.$object->GetRecordsCount(false) : $object->GetRecordsCount(); } + + /** + * Dynamic votes indicator + * + * @param Array $params + * + * @return string + */ + function VotesIndicator($params) + { + $object =& $this->getObject($params); + /* @var $object kDBItem */ + + $rating_helper =& $this->Application->recallObject('RatingHelper'); + /* @var $rating_helper RatingHelper */ + + $rating = isset($params['rating']) ? $params['rating'] : $object->GetDBField('Rating'); + $small_style = array_key_exists('small_style', $params) ? $params['small_style'] : false; + + return $rating_helper->ratingBarSimple($rating, '', null, $small_style); + } } \ No newline at end of file Index: branches/5.2.x/core/units/helpers/rating_helper.php =================================================================== diff -u -N -r14628 -r14778 --- branches/5.2.x/core/units/helpers/rating_helper.php (.../rating_helper.php) (revision 14628) +++ branches/5.2.x/core/units/helpers/rating_helper.php (.../rating_helper.php) (revision 14778) @@ -1,6 +1,6 @@ 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')); - + // 2. user isn't voting too frequently + $spam_helper =& $this->_getSpamHelper($object); $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 = ' - '; + $rater = $this->ratingBarSimple($this->getAverageRating($object), $voting_js, $msg_info, $additional_style); - // 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 . '
'; @@ -111,20 +89,91 @@ } /** + * Returns average rating + * + * @param kDBItem $object + * @return float|int + */ + function getAverageRating(&$object) + { + $total_votes = $object->GetDBField('CachedVotesQty'); + $total_rating = $object->GetDBField('CachedRating') * $total_votes; + + return $total_votes ? $total_rating / $total_votes : 0; + } + + /** + * Draws rating bar for a given category item + * + * @param float $average_rating + * @param string $voting_js + * @param Array $msg_info + * @param string $additional_style + * @return string + * @access public + */ + public function ratingBarSimple($average_rating, $voting_js = '', $msg_info = null, $additional_style = '') + { + if ( !isset($msg_info) || !is_array($msg_info) ) { + $msg_info = Array ('text' => '', 'class' => Array ()); + } + + $unit_selected_width = $additional_style ? $this->ratingSmallUnitWidth : $this->ratingUnitWidth; + $rating_width = $average_rating ? @number_format($average_rating, 2) * $unit_selected_width : 0; + + $rating2 = $average_rating ? @number_format($average_rating, 2) : 0; + $current_rating_text = $this->_replaceInPhrase('current_rating', Array ('' . $rating2 . '', $this->ratingMaximal)); + + $rater = ' + + '; + + // this part is disabled for now, will be addressed once properly review + /*$rating1 = $average_rating ? @number_format($average_rating, 1) : 0; + $rater .= '

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

';*/ + + if ( $voting_js ) { + $rater .= ' ' . $msg_info['text'] . ''; + } + else { + // adds div around rating stars (when drawing rating first time) + $rater = '
' . $rater . '
'; + } + + return $rater; + } + + /** + * Returns control, used to vote on a given $object + * + * @param kDBItem $object + * @param string $additional_style + * @return string + */ + function getVotingControl(&$object, $additional_style = '') + { + $ret = ''; + + for ($i = 1; $i <= $this->ratingMaximal; $i++) { + $ret .= '
  • ' . $i . '
  • ' . "\n"; + } + + return $ret; + } + + /** * Saves user's vote, when allowed * * @param kDBItem $object * @return string */ function makeVote(&$object) { - $spam_helper =& $this->Application->recallObject('SpamHelper'); - /* @var $spam_helper SpamHelper */ + $spam_helper =& $this->_getSpamHelper($object); - $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'); } @@ -194,4 +243,24 @@ return $value; } + + /** + * Returns SpamHelper object linked to given object + * + * @param kDBItem $object + * @return SpamHelper + * @access protected + */ + protected function &_getSpamHelper(&$object) + { + $spam_helper =& $this->Application->recallObject('SpamHelper'); + /* @var $spam_helper SpamHelper */ + + // 2. user isn't voting too frequently + $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')); + + return $spam_helper; + } } \ No newline at end of file