Comments

stomusic created an issue. See original summary.

stomusic’s picture

stomusic’s picture

Assigned: stomusic » Unassigned
Status: Active » Needs review
gg24’s picture

Hi @stomusic,

I tried applying the patch attached but couldn't do so. Attached an image too. Please refer.
So rerolled the above patch.
It works as expected.

Thanks!

gg24’s picture

Status: Needs review » Reviewed & tested by the community

Issuing RTBC.

Thanks!

tr’s picture

tr’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.54 KB

Patch no longer applies - no surprise since it's so old.

Here's a re-roll against HEAD. I also fixed the improper use of array_unique() in the patch.

This patch probably still needs schema definitions for these two new formatters. I will look into that when I test this code.

Status: Needs review » Needs work

The last submitted patch, 7: 2918679-7-rating-and-percentage-formatters.patch, failed testing. View results

tr’s picture

Issue tags: +Needs tests

I don't think the schema is needed, because the new formatters don't define any settings.

However, both these formatters crash if there are no votes, and don't display anything if there ARE votes.

Needs work.

We're also going to need tests to demonstrate these work as expected (display the rating as expected when a node is rated, for instance) and to prevent us from breaking this functionality in future commits.

tr’s picture

Status: Needs work » Needs review
StatusFileSize
new3.06 KB

Here's a patch with no tests yet, but you can apply the patch and test it manually.

tr’s picture

Committed #10, leaving issue open in order to add tests ...

  • TR committed beaab78 on 8.x-1.x
    Issue #2918679 by TR, gg24, stomusic: Rating and Percentage Formatter
    
inman’s picture

StatusFileSize
new13.77 KB

Hello.

Formats: Percentage (i.e. 92) and Rating (i.e. 4.2/5) not working properly.
Drupal Version: 9.3.0

inman’s picture

a_sh’s picture

StatusFileSize
new1.56 KB

The output of PercentageFormatter and ratingFormatter is totally incorrect.

When I try to use percentage or rating output of fivestar rating it shows erroneous results.
For example, for node with 3 votes and average rating 9.7 it shows percentage rating 90%.
I did check the output of PercentageFormatter.php and it outputs data with votes count=1 (should be 3) and value as shown.

The content of Field/FieldFormatter/PercentageFormatter.php in my installation is included. It works as described above. It seems tha aforementioned patch is already included in my installation.

a_sh’s picture

tr’s picture

Please provide steps for reproducing the problem, including details of how you set up your Fivestar field.

For example, for node with 3 votes and average rating 9.7 it shows percentage rating 90%.
I did check the output of PercentageFormatter.php and it outputs data with votes count=1 (should be 3) and value as shown.

But if there really is only one vote like it says, perhaps 90% is also correct? Are the 3 votes on different node types, or are they different vote types, etc? I guess maybe you have this configured for 10 stars not 5? If so, do you see the same problems with only 5 stars?

a_sh’s picture

Drupal 9.5.9. Fivestar is set-up in custom content types.
An example of problem.
Node id is 50. It have two vote types with two total votes.
Db content dump is:

INSERT INTO `votingapi_vote` (`id`, `type`, `uuid`, `entity_type`, `entity_id`, `value`, `value_type`, `user_id`, `timestamp`, `vote_source`) VALUES
(2248, 'vote', '5ceaf1a2-5566-4991-ac7a-daaf3934402e', 'node', 50, 100, 'percent', 1, 1685946862, 'd832a7497a537224185ea5484e5abda367e403c3b28d14a126a7d39de90a1502'),
(2249, 'tastevote', '4f5048c8-2789-4f5f-bc59-5bf09d6087e0', 'node', 50, 80, 'percent', 1, 1685946862, 'd832a7497a537224185ea5484e5abda367e403c3b28d14a126a7d39de90a1502'),
(2141, 'vote', '8fbcb524-ed70-4325-b52e-3ced5df08ea4', 'node', 50, 100, 'percent', 0, 1684564841, '4743d4ef68c8d8c307eecb571f206949ba40b9b8083fb7f055b2747caf84fbd8'),
(2142, 'tastevote', '4897c131-77f8-49b7-98a2-d3b2fc29ce2e', 'node', 50, 100, 'percent', 0, 1684564841, '4743d4ef68c8d8c307eecb571f206949ba40b9b8083fb7f055b2747caf84fbd8');

INSERT INTO `votingapi_result` (`id`, `type`, `entity_type`, `entity_id`, `value`, `value_type`, `function`, `timestamp`) VALUES
(8629, 'vote', 'node', 50, 100, 'percent', 'vote_average', 1685946862),
(8630, 'vote', 'node', 50, 2, 'percent', 'vote_count', 1685946862),
(8631, 'vote', 'node', 50, 200, 'percent', 'vote_sum', 1685946862),
(8632, 'tastevote', 'node', 50, 90, 'percent', 'vote_average', 1685946862),
(8633, 'tastevote', 'node', 50, 2, 'percent', 'vote_count', 1685946862),
(8634, 'tastevote', 'node', 50, 180, 'percent', 'vote_sum', 1685946862);

For tastevote:
Stars formatter shows rating 9 of 10 (2 votes), that is correct.
Percentage formatter shows rating 80% - that is incorrect. It is always equal with value of the last vote - I just checked this. If I add vote 4 stars it becomes 40%.

Changing number of stars from 10 to 5 do not affect the correctness of Percentage formatter - it is still incorrect.

a_sh’s picture

The partial dump of StarsFormatter.php $elements output
15 votes with average 5.3 for tastevote

array(1) { [0]=> array(29) { ["#attributes"]=> array(3) { ["class"]=> array(1) @ [0]=> string(15) "fivestar-form-1" ["id"]=> string(4) "vote" ["data-drupal-selector"]=> string(15) "fivestar-form-1" } ["vote"]=> array(35) { ["#type"]=> string(8) "fivestar" ["#stars"]=> int(10) ["#allow_clear"]=> bool(false) ["#allow_revote"]=> bool(true) ["#allow_ownvote"]=> bool(true) ["#vote_type"]=> string(9) "tastevote" ["#widget"]=> array(4) @ ["name"]=> string(6) "oxygen" ["text_format"]=> string(7) "average" ["display_format"]=> string(7) "average" ["fivestar_widget"]=> string(6) "oxygen" ["#default_value"]=> string(2) "70" ["#values"]=> array(3) @ ["vote_sum"]=> string(3) "790" ["vote_count"]=> string(2) "15" ["vote_average"]=> string(7) "52.6667" ["#settings"]=> array(4) @ ["name"]=> string(6) "oxygen" ["text_format"]=> string(7) "average" ["display_format"]=> string(7) "average" ["fivestar_widget"]=> string(6) "oxygen" ["#show_static_result"]=> bool(false) ["#attributes"]=> array(2) { ["class"]=> array(1) @ [0]=> string(4) "vote" ["data-drupal-selector"]=> string(9) "edit-vote" } ["#input"]=> bool(true) ["#ajax"]=> NULL ["#process"]=> array(2) { [0]=> array(2) @ [0]=> string(32) "Drupal\fivestar\Element\Fivestar" [1]=> string(7) "process" [1]=> array(2) @ [0]=> string(32) "Drupal\fivestar\Element\Fivestar" [1]=> string(15) "processAjaxForm" } ["#theme_wrappers"]=> array(1) @ [0]=> string(12) "form_element" ["#value_callback"]=> array(2) @ [0]=> string(32) "Drupal\fivestar\Element\Fivestar" [1]=> string(13) "valueCallback" ["#defaults_loaded"]=> bool(true) ["#tree"]=> bool(false) ["#parents"]=> array(1) @ [0]=> string(4) "vote" ["#array_parents"]=> array(1) @ [0]=> string(4) "vote" ["#weight"]=> int(0) ["#processed"]=> bool(true) ["#required"]=> bool(false) ["#title_display"]=> string(6) "before" ["#description_display"]=> string(5) "after" ["#errors"]=> NULL ["#id"]=> string(9) "edit-vote" ["#name"]=> string(4) "vote" ["#value"]=> bool(false) ["vote"]=> array(31) { ["#type"]=> string(6) "select" ["#description"]=> array(5) @ ["#average_rating"]=> string(7) "52.6667" ["#votes"]=> string(2) "15" ["#theme"]=> string(16) "fivestar_summary" ["#stars"]=> int(10) ["#microdata"]=> NULL ["#options"]=> array(11) { ["-"]=> object(Drupal\Core\StringTranslation\TranslatableMarkup)#2681 (5) { ["string":protected]=> string(13) "Select rating" ["arguments":protected]=> array(0) @ ["translatedMarkup":protected]=> NULL ["options":protected]=> array(0) @ ["stringTranslation":protected]=> NULL } [10]=> object(Drupal\Core\StringTranslation\TranslatableMarkup)#2667 (5) { ["string":protected]=> string(24) "Give @title @star/@count" ["arguments":protected]=> array(3) @ ["@title"]=> string(2) "it" ["@star"]=> int(1) ["@count"]=> int(10) ["translatedMarkup":protected]=> NULL ["options":protected]=> array(0) @ ["stringTranslation":protected]=> NULL } [20]=> object(Drupal\Core\StringTranslation\TranslatableMarkup)#2666 (5) { ["string":protected]=> string(24) "Give @title @star/@count" ["arguments":protected]=> array(3) @ ["@title"]=> string(2) "it" ["@star"]=> int(2) ["@count"]=> int(10) ["translatedMarkup":protected]=> NULL ["options":protected]=> array(0) @ ["stringTranslation":protected]=> NULL } [30]=> object(Drupal\Core\StringTranslation\TranslatableMarkup)#2690 (5) { ["string":protected]=> string(24) "Give @title @star/@count" ["arguments":protected]=> array(3) @ ["@title"]=> string(2) "it" ["@star"]=> int(3) ["@count"]=> int(10) ["translatedMarkup":protected]=> NULL ["options":protected]=> array(0) @ ["stringTranslation":protected]=> NULL } [40]=> object(Drupal\Core\StringTranslation\TranslatableMarkup)#2659 (5) { ["string":protected]=> string(24) "Give @title @star/@count" ["arguments":protected]=> array(3) @ ["@title"]=> string(2) "it" ["@star"]=> int(4) ["@count"]=> int(10) ["translatedMarkup":protected]=> NULL ["options":protected]=> array(0) @ ["stringTranslation":protected]=> NULL } [50]=> object(Drupal\Core\StringTranslation\TranslatableMarkup)#2691 (5) { ["string":protected]=> string(24) "Give @title @star/@count" ["arguments":protected]=> array(3) @ ["@title"]=> string(2) "it" ["@star"]=> int(5) ["@count"]=> int(10) ["translatedMarkup":protected]=> NULL ["options":protected]=> array(0) @ ["stringTranslation":protected]=> NULL } [60]=> object(Drupal\Core\StringTranslation\TranslatableMarkup)#2692 (5) { ["string":protected]=> string(24) "Give @title @star/@count" ["arguments":protected]=> array(3) @ ["@title"]=> string(2) "it" ["@star"]=> int(6) ["@count"]=> int(10) ["translatedMarkup":protected]=> NULL ["options":protected]=> array(0) @ ["stringTranslation":protected]=> NULL } [70]=> object(Drupal\Core\StringTranslation\TranslatableMarkup)#2693 (5) { ["string":protected]=> string(24) "Give @title @star/@count" ["arguments":protected]=> array(3) @ ["@title"]=> string(2) "it" ["@star"]=> int(7) ["@count"]=> int(10) ["translatedMarkup":protected]=> NULL ["options":protected]=> array(0) @ ["stringTranslation":protected]=> NULL } [80]=> object(Drupal\Core\StringTranslation\TranslatableMarkup)#2694 (5) { ["string":protected]=> string(24) "Give @title @star/@count" ["arguments":protected]=> array(3) @ ["@title"]=> string(2) "it" ["@star"]=> int(8) ["@count"]=> int(10) ["translatedMarkup":protected]=> NULL ["options":protected]=> array(0) @ ["stringTranslation":protected]=> NULL } [90]=> object(Drupal\Core\StringTranslation\TranslatableMarkup)#2695 (5) { ["string":protected]=> string(24) "Give @title @star/@count" ["arguments":protected]=> array(3) @ ["@title"]=> string(2) "it" ["@star"]=> int(9) ["@count"]=> int(10) ["translatedMarkup":protected]=> NULL ["options":protected]=> array(0) @ ["stringTranslation":protected]=> NULL } [100]=> object(Drupal\Core\StringTranslation\TranslatableMarkup)#2696 (5) { ["string":protected]=> string(24) "Give @title @star/@count" ["arguments":protected]=> array(3) @ ["@title"]=> string(2) "it" ["@star"]=> int(10) ["@count"]=> int(10) ["translatedMarkup":protected]=> NULL ["options":protected]=> array(0) @ ["stringTranslation":protected]=> NULL } } ["#rating"]=> string(7) "52.6667" ["#required"]=> bool(false) ["#attributes"]=> array(3) { ["class"]=> array(1) @ [0]=> string(4) "vote" ["data-drupal-selector"]=> string(9) "edit-vote" ["aria-describedby"]=> string(25) "edit-vote--2--description" } ["#default_value"]=> float(60) ["#weight"]=> int(-2) ["#ajax"]=> NULL ["#parents"]=> array(1) @ [0]=> string(4) "vote" ["#input"]=> bool(true) ["#multiple"]=> bool(false) ["#sort_options"]=> bool(false) ["#sort_start"]=> int(0) ["#process"]=> array(2) { [0]=> array(2) @ [0]=> string(33) "Drupal\Core\Render\Element\Select" [1]=> string(13) "processSelect" [1]=> array(2) @ [0]=> string(33) "Drupal\Core\Render\Element\Select" [1]=> string(15) "processAjaxForm" } ["#pre_render"]=> array(1) { [0]=> array(2) @ [0]=> string(33) "Drupal\Core\Render\Element\Select" [1]=> string(15) "preRenderSelect" } ["#theme"]=> string(6) "select" ["#theme_wrappers"]=> array(1) @ [0]=> string(12) "form_element" ["#value_callback"]=> array(2) @ [0]=> string(33) "Drupal\Core\Render\Element\Select" [1]=> string(13) "valueCallback" ["#defaults_loaded"]=> bool(true) ["#tree"]=> bool(false) ["#array_parents"]=> array(2) @ [0]=> string(4) "vote" [1]=> string(4) "vote" ["#processed"]=> bool(true) ["#title_display"]=> string(6) "before" ["#description_display"]=> string(5) "after" ["#errors"]=> NULL ["#id"]=> string(12) "edit-vote--2" ["#name"]=> string(4) "vote" ["#value"]=> float(60) ["#ajax_processed"]=> bool(false) ["#sorted"]=> bool(true) } ["#attached"]=> array(1) { ["library"]=> array(2) @ [0]=> string(15) "fivestar/oxygen" [1]=> string(22) "fivestar/fivestar.base" } ["#prefix"]=> string(103) "
" ["#suffix"]=> string(6) "

a_sh’s picture

The full dump of PercentageFormatter.php $elements output
15 votes with average 5.3 for tastevote

array(1) { [0]=> array(3) { ["#theme"]=> string(29) "fivestar_formatter_percentage" ["#instance_settings"]=> array(9) @ ["stars"]=> int(10) ["allow_clear"]=> bool(false) ["allow_revote"]=> bool(true) ["allow_ownvote"]=> bool(true) ["rated_while"]=> string(7) "viewing" ["enable_voting_target"]=> bool(false) ["target_bridge_field"]=> string(0) "" ["target_fivestar_field"]=> string(0) "" ["vote_type"]=> string(9) "tastevote" ["#item"]=> array(3) @ ["user"]=> int(1) ["average"]=> int(70) ["count"]=> int(1) } }

a_sh’s picture

I did check the output of StarsFormatter.
It calls FormBuilder that populates output with correct data, using resultManager->getResultsByVoteType function call.

However, in PercentageFormatter this function isn't used........
PercentageFormatter use incorrectly populated $items input for calculations....

That's why StarsFormatter produces correct results, unlike PercentageFormatter.

a_sh’s picture

The same error is affect sorting of records using views and vote:rating as a sort criteria. Sort is using the last vote only.

a_sh’s picture

It seems that problem occur during the population of $elements array. How is it populated by votes data?

a_sh’s picture

Here is a related issue:

Make rating average indexable on Search API
cite
"I tried to index a field rating to a Solr Search API index, but the field only contains the last vote."

The same as this issue.

a_sh’s picture

And one more
Fivestar raiting formatter doesn't show average correctly (3167686)

tr’s picture

Version: 8.x-1.x-dev » 3.0.x-dev