summaryrefslogtreecommitdiff
path: root/addons/website_slides_survey/models
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/website_slides_survey/models
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/website_slides_survey/models')
-rw-r--r--addons/website_slides_survey/models/__init__.py6
-rw-r--r--addons/website_slides_survey/models/slide_channel.py10
-rw-r--r--addons/website_slides_survey/models/slide_slide.py121
-rw-r--r--addons/website_slides_survey/models/survey_survey.py54
-rw-r--r--addons/website_slides_survey/models/survey_user.py64
5 files changed, 255 insertions, 0 deletions
diff --git a/addons/website_slides_survey/models/__init__.py b/addons/website_slides_survey/models/__init__.py
new file mode 100644
index 00000000..f0fb0a0c
--- /dev/null
+++ b/addons/website_slides_survey/models/__init__.py
@@ -0,0 +1,6 @@
+# -*- coding: utf-8 -*-
+
+from . import slide_slide
+from . import slide_channel
+from . import survey_user
+from . import survey_survey
diff --git a/addons/website_slides_survey/models/slide_channel.py b/addons/website_slides_survey/models/slide_channel.py
new file mode 100644
index 00000000..1323b98d
--- /dev/null
+++ b/addons/website_slides_survey/models/slide_channel.py
@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import fields, models
+
+
+class Channel(models.Model):
+ _inherit = 'slide.channel'
+
+ nbr_certification = fields.Integer("Number of Certifications", compute='_compute_slides_statistics', store=True)
diff --git a/addons/website_slides_survey/models/slide_slide.py b/addons/website_slides_survey/models/slide_slide.py
new file mode 100644
index 00000000..61afd804
--- /dev/null
+++ b/addons/website_slides_survey/models/slide_slide.py
@@ -0,0 +1,121 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models
+
+
+class SlidePartnerRelation(models.Model):
+ _inherit = 'slide.slide.partner'
+
+ user_input_ids = fields.One2many('survey.user_input', 'slide_partner_id', 'Certification attempts')
+ survey_scoring_success = fields.Boolean('Certification Succeeded', compute='_compute_survey_scoring_success', store=True)
+
+ @api.depends('partner_id', 'user_input_ids.scoring_success')
+ def _compute_survey_scoring_success(self):
+ succeeded_user_inputs = self.env['survey.user_input'].sudo().search([
+ ('slide_partner_id', 'in', self.ids),
+ ('scoring_success', '=', True)
+ ])
+ succeeded_slide_partners = succeeded_user_inputs.mapped('slide_partner_id')
+ for record in self:
+ record.survey_scoring_success = record in succeeded_slide_partners
+
+ def _compute_field_value(self, field):
+ super()._compute_field_value(field)
+ if field.name == 'survey_scoring_success':
+ self.filtered('survey_scoring_success').write({
+ 'completed': True
+ })
+
+class Slide(models.Model):
+ _inherit = 'slide.slide'
+
+ slide_type = fields.Selection(selection_add=[
+ ('certification', 'Certification')
+ ], ondelete={'certification': 'set default'})
+ survey_id = fields.Many2one('survey.survey', 'Certification')
+ nbr_certification = fields.Integer("Number of Certifications", compute='_compute_slides_statistics', store=True)
+
+ _sql_constraints = [
+ ('check_survey_id', "CHECK(slide_type != 'certification' OR survey_id IS NOT NULL)", "A slide of type 'certification' requires a certification."),
+ ('check_certification_preview', "CHECK(slide_type != 'certification' OR is_preview = False)", "A slide of type certification cannot be previewed."),
+ ]
+
+ @api.onchange('survey_id')
+ def _on_change_survey_id(self):
+ if self.survey_id:
+ self.slide_type = 'certification'
+
+ @api.model
+ def create(self, values):
+ rec = super(Slide, self).create(values)
+ if rec.survey_id:
+ rec.slide_type = 'certification'
+ if 'survey_id' in values:
+ rec._ensure_challenge_category()
+ return rec
+
+ def write(self, values):
+ old_surveys = self.mapped('survey_id')
+ result = super(Slide, self).write(values)
+ if 'survey_id' in values:
+ self._ensure_challenge_category(old_surveys=old_surveys - self.mapped('survey_id'))
+ return result
+
+ def unlink(self):
+ old_surveys = self.mapped('survey_id')
+ result = super(Slide, self).unlink()
+ self._ensure_challenge_category(old_surveys=old_surveys, unlink=True)
+ return result
+
+ def _ensure_challenge_category(self, old_surveys=None, unlink=False):
+ """ If a slide is linked to a survey that gives a badge, the challenge category of this badge must be
+ set to 'slides' in order to appear under the certification badge list on ranks_badges page.
+ If the survey is unlinked from the slide, the challenge category must be reset to 'certification'"""
+ if old_surveys:
+ old_certification_challenges = old_surveys.mapped('certification_badge_id').challenge_ids
+ old_certification_challenges.write({'challenge_category': 'certification'})
+ if not unlink:
+ certification_challenges = self.mapped('survey_id').mapped('certification_badge_id').challenge_ids
+ certification_challenges.write({'challenge_category': 'slides'})
+
+ def _generate_certification_url(self):
+ """ get a map of certification url for certification slide from `self`. The url will come from the survey user input:
+ 1/ existing and not done user_input for member of the course
+ 2/ create a new user_input for member
+ 3/ for no member, a test user_input is created and the url is returned
+ Note: the slide.slides.partner should already exist
+
+ We have to generate a new invite_token to differentiate pools of attempts since the
+ course can be enrolled multiple times.
+ """
+ certification_urls = {}
+ for slide in self.filtered(lambda slide: slide.slide_type == 'certification' and slide.survey_id):
+ if slide.channel_id.is_member:
+ user_membership_id_sudo = slide.user_membership_id.sudo()
+ if user_membership_id_sudo.user_input_ids:
+ last_user_input = next(user_input for user_input in user_membership_id_sudo.user_input_ids.sorted(
+ lambda user_input: user_input.create_date, reverse=True
+ ))
+ certification_urls[slide.id] = last_user_input.get_start_url()
+ else:
+ user_input = slide.survey_id.sudo()._create_answer(
+ partner=self.env.user.partner_id,
+ check_attempts=False,
+ **{
+ 'slide_id': slide.id,
+ 'slide_partner_id': user_membership_id_sudo.id
+ },
+ invite_token=self.env['survey.user_input']._generate_invite_token()
+ )
+ certification_urls[slide.id] = user_input.get_start_url()
+ else:
+ user_input = slide.survey_id.sudo()._create_answer(
+ partner=self.env.user.partner_id,
+ check_attempts=False,
+ test_entry=True, **{
+ 'slide_id': slide.id
+ }
+ )
+ certification_urls[slide.id] = user_input.get_start_url()
+ return certification_urls
diff --git a/addons/website_slides_survey/models/survey_survey.py b/addons/website_slides_survey/models/survey_survey.py
new file mode 100644
index 00000000..a5edc7f4
--- /dev/null
+++ b/addons/website_slides_survey/models/survey_survey.py
@@ -0,0 +1,54 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models, _
+
+
+class Survey(models.Model):
+ _inherit = 'survey.survey'
+
+ slide_ids = fields.One2many(
+ 'slide.slide', 'survey_id', string="Certification Slides",
+ help="The slides this survey is linked to through the e-learning application")
+ slide_channel_ids = fields.One2many(
+ 'slide.channel', string="Certification Courses", compute='_compute_slide_channel_data',
+ help="The courses this survey is linked to through the e-learning application",
+ groups='website_slides.group_website_slides_officer')
+ slide_channel_count = fields.Integer("Courses Count", compute='_compute_slide_channel_data', groups='website_slides.group_website_slides_officer')
+
+ @api.depends('slide_ids.channel_id')
+ def _compute_slide_channel_data(self):
+ for survey in self:
+ survey.slide_channel_ids = survey.slide_ids.mapped('channel_id')
+ survey.slide_channel_count = len(survey.slide_channel_ids)
+
+ # ---------------------------------------------------------
+ # Actions
+ # ---------------------------------------------------------
+
+ def action_survey_view_slide_channels(self):
+ action = self.env["ir.actions.actions"]._for_xml_id("website_slides.slide_channel_action_overview")
+ action['display_name'] = _("Courses")
+ if self.slide_channel_count == 1:
+ action.update({'views': [(False, 'form')],
+ 'res_id': self.slide_channel_ids[0].id})
+ else:
+ action.update({'views': [[False, 'tree'], [False, 'form']],
+ 'domain': [('id', 'in', self.slide_channel_ids.ids)]})
+ return action
+
+ # ---------------------------------------------------------
+ # Business
+ # ---------------------------------------------------------
+
+ def _check_answer_creation(self, user, partner, email, test_entry=False, check_attempts=True, invite_token=False):
+ """ Overridden to allow website_slides_officer to test certifications. """
+ self.ensure_one()
+ if test_entry and user.has_group('website_slides.group_website_slides_officer'):
+ return True
+
+ return super(Survey, self)._check_answer_creation(user, partner, email, test_entry=test_entry, check_attempts=check_attempts, invite_token=invite_token)
+
+ def _prepare_challenge_category(self):
+ slide_survey = self.env['slide.slide'].search([('survey_id', '=', self.id)])
+ return 'slides' if slide_survey else 'certification'
diff --git a/addons/website_slides_survey/models/survey_user.py b/addons/website_slides_survey/models/survey_user.py
new file mode 100644
index 00000000..f19aaa4d
--- /dev/null
+++ b/addons/website_slides_survey/models/survey_user.py
@@ -0,0 +1,64 @@
+# -*- 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)