From 15bff5af96280be2dc3d3918804359cef2c53039 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Fri, 16 Sep 2022 10:55:51 +0200 Subject: [PATCH] Smarter survey and question deletion/duplucation --- ui/src/components/SurveyAdmin.svelte | 17 ++++++++++---- ui/src/components/WorkAdmin.svelte | 6 ++--- ui/src/lib/questions.js | 6 +++++ ui/src/lib/surveys.js | 33 +++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/ui/src/components/SurveyAdmin.svelte b/ui/src/components/SurveyAdmin.svelte index 07b1455..ab37557 100644 --- a/ui/src/components/SurveyAdmin.svelte +++ b/ui/src/components/SurveyAdmin.svelte @@ -13,17 +13,21 @@ dispatch('saved', response); }, (error) => { ToastsStore.addErrorToast({ - msg: error.errmsg, + msg: error, }); }) } + let deleteInProgress = false; function deleteSurvey() { + deleteInProgress = true; survey.delete().then((response) => { + deleteInProgress = false; goto(`surveys`); }, (error) => { + deleteInProgress = false; ToastsStore.addErrorToast({ - msg: error.errmsg, + msg: error, }); }) } @@ -33,7 +37,7 @@ goto(`surveys/${response.id}`); }).catch((error) => { ToastsStore.addErrorToast({ - msg: error.errmsg, + msg: error, }); }) } @@ -137,7 +141,12 @@
{#if survey.id} - + {/if}
diff --git a/ui/src/components/WorkAdmin.svelte b/ui/src/components/WorkAdmin.svelte index b9e7c55..c493a06 100644 --- a/ui/src/components/WorkAdmin.svelte +++ b/ui/src/components/WorkAdmin.svelte @@ -12,7 +12,7 @@ dispatch('saved', response); }, (error) => { ToastsStore.addErrorToast({ - msg: error.errmsg, + msg: error, }); }) } @@ -22,7 +22,7 @@ goto(`works`); }, (error) => { ToastsStore.addErrorToast({ - msg: error.errmsg, + msg: error, }); }) } @@ -32,7 +32,7 @@ goto(`works/${response.id}`); }).catch((error) => { ToastsStore.addErrorToast({ - msg: error.errmsg, + msg: error, }); }) } diff --git a/ui/src/lib/questions.js b/ui/src/lib/questions.js index bb81ab0..53e2115 100644 --- a/ui/src/lib/questions.js +++ b/ui/src/lib/questions.js @@ -120,6 +120,12 @@ export class Question { async delete() { if (this.id) { + // Start by deleting proposals + const proposals = await this.getProposals(); + for (const p of proposals) { + await p.delete(); + } + const res = await fetch(`api/questions/${this.id}`, { method: 'DELETE', headers: {'Accept': 'application/json'}, diff --git a/ui/src/lib/surveys.js b/ui/src/lib/surveys.js index b3904d3..9e1e69f 100644 --- a/ui/src/lib/surveys.js +++ b/ui/src/lib/surveys.js @@ -1,3 +1,4 @@ +import { getCorrectionTemplates } from './correctionTemplates'; import { getQuestions } from './questions'; import { Response } from './response'; import { Work } from './works'; @@ -101,9 +102,33 @@ export class Survey { // Now recopy questions const questions = await getQuestions(oldSurveyId); for (const q of questions) { + const oldQuestionId = q.id; + delete q.id; q.id_survey = response.id; - q.save(); + q.save().then((question) => { + q.id = oldQuestionId; + + // Now recopy proposals + if (q.kind == "mcq" || q.kind == "ucq") { + q.getProposals().then((proposals) => { + for (const p of proposals) { + delete p.id; + p.id_question = question.id; + p.save(); + } + }); + } + + // Now recopy correction templates + getCorrectionTemplates(oldQuestionId).then((cts) => { + for (const ct of cts) { + delete ct.id; + ct.id_question = question.id; + ct.save(); + } + }); + }); } return response; @@ -115,6 +140,12 @@ export class Survey { async delete() { if (this.id) { + // Start by deleting questions + const questions = await getQuestions(this.id); + for (const q of questions) { + await q.delete(); + } + const res = await fetch(`api/surveys/${this.id}`, { method: 'DELETE', headers: {'Accept': 'application/json'},