Giorgi625 VIP
Total posts: 655
26 Mar 2018 14:39

On the image below I marked two lines.

Top line in red is vote of user currently viewing record.

Bottom line in blue is total rating of record by all users who have rated.

In Cobalt it looks similar but stars also display total rating.

How to display vote (using stars) of current user viewing record he has rated and below display total rating (as text)? Exactly like it is on screenshot

I have asked two years ago and was addressed to API reference page in docs

I don't remember if I did it or not but I was not able to find in my old template files working solution for this rating view.

Also I tried to use that docs page to create what I wanted by myself but without success. There are examples where you need to type id of section, type and user. Is not it possible to have universal tempalte for rating? because I use same template in multiple sections. Also I need to display vote of currently viewing user, not a static user by ID.

Rating

Last Modified: 12 Mar 2019


Sergey
Total posts: 13,748
04 Apr 2018 06:41

try this

<?php $rating = CobaltApi::renderRating(1, 2, 'r.user_id = 10');?>
<div>
    Your rating: <?php echo $rating['html'] ?>
</div>

wheer 1 is ID of the type and 2 is ID of the section and 10 ID of the user. If you what current user.

<?php $rating = CobaltApi::renderRating(1, 2, 'r.user_id = ' . JFactory::getUser()->get('id'));?>

But in this case you have wrap all code in IF condition and check that there is a logged in user. Also after geting $ratings from API check if there is something.

Also in this case user will not be able to apply rating to that display. It will only act as display.


Giorgi625 VIP
Total posts: 655
04 Apr 2018 11:15

As you can see on image below, on top of green line is rating, shown using default code. It show average rating for current record (cuyrrentyl there is only one vote left by me).

Rating below green line is added using code you suggested. It displays average rating left by current logged in user in all records of current section. But not for current record.

How to display for current record?

Rating


Sergey
Total posts: 13,748
04 Apr 2018 11:45
<?php $rating = CobaltApi::renderRating(1, 2, 'r.user_id = 10 AND r.id = ' . $item->id);?>

Giorgi625 VIP
Total posts: 655
04 Apr 2018 12:15

Great, this is what I needed.

Thanks for help


Giorgi625 VIP
Total posts: 655
04 Apr 2018 12:44

New problem. I have checked in record where item has two votes.

My vote is 80%, another user rated record for 100%. So average rating is 90%.

In my vote it displays 45%. It should display 80%

Rating

I use this code, is it correct?

<?php $rating = CobaltApi::renderRating(4, 4, 'r.user_id = ' . JFactory::getUser()->get('id') . ' AND r.id = ' . $item->id);?>
    <div class="your-vote">
        <b>თქვენი შეფასება:</b> <?php echo $rating['html'] ?>
    </div>

Sergey
Total posts: 13,748
05 Apr 2018 04:16

Look SQL in debug console, then run this SQL against your DB and see what it will return. Will it return one record or few?


Giorgi625 VIP
Total posts: 655
07 Mar 2019 17:58

So I have returned to this task after almost a year. That time I just gave up for some reasons. So now what I have as result is shown on image below

https://i.postimg.cc/D0X1dcGy/rating.png

Rating from blue box is shown via this code

<?php if($params->get('tmpl_core.item_rating')):?>
<div class="movie-rating">
<b>ფილმის რეიტინგი:</b><?php echo $item->rating;?>
</div>
<?php endif;?>

Rating from red box is shown via this code

<?php $rating = CobaltApi::renderRating(3, 3, 'r.user_id = ' . JFactory::getUser()->get('id') . ' AND r.id = ' . $item->id);?>
<div class="movie-rating">
<b>თქვენი შეფასება:</b><?php echo $rating['html'] ?>
</div>

When I view record as guest I see only gray stars which means guest has not voted. Of course I need to hide second rating block from guests which I don't know how to do, but anyway it means that it shows correct data because guest has not rated article.

When I view same record with another user who has not rated it I also see 0% rating, thats good.

But When I view it with user who rated it for 80% it shows that rating left by that user is 40%.

Then what I did is I have rated it with 3rd users for 60% and this is result:

https://i.postimg.cc/kGWNxcrF/rating2.png

But this result is shown to second user. To user who voted last (3rd user) it shows this

https://i.postimg.cc/rwG8vtTn/rating3.jpg

So this rating in red box says that this user has not voted but modul on right with circle avatars, also in red box, show users who has rated and right aatar without image is current user who views this page. and also Top rating which displays total rating show that there are 3 users who voted.

So even when I have used getUser code only one user sees value different from 0 and also value which he sees is not correct.

By the way this is url of that page Click here to link...


Giorgi625 VIP
Total posts: 655
07 Mar 2019 18:04

Also I use plain rating if it changes anything. And I am good at design but not in php/mysql so I don't understand what to do with database from your last post.

For now I have removed from site code for second rating. IF you will be able to check let me know to add it again.


Sergey
Total posts: 13,748
08 Mar 2019 06:42

Giorgi625 Of course I need to hide second rating block from guests which I don't know how to do

<?php  if(JFactory::getUser()->get('id') > 0): ?>
    <?php $rating = CobaltApi::renderRating(3, 3, 'r.user_id = ' . JFactory::getUser()->get('id') . ' AND r.id = ' . $item->id);?>
    <?php if($rating['total'] > 0): ?>
        <div class="movie-rating">
        <b>თქვენი შეფასება:</b><?php echo $rating['html'] ?>
        </div>
    <?php endif; ?>
<?php endif; ?>

In general this r.user_id = select articles of that user not ratings.

What you meed is this. I write this example without checking I think you can get an idea and debug it.

if(JFactory::getUser()->get('id') > 0) {
    $db    = JFactory::getDbo();
    $query = $db->getQuery(TRUE);

    $query->select('r.vote');
    $query->from('#__js_res_vote AS r');
    $query->where('r.ref_id = ' . $item->id);
    $query->where('r.ref_type = "record"');
    $query->where('r.user_id = ' . JFactory::getUser()->get('id'));
    $db->setQuery($query);
    $result = $db->loadResult();

    if($result > 0) {
        $record = json_encode([
            "votes_result" => $result,
            "votes" => 1,
            "id" => $item->id,
            "user_id" => JFactory::getUser()->get('id'),
            "multirating" => []
        ]);

        echo RatingHelp::loadMultiratings($record, $this->type, $this->section, TRUE);
    }
}

Giorgi625 VIP
Total posts: 655
08 Mar 2019 13:04

Thanks it worked, I just changed last line to echo $result and got result as only number without stars so I can manipulate with it using css to display as I want. Important is that results are correct.

Now last question about ratings. This question is about default rating not for current user, but which shows all votes.

How can I change value of tag small where it shows text like this "100% rating from 1 vote" I don't want to display percentage, I want to display values from 0 to 10, and if rating is 73% for example to display 7.3

In rating template I found only solution to change tooltip text for stars.

For current user rating I simply divided $result by 10,but was not able to find solution for that text.


Sergey
Total posts: 13,748
12 Mar 2019 05:52

Int he article template

include JPATH_COMPONENT . 'myapi.php';
$rating = MyAPI::loadMultiratings($this->item, $this->type, $this->section);

Create myapi.php file in cobalt root and add there loadMultiratings function from components/com_cobalt/library/php/helpers/rating.php. And all other functions that willbe needed.

Then look line 143, there is a tеучеюyou can now manage your own text without braking changes on next update.

Powered by Cobalt