event_quiz.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models, _
  4. from odoo.exceptions import ValidationError
  5. class Quiz(models.Model):
  6. _name = "event.quiz"
  7. _description = "Quiz"
  8. name = fields.Char('Name', required=True, translate=True)
  9. question_ids = fields.One2many('event.quiz.question', 'quiz_id', string="Questions")
  10. event_track_id = fields.Many2one('event.track', readonly=True)
  11. event_id = fields.Many2one(
  12. 'event.event', related='event_track_id.event_id',
  13. readonly=True, store=True)
  14. repeatable = fields.Boolean('Unlimited Tries',
  15. help='Let attendees reset the quiz and try again.')
  16. class QuizQuestion(models.Model):
  17. _name = "event.quiz.question"
  18. _description = "Content Quiz Question"
  19. _order = "quiz_id, sequence, id"
  20. name = fields.Char("Question", required=True, translate=True)
  21. sequence = fields.Integer("Sequence")
  22. quiz_id = fields.Many2one("event.quiz", "Quiz", required=True, ondelete='cascade')
  23. correct_answer_id = fields.One2many('event.quiz.answer', compute='_compute_correct_answer_id')
  24. awarded_points = fields.Integer("Number of Points", compute='_compute_awarded_points')
  25. answer_ids = fields.One2many('event.quiz.answer', 'question_id', string="Answer")
  26. @api.depends('answer_ids.awarded_points')
  27. def _compute_awarded_points(self):
  28. for question in self:
  29. question.awarded_points = sum(question.answer_ids.mapped('awarded_points'))
  30. @api.depends('answer_ids.is_correct')
  31. def _compute_correct_answer_id(self):
  32. for question in self:
  33. question.correct_answer_id = question.answer_ids.filtered(lambda e: e.is_correct)
  34. @api.constrains('answer_ids')
  35. def _check_answers_integrity(self):
  36. for question in self:
  37. if len(question.correct_answer_id) != 1:
  38. raise ValidationError(_('Question "%s" must have 1 correct answer to be valid.', question.name))
  39. if len(question.answer_ids) < 2:
  40. raise ValidationError(_('Question "%s" must have 1 correct answer and at least 1 incorrect answer to be valid.', question.name))
  41. class QuizAnswer(models.Model):
  42. _name = "event.quiz.answer"
  43. _rec_name = "text_value"
  44. _description = "Question's Answer"
  45. _order = 'question_id, sequence, id'
  46. sequence = fields.Integer("Sequence")
  47. question_id = fields.Many2one('event.quiz.question', string="Question", required=True, ondelete='cascade')
  48. text_value = fields.Char("Answer", required=True, translate=True)
  49. is_correct = fields.Boolean('Correct', default=False)
  50. comment = fields.Text(
  51. 'Extra Comment', translate=True,
  52. help='''This comment will be displayed to the user if they select this answer, after submitting the quiz.
  53. It is used as a small informational text helping to understand why this answer is correct / incorrect.''')
  54. awarded_points = fields.Integer('Points', default=0)