Skip to content

Commit

Permalink
[MIG] survey_question_type_five_star: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
youstina.nabil committed May 9, 2024
1 parent ef5a882 commit 17ccba9
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 207 deletions.
29 changes: 13 additions & 16 deletions survey_question_type_five_star/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ Survey five stars question type
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsurvey-lightgray.png?logo=github
:target: https://github.com/OCA/survey/tree/17.0/survey_question_type_five_star
:target: https://github.com/OCA/survey/tree/16.0/survey_question_type_five_star
:alt: OCA/survey
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/survey-17-0/survey-17-0-survey_question_type_five_star
:target: https://translation.odoo-community.org/projects/survey-16-0/survey-16-0-survey_question_type_five_star
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/survey&target_branch=17.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/survey&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand All @@ -40,40 +40,37 @@ Usage

To use this module, you need to:

1. Go to Surveys, create a new survey, add a question of type "Five
Stars Rating"
#. Go to Surveys, create a new survey, add a question of type "Five Stars Rating"

|image|

.. |image| image:: https://raw.githubusercontent.com/OCA/survey/survey_question_type_five_star/static/description/five_star_type.png
.. image:: https://raw.githubusercontent.com/OCA/survey/survey_question_type_five_star/static/description/five_star_type.png

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/survey/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/survey/issues/new?body=module:%20survey_question_type_five_star%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/survey/issues/new?body=module:%20survey_question_type_five_star%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------
~~~~~~~

* ACSONE SA/NV

Contributors
------------
~~~~~~~~~~~~

- Souheil Bejaoui <souheil.bejaoui@acsone.eu>
- Olga Marco <olga.marco@creublanca.es>
- Benoit Aimont <benoit.aimont@acsone.eu>
* Souheil Bejaoui <souheil.bejaoui@acsone.eu>
* Olga Marco <olga.marco@creublanca.es>
* Benoit Aimont <benoit.aimont@acsone.eu>

Maintainers
-----------
~~~~~~~~~~~

This module is maintained by the OCA.

Expand All @@ -85,6 +82,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/survey <https://github.com/OCA/survey/tree/17.0/survey_question_type_five_star>`_ project on GitHub.
This module is part of the `OCA/survey <https://github.com/OCA/survey/tree/16.0/survey_question_type_five_star>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
91 changes: 45 additions & 46 deletions survey_question_type_five_star/models/survey_question.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
# Copyright 2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import collections
import contextlib

from odoo import fields, models, tools
from odoo import fields, models


class SurveyQuestion(models.Model):
_inherit = "survey.question"

question_type = fields.Selection(
selection_add=[("star_rate", "Five Stars Rating")], ondelete={"foo": "set null"}
)
_inherit = "survey.question"

def _get_stats_summary_data(self, user_input_lines):
stats = super()._get_stats_summary_data(user_input_lines)
if self.question_type in ["star_rate"]:
stats.update(self._get_stats_summary_data_numerical(user_input_lines))
stats.update(
{
"common_lines": collections.Counter(
user_input_lines.filtered(lambda line: not line.skipped).mapped(
"value_numerical_box"
)
).most_common(5),
"right_inputs_count": len(
user_input_lines.filtered(
lambda line: line.answer_is_correct
).mapped("user_input_id")
),
}
)
return stats
question_type = fields.Selection(
selection_add=[("star_rate", "Five Stars Rating")],
ondelete={"foo": "set null"},
)

def validate_star_rate(self, post, answer_tag):
self.ensure_one()
errors = {}
answer = post[answer_tag].strip()
# Empty answer to mandatory question
if self.constr_mandatory and not answer:
errors.update({answer_tag: self.constr_error_msg})
# Checks if user input is a number
if answer:
try:
floatanswer = float(answer)
except ValueError:
errors.update({answer_tag: "This is not a number"})
return errors
# Answer is not in the right range
with tools.ignore(Exception):
# 0 answer to mandatory question
if self.constr_mandatory:
if floatanswer == 0:
errors.update({answer_tag: self.constr_error_msg})
if not (0 <= floatanswer <= 5):
errors.update({answer_tag: "Answer is not in the right range"})
def _get_stats_summary_data(self, user_input_lines):
stats = super()._get_stats_summary_data(user_input_lines)
if self.question_type in ["star_rate"]:
stats.update(self._get_stats_summary_data_numerical(user_input_lines))
stats.update({
"common_lines":
collections.Counter(
user_input_lines.filtered(lambda line: not line.skipped).mapped(
"value_numerical_box")).most_common(5),
"right_inputs_count":
len(
user_input_lines.filtered(lambda line: line.answer_is_correct).mapped(
"user_input_id")),
})
return stats

def validate_star_rate(self, post, answer_tag):
self.ensure_one()
errors = {}
answer = post[answer_tag].strip()
# Empty answer to mandatory question
if self.constr_mandatory and not answer:
errors.update({answer_tag: self.constr_error_msg})
# Checks if user input is a number
if answer:
try:
floatanswer = float(answer)
except ValueError:
errors.update({answer_tag: "This is not a number"})
return errors
# Answer is not in the right range
with contextlib.suppress(Exception):
# 0 answer to mandatory question
if self.constr_mandatory:
if floatanswer == 0:
errors.update({answer_tag: self.constr_error_msg})
if not (0 <= floatanswer <= 5):
errors.update({answer_tag: "Answer is not in the right range"})
return errors
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
class SurveyUserInput(models.Model):
_inherit = "survey.user_input"

def save_lines(self, question, answer, comment=None):
def _save_lines(self, question, answer, comment=None, overwrite_existing=True):
old_answers = self.env["survey.user_input.line"].search(
[("user_input_id", "=", self.id), ("question_id", "=", question.id)]
)

if question.question_type in ["star_rate"]:
self._save_line_simple_answer(question, old_answers, answer)
else:
return super().save_lines(question, answer, comment=comment)
return self._save_line_simple_answer(question, old_answers, answer)

return super()._save_lines(question, answer, comment=comment, overwrite_existing=overwrite_existing)

def _get_line_answer_values(self, question, answer, answer_type):
vals = super()._get_line_answer_values(question, answer, answer_type)
Expand Down
3 changes: 0 additions & 3 deletions survey_question_type_five_star/pyproject.toml

This file was deleted.

3 changes: 0 additions & 3 deletions survey_question_type_five_star/readme/CONTRIBUTORS.md

This file was deleted.

3 changes: 3 additions & 0 deletions survey_question_type_five_star/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Souheil Bejaoui <souheil.bejaoui@acsone.eu>
* Olga Marco <olga.marco@creublanca.es>
* Benoit Aimont <benoit.aimont@acsone.eu>
6 changes: 0 additions & 6 deletions survey_question_type_five_star/readme/USAGE.md

This file was deleted.

5 changes: 5 additions & 0 deletions survey_question_type_five_star/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
To use this module, you need to:

#. Go to Surveys, create a new survey, add a question of type "Five Stars Rating"

.. image:: /OCA/survey/survey_question_type_five_star/static/description/five_star_type.png
23 changes: 10 additions & 13 deletions survey_question_type_five_star/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand All @@ -8,11 +9,10 @@

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.
See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +275,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: gray; } /* line numbers */
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic, pre.problematic {
span.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -369,7 +369,7 @@ <h1 class="title">Survey five stars question type</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:990c7089fbe8a6f87264093fb024a545cd4472a71fcda84ee5f6fbf840a07ffa
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/survey/tree/17.0/survey_question_type_five_star"><img alt="OCA/survey" src="https://img.shields.io/badge/github-OCA%2Fsurvey-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/survey-17-0/survey-17-0-survey_question_type_five_star"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/survey&amp;target_branch=17.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/survey/tree/16.0/survey_question_type_five_star"><img alt="OCA/survey" src="https://img.shields.io/badge/github-OCA%2Fsurvey-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/survey-16-0/survey-16-0-survey_question_type_five_star"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/survey&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module adds five stars rating question type for survey page</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
Expand All @@ -388,17 +388,16 @@ <h1 class="title">Survey five stars question type</h1>
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>To use this module, you need to:</p>
<ol class="arabic simple">
<li>Go to Surveys, create a new survey, add a question of type “Five
Stars Rating”</li>
<li>Go to Surveys, create a new survey, add a question of type “Five Stars Rating”</li>
</ol>
<p><img alt="image" src="https://raw.githubusercontent.com/OCA/survey/survey_question_type_five_star/static/description/five_star_type.png" /></p>
<img alt="https://raw.githubusercontent.com/OCA/survey/survey_question_type_five_star/static/description/five_star_type.png" src="https://raw.githubusercontent.com/OCA/survey/survey_question_type_five_star/static/description/five_star_type.png" />
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/survey/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/survey/issues/new?body=module:%20survey_question_type_five_star%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/survey/issues/new?body=module:%20survey_question_type_five_star%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand All @@ -420,13 +419,11 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/survey/tree/17.0/survey_question_type_five_star">OCA/survey</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/survey/tree/16.0/survey_question_type_five_star">OCA/survey</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
71 changes: 35 additions & 36 deletions survey_question_type_five_star/static/src/js/survey.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
/* Copyright 2018 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).*/
odoo.define("survey_question_type_five_star.survey", function (require) {
"use strict";
var SurveyFormWidget = require("survey.form");
SurveyFormWidget.include({
events: _.extend({}, SurveyFormWidget.prototype.events, {
"click .rate > label": "_onClickFiveStarLabel",
}),
_onClickFiveStarLabel: function (event) {
if (this.readonly) {
return;
}
var target = event.target;
var label_items = $(target).parent().find("label");
odoo.define("survey_question_type_five_star", [], function (require) {
"use strict";

var value = label_items.length - $(target).index();
label_items.removeClass("checked fa-star").addClass("fa-star-o");
label_items
.slice($(target).index())
.addClass("checked fa-star")
.removeClass("fa-star-o");
var $input = $(target).parent().find("input");
$input.val(value);
// We will trigger the change in order to make it compatible with conditional.
// If it is not installed, it has no effects
$input.trigger("change");
},
_prepareSubmitValues: function (formData, params) {
this._super.apply(this, arguments);
this.$("[data-question-type]").each(function () {
switch ($(this).data("questionType")) {
case "star_rate":
params[this.name] = this.value;
break;
}
});
},
});
const survey_form = odoo.loader.modules.get("@survey/js/survey_form")[Symbol.for("default")];

survey_form.include({
events: $.extend({}, survey_form.prototype.events, {
"click .rate > label": "_onClickFiveStarLabel",
}),
_onClickFiveStarLabel: function (event) {
if (this.readonly) {
return;
}
var target = event.target;
var label_items = $(target).parent().find("label");

var value = label_items.length - $(target).index();
label_items.removeClass("checked fa-star").addClass("fa-star-o");
label_items.slice($(target).index()).addClass("checked fa-star").removeClass("fa-star-o");
var $input = $(target).parent().find("input");
$input.val(value);
// We will trigger the change in order to make it compatible with conditional.
// If it is not installed, it has no effects
$input.trigger("change");
},
_prepareSubmitValues: function (formData, params) {
this._super.apply(this, arguments);
this.$("[data-question-type]").each(function () {
switch ($(this).data("questionType")) {
case "star_rate":
params[this.name] = this.value;
break;
}
});
},
});
});
Loading

0 comments on commit 17ccba9

Please sign in to comment.