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.

118 lines
5.7 KiB

  1. from odoo import api, fields, models
  2. def dict_keys_startswith(dictionary, string):
  3. """Returns a dictionary containing the elements of <dict> whose keys start with <string>.
  4. .. note::
  5. This function uses dictionary comprehensions (Python >= 2.7)
  6. """
  7. return {k: v for k, v in dictionary.items() if k.startswith(string)}
  8. class SurveyQuestion(models.Model):
  9. _inherit = 'survey.question'
  10. matrix_subtype = fields.Selection([('simple', 'One choice per row'),
  11. ('multiple', 'Multiple choices per row'), ('custom_row', 'Custom Matrix')], string='Matrix Type', default='simple')
  12. @api.multi
  13. def validate_matrix(self, post, answer_tag):
  14. self.ensure_one()
  15. errors = {}
  16. if self.constr_mandatory:
  17. lines_number = len(self.labels_ids_2)
  18. answer_candidates = dict_keys_startswith(post, answer_tag)
  19. answer_candidates.pop(("%s_%s" % (answer_tag, 'comment')), '').strip()
  20. # Number of lines that have been answered
  21. if self.matrix_subtype == 'simple':
  22. answer_number = len(answer_candidates)
  23. elif self.matrix_subtype == 'multiple':
  24. answer_number = len({sk.rsplit('_', 1)[0] for sk in answer_candidates})
  25. elif self.matrix_subtype == 'custom_row':
  26. answer_number = len({sk.rsplit('_', 1)[0] for sk in answer_candidates})
  27. else:
  28. raise RuntimeError("Invalid matrix subtype")
  29. # Validate that each line has been answered
  30. if answer_number != lines_number:
  31. errors.update({answer_tag: self.constr_error_msg})
  32. return errors
  33. class SurveyUserInputLine(models.Model):
  34. _inherit = 'survey.user_input_line'
  35. answer_type = fields.Selection([
  36. ('text', 'Text'),
  37. ('number', 'Number'),
  38. ('date', 'Date'),
  39. ('free_text', 'Free Text'),
  40. ('suggestion', 'Suggestion'), ('dropdown', 'Dropdown')], string='Answer Type')
  41. value_dropdown = fields.Many2one('survey.label.value', string="Value Dropdown")
  42. matrix_subtype_id = fields.Selection(related='question_id.matrix_subtype', string="Matrix subtype")
  43. @api.model
  44. def save_line_matrix(self, user_input_id, question, post, answer_tag):
  45. vals = {
  46. 'user_input_id': user_input_id,
  47. 'question_id': question.id,
  48. 'survey_id': question.survey_id.id,
  49. 'skipped': False
  50. }
  51. old_uil = self.search([
  52. ('user_input_id', '=', user_input_id),
  53. ('survey_id', '=', question.survey_id.id),
  54. ('question_id', '=', question.id)
  55. ])
  56. old_uil.sudo().unlink()
  57. no_answers = True
  58. ca_dict = dict_keys_startswith(post, answer_tag + '_')
  59. comment_answer = ca_dict.pop(("%s_%s" % (answer_tag, 'comment')), '').strip()
  60. if comment_answer:
  61. vals.update({'answer_type': 'text', 'value_text': comment_answer})
  62. self.create(vals)
  63. no_answers = False
  64. if question.matrix_subtype == 'simple':
  65. for row in question.labels_ids_2:
  66. a_tag = "%s_%s" % (answer_tag, row.id)
  67. if a_tag in ca_dict:
  68. no_answers = False
  69. vals.update({'answer_type': 'suggestion', 'value_suggested': ca_dict[a_tag], 'value_suggested_row': row.id})
  70. self.create(vals)
  71. elif question.matrix_subtype == 'multiple':
  72. for col in question.labels_ids:
  73. for row in question.labels_ids_2:
  74. a_tag = "%s_%s_%s" % (answer_tag, row.id, col.id)
  75. if a_tag in ca_dict:
  76. no_answers = False
  77. vals.update({'answer_type': 'suggestion', 'value_suggested': col.id, 'value_suggested_row': row.id})
  78. self.create(vals)
  79. elif question.matrix_subtype == 'custom_row':
  80. for col in question.labels_ids:
  81. for row in question.labels_ids_2:
  82. a_tag = "%s_%s_%s" % (answer_tag, row.id, col.id)
  83. if a_tag in ca_dict:
  84. no_answers = False
  85. if post.get(a_tag):
  86. sline = a_tag.split('_')[-1]
  87. label_obj = question.labels_ids.browse(int(sline))
  88. if label_obj.type == 'textbox':
  89. vals.update({'answer_type': 'text', 'value_suggested': col.id, 'value_suggested_row': row.id, 'value_text': post.get(a_tag)})
  90. elif label_obj.type == 'free_text':
  91. vals.update({'answer_type': 'free_text', 'value_suggested': col.id, 'value_suggested_row': row.id, 'value_free_text': post.get(a_tag)})
  92. elif label_obj.type == 'numerical_box':
  93. vals.update({'answer_type': 'number', 'value_suggested': col.id, 'value_suggested_row': row.id, 'value_number': post.get(a_tag)})
  94. elif label_obj.type == 'date':
  95. vals.update({'answer_type': 'date', 'value_suggested': col.id, 'value_suggested_row': row.id, 'value_date': post.get(a_tag)})
  96. elif label_obj.type == 'dropdown':
  97. vals.update({'answer_type': 'dropdown', 'value_suggested': col.id, 'value_suggested_row': row.id, 'value_dropdown': int(post.get(a_tag))})
  98. else:
  99. vals.update({'answer_type': 'suggestion', 'value_suggested': col.id, 'value_suggested_row': row.id})
  100. self.create(vals)
  101. if no_answers:
  102. vals.update({'answer_type': None, 'skipped': True})
  103. self.create(vals)
  104. return True