And one more
Fivestar raiting formatter doesn't show average correctly (3167686)
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.
It seems that problem occur during the population of $elements array. How is it populated by votes data?
The same error is affect sorting of records using views and vote:rating as a sort criteria. Sort is using the last vote only.
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.
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) } }
The dump of StarsFormatter.php $elements output
array(1) { [0]=> array(29) { ["#attributes"]=> array(3) { ["class"]=> array(1) @ [0]=> string(15) "fivestar-form-2" ["id"]=> string(7) "vote--2" ["data-drupal-selector"]=> string(15) "fivestar-form-2" } ["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(4) "vote" ["#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) "60" ["#values"]=> array(3) @ ["vote_sum"]=> string(3) "930" ["vote_count"]=> string(2) "12" ["vote_average"]=> string(4) "77.5" ["#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"]=> boo array ( 0 => array ( '#theme' => 'fivestar_formatter_percentage', '#instance_settings' => array ( 'stars' => 10, 'allow_clear' => false, 'allow_revote' => true, 'allow_ownvote' => true, 'rated_while' => 'viewing', 'enable_voting_target' => false, 'target_bridge_field' => '', 'target_fivestar_field' => '', 'vote_type' => 'tastevote', ), '#item' => array ( 'user' => 1, 'average' => 70, 'count' => 1, ), ), )ratel(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(12) "edit-vote--3" ["#name"]=> string(4) "vote" ["#value"]=> bool(false) ["vote"]=> array(31) { ["#type"]=> string(6) "select" ["#description"]=> array(5) @ ["#average_rating"]=> string(4) "77.5" ["#votes"]=> string(2) "12" ["#theme"]=> string(16) "fivestar_summary" ["#stars"]=> int(10) ["#microdata"]=> NULL
As an example.
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.
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.
It seems to be not the same issue.
I did check the content of patch in issue #2918679 and it is included into corresponding formatters files in my installation.
However, output is totally incorrect for both Rating and Percentage Formatter.