Skip to content

Commit

Permalink
feat: Finalize Eco-Score knowledge panels (openfoodfacts#6017)
Browse files Browse the repository at this point in the history
* Finalize Eco-Score knowledge panels

* Finalize Eco-Score knowledge panels

* fix grade colors

* fix threatened species panel

* fix threatened species panel

* remove empty div if no elements

* add grades to panels

* Make downgraded score for non recyclable materials 79

* add total score details
  • Loading branch information
stephanegigandet committed Oct 26, 2021
1 parent 8e986b5 commit b14375d
Show file tree
Hide file tree
Showing 26 changed files with 616 additions and 163 deletions.
8 changes: 4 additions & 4 deletions html/js/product-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,16 @@ function display_product_summary(target, product) {
var color = "#eee";

if (attribute.status == "known") {
if (attribute.match <= 20) {
if (attribute.grade == "e") {
color = "hsl(0, 100%, 90%)";
}
else if (attribute.match <= 40) {
else if (attribute.grade == "d") {
color = "hsl(30, 100%, 90%)";
}
else if (attribute.match <= 60) {
else if (attribute.grade == "c") {
color = "hsl(60, 100%, 90%)";
}
else if (attribute.match <= 80) {
else if (attribute.grade == "b") {
color = "hsl(90, 100%, 90%)";
}
else {
Expand Down
36 changes: 31 additions & 5 deletions lib/ProductOpener/Attributes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -615,16 +615,16 @@ sub compute_attribute_ecoscore($$$) {
my $match = 0;

# Score ranges from 0 to 100 with some maluses and bonuses that can be added

# Warning: a score of 20 means D grade for the Eco-Score, but a match of 20 is E grade for the attributes
# So we substract 1 to the Eco-Score score to compute the match.
$match = $score - 1;

if ($score < 0) {
$match = 0;
}
elsif ($score > 100) {
$match = 100;
}
else {
$match = $score;
}

$attribute_ref->{match} = $match;

Expand Down Expand Up @@ -1375,10 +1375,11 @@ sub compute_attribute_ingredients_analysis($$$) {
$analysis_tag = "$analysis-status-unknown";
}
}

if (defined $match) {
$attribute_ref->{match} = $match;
}

$attribute_ref->{status} = $status;
$attribute_ref->{icon_url} = "$static_subdomain/images/attributes/$analysis_tag.svg";
# the ingredients_analysis taxonomy contains en:palm-oil and not en:contains-palm-oil
Expand Down Expand Up @@ -1429,6 +1430,31 @@ sub add_attribute_to_group($$$$) {
# Delete fields that are returned only by /api/v2/attribute_groups to list all the available attributes
delete $attribute_ref->{setting_name};
delete $attribute_ref->{setting_note};

# Compute a 5 level grade from the match score
# We do it server side to be sure that clients do it the same way
# and that a Nutri-Score E match of 20 has a grade "e".
if ($attribute_ref->{status} eq "known") {

if ($attribute_ref->{match} <= 20) {
$attribute_ref->{grade} = 'e';
}
elsif ($attribute_ref->{match} <= 40) {
$attribute_ref->{grade} = 'd';
}
elsif ($attribute_ref->{match} <= 60) {
$attribute_ref->{grade} = 'c';
}
elsif ($attribute_ref->{match} <= 80) {
$attribute_ref->{grade} = 'b';
}
else {
$attribute_ref->{grade} = 'a';
}
}
else {
$attribute_ref->{grade} = 'unknown';
}

my $group_ref;
# Select the requested group
Expand Down
6 changes: 4 additions & 2 deletions lib/ProductOpener/Ecoscore.pm
Original file line number Diff line number Diff line change
Expand Up @@ -707,9 +707,10 @@ sub compute_ecoscore($) {
# If a product has the grade A and it contains a non-biodegradable and non-recyclable material, downgrade to B
if (($product_ref->{ecoscore_data}{"grade" . $suffix} eq "a")
and ($product_ref->{ecoscore_data}{adjustments}{packaging}{non_recyclable_and_non_biodegradable_materials} > 0)) {


$product_ref->{"downgraded"} = "non_recyclable_and_non_biodegradable_materials";
$product_ref->{ecoscore_data}{"grade" . $suffix} = "b";
$product_ref->{downgraded} = "non_recyclable_and_non_biodegradable_materials";
$product_ref->{ecoscore_data}{"score" . $suffix} = 79;
}

$log->debug("compute_ecoscore - final score and grade", { score => $product_ref->{"score" . $suffix}, grade => $product_ref->{"grade" . $suffix}}) if $log->is_debug();
Expand Down Expand Up @@ -1441,6 +1442,7 @@ sub localize_ecoscore ($$) {
}

}

}

}
Expand Down
4 changes: 4 additions & 0 deletions lib/ProductOpener/KnowledgePanels.pm
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ sub create_ecoscore_panel($$$) {
$label_panel_data_ref, $product_ref, $target_lc, $target_cc);
}
}

# Add panel for the final Eco-Score of the product
create_panel_from_json_template("ecoscore_total", "api/knowledge-panels/environment/ecoscore/total.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);
}
else {
my $panel_data_ref = {};
Expand Down
76 changes: 58 additions & 18 deletions po/common/common.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5584,8 +5584,8 @@ msgid "Ingredients whose cultivation threatens species"
msgstr "Ingredients whose cultivation threatens species"

msgctxt "ecoscore_no_species_threatening_ingredients"
msgid "No ingredient which cultivation threatens species has been detected."
msgstr "No ingredient which cultivation threatens species has been detected."
msgid "No ingredient threatening species"
msgstr "No ingredient threatening species"

msgctxt "ecoscore_ingredients_unknown"
msgid "The information on the ingredients of this product has not been given."
Expand Down Expand Up @@ -5689,36 +5689,36 @@ msgid "Removing or changing the provided data will be possible only by experimen
msgstr "Removing or changing the provided data will be possible only by experimented contributors on the web site."

msgctxt "ecoscore_packaging_impact_high"
msgid "This product's packaging has an high impact on the environment."
msgstr "This product's packaging has an high impact on the environment."
msgid "Packaging with a high impact"
msgstr "Packaging with a high impact"

msgctxt "ecoscore_packaging_impact_medium"
msgid "This product's packaging has an average impact on the environment."
msgstr "This product's packaging has an average impact on the environment."
msgid "Packaging with a medium impact"
msgstr "Packaging with a medium impact"

msgctxt "ecoscore_packaging_impact_low"
msgid "This product's packaging has a low impact on the environment."
msgstr "This product's packaging has a low impact on the environment."
msgid "Packaging with a low impact"
msgstr "Packaging with a low impact"

msgctxt "ecoscore_packaging_missing_information"
msgid "The information about this product's packaging is missing."
msgstr "The information about this product's packaging is missing."
msgid "Missing packaging information for this product"
msgstr "Missing packaging information for this product"

msgctxt "ecoscore_origins_of_ingredients_impact_high"
msgid "Transportation and origins of ingredients have a high impact."
msgstr "Transportation and origins of ingredients have a high impact."
msgid "Origins of ingredients with a high impact"
msgstr "Origins of ingredients with a high impact"

msgctxt "ecoscore_origins_of_ingredients_impact_medium"
msgid "Transportation and origins of ingredients have a medium impact."
msgstr "Transportation and origins of ingredients have a medium impact."
msgid "Origins of ingredients with a medium impact"
msgstr "Origins of ingredients with a medium impact"

msgctxt "ecoscore_origins_of_ingredients_impact_low"
msgid "Transportation and origins of ingredients have a low impact."
msgstr "Transportation and origins of ingredients have a low impact."
msgid "Origins of ingredients with a low impact"
msgstr "Origins of ingredients with a low impact"

msgctxt "ecoscore_origins_of_ingredients_missing_information"
msgid "Missing information about the origins of ingredients"
msgstr "Missing information about the origins of ingredients"
msgid "Missing origins of ingredients information"
msgstr "Missing origins of ingredients information"

msgctxt "percent_of_ingredients"
msgid "% of ingredients"
Expand Down Expand Up @@ -5827,3 +5827,43 @@ msgstr "Drives deforestation and threatens species such as the orangutan"
msgctxt "contains_palm_oil_description"
msgid "Tropical forests in Asia, Africa and Latin America are destroyed to create and expand oil palm tree plantations. The deforestation contributes to climate change, and it endangers species such as the orangutan, the pigmy elephant and the Sumatran rhino."
msgstr "Tropical forests in Asia, Africa and Latin America are destroyed to create and expand oil palm tree plantations. The deforestation contributes to climate change, and it endangers species such as the orangutan, the pigmy elephant and the Sumatran rhino."

msgctxt "bonus"
msgid "Bonus"
msgstr "Bonus"

msgctxt "malus"
msgid "Malus"
msgstr "Malus"

msgctxt "life_cycle_analysis"
msgid "Life cycle analysis"
msgstr "Life cycle analysis"

msgctxt "ecoscore_bonuses_and_maluses"
msgid "Bonuses and maluses"
msgstr "Bonuses and maluses"

msgctxt "ecoscore_for_this_product"
msgid "Eco-Score for this product"
msgstr "Eco-Score for this product"

msgctxt "average_impact_of_the_category"
msgid "Average impact of products of the same category"
msgstr "Average impact of products of the same category"

msgctxt "ecoscore_sum_of_bonuses_and_maluses"
msgid "Sum of bonuses and maluses"
msgstr "Sum of bonuses and maluses"

msgctxt "ecoscore_sum_of_bonuses_and_maluses_is_capped"
msgid "The sum of bonuses and maluses is capped at +25."
msgstr "The sum of bonuses and maluses is capped at +25."

msgctxt "ecoscore_lca_score"
msgid "Life cycle analysis score"
msgstr "Life cycle analysis score"

msgctxt "ecoscore_downgraded_non_recyclable_and_non_biodegradable_materials"
msgid "The score of products with non-recyclable and non-biodegradable packaging materials is capped at 79 (grade B)."
msgstr "The score of products with non-recyclable and non-biodegradable packaging materials is capped at 79 (grade B)."
76 changes: 58 additions & 18 deletions po/common/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -5601,8 +5601,8 @@ msgid "Ingredients whose cultivation threatens species"
msgstr "Ingredients whose cultivation threatens species"

msgctxt "ecoscore_no_species_threatening_ingredients"
msgid "No ingredient which cultivation threatens species has been detected."
msgstr "No ingredient which cultivation threatens species has been detected."
msgid "No ingredient threatening species"
msgstr "No ingredient threatening species"

msgctxt "ecoscore_ingredients_unknown"
msgid "The information on the ingredients of this product has not been given."
Expand Down Expand Up @@ -5706,36 +5706,36 @@ msgid "Removing or changing the provided data will be possible only by experimen
msgstr "Removing or changing the provided data will be possible only by experimented contributors on the web site."

msgctxt "ecoscore_packaging_impact_high"
msgid "This product's packaging has an high impact on the environment."
msgstr "This product's packaging has an high impact on the environment."
msgid "Packaging with a high impact"
msgstr "Packaging with a high impact"

msgctxt "ecoscore_packaging_impact_medium"
msgid "This product's packaging has an average impact on the environment."
msgstr "This product's packaging has an average impact on the environment."
msgid "Packaging with a medium impact"
msgstr "Packaging with a medium impact"

msgctxt "ecoscore_packaging_impact_low"
msgid "This product's packaging has a low impact on the environment."
msgstr "This product's packaging has a low impact on the environment."
msgid "Packaging with a low impact"
msgstr "Packaging with a low impact"

msgctxt "ecoscore_packaging_missing_information"
msgid "The information about this product's packaging is missing."
msgstr "The information about this product's packaging is missing."
msgid "Missing packaging information for this product"
msgstr "Missing packaging information for this product"

msgctxt "ecoscore_origins_of_ingredients_impact_high"
msgid "Transportation and origins of ingredients have a high impact."
msgstr "Transportation and origins of ingredients have a high impact."
msgid "Origins of ingredients with a high impact"
msgstr "Origins of ingredients with a high impact"

msgctxt "ecoscore_origins_of_ingredients_impact_medium"
msgid "Transportation and origins of ingredients have a medium impact."
msgstr "Transportation and origins of ingredients have a medium impact."
msgid "Origins of ingredients with a medium impact"
msgstr "Origins of ingredients with a medium impact"

msgctxt "ecoscore_origins_of_ingredients_impact_low"
msgid "Transportation and origins of ingredients have a low impact."
msgstr "Transportation and origins of ingredients have a low impact."
msgid "Origins of ingredients with a low impact"
msgstr "Origins of ingredients with a low impact"

msgctxt "ecoscore_origins_of_ingredients_missing_information"
msgid "Missing information about the origins of ingredients"
msgstr "Missing information about the origins of ingredients"
msgid "Missing origins of ingredients information"
msgstr "Missing origins of ingredients information"

msgctxt "percent_of_ingredients"
msgid "% of ingredients"
Expand Down Expand Up @@ -5844,3 +5844,43 @@ msgstr "Drives deforestation and threatens species such as the orangutan"
msgctxt "contains_palm_oil_description"
msgid "Tropical forests in Asia, Africa and Latin America are destroyed to create and expand oil palm tree plantations. The deforestation contributes to climate change, and it endangers species such as the orangutan, the pigmy elephant and the Sumatran rhino."
msgstr "Tropical forests in Asia, Africa and Latin America are destroyed to create and expand oil palm tree plantations. The deforestation contributes to climate change, and it endangers species such as the orangutan, the pigmy elephant and the Sumatran rhino."

msgctxt "bonus"
msgid "Bonus"
msgstr "Bonus"

msgctxt "malus"
msgid "Malus"
msgstr "Malus"

msgctxt "life_cycle_analysis"
msgid "Life cycle analysis"
msgstr "Life cycle analysis"

msgctxt "ecoscore_bonuses_and_maluses"
msgid "Bonuses and maluses"
msgstr "Bonuses and maluses"

msgctxt "ecoscore_for_this_product"
msgid "Eco-Score for this product"
msgstr "Eco-Score for this product"

msgctxt "average_impact_of_the_category"
msgid "Average impact of products of the same category"
msgstr "Average impact of products of the same category"

msgctxt "ecoscore_sum_of_bonuses_and_maluses"
msgid "Sum of bonuses and maluses"
msgstr "Sum of bonuses and maluses"

msgctxt "ecoscore_sum_of_bonuses_and_maluses_is_capped"
msgid "The sum of bonuses and maluses is capped at +25."
msgstr "The sum of bonuses and maluses is capped at +25."

msgctxt "ecoscore_lca_score"
msgid "Life cycle analysis score"
msgstr "Life cycle analysis score"

msgctxt "ecoscore_downgraded_non_recyclable_and_non_biodegradable_materials"
msgid "The score of products with non-recyclable and non-biodegradable packaging materials is capped at 79 (grade B)."
msgstr "The score of products with non-recyclable and non-biodegradable packaging materials is capped at 79 (grade B)."
Loading

0 comments on commit b14375d

Please sign in to comment.