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.

122 lines
5.3 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(selection_add=[('dropdown', 'Dropdown')], string='Answer Type')
  7. matrix_subtype = fields.Selection(related='question_id.matrix_subtype')
  8. value_id = fields.Many2one('survey.label.value', string="Value Dropdown")
  9. @api.model
  10. def save_line_matrix(self, user_input_id, question, post, answer_tag):
  11. vals = {
  12. 'user_input_id': user_input_id,
  13. 'question_id': question.id,
  14. 'survey_id': question.survey_id.id,
  15. 'skipped': False
  16. }
  17. suil = self.search([
  18. ('user_input_id', '=', user_input_id),
  19. ('survey_id', '=', question.survey_id.id),
  20. ('question_id', '=', question.id)
  21. ])
  22. suil.sudo().unlink()
  23. no_answers = True
  24. ca_dict = dict_keys_startswith(post, answer_tag + '_')
  25. comment_answer = ca_dict.pop(("%s_%s" % (answer_tag, 'comment')), '').strip()
  26. if comment_answer:
  27. vals.update({
  28. 'answer_type': 'text',
  29. 'value_text': comment_answer,
  30. })
  31. self.create(vals)
  32. no_answers = False
  33. if question.matrix_subtype == 'simple':
  34. for row in question.labels_ids_2:
  35. a_tag = "%s_%s" % (answer_tag, row.id)
  36. if a_tag in ca_dict:
  37. no_answers = False
  38. vals.update({
  39. 'answer_type': 'suggestion',
  40. 'value_suggested': ca_dict[a_tag],
  41. 'value_suggested_row': row.id,
  42. })
  43. self.create(vals)
  44. elif question.matrix_subtype == 'multiple':
  45. for col in question.labels_ids:
  46. for row in question.labels_ids_2:
  47. a_tag = "%s_%s_%s" % (answer_tag, row.id, col.id)
  48. if a_tag in ca_dict:
  49. no_answers = False
  50. vals.update({
  51. 'answer_type': 'suggestion',
  52. 'value_suggested': col.id,
  53. 'value_suggested_row': row.id,
  54. })
  55. self.create(vals)
  56. elif question.matrix_subtype == 'custom':
  57. for col in question.labels_ids:
  58. for row in question.labels_ids_2:
  59. a_tag = "%s_%s_%s" % (answer_tag, row.id, col.id)
  60. if a_tag in ca_dict:
  61. no_answers = False
  62. if post.get(a_tag):
  63. sline = a_tag.split('_')[-1]
  64. label_obj = question.labels_ids.browse(int(sline))
  65. if label_obj.type == 'textbox':
  66. vals.update({
  67. 'answer_type': 'text',
  68. 'value_suggested': col.id,
  69. 'value_suggested_row': row.id,
  70. 'value_text': post.get(a_tag),
  71. })
  72. elif label_obj.type == 'free_text':
  73. vals.update({
  74. 'answer_type': 'free_text',
  75. 'value_suggested': col.id,
  76. 'value_suggested_row': row.id,
  77. 'value_free_text': post.get(a_tag),
  78. })
  79. elif label_obj.type == 'numerical_box':
  80. vals.update({
  81. 'answer_type': 'number',
  82. 'value_suggested': col.id,
  83. 'value_suggested_row': row.id,
  84. 'value_number': post.get(a_tag),
  85. })
  86. elif label_obj.type == 'date':
  87. vals.update({
  88. 'answer_type': 'date',
  89. 'value_suggested': col.id,
  90. 'value_suggested_row': row.id,
  91. 'value_date': post.get(a_tag),
  92. })
  93. elif label_obj.type == 'dropdown':
  94. vals.update({
  95. 'answer_type': 'dropdown',
  96. 'value_suggested': col.id,
  97. 'value_suggested_row': row.id,
  98. 'value_id': int(post.get(a_tag)),
  99. })
  100. else:
  101. vals.update({
  102. 'answer_type': 'suggestion',
  103. 'value_suggested': col.id,
  104. 'value_suggested_row': row.id})
  105. self.create(vals)
  106. if no_answers:
  107. vals.update({
  108. 'answer_type': None,
  109. 'skipped': True,
  110. })
  111. self.create(vals)
  112. return True