Right now, if we want to add a quiz to the event track, we need to create it from the m2o field available in the form view. But it is not very intuitive as user will have to select 'Create and Edit' option from the m2o drop-down. Quick create is not really helpful here. Also, trying to add question for the quiz will pile up the modals which looks messy. With this commit, in the event track's form view, we: - Remove the m2o for `quiz_id` (from the track form view, not from model) - Introduce "Add Quiz" header button, which redirects user to form view of the quiz, with no option to create new quiz directly as only one quiz should be linked to quiz. Also, once the quiz is added, this button will be hidden. - Introduce new stat button "Go to Quiz", which redirects user to the form view of quiz (again, does not allow creation from the same reason). This button is visible only if the quiz is already added for the track. Apart from that, re-arrage the quiz form view `event_quiz_view_form` by putting 'Event' and 'Event Track' fields on the same line, and we utilize the same view while creating / updating quiz from the track. Note: Since the creation of quiz is now done in its own form view (with action button "Add Quiz" from track's form view), we are passing the default `event_track_id` while creating the quiz. It means that we don't need to compute it anymore. Instead, now we need to compute the `quiz_id` on the track, based on the `event_track_id` field alreay set on the quiz. So, we introduce new One2Many `quiz_ids` field to compute the `quiz_id` on the track, and we remove `event_track_ids` from the quiz. Basically, the logic of setting `quiz_id` (on the track) and `event_track_id` (on the quiz) is inverted with the commit, and the demo data is updated accordinlgy. TaskID-2486552 closes odoo/odoo#70067 Related: odoo/upgrade#2522 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
101 lines
4.2 KiB
Python
101 lines
4.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, fields, models
|
|
from odoo.osv import expression
|
|
|
|
|
|
class EventTrack(models.Model):
|
|
_inherit = ['event.track']
|
|
|
|
quiz_id = fields.Many2one('event.quiz', string="Quiz", compute='_compute_quiz_id', store=True, groups="event.group_event_user")
|
|
quiz_ids = fields.One2many('event.quiz', 'event_track_id', string="Quizzes")
|
|
quiz_questions_count = fields.Integer(string="# Quiz Questions", compute='_compute_quiz_questions_count', groups="event.group_event_user")
|
|
is_quiz_completed = fields.Boolean('Is Quiz Done', compute='_compute_quiz_data')
|
|
quiz_points = fields.Integer('Quiz Points', compute='_compute_quiz_data')
|
|
|
|
@api.depends('quiz_ids.event_track_id')
|
|
def _compute_quiz_id(self):
|
|
for track in self:
|
|
track.quiz_id = track.quiz_ids[0] if track.quiz_ids else False
|
|
|
|
@api.depends('quiz_id.question_ids')
|
|
def _compute_quiz_questions_count(self):
|
|
for track in self:
|
|
track.quiz_questions_count = len(track.quiz_id.question_ids)
|
|
|
|
@api.depends('quiz_id', 'event_track_visitor_ids.visitor_id',
|
|
'event_track_visitor_ids.partner_id', 'event_track_visitor_ids.quiz_completed',
|
|
'event_track_visitor_ids.quiz_points')
|
|
@api.depends_context('uid')
|
|
def _compute_quiz_data(self):
|
|
tracks_quiz = self.filtered(lambda track: track.quiz_id)
|
|
(self - tracks_quiz).is_quiz_completed = False
|
|
(self - tracks_quiz).quiz_points = 0
|
|
if tracks_quiz:
|
|
current_visitor = self.env['website.visitor']._get_visitor_from_request(force_create=False)
|
|
if self.env.user._is_public() and not current_visitor:
|
|
for track in tracks_quiz:
|
|
track.is_quiz_completed = False
|
|
track.quiz_points = 0
|
|
else:
|
|
if self.env.user._is_public():
|
|
domain = [('visitor_id', '=', current_visitor.id)]
|
|
elif current_visitor:
|
|
domain = [
|
|
'|',
|
|
('partner_id', '=', self.env.user.partner_id.id),
|
|
('visitor_id', '=', current_visitor.id)
|
|
]
|
|
else:
|
|
domain = [('partner_id', '=', self.env.user.partner_id.id)]
|
|
|
|
event_track_visitors = self.env['event.track.visitor'].sudo().search_read(
|
|
expression.AND([
|
|
domain,
|
|
[('track_id', 'in', tracks_quiz.ids)]
|
|
]), fields=['track_id', 'quiz_completed', 'quiz_points']
|
|
)
|
|
|
|
quiz_visitor_map = {
|
|
track_visitor['track_id'][0]: {
|
|
'quiz_completed': track_visitor['quiz_completed'],
|
|
'quiz_points': track_visitor['quiz_points']
|
|
} for track_visitor in event_track_visitors
|
|
}
|
|
for track in tracks_quiz:
|
|
if quiz_visitor_map.get(track.id):
|
|
track.is_quiz_completed = quiz_visitor_map[track.id]['quiz_completed']
|
|
track.quiz_points = quiz_visitor_map[track.id]['quiz_points']
|
|
else:
|
|
track.is_quiz_completed = False
|
|
track.quiz_points = 0
|
|
|
|
def action_add_quiz(self):
|
|
self.ensure_one()
|
|
event_quiz_form = self.env.ref('website_event_track_quiz.event_quiz_view_form')
|
|
return {
|
|
'type': 'ir.actions.act_window',
|
|
'view_mode': 'form',
|
|
'res_model': 'event.quiz',
|
|
'view_id': event_quiz_form.id,
|
|
'context': {
|
|
'default_event_track_id': self.id,
|
|
'create': False,
|
|
},
|
|
}
|
|
|
|
def action_view_quiz(self):
|
|
self.ensure_one()
|
|
event_quiz_form = self.env.ref('website_event_track_quiz.event_quiz_view_form')
|
|
return {
|
|
'type': 'ir.actions.act_window',
|
|
'view_mode': 'form',
|
|
'res_model': 'event.quiz',
|
|
'res_id' : self.quiz_id.id,
|
|
'view_id': event_quiz_form.id,
|
|
'context': {
|
|
'create': False,
|
|
}
|
|
}
|