It would be really nice to have some better handling of the filter options used for the rating value stored in the fivestar field. Right now we are using the default views filter for integers, which allows users to select equal, greater than, lower than etc.

What we should use instead is the in_operator which will create a query using "IN" or "NOT IN" and will give the flexibility of the admins to display results with fx 1 and 5 stars. It will also allow us to create a more natural selection for the users, who can use a select list to enter stars instead of users having tp enter 20, 40 etc and know how many stars that translate to.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

googletorp’s picture

Status: Active » Needs review

I've created the initial views integration for fivestar, handling this issue and added it to my Fivestar sandbox: b4ab341

The remote for this is http://git.drupal.org/sandbox/googletorp/1255156.git

ericduran’s picture

Status: Needs review » Needs work

Patches are welcome. Even proper git patches are welcome so you can get the proper commit credit. But we just changed a lot regarding the field and the widgets so patches would be simpler to work with. Sorry :(

ericduran’s picture

Status: Needs work » Closed (won't fix)

Marking as wont-fix. Patches are always welcome :)

googletorp’s picture

Status: Closed (won't fix) » Needs review
FileSize
3.67 KB

Been busy with other stuff, anyways cleaned up the the code so that it actual works, somehow some debug stuff landing with the code (Ups), also created this as a git patch for head, so should be easier for you to make it work as well.

Otherwise this is basically the same as before.

How it works:

We use hook_field_views_data to add a custom handler for the rating column and otherwise use views default.
The custom handler is an in type operator like content types, as we know the possible values (1 star, 2 stars.. foo stars). This data is fetched from the field settings and added as possible filter options. Works great for exposed filters.

TODO: A nicety would be to render the exposed form as a fivestart widget instead of a simple select box.

ericduran’s picture

AWESOME!!!

I'm testing this out. Thanks.

googletorp’s picture

Add patch for alpha 2 release.

googletorp’s picture

Fixed issue with previous patch for alpha 2 release

jasonglisson’s picture

Great job guys! This works great!

anemirovsky’s picture

The patch didin't work for me.

field_info_field($this->definition['field_name']) does not pull the configuration setting for the number of stars for the field. That setting is stored in field_config_instance and you get it by using field_info_instance. However, field_info_instance takes both the entity_type and bundle_name for the field along with the field_name for arguments. I had a bit of trouble figuring out how to get that info, so the solution I have is a bit hacky but seems to work. Basically, I get the field_info and entity_type from the field_info_field by doing some hacky stuff the the bundles array. Then, I call field_info_instance to get the number of stars configured for the field.

Status: Needs review » Needs work

The last submitted patch, views_filter_integration-1255160-alpha2-3.patch, failed testing.

anemirovsky’s picture

Let's try that again...

anemirovsky’s picture

Status: Needs work » Needs review
FileSize
3.92 KB

And again...

Status: Needs review » Needs work

The last submitted patch, views_filter_integration-1255160-alpha2-4.patch, failed testing.

anemirovsky’s picture

And again again...

anemirovsky’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, views_filter_integration-1255160-alpha2-5.patch, failed testing.

anemirovsky’s picture

Status: Needs work » Needs review
FileSize
2.09 KB

This should work now. I'm patching against 7.x rather than alpha 2.

Status: Needs review » Needs work

The last submitted patch, views_filter_integration-1255160-16.patch, failed testing.

anemirovsky’s picture

This is becoming comedic...if this doesn't work, I don't know what will.

anemirovsky’s picture

Status: Needs work » Needs review
ericduran’s picture

@anemirovsky well then good thing that one work ;-p

I'll review this this weekend.

Thanks for your hard work :)

jibize’s picture

Patch from #19 worked for me thank you!

ExTexan’s picture

#19 worked for me except for selecting "No stars". Evidently the result of no stars is blank (or null), not zero.

anemirovsky’s picture

This is a tougher problem to solve. There isn't actually a row created for the fivestar field if the value is no stars, so the Views query fails to find those nodes because it's looking for a row in the fivestar field's table.

One way I thought of to try to fix this is to modify the Views query if the selected filter option is 0 to not add the where clause that looks for a value in the joined field table. Instead it will add a where clause that checks that the node doesn't have a corresponding row in the field table, since if it doesn't appear there, the rating for that node must be 0. However, there are some serious performance issues with that, so I don't really think that's a good choice.

To be honest, I'm not sure what the best option is here. Ideally, the fivestar module would record a 0 rating for the node if the node has no stars, but I don't know how feasible that is.

ExTexan’s picture

Is it possible to modify the query it to do a left join and change the where to test for "zero or null" (test for both, I mean)?

anemirovsky’s picture

Try this one on for size. It's not the prettiest code I've written, but I tried to err on the side of understandable logic rather than minimal lines of code. This should handle the No Stars use case for multiple values in the filter as well as for the "Is one of" and "Is not one of" operators.

ExTexan’s picture

I installed patch #26. I'm able to add the exposed filter to my view just fine, but it doesn't seem to select the correct nodes.

My filter is defined as a single filter, with operator = "is one of", and the default options (Select all, No stars, 1 star, 2 stars, etc.).

I have a view that (unfiltered) returns 30 nodes with average votes ranging from 2.1 to 4.8.

Select "No Stars" - I get no results (as expected).
Select "1 Star" - I get 3 nodes, with averages of 2.3, 2.3, and 3.1.
Select "2 Stars" - I get 3 nodes, with averages of 3, 2.9, and 3.1.
Select "3 Stars" - I get 5 nodes, with averages ranging from 2.8 to 3.5.
Select "4 Stars" - I get 5 nodes, with averages of 3.6 for one node, and 4 for the other 4 (1 vote each).
Select "5 Stars" - I get 14 nodes, with averages ranging from 2.1 to 4.8.

My guess is that if any single vote on any node matches the selection, the node is shown. If that's the case, that's counter-intuitive to what users would expect, IMO.

If I filter on "3 Stars", for example, I would expect to see all nodes with an average rating from 3 to 3.9.

Is there a way to get this patch to accomplish that?

ydahi’s picture

#26 fails to patch for Fivestar 7.x-2.1 with the following error:

patching file fivestar.info
Hunk #1 FAILED at 6.
1 out of 1 hunk FAILED -- saving rejects to file fivestar.info.rej
patching file fivestar.module
Hunk #1 succeeded at 136 (offset 15 lines).
patching file includes/fivestar.field.inc
Hunk #1 succeeded at 125 (offset 15 lines).
patching file includes/fivestar_views_handler_filter_in_operator.inc

inside fivestar.info.rej:

--- fivestar.info
+++ fivestar.info
@@ -6,3 +6,4 @@
 configure = admin/config/content/fivestar
 files[] = test/fivestar.base.test
 files[] = test/fivestar.field.test
+files[] = includes/fivestar_views_handler_filter_in_operator.inc

In views, you the "Broken/Missing handler" error and are unable to use the filter.

Xen’s picture

Rerolled aganst 2.1.

Removed the "is empty"/"is not empty" operator options for better UX (they're covered by the "0 stars" option).

Replaced the four massive, nested, "if" statements and replaced them with more nimble conditions.

Fixed fivestar_field_views_data to not use the title for identification.

Status: Needs review » Needs work

The last submitted patch, 29: add_better_views-1255160-29.patch, failed testing.

Xen’s picture

Removed unintended whitespace fix.

Xen’s picture

Status: Needs work » Needs review
Selfirian’s picture

Hi,

I tried the patch in #31, using latest dev version: 7x-2.0+5
I get this error (in views, I have BEF enabled)

(Products referenced by field_product) Broken/missing handler (= )

I'm using Fivestar on products inside Drupal commerce if that matters at all.

Thanks for the great work

Xen’s picture

Okay, using the patch in drush make doesn't seem to work. Looks like it chokes on the lines that the packaging system adds to the info file.

New patch that moves the added lines a few up. Also seems more "right" to have includes before tests.

Xen’s picture

Doh, missed a file.

J-Lee’s picture

Patch #35 is working for me.
Thank you.

spgd01’s picture

Patch works but I got following error in drush:

Patch #35 failed:

Hunk #1 Failed at 4 Saving rejects to fivestar.info.rej

spgd01’s picture

Removed

spgd01’s picture

Patch #35 always selects "no Stars" when using grouped filters even when deselected.

Xen’s picture

Rerolled against 7.x-2.2.

gg24’s picture

Status: Needs review » Needs work
+++ b/includes/fivestar_views_handler_filter_in_operator.inc
@@ -0,0 +1,66 @@
+      $or_statement->condition("$this->table_alias.$this->real_field", NULL, $nul_operator);

"IN" condition works fine after applying the patch but for "NOT IN" view will display all the results.
As query contains:
WHERE (( (node.status = '1') AND( (field_data_field_rate.field_rate_rating NOT IN ('0', '20', '40', '60', '80', '100')) OR (field_data_field_rate.field_rate_rating IS NOT NULL ) )))

And data will pass through this filter, as it is NOT NULL.

gg24’s picture

Assigned: Unassigned » gg24

I am working on this issue.

gg24’s picture

Status: Needs work » Needs review
FileSize
6.6 KB

Status: Needs review » Needs work

The last submitted patch, 43: add_better_views-1255160-43.patch, failed testing.

gg24’s picture

Status: Needs work » Needs review
FileSize
6.61 KB
4.26 KB
navneet0693’s picture

Status: Needs review » Needs work
+++ b/includes/fivestar_views_handler_filter_in_operator.inc
@@ -56,11 +57,57 @@ class fivestar_views_handler_filter_in_operator extends views_handler_filter_in_
+    if ($this->operator == 'in') {
...
+    if ($this->operator == 'not in') {

May be we could convert this in a switch case. It looks nice! It is only a suggestion not necessary though :)

gg24’s picture

Assigned: gg24 » Unassigned
Status: Needs work » Needs review
FileSize
6.46 KB
3.4 KB
dbt102’s picture

+1 bump

gg24’s picture

This feature introduced to have a filter handler inside view. This patch handles both "IN" and "NOT IN" conditions to filter out the correct set of data.

navneet0693’s picture

Working as per expectations !

navneet0693’s picture

Status: Needs review » Reviewed & tested by the community

Issuing RTBC

  • dbt102 committed 15b01d3 on 7.x-2.x authored by gg24
    Issue #1255160 by anemirovsky, Xen, gg24, googletorp, dbt102: Add better...
dbt102’s picture

Thanks for the patches and reviews everyone !!!

dbt102’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

thommyboy’s picture

I use the filter because I simply want to filter (exposed) voted from unvoted nodes. When configuring the filter I can choose 1star, 2stars, ... BUT there is a "No star" setting that ALWAYS gets set automnatically when saving the filter making it useless?

EDIT: I use a grouped filter (seems to work on a single filter) with three (desired) options (Any, NOT IN NO STARS, IN 1..6 STARS)