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.

147 lines
6.1 KiB

  1. # -*- coding: utf-8 -*-
  2. from odoo import models, fields, api
  3. from odoo.addons.survey.models.survey import dict_keys_startswith
  4. class SurveyUserInputLine(models.Model):
  5. _inherit = "survey.user_input_line"
  6. answer_type = fields.Selection(
  7. selection_add=[("dropdown", "Dropdown")], string="Answer Type"
  8. )
  9. matrix_subtype = fields.Selection(related="question_id.matrix_subtype")
  10. value_id = fields.Many2one("survey.label.value", string="Value Dropdown")
  11. @api.model
  12. def save_line_matrix(self, user_input_id, question, post, answer_tag):
  13. vals = {
  14. "user_input_id": user_input_id,
  15. "question_id": question.id,
  16. "survey_id": question.survey_id.id,
  17. "skipped": False,
  18. }
  19. suil = self.search(
  20. [
  21. ("user_input_id", "=", user_input_id),
  22. ("survey_id", "=", question.survey_id.id),
  23. ("question_id", "=", question.id),
  24. ]
  25. )
  26. suil.sudo().unlink()
  27. no_answers = True
  28. ca_dict = dict_keys_startswith(post, answer_tag + "_")
  29. comment_answer = ca_dict.pop(("%s_%s" % (answer_tag, "comment")), "").strip()
  30. if comment_answer:
  31. vals.update(
  32. {
  33. "answer_type": "text",
  34. "value_text": comment_answer,
  35. }
  36. )
  37. self.create(vals)
  38. no_answers = False
  39. if question.matrix_subtype == "simple":
  40. for row in question.labels_ids_2:
  41. a_tag = "%s_%s" % (answer_tag, row.id)
  42. if a_tag in ca_dict:
  43. no_answers = False
  44. vals.update(
  45. {
  46. "answer_type": "suggestion",
  47. "value_suggested": ca_dict[a_tag],
  48. "value_suggested_row": row.id,
  49. }
  50. )
  51. self.create(vals)
  52. elif question.matrix_subtype == "multiple":
  53. for col in question.labels_ids:
  54. for row in question.labels_ids_2:
  55. a_tag = "%s_%s_%s" % (answer_tag, row.id, col.id)
  56. if a_tag in ca_dict:
  57. no_answers = False
  58. vals.update(
  59. {
  60. "answer_type": "suggestion",
  61. "value_suggested": col.id,
  62. "value_suggested_row": row.id,
  63. }
  64. )
  65. self.create(vals)
  66. elif question.matrix_subtype == "custom":
  67. for col in question.labels_ids:
  68. for row in question.labels_ids_2:
  69. a_tag = "%s_%s_%s" % (answer_tag, row.id, col.id)
  70. if a_tag in ca_dict:
  71. no_answers = False
  72. if post.get(a_tag):
  73. sline = a_tag.split("_")[-1]
  74. label_obj = question.labels_ids.browse(int(sline))
  75. if label_obj.type == "textbox":
  76. vals.update(
  77. {
  78. "answer_type": "text",
  79. "value_suggested": col.id,
  80. "value_suggested_row": row.id,
  81. "value_text": post.get(a_tag),
  82. }
  83. )
  84. elif label_obj.type == "free_text":
  85. vals.update(
  86. {
  87. "answer_type": "free_text",
  88. "value_suggested": col.id,
  89. "value_suggested_row": row.id,
  90. "value_free_text": post.get(a_tag),
  91. }
  92. )
  93. elif label_obj.type == "numerical_box":
  94. vals.update(
  95. {
  96. "answer_type": "number",
  97. "value_suggested": col.id,
  98. "value_suggested_row": row.id,
  99. "value_number": post.get(a_tag),
  100. }
  101. )
  102. elif label_obj.type == "date":
  103. vals.update(
  104. {
  105. "answer_type": "date",
  106. "value_suggested": col.id,
  107. "value_suggested_row": row.id,
  108. "value_date": post.get(a_tag),
  109. }
  110. )
  111. elif label_obj.type == "dropdown":
  112. vals.update(
  113. {
  114. "answer_type": "dropdown",
  115. "value_suggested": col.id,
  116. "value_suggested_row": row.id,
  117. "value_id": int(post.get(a_tag)),
  118. }
  119. )
  120. else:
  121. vals.update(
  122. {
  123. "answer_type": "suggestion",
  124. "value_suggested": col.id,
  125. "value_suggested_row": row.id,
  126. }
  127. )
  128. self.create(vals)
  129. if no_answers:
  130. vals.update(
  131. {
  132. "answer_type": None,
  133. "skipped": True,
  134. }
  135. )
  136. self.create(vals)
  137. return True