# Topic Ratings Plugin

**URL:** https://meta.discourse.org/t/topic-ratings-plugin/39578
**Category:** Plugin
**Tags:** pavilion
**Created:** [February 14, 2016, 9:06pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578 "2016-02-14T21:06:53Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 4, 2016, 7:57pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/21 "2016-03-04T19:57:12Z")

</div>

@pfaffman Thanks for the bug report!

Should be fixed now:

[https://github.com/angusmcleod/discourse-ratings/commit/9d441c4965db3946973160ba17b888207d5c7804](https://github.com/angusmcleod/discourse-ratings/commit/9d441c4965db3946973160ba17b888207d5c7804)

---

<div class="post-metadata">

### Author: ![fackelwind](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@fackelwind](https://meta.discourse.org/u/fackelwind)
#### Post date: [March 4, 2016, 10:07pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/22 "2016-03-04T22:07:19Z")

</div>

Fixed:

- I can freely enable and disable the rating tag from a thread and the ratings appear and disappear accordingly. Fantastic!
- If I have already answered in a thread and answer again, I can’t vote a second time. 👍

Buggy/Quirky:

- If I add a “rating” tag while creating a thread, I have to give it a rating. If I modify the thread opening post and add the rating tag, I do not have to rate (and end up with 0 zero star rating…).
- As a normal user, I am unable to reply without selecting a rating. I’d rather not force my users to have to vote to be able to reply, but it’s ok if other solutions are more work.  
However:
- If a user ends up having voted multiple times for whatever reason (in this case, because the posts where made with an older plugin version), he is unable to correct that: He can not remove having voted from a post. Otherwise, I as an admin could fix up posts from other users as well as my own…
- Deleted posts count towards votes. I’d strenuously argue that these votes should not count.
- If a post has no vote (because the topic did not have the “rating” tag when the post was made), it’s not possible to add it in afterwards. The only way is to make a new reply.

Except for the deleted post voting, none of this is really a bug. But I get the feeling that giving the option to add and remove your vote from your post would make this voting system more intuitive, flexible and robust. 😉

After seeing that I can reliably disable the plugin by removing the tag “rating”, I have added the plugin into the productive discourse instance again. So I’ll be able to report any other oddities I observer. 😃

Other than that, I am looking forward to the exact vote average, and maybe also to a listing of how many people have rated the topic how highly. 😉

Thanks a lot for your continuing work. The plugin is improving by leaps and bounds! 🎉

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 5, 2016, 3:05pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/23 "2016-03-05T15:05:10Z")

</div>

As always, my thanks for your fantastic feedback.

> If I add a “rating” tag while creating a thread, I have to give it a rating. If I modify the thread opening post and add the rating tag, I do not have to rate (and end up with 0 zero star rating…).

This is fixed.

[https://github.com/angusmcleod/discourse-ratings/commit/2a7ab4a4c589fa0d5824ce18c1d09f0c908f38e1](https://github.com/angusmcleod/discourse-ratings/commit/2a7ab4a4c589fa0d5824ce18c1d09f0c908f38e1)

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 5, 2016, 7:14pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/24 "2016-03-05T19:14:35Z")

</div>

> Deleted posts count towards votes. I’d strenuously argue that these votes should not count.

This should be fixed. If a post is deleted its rating will automatically be removed from the average. If the post is restored, its rating will be added back to the average. This should also work with existing ratings topics.

There may be some remaining issues to iron out with this one, but it seems to be working in my local testing.

[https://github.com/angusmcleod/discourse-ratings/commit/74fd87d41e42caf29790b6dba0532b8b66862a01](https://github.com/angusmcleod/discourse-ratings/commit/74fd87d41e42caf29790b6dba0532b8b66862a01)

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 5, 2016, 7:22pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/25 "2016-03-05T19:22:47Z")

</div>

With these two, I see your point, however I think I might work on this kind of thing a little later. They are not functionality issues _per se_.

> As a normal user, I am unable to reply without selecting a rating. I’d rather not force my users to have to vote to be able to reply, but it’s ok if other solutions are more work.

> If a post has no vote (because the topic did not have the “rating” tag when the post was made), it’s not possible to add it in afterwards. The only way is to make a new reply.

For this one

> If a user ends up having voted multiple times for whatever reason (in this case, because the posts where made with an older plugin version), he is unable to correct that: He can not remove having voted from a post. Otherwise, I as an admin could fix up posts from other users as well as my own…

This is a ‘legacy’ issue. I can fix it for you if it’s an issue on your live site, but if it’s just a dev issue, I might leave this one for now.

Cheers!

When I next have time in the next few days, I’m going to test the deletion discount from averages a bit more and add the exact averages and an indication of hidden ratings.

---

<div class="post-metadata">

### Author: ![fackelwind](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@fackelwind](https://meta.discourse.org/u/fackelwind)
#### Post date: [March 6, 2016, 8:49am UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/26 "2016-03-06T08:49:33Z")

</div>

Thanks for your fast replies and changes!  
No, I only have a single thread with votes atm, which is intended for testing. Having voted multiple times there makes playing around easier. 😃

Something is more weird after the plugin update than before:  
I have currently the votes 0,4,2 which result in a one star rating. But I swear that before the update, changing that 2 into a 3 was enough to give me an average 2 star rating.  
I have now changed the ratings to 0,4,5 and raised the rating of the deleted comment to 5. I still only get a 1 star average.  
Is the rating maybe not getting updated at all anymore? There are two more comments in the thread without a rating (no stars shown).

Found another bug, and a pretty weird one:  
Normally, when you refresh (F5) the browser window after having edited a post, it simply reloads the current thread. If you refresh a rated thread after having edited a post, the edit is shown again (I then cancel the edit, and everything is fine again.)  
wtf?

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 7, 2016, 1:20pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/27 "2016-03-07T13:20:13Z")

</div>

> [@fackelwind](#):
>
> I have currently the votes 0,4,2 which result in a one star rating. But I swear that before the update, changing that 2 into a 3 was enough to give me an average 2 star rating.I have now changed the ratings to 0,4,5 and raised the rating of the deleted comment to 5. I still only get a 1 star average.Is the rating maybe not getting updated at all anymore? There are two more comments in the thread without a rating (no stars shown).

Indeed. I had to add a ‘rating\_weight’ property to accommodate deleted posts with ratings (and perhaps to use in the future for weighting ratings differently according to poster, or other metadata such as likes). This subsequently changed the calculation of the topic average from this:

```plaintext
@topic_posts = Post.where(topic_id: post.topic_id)
@all_ratings = PostCustomField.where(post_id: @topic_posts.map(&:id), name: "rating").pluck('value').map(&:to_i)
average = @all_ratings.inject(:+).to_f / @all_ratings.length

```

to this:

```plaintext
@topic_posts = Post.with_deleted.where(topic_id: post.topic_id)
@ratings = []
@topic_posts.each do |post|
  weight = post.custom_fields["rating_weight"]
  if weight.blank? || weight.to_i > 0
    rating = post.custom_fields["rating"].to_i
    @ratings.push(rating)
  end
end
average = @ratings.inject(:+).to_f / @ratings.length

```

The `rating_weight` of deleted posts is set to 0. This logic (I think) accommodates rating weight by only counting ratings if the weight is greater than 0 or is blank (to support posts with ratings posted prior to this change). However, the problem is that it also counts posts without a rating at all.. So I’ve changed it to:

```plaintext
@topic_posts = Post.with_deleted.where(topic_id: post.topic_id)
@ratings = []
@topic_posts.each do |post|
  weight = post.custom_fields["rating_weight"]
  if post.custom_fields["rating"] && (weight.blank? || weight.to_i > 0)
   rating = post.custom_fields["rating"].to_i
   @ratings.push(rating)
  end
end
average = @ratings.inject(:+).to_f / @ratings.length 

```

This also checks if the post has a rating before counting it. A silly mistake on my part really. The problem is that I’m always working on this either early in the morning before work or late at night when the brain is softer…

I hope that fixes it.

[https://github.com/angusmcleod/discourse-ratings/commit/615e2388e7475077e348241113fe335a631188b0](https://github.com/angusmcleod/discourse-ratings/commit/615e2388e7475077e348241113fe335a631188b0)

---

<div class="post-metadata">

### Author: ![fackelwind](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@fackelwind](https://meta.discourse.org/u/fackelwind)
#### Post date: [March 8, 2016, 4:56am UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/28 "2016-03-08T04:56:21Z")

</div>

It does fix it. 5,4 and 0 now give me a rating of 3. Perfect!

At some point please take a look at the other weird bug I described as well.

Thanks a bunch for your work!

---

<div class="post-metadata">

### Author: ![smartwatchme](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/smartwatchme/32/41275_2.png) [@smartwatchme](https://meta.discourse.org/u/smartwatchme)
#### Post date: [March 10, 2016, 2:09pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/29 "2016-03-10T14:09:43Z")

</div>

This looks great - would the rating of the topic be displayed in Google at all? i.e:  
 ![](https://global.discourse-cdn.com/meta/original/3X/8/2/82095090664c139e3f40e05cd184da8d8b23993d.png)

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 11, 2016, 2:17pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/30 "2016-03-11T14:17:08Z")

</div>

> Normally, when you refresh (F5) the browser window after having edited a post, it simply reloads the current thread. If you refresh a rated thread after having edited a post, the edit is shown again (I then cancel the edit, and everything is fine again.)

Yes this is a tricky one. I’ve been trying to pin it down this morning. This is what I’ve discovered so far:

1. It only occurs if you _only_ change the rating on a _reply_ by editing it. If you change the rating on the first post in a topic or change a rating in a reply and also change the content of the post, it does not occur.

2. The composer opening is triggered by [this method](https://github.com/discourse/discourse/blob/eb5cee3150fbd0b2de08d2a93b3959a20e41a691/app/assets/javascripts/discourse/routes/topic-from-params.js.es6#L51) in the topic-from-params route:

````plaintext
if (!Ember.isEmpty(topic.get('draft'))) {
        composerController.open({
          draft: Draft.getLocal(topic.get('draft_key'), topic.get('draft')),
          draftKey: topic.get('draft_key'),
          draftSequence: topic.get('draft_sequence'),
          topic: topic,
          ignoreIfChanged: true
        });
      }
```

So in other words, if only the rating is changed and the title and reply remain the same when an edit to a reply is saved, the ``draft`` attribute of the topic view is populated on the server.

However, it turns out that this is true generally of Discourse. If you edit a reply, do not change the title or reply content and then 'save' the edit, the composer will re-open if you refresh the page: http://quick.as/m1vrS6JxV

Is this a bug or is it intentional? @riking @zogstrip @sam
````

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [March 11, 2016, 5:06pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/31 "2016-03-11T17:06:02Z")

</div>

> [@angus](#):
>
> Is this a bug or is it intentional?

Not sure, I haven’t touched that part. Maybe @eviltrout knows better?

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 12, 2016, 2:13pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/32 "2016-03-12T14:13:50Z")

</div>

Just added exact topic averages next to topic star ratings, controlled by the Plugins setting `Show exact average rating`

[https://github.com/angusmcleod/discourse-ratings/commit/e2a4fc5e189d445eea4dd6d2257719782dabe401](https://github.com/angusmcleod/discourse-ratings/commit/e2a4fc5e189d445eea4dd6d2257719782dabe401)

 ![](https://global.discourse-cdn.com/meta/original/3X/a/9/a9e64dfe61bf6cd067b2fa21c75759a651975d8e.png)

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 12, 2016, 2:28pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/33 "2016-03-12T14:28:09Z")

</div>

Added a note for admins if a topic has hidden ratings (if category or tags were changed).

[https://github.com/angusmcleod/discourse-ratings/commit/25cdf0c3cd24c507c8d56811d9f82add63d6c92a](https://github.com/angusmcleod/discourse-ratings/commit/25cdf0c3cd24c507c8d56811d9f82add63d6c92a)

 ![](https://global.discourse-cdn.com/meta/original/3X/a/3/a3d354bc526d5fe239b9ece68d89fc105d80dd27.png)

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 12, 2016, 2:30pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/34 "2016-03-12T14:30:57Z")

</div>

> [@smartwatchme](#):
>
> This looks great - would the rating of the topic be displayed in Google at all? i.e:

Probably not, but it’s a good feature to add! Will have a look at this soon.

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 12, 2016, 3:14pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/35 "2016-03-12T15:14:56Z")

</div>

This plugin is now at v0.2.

Apart from the composer re-opening issue, which may be a Discourse bug, the plugin is now reliably working as described. To do list in first post reflects ongoing agenda.

> **[Release Stable · paviliondev/discourse-ratings](https://github.com/paviliondev/discourse-ratings/releases/tag/0.2)**
>
> Plugin working as described. See further: https://meta.discourse.org/t/topic-ratings-plugin/39578

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 18, 2016, 12:34pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/36 "2016-03-18T12:34:06Z")

</div>

Forgot to mention here, but the bug with the composer reopening should be fixed now if you update Discourse to include this commit.

[https://github.com/discourse/discourse/commit/91a2750084a474a62fe2a61c43b91f6f9396b2d0](https://github.com/discourse/discourse/commit/91a2750084a474a62fe2a61c43b91f6f9396b2d0)

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 21, 2016, 10:06pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/37 "2016-03-21T22:06:36Z")

</div>

Feature request [from github](https://github.com/angusmcleod/discourse-ratings/issues/3):

> My team is building a site called Science Commons where academics can discuss research papers. Each topic is about a single paper; the first post is a “wiki” that contains a summary of the paper. I would like to use your rating plug-in for Science Commons. But my concern is that when I turn on the rating tag, I am forced to give a rating on the first post. Is there a way such that no rating is required for the first post? I want to be an impartial admin who posts the paper and don’t want to give my own opinion about the paper.

I’ve thought of adding this previously and will add this as a setting in a few days time, after finishing the current batch of work on [Quick Messages](https://meta.discourse.org/t/quick-messages-plugin/39188).

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [March 28, 2016, 12:41am UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/38 "2016-03-28T00:41:42Z")

</div>

I’ve added a feature which covers the use case previously mentioned (i.e. omission of a rating from the first post in a ratings topic), and also lets any user remove a rating entirely from a previous post in a ratings topic. Complete removal of a previous rating allows the user to rate in another post on that topic if they so wish.

[https://github.com/angusmcleod/discourse-ratings/commit/c6ddc5a6546a594d7fa54bc5ce0238b02b0b7471](https://github.com/angusmcleod/discourse-ratings/commit/c6ddc5a6546a594d7fa54bc5ce0238b02b0b7471)

 ![](https://global.discourse-cdn.com/meta/original/3X/1/a/1a5a50077ba781b8108b05e145b236a53c74c939.png)

 ![](https://global.discourse-cdn.com/meta/original/3X/f/d/fd1f666372967175347262702734babaeb3a5f22.png)

 ![](https://global.discourse-cdn.com/meta/original/3X/c/3/c37b74d8e67d90319992e72728efda7aead5710a.png)

---

<div class="post-metadata">

### Author: ![meglio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/meglio/32/71444_2.png) [@meglio](https://meta.discourse.org/u/meglio)
#### Post date: [April 10, 2016, 2:02am UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/39 "2016-04-10T02:02:39Z")

</div>

Is it possible to customize the “rating” tag to be any other existing tag of my selection?

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [April 10, 2016, 3:31pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/40 "2016-04-10T15:31:05Z")

</div>

> [@meglio](#):
>
> Is it possible to customize the “rating” tag to be any other existing tag of my selection?

Hey, that’s number ‘4’ on my [list of to dos](https://meta.discourse.org/t/topic-ratings-plugin/39578). As you’ve seen on the topics for my other plugins where you have posted I have a few ‘to dos’ on my plate at the moment, so it’ll be a little while before I add that. But I am happy to accept a PR in the meantime.

[Previous page](https://meta.discourse.org/t/topic-ratings-plugin/39578.md?page=1)

[Next page](https://meta.discourse.org/t/topic-ratings-plugin/39578.md?page=3)
