| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from werkzeug.exceptions import Forbidden |
| 5 |
|
| 6 |
from odoo import http |
| 7 |
from odoo.addons.website_event_track.controllers.event_track import EventTrackController |
| 8 |
from odoo.http import request |
| 9 |
|
| 10 |
|
| 11 |
class WebsiteEventTrackQuiz(EventTrackController): |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
@http.route('/event_track/quiz/submit', type="jsonrpc", auth="public", website=True) |
| 17 |
def event_track_quiz_submit(self, event_id, track_id, answer_ids): |
| 18 |
track = self._fetch_track(track_id) |
| 19 |
track_sudo = track.sudo() |
| 20 |
|
| 21 |
event_track_visitor = track._get_event_track_visitors(force_create=True) |
| 22 |
if event_track_visitor.quiz_completed: |
| 23 |
return {'error': 'track_quiz_done'} |
| 24 |
|
| 25 |
|
| 26 |
answers_details = self._get_quiz_answers_details(track_sudo, answer_ids) |
| 27 |
if answers_details.get('error'): |
| 28 |
return answers_details |
| 29 |
|
| 30 |
event_track_visitor.write({ |
| 31 |
'quiz_completed': True, |
| 32 |
'quiz_points': answers_details['points'], |
| 33 |
}) |
| 34 |
|
| 35 |
result = { |
| 36 |
'answers': { |
| 37 |
answer.question_id.id: { |
| 38 |
'awarded_points': answer.awarded_points, |
| 39 |
'correct_answer': answer.question_id.correct_answer_id.text_value, |
| 40 |
'is_correct': answer.is_correct, |
| 41 |
'comment': answer.comment |
| 42 |
} for answer in answers_details['user_answers'] |
| 43 |
}, |
| 44 |
'quiz_completed': event_track_visitor.quiz_completed, |
| 45 |
'quiz_points': answers_details['points'] |
| 46 |
} |
| 47 |
return result |
| 48 |
|
| 49 |
@http.route('/event_track/quiz/reset', type="jsonrpc", auth="public", website=True) |
| 50 |
def quiz_reset(self, event_id, track_id): |
| 51 |
track = self._fetch_track(track_id) |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
if not request.env.user.has_group('event.group_event_manager') and not track.sudo().quiz_id.repeatable: |
| 57 |
raise Forbidden() |
| 58 |
|
| 59 |
event_track_visitor = track._get_event_track_visitors(force_create=True) |
| 60 |
event_track_visitor.write({ |
| 61 |
'quiz_completed': False, |
| 62 |
'quiz_points': 0, |
| 63 |
}) |
| 64 |
|
| 65 |
def _get_quiz_answers_details(self, track, answer_ids): |
| 66 |
questions_count = track.quiz_questions_count |
| 67 |
user_answers = request.env['event.quiz.answer'].sudo().search([('id', 'in', answer_ids)]) |
| 68 |
|
| 69 |
if len(user_answers.mapped('question_id')) != questions_count: |
| 70 |
return {'error': 'quiz_incomplete'} |
| 71 |
|
| 72 |
return { |
| 73 |
'user_answers': user_answers, |
| 74 |
'points': sum([ |
| 75 |
answer.awarded_points |
| 76 |
for answer in user_answers |
| 77 |
]) |
| 78 |
} |