survey_question.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import collections
  4. import contextlib
  5. import json
  6. import itertools
  7. import operator
  8. from odoo import api, fields, models, tools, _
  9. from odoo.exceptions import UserError, ValidationError
  10. class SurveyQuestion(models.Model):
  11. """ Questions that will be asked in a survey.
  12. Each question can have one of more suggested answers (eg. in case of
  13. multi-answer checkboxes, radio buttons...).
  14. Technical note:
  15. survey.question is also the model used for the survey's pages (with the "is_page" field set to True).
  16. A page corresponds to a "section" in the interface, and the fact that it separates the survey in
  17. actual pages in the interface depends on the "questions_layout" parameter on the survey.survey model.
  18. Pages are also used when randomizing questions. The randomization can happen within a "page".
  19. Using the same model for questions and pages allows to put all the pages and questions together in a o2m field
  20. (see survey.survey.question_and_page_ids) on the view side and easily reorganize your survey by dragging the
  21. items around.
  22. It also removes on level of encoding by directly having 'Add a page' and 'Add a question'
  23. links on the tree view of questions, enabling a faster encoding.
  24. However, this has the downside of making the code reading a little bit more complicated.
  25. Efforts were made at the model level to create computed fields so that the use of these models
  26. still seems somewhat logical. That means:
  27. - A survey still has "page_ids" (question_and_page_ids filtered on is_page = True)
  28. - These "page_ids" still have question_ids (questions located between this page and the next)
  29. - These "question_ids" still have a "page_id"
  30. That makes the use and display of these information at view and controller levels easier to understand.
  31. """
  32. _name = 'survey.question'
  33. _description = 'Survey Question'
  34. _rec_name = 'title'
  35. _order = 'sequence,id'
  36. # question generic data
  37. title = fields.Char('Title', required=True, translate=True)
  38. description = fields.Html(
  39. 'Description', translate=True, sanitize=True, sanitize_overridable=True,
  40. help="Use this field to add additional explanations about your question or to illustrate it with pictures or a video")
  41. question_placeholder = fields.Char("Placeholder", translate=True, compute="_compute_question_placeholder", store=True, readonly=False)
  42. background_image = fields.Image("Background Image", compute="_compute_background_image", store=True, readonly=False)
  43. background_image_url = fields.Char("Background Url", compute="_compute_background_image_url")
  44. survey_id = fields.Many2one('survey.survey', string='Survey', ondelete='cascade')
  45. scoring_type = fields.Selection(related='survey_id.scoring_type', string='Scoring Type', readonly=True)
  46. sequence = fields.Integer('Sequence', default=10)
  47. # page specific
  48. is_page = fields.Boolean('Is a page?')
  49. question_ids = fields.One2many('survey.question', string='Questions', compute="_compute_question_ids")
  50. questions_selection = fields.Selection(
  51. related='survey_id.questions_selection', readonly=True,
  52. help="If randomized is selected, add the number of random questions next to the section.")
  53. random_questions_count = fields.Integer(
  54. '# Questions Randomly Picked', default=1,
  55. help="Used on randomized sections to take X random questions from all the questions of that section.")
  56. # question specific
  57. page_id = fields.Many2one('survey.question', string='Page', compute="_compute_page_id", store=True)
  58. question_type = fields.Selection([
  59. ('simple_choice', 'Multiple choice: only one answer'),
  60. ('multiple_choice', 'Multiple choice: multiple answers allowed'),
  61. ('text_box', 'Multiple Lines Text Box'),
  62. ('char_box', 'Single Line Text Box'),
  63. ('numerical_box', 'Numerical Value'),
  64. ('date', 'Date'),
  65. ('datetime', 'Datetime'),
  66. ('matrix', 'Matrix')], string='Question Type',
  67. compute='_compute_question_type', readonly=False, store=True)
  68. is_scored_question = fields.Boolean(
  69. 'Scored', compute='_compute_is_scored_question',
  70. readonly=False, store=True, copy=True,
  71. help="Include this question as part of quiz scoring. Requires an answer and answer score to be taken into account.")
  72. # -- scoreable/answerable simple answer_types: numerical_box / date / datetime
  73. answer_numerical_box = fields.Float('Correct numerical answer', help="Correct number answer for this question.")
  74. answer_date = fields.Date('Correct date answer', help="Correct date answer for this question.")
  75. answer_datetime = fields.Datetime('Correct datetime answer', help="Correct date and time answer for this question.")
  76. answer_score = fields.Float('Score', help="Score value for a correct answer to this question.")
  77. # -- char_box
  78. save_as_email = fields.Boolean(
  79. "Save as user email", compute='_compute_save_as_email', readonly=False, store=True, copy=True,
  80. help="If checked, this option will save the user's answer as its email address.")
  81. save_as_nickname = fields.Boolean(
  82. "Save as user nickname", compute='_compute_save_as_nickname', readonly=False, store=True, copy=True,
  83. help="If checked, this option will save the user's answer as its nickname.")
  84. # -- simple choice / multiple choice / matrix
  85. suggested_answer_ids = fields.One2many(
  86. 'survey.question.answer', 'question_id', string='Types of answers', copy=True,
  87. help='Labels used for proposed choices: simple choice, multiple choice and columns of matrix')
  88. # -- matrix
  89. matrix_subtype = fields.Selection([
  90. ('simple', 'One choice per row'),
  91. ('multiple', 'Multiple choices per row')], string='Matrix Type', default='simple')
  92. matrix_row_ids = fields.One2many(
  93. 'survey.question.answer', 'matrix_question_id', string='Matrix Rows', copy=True,
  94. help='Labels used for proposed choices: rows of matrix')
  95. # -- display & timing options
  96. is_time_limited = fields.Boolean("The question is limited in time",
  97. help="Currently only supported for live sessions.")
  98. time_limit = fields.Integer("Time limit (seconds)")
  99. # -- comments (simple choice, multiple choice, matrix (without count as an answer))
  100. comments_allowed = fields.Boolean('Show Comments Field')
  101. comments_message = fields.Char('Comment Message', translate=True)
  102. comment_count_as_answer = fields.Boolean('Comment is an answer')
  103. # question validation
  104. validation_required = fields.Boolean('Validate entry', compute='_compute_validation_required', readonly=False, store=True)
  105. validation_email = fields.Boolean('Input must be an email')
  106. validation_length_min = fields.Integer('Minimum Text Length', default=0)
  107. validation_length_max = fields.Integer('Maximum Text Length', default=0)
  108. validation_min_float_value = fields.Float('Minimum value', default=0.0)
  109. validation_max_float_value = fields.Float('Maximum value', default=0.0)
  110. validation_min_date = fields.Date('Minimum Date')
  111. validation_max_date = fields.Date('Maximum Date')
  112. validation_min_datetime = fields.Datetime('Minimum Datetime')
  113. validation_max_datetime = fields.Datetime('Maximum Datetime')
  114. validation_error_msg = fields.Char('Validation Error message', translate=True)
  115. constr_mandatory = fields.Boolean('Mandatory Answer')
  116. constr_error_msg = fields.Char('Error message', translate=True)
  117. # answers
  118. user_input_line_ids = fields.One2many(
  119. 'survey.user_input.line', 'question_id', string='Answers',
  120. domain=[('skipped', '=', False)], groups='survey.group_survey_user')
  121. # Conditional display
  122. is_conditional = fields.Boolean(
  123. string='Conditional Display', copy=True, help="""If checked, this question will be displayed only
  124. if the specified conditional answer have been selected in a previous question""")
  125. triggering_question_id = fields.Many2one(
  126. 'survey.question', string="Triggering Question", copy=False, compute="_compute_triggering_question_id",
  127. store=True, readonly=False, help="Question containing the triggering answer to display the current question.",
  128. domain="[('survey_id', '=', survey_id), \
  129. '&', ('question_type', 'in', ['simple_choice', 'multiple_choice']), \
  130. '|', \
  131. ('sequence', '<', sequence), \
  132. '&', ('sequence', '=', sequence), ('id', '<', id)]")
  133. triggering_answer_id = fields.Many2one(
  134. 'survey.question.answer', string="Triggering Answer", copy=False, compute="_compute_triggering_answer_id",
  135. store=True, readonly=False, help="Answer that will trigger the display of the current question.",
  136. domain="[('question_id', '=', triggering_question_id)]")
  137. _sql_constraints = [
  138. ('positive_len_min', 'CHECK (validation_length_min >= 0)', 'A length must be positive!'),
  139. ('positive_len_max', 'CHECK (validation_length_max >= 0)', 'A length must be positive!'),
  140. ('validation_length', 'CHECK (validation_length_min <= validation_length_max)', 'Max length cannot be smaller than min length!'),
  141. ('validation_float', 'CHECK (validation_min_float_value <= validation_max_float_value)', 'Max value cannot be smaller than min value!'),
  142. ('validation_date', 'CHECK (validation_min_date <= validation_max_date)', 'Max date cannot be smaller than min date!'),
  143. ('validation_datetime', 'CHECK (validation_min_datetime <= validation_max_datetime)', 'Max datetime cannot be smaller than min datetime!'),
  144. ('positive_answer_score', 'CHECK (answer_score >= 0)', 'An answer score for a non-multiple choice question cannot be negative!'),
  145. ('scored_datetime_have_answers', "CHECK (is_scored_question != True OR question_type != 'datetime' OR answer_datetime is not null)",
  146. 'All "Is a scored question = True" and "Question Type: Datetime" questions need an answer'),
  147. ('scored_date_have_answers', "CHECK (is_scored_question != True OR question_type != 'date' OR answer_date is not null)",
  148. 'All "Is a scored question = True" and "Question Type: Date" questions need an answer')
  149. ]
  150. # -------------------------------------------------------------------------
  151. # CONSTRAINT METHODS
  152. # -------------------------------------------------------------------------
  153. @api.constrains("is_page")
  154. def _check_question_type_for_pages(self):
  155. invalid_pages = self.filtered(lambda question: question.is_page and question.question_type)
  156. if invalid_pages:
  157. raise ValidationError(_("Question type should be empty for these pages: %s", ', '.join(invalid_pages.mapped('title'))))
  158. # -------------------------------------------------------------------------
  159. # COMPUTE METHODS
  160. # -------------------------------------------------------------------------
  161. @api.depends('question_type')
  162. def _compute_question_placeholder(self):
  163. for question in self:
  164. if question.question_type in ('simple_choice', 'multiple_choice', 'matrix') \
  165. or not question.question_placeholder: # avoid CacheMiss errors
  166. question.question_placeholder = False
  167. @api.depends('is_page')
  168. def _compute_background_image(self):
  169. """ Background image is only available on sections. """
  170. for question in self.filtered(lambda q: not q.is_page):
  171. question.background_image = False
  172. @api.depends('survey_id.access_token', 'background_image', 'page_id', 'survey_id.background_image_url')
  173. def _compute_background_image_url(self):
  174. """ How the background url is computed:
  175. - For a question: it depends on the related section (see below)
  176. - For a section:
  177. - if a section has a background, then we create the background URL using this section's ID
  178. - if not, then we fallback on the survey background url """
  179. base_bg_url = "/survey/%s/%s/get_background_image"
  180. for question in self:
  181. if question.is_page:
  182. background_section_id = question.id if question.background_image else False
  183. else:
  184. background_section_id = question.page_id.id if question.page_id.background_image else False
  185. if background_section_id:
  186. question.background_image_url = base_bg_url % (
  187. question.survey_id.access_token,
  188. background_section_id
  189. )
  190. else:
  191. question.background_image_url = question.survey_id.background_image_url
  192. @api.depends('is_page')
  193. def _compute_question_type(self):
  194. pages = self.filtered(lambda question: question.is_page)
  195. pages.question_type = False
  196. (self - pages).filtered(lambda question: not question.question_type).question_type = 'simple_choice'
  197. @api.depends('survey_id.question_and_page_ids.is_page', 'survey_id.question_and_page_ids.sequence')
  198. def _compute_question_ids(self):
  199. """Will take all questions of the survey for which the index is higher than the index of this page
  200. and lower than the index of the next page."""
  201. for question in self:
  202. if question.is_page:
  203. next_page_index = False
  204. for page in question.survey_id.page_ids:
  205. if page._index() > question._index():
  206. next_page_index = page._index()
  207. break
  208. question.question_ids = question.survey_id.question_ids.filtered(
  209. lambda q: q._index() > question._index() and (not next_page_index or q._index() < next_page_index)
  210. )
  211. else:
  212. question.question_ids = self.env['survey.question']
  213. @api.depends('survey_id.question_and_page_ids.is_page', 'survey_id.question_and_page_ids.sequence')
  214. def _compute_page_id(self):
  215. """Will find the page to which this question belongs to by looking inside the corresponding survey"""
  216. for question in self:
  217. if question.is_page:
  218. question.page_id = None
  219. else:
  220. page = None
  221. for q in question.survey_id.question_and_page_ids.sorted():
  222. if q == question:
  223. break
  224. if q.is_page:
  225. page = q
  226. question.page_id = page
  227. @api.depends('question_type', 'validation_email')
  228. def _compute_save_as_email(self):
  229. for question in self:
  230. if question.question_type != 'char_box' or not question.validation_email:
  231. question.save_as_email = False
  232. @api.depends('question_type')
  233. def _compute_save_as_nickname(self):
  234. for question in self:
  235. if question.question_type != 'char_box':
  236. question.save_as_nickname = False
  237. @api.depends('question_type')
  238. def _compute_validation_required(self):
  239. for question in self:
  240. if not question.validation_required or question.question_type not in ['char_box', 'numerical_box', 'date', 'datetime']:
  241. question.validation_required = False
  242. @api.depends('is_conditional')
  243. def _compute_triggering_question_id(self):
  244. """ Used as an 'onchange' : Reset the triggering question if user uncheck 'Conditional Display'
  245. Avoid CacheMiss : set the value to False if the value is not set yet."""
  246. for question in self:
  247. if not question.is_conditional or question.triggering_question_id is None:
  248. question.triggering_question_id = False
  249. @api.depends('triggering_question_id')
  250. def _compute_triggering_answer_id(self):
  251. """ Used as an 'onchange' : Reset the triggering answer if user unset or change the triggering question
  252. or uncheck 'Conditional Display'.
  253. Avoid CacheMiss : set the value to False if the value is not set yet."""
  254. for question in self:
  255. if not question.triggering_question_id \
  256. or question.triggering_question_id != question.triggering_answer_id.question_id\
  257. or question.triggering_answer_id is None:
  258. question.triggering_answer_id = False
  259. @api.depends('question_type', 'scoring_type', 'answer_date', 'answer_datetime', 'answer_numerical_box')
  260. def _compute_is_scored_question(self):
  261. """ Computes whether a question "is scored" or not. Handles following cases:
  262. - inconsistent Boolean=None edge case that breaks tests => False
  263. - survey is not scored => False
  264. - 'date'/'datetime'/'numerical_box' question types w/correct answer => True
  265. (implied without user having to activate, except for numerical whose correct value is 0.0)
  266. - 'simple_choice / multiple_choice': set to True even if logic is a bit different (coming from answers)
  267. - question_type isn't scoreable (note: choice questions scoring logic handled separately) => False
  268. """
  269. for question in self:
  270. if question.is_scored_question is None or question.scoring_type == 'no_scoring':
  271. question.is_scored_question = False
  272. elif question.question_type == 'date':
  273. question.is_scored_question = bool(question.answer_date)
  274. elif question.question_type == 'datetime':
  275. question.is_scored_question = bool(question.answer_datetime)
  276. elif question.question_type == 'numerical_box' and question.answer_numerical_box:
  277. question.is_scored_question = True
  278. elif question.question_type in ['simple_choice', 'multiple_choice']:
  279. question.is_scored_question = True
  280. else:
  281. question.is_scored_question = False
  282. # ------------------------------------------------------------
  283. # CRUD
  284. # ------------------------------------------------------------
  285. @api.ondelete(at_uninstall=False)
  286. def _unlink_except_live_sessions_in_progress(self):
  287. running_surveys = self.survey_id.filtered(lambda survey: survey.session_state == 'in_progress')
  288. if running_surveys:
  289. raise UserError(_(
  290. 'You cannot delete questions from surveys "%(survey_names)s" while live sessions are in progress.',
  291. survey_names=', '.join(running_surveys.mapped('title')),
  292. ))
  293. # ------------------------------------------------------------
  294. # VALIDATION
  295. # ------------------------------------------------------------
  296. def validate_question(self, answer, comment=None):
  297. """ Validate question, depending on question type and parameters
  298. for simple choice, text, date and number, answer is simply the answer of the question.
  299. For other multiple choices questions, answer is a list of answers (the selected choices
  300. or a list of selected answers per question -for matrix type-):
  301. - Simple answer : answer = 'example' or 2 or question_answer_id or 2019/10/10
  302. - Multiple choice : answer = [question_answer_id1, question_answer_id2, question_answer_id3]
  303. - Matrix: answer = { 'rowId1' : [colId1, colId2,...], 'rowId2' : [colId1, colId3, ...] }
  304. return dict {question.id (int): error (str)} -> empty dict if no validation error.
  305. """
  306. self.ensure_one()
  307. if isinstance(answer, str):
  308. answer = answer.strip()
  309. # Empty answer to mandatory question
  310. if self.constr_mandatory and not answer and self.question_type not in ['simple_choice', 'multiple_choice']:
  311. return {self.id: self.constr_error_msg or _('This question requires an answer.')}
  312. # because in choices question types, comment can count as answer
  313. if answer or self.question_type in ['simple_choice', 'multiple_choice']:
  314. if self.question_type == 'char_box':
  315. return self._validate_char_box(answer)
  316. elif self.question_type == 'numerical_box':
  317. return self._validate_numerical_box(answer)
  318. elif self.question_type in ['date', 'datetime']:
  319. return self._validate_date(answer)
  320. elif self.question_type in ['simple_choice', 'multiple_choice']:
  321. return self._validate_choice(answer, comment)
  322. elif self.question_type == 'matrix':
  323. return self._validate_matrix(answer)
  324. return {}
  325. def _validate_char_box(self, answer):
  326. # Email format validation
  327. # all the strings of the form "<something>@<anything>.<extension>" will be accepted
  328. if self.validation_email:
  329. if not tools.email_normalize(answer):
  330. return {self.id: _('This answer must be an email address')}
  331. # Answer validation (if properly defined)
  332. # Length of the answer must be in a range
  333. if self.validation_required:
  334. if not (self.validation_length_min <= len(answer) <= self.validation_length_max):
  335. return {self.id: self.validation_error_msg or _('The answer you entered is not valid.')}
  336. return {}
  337. def _validate_numerical_box(self, answer):
  338. try:
  339. floatanswer = float(answer)
  340. except ValueError:
  341. return {self.id: _('This is not a number')}
  342. if self.validation_required:
  343. # Answer is not in the right range
  344. with contextlib.suppress(Exception):
  345. if not (self.validation_min_float_value <= floatanswer <= self.validation_max_float_value):
  346. return {self.id: self.validation_error_msg or _('The answer you entered is not valid.')}
  347. return {}
  348. def _validate_date(self, answer):
  349. isDatetime = self.question_type == 'datetime'
  350. # Checks if user input is a date
  351. try:
  352. dateanswer = fields.Datetime.from_string(answer) if isDatetime else fields.Date.from_string(answer)
  353. except ValueError:
  354. return {self.id: _('This is not a date')}
  355. if self.validation_required:
  356. # Check if answer is in the right range
  357. if isDatetime:
  358. min_date = fields.Datetime.from_string(self.validation_min_datetime)
  359. max_date = fields.Datetime.from_string(self.validation_max_datetime)
  360. dateanswer = fields.Datetime.from_string(answer)
  361. else:
  362. min_date = fields.Date.from_string(self.validation_min_date)
  363. max_date = fields.Date.from_string(self.validation_max_date)
  364. dateanswer = fields.Date.from_string(answer)
  365. if (min_date and max_date and not (min_date <= dateanswer <= max_date))\
  366. or (min_date and not min_date <= dateanswer)\
  367. or (max_date and not dateanswer <= max_date):
  368. return {self.id: self.validation_error_msg or _('The answer you entered is not valid.')}
  369. return {}
  370. def _validate_choice(self, answer, comment):
  371. # Empty comment
  372. if self.constr_mandatory \
  373. and not answer \
  374. and not (self.comments_allowed and self.comment_count_as_answer and comment):
  375. return {self.id: self.constr_error_msg or _('This question requires an answer.')}
  376. return {}
  377. def _validate_matrix(self, answers):
  378. # Validate that each line has been answered
  379. if self.constr_mandatory and len(self.matrix_row_ids) != len(answers):
  380. return {self.id: self.constr_error_msg or _('This question requires an answer.')}
  381. return {}
  382. def _index(self):
  383. """We would normally just use the 'sequence' field of questions BUT, if the pages and questions are
  384. created without ever moving records around, the sequence field can be set to 0 for all the questions.
  385. However, the order of the recordset is always correct so we can rely on the index method."""
  386. self.ensure_one()
  387. return list(self.survey_id.question_and_page_ids).index(self)
  388. # ------------------------------------------------------------
  389. # STATISTICS / REPORTING
  390. # ------------------------------------------------------------
  391. def _prepare_statistics(self, user_input_lines):
  392. """ Compute statistical data for questions by counting number of vote per choice on basis of filter """
  393. all_questions_data = []
  394. for question in self:
  395. question_data = {'question': question, 'is_page': question.is_page}
  396. if question.is_page:
  397. all_questions_data.append(question_data)
  398. continue
  399. # fetch answer lines, separate comments from real answers
  400. all_lines = user_input_lines.filtered(lambda line: line.question_id == question)
  401. if question.question_type in ['simple_choice', 'multiple_choice', 'matrix']:
  402. answer_lines = all_lines.filtered(
  403. lambda line: line.answer_type == 'suggestion' or (
  404. line.skipped and not line.answer_type) or (
  405. line.answer_type == 'char_box' and question.comment_count_as_answer)
  406. )
  407. comment_line_ids = all_lines.filtered(lambda line: line.answer_type == 'char_box')
  408. else:
  409. answer_lines = all_lines
  410. comment_line_ids = self.env['survey.user_input.line']
  411. skipped_lines = answer_lines.filtered(lambda line: line.skipped)
  412. done_lines = answer_lines - skipped_lines
  413. question_data.update(
  414. answer_line_ids=answer_lines,
  415. answer_line_done_ids=done_lines,
  416. answer_input_done_ids=done_lines.mapped('user_input_id'),
  417. answer_input_skipped_ids=skipped_lines.mapped('user_input_id'),
  418. comment_line_ids=comment_line_ids)
  419. question_data.update(question._get_stats_summary_data(answer_lines))
  420. # prepare table and graph data
  421. table_data, graph_data = question._get_stats_data(answer_lines)
  422. question_data['table_data'] = table_data
  423. question_data['graph_data'] = json.dumps(graph_data)
  424. all_questions_data.append(question_data)
  425. return all_questions_data
  426. def _get_stats_data(self, user_input_lines):
  427. if self.question_type == 'simple_choice':
  428. return self._get_stats_data_answers(user_input_lines)
  429. elif self.question_type == 'multiple_choice':
  430. table_data, graph_data = self._get_stats_data_answers(user_input_lines)
  431. return table_data, [{'key': self.title, 'values': graph_data}]
  432. elif self.question_type == 'matrix':
  433. return self._get_stats_graph_data_matrix(user_input_lines)
  434. return [line for line in user_input_lines], []
  435. def _get_stats_data_answers(self, user_input_lines):
  436. """ Statistics for question.answer based questions (simple choice, multiple
  437. choice.). A corner case with a void record survey.question.answer is added
  438. to count comments that should be considered as valid answers. This small hack
  439. allow to have everything available in the same standard structure. """
  440. suggested_answers = [answer for answer in self.mapped('suggested_answer_ids')]
  441. if self.comment_count_as_answer:
  442. suggested_answers += [self.env['survey.question.answer']]
  443. count_data = dict.fromkeys(suggested_answers, 0)
  444. for line in user_input_lines:
  445. if line.suggested_answer_id in count_data\
  446. or (line.value_char_box and self.comment_count_as_answer):
  447. count_data[line.suggested_answer_id] += 1
  448. table_data = [{
  449. 'value': _('Other (see comments)') if not sug_answer else sug_answer.value,
  450. 'suggested_answer': sug_answer,
  451. 'count': count_data[sug_answer]
  452. }
  453. for sug_answer in suggested_answers]
  454. graph_data = [{
  455. 'text': _('Other (see comments)') if not sug_answer else sug_answer.value,
  456. 'count': count_data[sug_answer]
  457. }
  458. for sug_answer in suggested_answers]
  459. return table_data, graph_data
  460. def _get_stats_graph_data_matrix(self, user_input_lines):
  461. suggested_answers = self.mapped('suggested_answer_ids')
  462. matrix_rows = self.mapped('matrix_row_ids')
  463. count_data = dict.fromkeys(itertools.product(matrix_rows, suggested_answers), 0)
  464. for line in user_input_lines:
  465. if line.matrix_row_id and line.suggested_answer_id:
  466. count_data[(line.matrix_row_id, line.suggested_answer_id)] += 1
  467. table_data = [{
  468. 'row': row,
  469. 'columns': [{
  470. 'suggested_answer': sug_answer,
  471. 'count': count_data[(row, sug_answer)]
  472. } for sug_answer in suggested_answers],
  473. } for row in matrix_rows]
  474. graph_data = [{
  475. 'key': sug_answer.value,
  476. 'values': [{
  477. 'text': row.value,
  478. 'count': count_data[(row, sug_answer)]
  479. }
  480. for row in matrix_rows
  481. ]
  482. } for sug_answer in suggested_answers]
  483. return table_data, graph_data
  484. def _get_stats_summary_data(self, user_input_lines):
  485. stats = {}
  486. if self.question_type in ['simple_choice', 'multiple_choice']:
  487. stats.update(self._get_stats_summary_data_choice(user_input_lines))
  488. elif self.question_type == 'numerical_box':
  489. stats.update(self._get_stats_summary_data_numerical(user_input_lines))
  490. if self.question_type in ['numerical_box', 'date', 'datetime']:
  491. stats.update(self._get_stats_summary_data_scored(user_input_lines))
  492. return stats
  493. def _get_stats_summary_data_choice(self, user_input_lines):
  494. right_inputs, partial_inputs = self.env['survey.user_input'], self.env['survey.user_input']
  495. right_answers = self.suggested_answer_ids.filtered(lambda label: label.is_correct)
  496. if self.question_type == 'multiple_choice':
  497. for user_input, lines in tools.groupby(user_input_lines, operator.itemgetter('user_input_id')):
  498. user_input_answers = self.env['survey.user_input.line'].concat(*lines).filtered(lambda l: l.answer_is_correct).mapped('suggested_answer_id')
  499. if user_input_answers and user_input_answers < right_answers:
  500. partial_inputs += user_input
  501. elif user_input_answers:
  502. right_inputs += user_input
  503. else:
  504. right_inputs = user_input_lines.filtered(lambda line: line.answer_is_correct).mapped('user_input_id')
  505. return {
  506. 'right_answers': right_answers,
  507. 'right_inputs_count': len(right_inputs),
  508. 'partial_inputs_count': len(partial_inputs),
  509. }
  510. def _get_stats_summary_data_numerical(self, user_input_lines):
  511. all_values = user_input_lines.filtered(lambda line: not line.skipped).mapped('value_numerical_box')
  512. lines_sum = sum(all_values)
  513. return {
  514. 'numerical_max': max(all_values, default=0),
  515. 'numerical_min': min(all_values, default=0),
  516. 'numerical_average': round(lines_sum / (len(all_values) or 1), 2),
  517. }
  518. def _get_stats_summary_data_scored(self, user_input_lines):
  519. return {
  520. 'common_lines': collections.Counter(
  521. user_input_lines.filtered(lambda line: not line.skipped).mapped('value_%s' % self.question_type)
  522. ).most_common(5),
  523. 'right_inputs_count': len(user_input_lines.filtered(lambda line: line.answer_is_correct).mapped('user_input_id'))
  524. }
  525. class SurveyQuestionAnswer(models.Model):
  526. """ A preconfigured answer for a question. This model stores values used
  527. for
  528. * simple choice, multiple choice: proposed values for the selection /
  529. radio;
  530. * matrix: row and column values;
  531. """
  532. _name = 'survey.question.answer'
  533. _rec_name = 'value'
  534. _order = 'sequence, id'
  535. _description = 'Survey Label'
  536. # question and question related fields
  537. question_id = fields.Many2one('survey.question', string='Question', ondelete='cascade')
  538. matrix_question_id = fields.Many2one('survey.question', string='Question (as matrix row)', ondelete='cascade')
  539. question_type = fields.Selection(related='question_id.question_type')
  540. sequence = fields.Integer('Label Sequence order', default=10)
  541. scoring_type = fields.Selection(related='question_id.scoring_type')
  542. # answer related fields
  543. value = fields.Char('Suggested value', translate=True, required=True)
  544. value_image = fields.Image('Image', max_width=1024, max_height=1024)
  545. value_image_filename = fields.Char('Image Filename')
  546. is_correct = fields.Boolean('Correct')
  547. answer_score = fields.Float('Score', help="A positive score indicates a correct choice; a negative or null score indicates a wrong answer")
  548. @api.constrains('question_id', 'matrix_question_id')
  549. def _check_question_not_empty(self):
  550. """Ensure that field question_id XOR field matrix_question_id is not null"""
  551. for label in self:
  552. if not bool(label.question_id) != bool(label.matrix_question_id):
  553. raise ValidationError(_("A label must be attached to only one question."))