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.

95 lines
3.4 KiB

  1. from odoo import models, fields, api, _
  2. from odoo.osv.expression import normalize_domain, AND
  3. class SurveySurvey(models.Model):
  4. _inherit = "survey.survey"
  5. answer_start_count = fields.Integer(
  6. "Started answer count", compute="_compute_answer_statistics"
  7. )
  8. answer_start_ratio = fields.Integer(
  9. string="Started answer ratio", compute="_compute_answer_statistics"
  10. )
  11. answer_done_ratio = fields.Integer(
  12. string="Completed answer ratio", compute="_compute_answer_statistics"
  13. )
  14. # COMPUTES
  15. def _compute_answer_statistics(self):
  16. default_vals = {
  17. "answer_count": 0,
  18. "answer_start_count": 0,
  19. "answer_done_count": 0,
  20. "answer_start_ratio": 0,
  21. "answer_done_ratio": 0,
  22. }
  23. stat = dict((sid, default_vals) for sid in self.ids)
  24. UserInput = self.env["survey.user_input"]
  25. base_domain = [
  26. ("survey_id", "in", self.ids),
  27. ("invite_token", "!=", False),
  28. ]
  29. read_group_res = UserInput._read_group(
  30. base_domain,
  31. ["survey_id", "state"],
  32. ["survey_id", "state"],
  33. lazy=False,
  34. )
  35. for item in read_group_res:
  36. stat[item["survey_id"][0]]["answer_count"] += item["__count"]
  37. if item["state"] in ["in_progress", "done"]:
  38. stat[item["survey_id"][0]]["answer_start_count"] += item["__count"]
  39. if item["state"] == "done":
  40. stat[item["survey_id"][0]]["answer_done_count"] += item["__count"]
  41. for survey_stats in stat.values():
  42. survey_stats["answer_start_ratio"] = (
  43. survey_stats["answer_start_count"]
  44. / (survey_stats["answer_count"] or 1)
  45. * 100
  46. )
  47. survey_stats["answer_done_ratio"] = (
  48. survey_stats["answer_done_count"]
  49. / (survey_stats["answer_count"] or 1)
  50. * 100
  51. )
  52. for survey in self:
  53. survey.update(stat.get(survey._origin.id, default_vals))
  54. @api.depends("answer_start_count", "answer_count")
  55. def _get_answer_start_ratio(self):
  56. for survey in self:
  57. survey.answer_start_ratio = int(
  58. round(100 * (survey.answer_start_count) / (survey.answer_count or 1), 0)
  59. )
  60. @api.depends("answer_done_count", "answer_count")
  61. def _get_answer_done_ratio(self):
  62. for survey in self:
  63. survey.answer_done_ratio = int(
  64. round(100 * survey.answer_done_count / (survey.answer_count or 1), 0)
  65. )
  66. # ACTIONS
  67. def action_survey_user_input(self):
  68. ctx = dict(self.env.context)
  69. search_done = ctx.get("search_default_completed", None)
  70. action = super(SurveySurvey, self).action_survey_user_input()
  71. if ctx.get("search_default_shared_invite", False):
  72. action["display_name"] += _(" (shared)")
  73. if search_done is not None:
  74. act_ctx = action.get("context") or dict()
  75. if isinstance(act_ctx, str):
  76. act_ctx = eval(act_ctx)
  77. if "search_default_completed" in act_ctx and bool(
  78. act_ctx["search_default_completed"]
  79. ) is not bool(search_done):
  80. act_ctx["search_default_completed"] = int(bool(search_done))
  81. action["context"] = act_ctx
  82. return action