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