From b3b26cbf05b49f0be9922ba84e5cff42a9b52437 Mon Sep 17 00:00:00 2001
From: benjamin <benjamin@le-filament.com>
Date: Fri, 26 Apr 2024 13:01:18 +0200
Subject: [PATCH] [IMP] link between user input and training

---
 models/__init__.py          |  2 ++
 models/survey.py            |  2 +-
 models/survey_user_input.py | 29 +++++++++++++++++++++++++++++
 models/training.py          | 27 +++++++++++++++++++++++++++
 models/training_student.py  | 22 ++++++++++++++++++++++
 views/survey.xml            |  2 +-
 views/training.xml          | 30 ++++++++++++++++++++++++++++--
 7 files changed, 110 insertions(+), 4 deletions(-)
 create mode 100644 models/survey_user_input.py
 create mode 100644 models/training_student.py

diff --git a/models/__init__.py b/models/__init__.py
index 972ef02..f4c21ac 100644
--- a/models/__init__.py
+++ b/models/__init__.py
@@ -1,3 +1,5 @@
 from . import survey
+from . import survey_user_input
 from . import training
 from . import training_program
+from . import training_student
diff --git a/models/survey.py b/models/survey.py
index be2104d..388c46e 100644
--- a/models/survey.py
+++ b/models/survey.py
@@ -20,7 +20,7 @@ class Survey(models.Model):
             ("aect", "AECT"),
             ("satisfaction", "Satisfaction"),
         ],
-        string="Type de questionnaire",
+        string="Type de questionnaire formation",
     )
     training_ids = fields.One2many(
         comodel_name="training.training",
diff --git a/models/survey_user_input.py b/models/survey_user_input.py
new file mode 100644
index 0000000..0d69100
--- /dev/null
+++ b/models/survey_user_input.py
@@ -0,0 +1,29 @@
+# Copyright 2019-2022 Le Filament (<https://le-filament.com>)
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
+
+from odoo import fields, models
+
+
+class SurveyUserInput(models.Model):
+    _inherit = "survey.user_input"
+
+    student_id = fields.Many2one(
+        comodel_name="training.student",
+        string="Stagiaire",
+    )
+
+    # ------------------------------------------------------
+    # Inherit parent
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Override ORM
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Compute
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Buttons
+    # ------------------------------------------------------
diff --git a/models/training.py b/models/training.py
index ab4f3fa..3b294f7 100644
--- a/models/training.py
+++ b/models/training.py
@@ -18,6 +18,11 @@ class Training(models.Model):
         ondelete="restrict",
     )
 
+    survey_ids = fields.One2many(
+        comodel_name="survey.survey",
+        compute="_compute_survey_ids",
+    )
+
     # ------------------------------------------------------
     # Override ORM
     # ------------------------------------------------------
@@ -25,6 +30,28 @@ class Training(models.Model):
     # ------------------------------------------------------
     # 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_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},
+        }
 
     # ------------------------------------------------------
     # Inherit parent
diff --git a/models/training_student.py b/models/training_student.py
new file mode 100644
index 0000000..f4ebb56
--- /dev/null
+++ b/models/training_student.py
@@ -0,0 +1,22 @@
+# 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
+
+
+class TrainingStudent(models.Model):
+    _inherit = "training.student"
+
+    student_survey_ids = fields.One2many(
+        comodel_name="survey.user_input",
+        inverse_name="student_id",
+        string="Questionnaires",
+    )
+
+    # ------------------------------------------------------
+    # Compute
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Inherit parent
+    # ------------------------------------------------------
diff --git a/views/survey.xml b/views/survey.xml
index 88027b3..1a6cd72 100644
--- a/views/survey.xml
+++ b/views/survey.xml
@@ -118,7 +118,7 @@
             </xpath>
             <xpath expr="//group" position="after">
                 <searchpanel>
-                    <field name="training_survey_type" enable_counters="1"/>
+                    <field name="training_survey_type" enable_counters="1" />
                 </searchpanel>
             </xpath>
         </field>
diff --git a/views/training.xml b/views/training.xml
index 0a4ca85..e3c4765 100644
--- a/views/training.xml
+++ b/views/training.xml
@@ -1,12 +1,21 @@
 <?xml version="1.0" encoding="utf-8" ?>
-<!-- Copyright 2019-2022 Le Filament (<https://le-filament.com>)
-     License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
 <odoo>
+    <!-- Form View -->
     <record id="training_training_form" model="ir.ui.view">
         <field name="name">training.training.survey.form</field>
         <field name="model">training.training</field>
         <field name="inherit_id" ref="training.training_training_form" />
         <field name="arch" type="xml">
+            <xpath expr="//div[@name='button_box']" position="inside">
+                <button
+                    name="action_view_survey"
+                    type="object"
+                    icon="fa-question-circle-o"
+                    class="oe_stat_button"
+                >
+                    <span class="o_stat_text">Questionnaires</span>
+                </button>
+            </xpath>
             <xpath expr="//notebook" position="inside">
                 <page string="Questionnaires" name="survey">
                     <group>
@@ -22,4 +31,21 @@
         </field>
     </record>
 
+    <!-- Tree View -->
+    <record id="training_training_tree" model="ir.ui.view">
+        <field name="name">training.training.survey.tree</field>
+        <field name="model">training.training</field>
+        <field name="inherit_id" ref="training.training_training_tree" />
+        <field name="arch" type="xml">
+            <xpath expr="//field[@name='price_intra']" position="after">
+                <button
+                    name="action_view_survey"
+                    type="object"
+                    string="Questionnaires"
+                    class="border"
+                />
+            </xpath>
+        </field>
+    </record>
+
 </odoo>
-- 
GitLab