From f94dfd3b7357d05d0cc4d4db4148c0c08a5b5b35 Mon Sep 17 00:00:00 2001
From: VIAUD Nathan <nathan.viaud@inria.fr>
Date: Thu, 14 Mar 2024 17:44:17 +0100
Subject: [PATCH] feat: add custom question content

---
 .../inputs/card/components/SelectInput.vue    | 29 +++++++-
 src/shared/data/forms/nodeForm.data.ts        | 27 ++------
 src/shared/data/forms/questionsForm.data.ts   | 67 ++++++++++++++++++-
 src/shared/data/sideBar.data.ts               |  5 ++
 4 files changed, 106 insertions(+), 22 deletions(-)

diff --git a/src/features/forms/components/inputs/card/components/SelectInput.vue b/src/features/forms/components/inputs/card/components/SelectInput.vue
index bdfe371c..533c0c96 100644
--- a/src/features/forms/components/inputs/card/components/SelectInput.vue
+++ b/src/features/forms/components/inputs/card/components/SelectInput.vue
@@ -21,7 +21,34 @@ const emit = defineEmits<{
 const currentNode = editorStore.getCurrentGraphNode;
 const currentContent = currentNode.data.elements.find(({ id }) => id === editorStore.openedElementId);
 
-const getOptions = () => (props.linkedOptions ? currentContent.formValues[props.linkedOptions] : props.options);
+function getOptions() {
+    if(!props.linkedOptions) return props.options;
+
+    // In this case we have to change the epoc formValues
+    //? refactor this if another case is needed
+    if(props.id === 'plugin') {
+        const epocNode = editorStore.getEpocNode;
+
+        return walkObjectPath(epocNode.data.formValues, props.linkedOptions);
+    } else {
+        return currentContent.formValues[props.linkedOptions];
+
+    }
+}
+
+function walkObjectPath(object: any, path: string) {
+    const currentKey = path.split('.')[0];
+
+    if(!currentKey) {
+        return object;
+    }
+
+    if(currentKey === '*') {
+        return object.map((item: any) => walkObjectPath(item, path.slice(2)));
+    } else {
+        return walkObjectPath(object[currentKey], path.slice(currentKey.length + 1));
+    }
+}
 
 function onChange(event: Event) {
     const target = event.target as HTMLInputElement;
diff --git a/src/shared/data/forms/nodeForm.data.ts b/src/shared/data/forms/nodeForm.data.ts
index eb1d4c1c..66658238 100644
--- a/src/shared/data/forms/nodeForm.data.ts
+++ b/src/shared/data/forms/nodeForm.data.ts
@@ -304,36 +304,23 @@ export const epocForm: Form = {
                     value: [],
                     inputs: [
                         {
-                            id: '',
+                            id: 'script',
                             type: 'file',
-                            label: 'Fichier de plugin',
-                            placeholder: 'Ajouter un plugin',
+                            label: 'Fichier de script',
+                            placeholder: 'Ajouter un script',
                             targetDirectory: 'plugins',
                             value: '',
                             accept: '.js',
                         },
-                    ],
-                },
-            ],
-        },
-        {
-            name: 'Plugins templates',
-            inputs: [
-                {
-                    id: 'templates',
-                    label: 'Plugin templates',
-                    type: 'repeat',
-                    value: [],
-                    inputs: [
                         {
-                            id: '',
+                            id: 'template',
                             type: 'file',
-                            label: 'Template html de plugin',
+                            label: 'Template html du plugin',
                             placeholder: 'Ajouter un template',
                             targetDirectory: 'plugins',
                             value: '',
-                            accept: '.html',
-                        },
+                            accept: 'html'
+                        }
                     ],
                 },
             ],
diff --git a/src/shared/data/forms/questionsForm.data.ts b/src/shared/data/forms/questionsForm.data.ts
index 763c54b5..6761a6c1 100644
--- a/src/shared/data/forms/questionsForm.data.ts
+++ b/src/shared/data/forms/questionsForm.data.ts
@@ -476,4 +476,69 @@ export const listForm: Form = {
     ],
 };
 
-export const questionForms: Form[] = [qcmForm, swipeForm, reorderForm, dragDropForm, listForm];
+
+export const customQuestionForm: Form = {
+    type: 'custom-question',
+    name: 'Question personnalisée',
+    icon: 'icon-terminal',
+    displayFieldIndex: true,
+    buttons: contentButtons,
+    fields: [
+        {
+            name: "Configuration de l'activité",
+            inputs: [
+                {
+                    id: 'score',
+                    type: 'score',
+                    label: 'Score',
+                    value: 0,
+                },
+            ],
+        },
+        {
+            name: 'Question',
+            inputs: [
+                {
+                    id: 'label',
+                    type: 'textarea',
+                    label: 'Question',
+                    value: '',
+                    placeholder: 'Posez la question',
+                },
+                {
+                    id: 'plugin',
+                    type: 'select',
+                    label: 'Selectionnez un plugin',
+                    value: '',
+                    options: [],
+                    linkedOptions: "plugins.*.template",
+                }
+            ],
+        },
+        {
+            name: 'Réponses',
+            inputs: [
+                {
+                    id: 'response',
+                    label: 'Réponse',
+                    type: 'text',
+                    value: ''
+                }
+            ],
+        },
+        {
+            name: 'Explication',
+            inputs: [
+                {
+                    id: 'explanation',
+                    type: 'html',
+                    label: '',
+                    value: '',
+                    placeholder: 'Saisissez une explication',
+                },
+            ],
+        },
+    ],
+};
+
+export const questionForms: Form[] = [qcmForm, swipeForm, reorderForm, dragDropForm, listForm, customQuestionForm];
diff --git a/src/shared/data/sideBar.data.ts b/src/shared/data/sideBar.data.ts
index 127e71e5..11a92625 100644
--- a/src/shared/data/sideBar.data.ts
+++ b/src/shared/data/sideBar.data.ts
@@ -26,6 +26,11 @@ export const questions: SideAction[] = [
         type: 'dropdown-list',
         label: 'Liste déroulantes',
     },
+    {
+        icon: 'icon-terminal',
+        type: 'custom-question',
+        label: 'Question personnalisée',
+    }
 ];
 
 const contents: SideAction[] = [
-- 
GitLab