Keyword

How to show ratings in k2 related items?

  • oshyne
  • oshyne's Avatar Topic Author
  • Offline
  • New Member
More
6 years 7 months ago #163870 by oshyne
So far, with the code below i am able to rate an item under the related items in the item view but my challenge is that, the
yellow rating stars and the number of times an item has been rated doesn't show.

Please, i need your assistance and thanks in advance.
<div class="itemRatingBlock">
		<div class="itemRatingForm">
			<ul class="itemRatingList">
				<li class="itemCurrentRating" id="itemCurrentRating<?php echo $item->id; ?>" style="width:<?php echo $item->votingPercentage; ?>%;"></li>
				<li><a href="#" data-id="<?php echo $item->id; ?>" title="<?php echo JText::_('K2_1_STAR_OUT_OF_5'); ?>" class="one-star">1</a></li>
				<li><a href="#" data-id="<?php echo $item->id; ?>" title="<?php echo JText::_('K2_2_STARS_OUT_OF_5'); ?>" class="two-stars">2</a></li>
				<li><a href="#" data-id="<?php echo $item->id; ?>" title="<?php echo JText::_('K2_3_STARS_OUT_OF_5'); ?>" class="three-stars">3</a></li>
				<li><a href="#" data-id="<?php echo $item->id; ?>" title="<?php echo JText::_('K2_4_STARS_OUT_OF_5'); ?>" class="four-stars">4</a></li>
				<li><a href="#" data-id="<?php echo $item->id; ?>" title="<?php echo JText::_('K2_5_STARS_OUT_OF_5'); ?>" class="five-stars">5</a></li>
			</ul>
			<div id="itemRatingLog<?php echo $item->id; ?>" class="itemRatingLog"><?php echo $item->numOfvotes; ?></div>
			<div class="clr"></div>
		</div>
		<div class="clr"></div>
	</div>

Please Log in or Create an account to join the conversation.

More
6 years 7 months ago - 6 years 7 months ago #163872 by esedic
Replied by esedic on topic How to show ratings in k2 related items?
I took a quick look and it seems that item rating information is not there for related items in item view.
You could query that information directly in item.php template file, something like this:
After this line
<?php foreach($this->relatedItems as $key=>$item): ?>
add this:
<?php 
	$db = JFactory::getDbo();
	$query = "SELECT * FROM #__k2_rating WHERE itemID = ".$item->id;
	$db->setQuery($query);
	try
	{
		$db->execute();
		$vote = $db->loadObject();
		if (!is_null($vote) && $vote->rating_count != 0) {
			$votingPercentage = number_format(intval($vote->rating_sum) / intval($vote->rating_count), 2) * 20;
		}
		else {
			$votingPercentage = 0;
		}
	}
	catch (RuntimeException $e)
	{
		JFactory::getApplication()->enqueueMessage($e->getMessage());
	}
?>

Then you have to change this line
<li class="itemCurrentRating" id="itemCurrentRating<?php echo $item->id; ?>" style="width:<?php echo $item->votingPercentage; ?>%;"></li>
to this
<li class="itemCurrentRating" id="itemCurrentRating<?php echo $item->id; ?>" style="width:<?php echo $votingPercentage; ?>%;"></li>

Use template override and test it.
This is a quick solution and it's not in line with MVC pattern (database queries do not belong in views).
Last edit: 6 years 7 months ago by esedic.

Please Log in or Create an account to join the conversation.

  • oshyne
  • oshyne's Avatar Topic Author
  • Offline
  • New Member
More
6 years 7 months ago #163876 by oshyne
Replied by oshyne on topic How to show ratings in k2 related items?
Thanks esedic,

So far, the rating stars now display but the votes or rating count don't.

Thanks in advance.

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
6 years 7 months ago #163906 by Krikor Boghossian
Replied by Krikor Boghossian on topic How to show ratings in k2 related items?
The rating won't really work since the function itself is bound to the current item.

You will need to write a custom JS function and remove all the duplicate ids.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • oshyne
  • oshyne's Avatar Topic Author
  • Offline
  • New Member
More
6 years 7 months ago #163908 by oshyne
Replied by oshyne on topic How to show ratings in k2 related items?
To recap:

From @esedic assistance, i can now rate a related item and the rating stars will
be displayed respectively but what remains is the vote count to display alongside the ratings.

Please, i need assistance on how to get the votes count to display alongside the ratings in the related items.

@Krikor has suggested that i need a custom JS function to achieve that.

Please, if you can help with the custom JS function to achieve that or using any other method, i highly appreciate.

Thanks

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
6 years 7 months ago #163972 by Krikor Boghossian
Replied by Krikor Boghossian on topic How to show ratings in k2 related items?
The function is pretty agnostic github.com/getk2/k2/blob/master/media/k2/assets/js/k2.frontend.js#L168-L200

You need make sure that a) the .itemRatingForm is present and b) the rel attribute prints the correct ID.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum