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.

137 lines
6.4 KiB

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