Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Thu, Feb 6, 1:37 PM

in-portal

Index: branches/5.2.x/core/units/helpers/rating_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/rating_helper.php (revision 14777)
+++ branches/5.2.x/core/units/helpers/rating_helper.php (revision 14778)
@@ -1,197 +1,266 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class RatingHelper extends kHelper {
/**
* One star width/height in pixels
*
* @var int
*/
var $ratingUnitWidth = 25;
- var $ratingSmallUnitWidth = 20;
+ var $ratingSmallUnitWidth = 10; //20;
/**
* Maximal star count
*
* @var int
*/
var $ratingMaximal = 5;
var $_phrases = Array (
'current_rating' => '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 = '')
{
+ // 1. user is allowed to vote by permissions
$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'));
-
+ // 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 = '<span class="inline-rating">
- <ul class="star-rating '.$additional_style.'" style="width: ' . $unit_selected_width * $this->ratingMaximal . 'px;">
- <li class="current-rating" style="width: ' . $rating_width . 'px;">' . $this->_replaceInPhrase('current_rating', Array ('<strong>' . $rating2 . '</strong>', $this->ratingMaximal)) . '</li>'."\n";
-
if ( !$static && !$user_voted ) {
// allow to set rating when not static and user not voted before
- for ($ncount = 1; $ncount <= $this->ratingMaximal; $ncount++) {
- $rater .= '<li><a href="#vote-' . $ncount . '" onclick="aRatingManager.makeVote(' . $ncount . ', \'' . $object->Prefix . '\', ' . $object->GetID() . ', \'' . $additional_style . '\'); return false;" title="' . $this->_replaceInPhrase('vote_title', Array ($ncount, $this->ratingMaximal)) . '" class="r' . $ncount . '-unit rater" rel="nofollow">' . $ncount . '</a></li>' . "\n";
- }
+ $voting_js = $this->getVotingControl($object, $additional_style);
+ }
+ else {
+ $voting_js = '';
}
- $msg_class = Array ();
+ $msg_info = Array ('text' => $additional_msg, 'class' => Array ());
if ( $static ) {
- $msg_class[] = 'static';
+ $msg_info['class'][] = 'static';
}
if ( $user_voted ) {
- $msg_class[] = 'voted';
+ $msg_info['class'][] = 'voted';
}
- $rater .= ' </ul></span>';
-
- // this part is disabled for now, will be addressed once properly review
-// $rater .= ' <p class="' . implode(' ', $msg_class) . '">' .
- $this->_replaceInPhrase('vote_title', Array ('<strong>' . $rating1 . '</strong>', $this->ratingMaximal)) . ' (' . $this->_replaceInPhrase('vote_count', Array ($total_votes)) . ') </p>';
-
- $rater .= '&nbsp;<span class="' . implode(' ', $msg_class) . '">' . $additional_msg . '</span>';
+ $rater = $this->ratingBarSimple($this->getAverageRating($object), $voting_js, $msg_info, $additional_style);
if ( $show_div ) {
// adds div around rating stars (when drawing rating first time)
$rater = '<div class="inline-rating" id="page_rating_' . $object->GetID() . '">' . $rater . '</div>';
}
return $rater;
}
/**
+ * 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 ('<strong>' . $rating2 . '</strong>', $this->ratingMaximal));
+
+ $rater = ' <span class="inline-rating">
+ <ul class="star-rating ' . $additional_style . '" style="width: ' . $unit_selected_width * $this->ratingMaximal . 'px;">
+ <li class="current-rating" style="width: ' . $rating_width . 'px;">' . $current_rating_text . '</li>' . "\n" .
+ $voting_js . '
+ </ul>
+ </span>';
+
+ // this part is disabled for now, will be addressed once properly review
+ /*$rating1 = $average_rating ? @number_format($average_rating, 1) : 0;
+ $rater .= ' <p class="' . implode(' ', $msg_info['class']) . '">' . $this->_replaceInPhrase('vote_title', Array ('<strong>' . $rating1 . '</strong>', $this->ratingMaximal)) . ' (' . $this->_replaceInPhrase('vote_count', Array ($total_votes)) . ') </p>';*/
+
+ if ( $voting_js ) {
+ $rater .= '&nbsp;<span class="' . implode(' ', $msg_info['class']) . '">' . $msg_info['text'] . '</span>';
+ }
+ else {
+ // adds div around rating stars (when drawing rating first time)
+ $rater = '<div class="inline-rating">' . $rater . '</div>';
+ }
+
+ 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 .= '<li><a href="#vote-' . $i . '" onclick="aRatingManager.makeVote(' . $i . ', \'' . $object->Prefix . '\', ' . $object->GetID() . ', \'' . $additional_style . '\'); return false;" title="' . $this->_replaceInPhrase('vote_title', Array ($i, $this->ratingMaximal)) . '" class="r' . $i . '-unit rater" rel="nofollow">' . $i . '</a></li>' . "\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 */
-
- $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'));
+ $spam_helper =& $this->_getSpamHelper($object);
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, '<span class="thanks">' . $this->_replaceInPhrase('thanks_for_voting') . '</span>', $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;
}
+
+ /**
+ * 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
Index: branches/5.2.x/core/units/reviews/reviews_tag_processor.php
===================================================================
--- branches/5.2.x/core/units/reviews/reviews_tag_processor.php (revision 14777)
+++ branches/5.2.x/core/units/reviews/reviews_tag_processor.php (revision 14778)
@@ -1,220 +1,241 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class ReviewsTagProcessor extends kDBTagProcessor
{
/**
* Returns a link for editing product
*
* @param Array $params
* @return string
*/
function ItemEditLink($params)
{
$object =& $this->getObject();
/* @var $object kDBList */
$item_prefix = $this->Application->findModule('Name', $object->GetDBField('Module'), 'Var');
$edit_template = $this->Application->getUnitOption($item_prefix, 'AdminTemplatePath') . '/' . $this->Application->getUnitOption($item_prefix, 'AdminTemplatePrefix') . 'edit';
$url_params = Array (
'm_opener' => 'd',
$item_prefix.'_mode' => 't',
$item_prefix.'_event' => 'OnEdit',
$item_prefix.'_id' => $object->GetDBField('CatalogItemId'),
'm_cat_id' => $object->GetDBField('CatalogItemCategory'),
'pass' => 'all,'.$item_prefix,
'no_pass_through' => 1,
);
return $this->Application->HREF($edit_template,'', $url_params);
}
function HelpfulLink($params)
{
$object =& $this->getObject($params);
/* @var $object kDBItem */
$parent_prefix = $this->Application->getUnitOption($this->Prefix, 'ParentPrefix');
$params['events[' . $parent_prefix . ']'] = 'OnReviewHelpful';
$params['review_id'] = $object->GetID();
return $this->Application->ProcessParsedTag($parent_prefix, 'ItemLink', $params);
}
/**
* Prints overall rating statistics
*
* @param Array $params
* @return string
*/
protected function PrintRatingPercents($params)
{
static $cache = null;
$object =& $this->getObject($params);
/* @var $object kDBItem */
$parent_prefix = $this->Application->getUnitOption($this->Prefix, 'ParentPrefix');
$main_object =& $this->Application->recallObject($parent_prefix);
/* @var $main_object kCatDBItem */
if ( !isset($cache) ) {
$sql = 'SELECT COUNT(*), Rating
FROM ' . $object->TableName . '
WHERE ItemId = ' . $main_object->GetDBField('ResourceId') . '
GROUP BY Rating';
$cache = $this->Conn->GetCol($sql, 'Rating');
}
$ratings = array_reverse( array_keys( $object->GetFieldOption('Rating', 'options') ) );
if ( !isset($params['show_none']) || !$params['show_none'] ) {
$none_index = array_search(0, $ratings);
if ( $none_index !== false ) {
unset($ratings[$none_index]);
}
}
$ret = '';
$total = array_sum($cache);
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $params['render_as'];
$block_params['strip_nl'] = 2;
foreach ($ratings as $rating) {
$block_params['rating'] = $rating;
$block_params['count'] = isset($cache[$rating]) ? $cache[$rating] : 0;
$block_params['percent'] = round(($block_params['count'] / $total) * 100);
$ret .= $this->Application->ParseBlock($block_params);
}
return $ret;
}
/**
* Returns requested field value
*
* @param Array $params
* @return string
* @access public
*/
function Field($params)
{
$field = $this->SelectParam($params, 'name,field');
$object =& $this->getObject($params);
/* @var $object kDBItem */
if ($field == 'ReviewText') {
if ($object->GetDBField('TextFormat') == 1) {
$params['no_special'] = 'no_special';
}
else {
unset($params['no_special']);
}
}
return parent::Field($params);
}
function AlreadyReviewed($params)
{
$parent_prefix = $this->Application->getUnitOption($this->Prefix, 'ParentPrefix');
$main_object =& $this->Application->recallObject($parent_prefix);
/* @var $main_object kCatDBItem */
$spam_helper =& $this->Application->recallObject('SpamHelper');
/* @var $spam_helper SpamHelper */
$spam_helper->InitHelper($main_object->GetDBField('ResourceId'), 'Review', 0, $main_object->GetCol('ResourceId'));
return $spam_helper->InSpamControl();
}
function HasError($params)
{
$object =& $this->Application->recallObject($this->getPrefixSpecial(), $this->Prefix, $params);
if (method_exists($object, 'GetErrorMsg')) {
return parent::HasError($params);
}
else {
return 0;
}
}
/**
* Preserve main item id in subitem pagination url
*
* @param Array $params
* @return string
*/
function PageLink($params)
{
$object =& $this->getObject($params);
/* @var kDBList */
$parent_info = $object->getLinkedInfo();
if ($parent_info['ParentId'] > 0) {
$params['pass'] = 'm,'.$this->getPrefixSpecial().','.$parent_info['ParentPrefix'];
}
return parent::PageLink($params);
}
function InitCatalogTab($params)
{
$tab_params['mode'] = $this->Application->GetVar('tm'); // single/multi selection possible
$tab_params['special'] = $this->Application->GetVar('ts'); // use special for this tab
$tab_params['dependant'] = $this->Application->GetVar('td'); // is grid dependant on categories grid
// set default params (same as in catalog)
if ($tab_params['mode'] === false) $tab_params['mode'] = 'multi';
if ($tab_params['special'] === false) $tab_params['special'] = '';
if ($tab_params['dependant'] === false) $tab_params['dependant'] = 'yes';
// pass params to block with tab content
$params['name'] = $params['render_as'];
$params['prefix'] = trim($this->Prefix.'.'.($tab_params['special'] ? $tab_params['special'] : $this->Special), '.');
$params['cat_prefix'] = trim('c.'.($tab_params['special'] ? $tab_params['special'] : $this->Special), '.');
$params['tab_mode'] = $tab_params['mode'];
$params['grid_name'] = ($tab_params['mode'] == 'multi') ? $params['default_grid'] : $params['radio_grid'];
$params['tab_dependant'] = $tab_params['dependant'];
$params['show_category'] = $tab_params['special'] == 'showall' ? 1 : 0; // this is advanced view -> show category name
$params['tab_name'] = $this->Application->GetVar('tab_name');
return $this->Application->ParseBlock($params, 1);
}
/**
* Returns reviews count for each item type (in "Reviews" section)
*
* @param Array $params
* @return string
*/
function CatalogItemCount($params)
{
$params['skip_quering'] = true;
$object =& $this->GetList($params);
return $object->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

Event Timeline