Odoo modules related to surveys
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. from odoo import models, fields, api, _
  2. class SurveySurvey(models.Model):
  3. _inherit = "survey.survey"
  4. answer_selection = fields.Boolean(
  5. string="Manual selection",
  6. help="Checking this box allows you to select completed real answers (regardless of the scoring success, if enable).",
  7. )
  8. answer_selected_count = fields.Integer(
  9. "Number of selected answers", compute="_compute_selected_answer"
  10. )
  11. answer_selected_ratio = fields.Integer(
  12. "Ratio of selected anwers", compute="_compute_selected_answer"
  13. )
  14. @api.depends("user_input_ids", "user_input_ids.select")
  15. def _compute_selected_answer(self):
  16. default_vals = {
  17. "answer_count": 0,
  18. "answer_selected_count": 0,
  19. }
  20. stats = dict((sid, default_vals) for sid in self.ids)
  21. UserInput = self.env["survey.user_input"]
  22. base_domain = [
  23. ("survey_id", "in", self.ids),
  24. ("state", "=", "done"),
  25. ("test_entry", "=", False),
  26. ]
  27. read_group_res = UserInput._read_group(
  28. base_domain,
  29. ["survey_id", "select"],
  30. ["survey_id", "select"],
  31. lazy=False,
  32. )
  33. for item in read_group_res:
  34. stats[item["survey_id"][0]]["answer_count"] += item["__count"]
  35. if item["select"] == "done":
  36. stats[item["survey_id"][0]]["answer_selected_count"] += item["__count"]
  37. for survey_stats in stats.values():
  38. survey_stats["answer_selected_ratio"] = (
  39. survey_stats["answer_selected_count"]
  40. / (survey_stats["answer_count"] or 1)
  41. * 100
  42. )
  43. for survey in self:
  44. survey.update(stats.get(survey._origin.id, default_vals))
  45. # ACTIONS
  46. def action_survey_user_input(self):
  47. action = super(SurveySurvey, self).action_survey_user_input()
  48. if self.env.context.get("search_default_selected", False):
  49. action["display_name"] += _(" selected")
  50. return action