Sélectionner une révision Git
training.py 5,09 Kio
# Copyright 2024 Le Filament (<https://le-filament.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import _, fields, models
from odoo.exceptions import UserError
class Training(models.Model):
_inherit = "training.training"
satisfaction_survey_id = fields.Many2one(
comodel_name="survey.survey",
string="Satisfaction",
domain=[
("survey_type", "=", "training"),
("training_survey_type", "=", "satisfaction"),
],
ondelete="restrict",
)
registration_survey_id = fields.Many2one(
comodel_name="survey.survey",
string="Pré-inscription/Positionnement",
related="program_id.registration_survey_id",
)
aeci_survey_id = fields.Many2one(
comodel_name="survey.survey",
string="AECI",
related="program_id.aeci_survey_id",
)
aect_survey_id = fields.Many2one(
comodel_name="survey.survey",
string="AECT",
related="program_id.aect_survey_id",
)
survey_ids = fields.One2many(
comodel_name="survey.survey",
compute="_compute_survey_ids",
)
# ------------------------------------------------------
# Override ORM
# ------------------------------------------------------
# ------------------------------------------------------
# Compute
# ------------------------------------------------------
def _compute_survey_ids(self):
for training in self:
training.survey_ids = (
training.satisfaction_survey_id
+ training.program_id.aeci_survey_id
+ training.program_id.aect_survey_id
+ training.program_id.registration_survey_id
)
# ------------------------------------------------------
# Actions
# ------------------------------------------------------
def action_export_registration(self):
if self.registration_survey_id:
return self._get_survey_xlsx(self.registration_survey_id)
def action_export_aeci(self):
if self.aeci_survey_id:
return self._get_survey_xlsx(self.aeci_survey_id)
def action_export_aect(self):
if self.aect_survey_id:
return self._get_survey_xlsx(self.aect_survey_id)
def action_export_satisfaction(self):
if self.satisfaction_survey_id:
return self._get_survey_xlsx(self.satisfaction_survey_id)
def action_view_survey(self):
self.ensure_one()
return {
"name": f"Questionnaires {self.program_id.name}",
"type": "ir.actions.act_window",
"view_mode": "kanban,tree,form,activity",
"res_model": "survey.survey",
"domain": [("id", "in", self.survey_ids.ids)],
"context": {"create": 0, "delete": 0},
}
def action_send_end_training_data(self):
self.ensure_one()
self.action_done()
end_template_id = self.env.ref("training_survey.mail_template_training_end")
student_ids = self.student_ids.filtered(
lambda s: s.state == "confirmed"
and any(
s.student_slot_ids.mapped(
lambda slot: True if slot.state == "present" else False
)
)
)
for student in student_ids:
aect_answer = student._create_answer(self.aect_survey_id)
satisfaction_answer = student._create_answer(self.satisfaction_survey_id)
end_template_id.with_context(
aect_answer=aect_answer, satisfaction_answer=satisfaction_answer
).send_mail(
student.id,
email_layout_xmlid="training.mail_training_layout",
)
self.message_post(
subject="Questionnaires AECT et Satisfaction envoyés",
body=f"Participants : {', '.join(student_ids.mapped('partner_id.name'))}",
)
def action_send_satisfaction(self):
self.ensure_one()
satisfaction_template_id = self.env.ref(
"training_survey.mail_template_training_satisfaction"
)
student_ids = self.student_ids.filtered(lambda s: s.state == "confirmed")
for student in student_ids:
student._create_and_send_survey(
self.satisfaction_survey_id, satisfaction_template_id
)
self.message_post(
subject="Questionnaire de satisfaction envoyé",
body=f"Participants : {', '.join(student_ids.mapped('partner_id.name'))}",
)
# ------------------------------------------------------
# Inherit parent
# ------------------------------------------------------
def action_valid(self):
if not self.satisfaction_survey_id:
raise UserError(_("Le questionnaire de satisfaction doit être configuré."))
super().action_valid()
# ------------------------------------------------------
# Business function
# ------------------------------------------------------
def _get_survey_xlsx(self, survey_id):
return self.env.ref("survey_xlsx.report_survey_xlsx").report_action(survey_id)