PURPOSE As new features are about to land in survey, notably live interactions [1] and new survey building [2] performing a pre cleaning is necessary. In this PR we clean survey models by: removing unnecessary fields, cleaning some code and finally renaming models. SPECIFICATIONS: RENAME QUIZ_SCORE ON SURVEY.USER_INPUT On survey.user_input, quiz_score field to scoring_percentage ``quiz_score`` field name is related to the old "quiz" behavior of surveys that is replaced by certifications and scoring mechanisms. Let us propagate the renaming, beginning with a ``scoring_`` prefix. SPECIFICATIONS: RENAME QUIZZ_PASSED ON SURVEY.USER_INPUT on survey.user_input, rename quizz_passed field to scoring_success ``quizz_passed`` field name is related to the old "quiz" behavior of surveys that is replaced by certifications and scoring mechanisms. Let us propagate the renaming, beginning with a ``scoring_`` prefix. SPECIFICATIONS: RENAME TOKEN ON SURVEY.USER_INPUT on survey.user_input, rename token field to access_token Survey user input model holds two token field. One is used to distinguish a pool of attempts linked to a given invite: ``invite_token``. The other one is used to control access to a specific user input. It means that ``invite_token`` indicates a set of user inputs and each of them is accessed through its own ``token``. To be coherent with other naming in odoo this latter field is renamed to ``access_token``. LINKS [0] Related to Task ID 2061901 (survey models cleaning and preparation) [1] Task ID 1972640 (live interactions) [2] Task ID 2119587 (new frontend for building surveys) PR #40765
65 lines
2.7 KiB
Python
65 lines
2.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import fields, models, api
|
|
from odoo.osv import expression
|
|
|
|
|
|
class SurveyUserInput(models.Model):
|
|
_inherit = 'survey.user_input'
|
|
|
|
slide_id = fields.Many2one('slide.slide', 'Related course slide',
|
|
help="The related course slide when there is no membership information")
|
|
slide_partner_id = fields.Many2one('slide.slide.partner', 'Subscriber information',
|
|
help="Slide membership information for the logged in user")
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
records = super(SurveyUserInput, self).create(vals_list)
|
|
records._check_for_failed_attempt()
|
|
return records
|
|
|
|
def write(self, vals):
|
|
res = super(SurveyUserInput, self).write(vals)
|
|
if 'state' in vals:
|
|
self._check_for_failed_attempt()
|
|
return res
|
|
|
|
def _check_for_failed_attempt(self):
|
|
""" If the user fails his last attempt at a course certification,
|
|
we remove him from the members of the course (and he has to enroll again).
|
|
He receives an email in the process notifying him of his failure and suggesting
|
|
he enrolls to the course again.
|
|
|
|
The purpose is to have a 'certification flow' where the user can re-purchase the
|
|
certification when they have failed it."""
|
|
|
|
if self:
|
|
user_inputs = self.search([
|
|
('id', 'in', self.ids),
|
|
('state', '=', 'done'),
|
|
('scoring_success', '=', False),
|
|
('slide_partner_id', '!=', False)
|
|
])
|
|
|
|
if user_inputs:
|
|
for user_input in user_inputs:
|
|
removed_memberships_per_partner = {}
|
|
if user_input.survey_id._has_attempts_left(user_input.partner_id, user_input.email, user_input.invite_token):
|
|
# skip if user still has attempts left
|
|
continue
|
|
|
|
self.env.ref('website_slides_survey.mail_template_user_input_certification_failed').send_mail(
|
|
user_input.id, notif_layout="mail.mail_notification_light"
|
|
)
|
|
|
|
removed_memberships = removed_memberships_per_partner.get(
|
|
user_input.partner_id,
|
|
self.env['slide.channel']
|
|
)
|
|
removed_memberships |= user_input.slide_partner_id.channel_id
|
|
removed_memberships_per_partner[user_input.partner_id] = removed_memberships
|
|
|
|
for partner_id, removed_memberships in removed_memberships_per_partner.items():
|
|
removed_memberships._remove_membership(partner_id.ids)
|