diff --git a/backend/app/main.py b/backend/app/main.py index 82769b0190262702ca903c0fb5a5b18943eb860a..91fc0b3afb52f0f92983d70d4717e58be3d0b028 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -1249,6 +1249,29 @@ def get_survey_responses( return crud.get_survey_responses(db, survey_id) +@surveyRouter.get("/{survey_id}/score", response_model=dict) +def get_survey_score( + survey_id: int, + db: Session = Depends(get_db), +): + if not crud.get_survey(db, survey_id): + raise HTTPException(status_code=404, detail="Survey not found") + + responses = crud.get_survey_responses(db, survey_id) + + score = 0 + total = 0 + for response in responses: + question = crud.get_survey_question(db, response.question_id) + if not question: + continue + total += 1 + if response.selected_id == question.correct: + score += 1 + + return {"survey_id": survey_id, "score": round((score / total) * 100, 2)} + + v1Router.include_router(authRouter) v1Router.include_router(usersRouter) v1Router.include_router(sessionsRouter) diff --git a/frontend/src/lang/fr.json b/frontend/src/lang/fr.json index fbd331035fb46e5e4ef341d22b73195309f4b496..6062fb6011908f62b75ad31ec07992c748cfc17e 100644 --- a/frontend/src/lang/fr.json +++ b/frontend/src/lang/fr.json @@ -182,7 +182,8 @@ "loginUser": "Se connecter via un compte utilisateur", "loginEmail": "Utiliser une adresse e-mail", "invalidEmail": "Adresse e-mail invalide", - "complete": "Questionnaire complété. Merci pour votre participation !" + "complete": "Questionnaire complété. Merci pour votre participation !", + "score": "Vous avez obtenu un score de " }, "users": { "nickname": "Nom", diff --git a/frontend/src/lib/api/survey.ts b/frontend/src/lib/api/survey.ts index 93a27dc2c06701a4ca8e205e47b3621c692dafb3..47ce425ccec79643ab2e1aff8868974c5f74e74c 100644 --- a/frontend/src/lib/api/survey.ts +++ b/frontend/src/lib/api/survey.ts @@ -40,3 +40,12 @@ export async function sendSurveyResponseAPI( return true; } + +export async function getSurveyScoreAPI(survey_id: number) { + const response = await axiosInstance.get(`/surveys/${survey_id}/score`); + if (response.status !== 200) { + toastAlert('Failed to retrieve survey score'); + return null; + } + return response.data; +} diff --git a/frontend/src/routes/tests/[id]/+page.svelte b/frontend/src/routes/tests/[id]/+page.svelte index b4adc8d51ad165f177b4d8b2788c6cafa6c6e270..23dac4b2118f0d66bfd8dc284db9c07142563af5 100644 --- a/frontend/src/routes/tests/[id]/+page.svelte +++ b/frontend/src/routes/tests/[id]/+page.svelte @@ -1,5 +1,6 @@ <script lang="ts"> import { sendSurveyResponseAPI } from '$lib/api/survey'; + import { getSurveyScoreAPI } from '$lib/api/survey'; import Survey from '$lib/types/survey.js'; import { t } from '$lib/services/i18n'; @@ -36,6 +37,7 @@ let soundPlayer: HTMLAudioElement; let displayQuestionOptions: string[] = [...(currentQuestion.options ?? [])]; shuffle(displayQuestionOptions); + let finalScore: number | null = null; //source: shuffle function code taken from https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array/18650169#18650169 function shuffle(array: string[]) { @@ -75,7 +77,7 @@ sid, survey.id, currentGroupId, - currentQuestionId, + questionsRandomized[currentQuestionId]['_id'], currentQuestion.options.findIndex((o: string) => o === option), (new Date().getTime() - startTime) / 1000 )) @@ -105,7 +107,7 @@ sid, survey.id, currentGroupId, - currentQuestionId, + questionsRandomized[currentQuestionId]['_id'], -1, (new Date().getTime() - startTime) / 1000, gapTexts @@ -121,10 +123,14 @@ } } - function nextGroup() { + async function nextGroup() { if (currentGroupId < survey.groups.length - 1) { setGroupId(currentGroupId + 1); } else { + const scoreData = await getSurveyScoreAPI(survey.id); + if (scoreData) { + finalScore = scoreData.score; + } step++; } } @@ -251,5 +257,8 @@ {:else if step == 3} <div class="mx-auto mt-16 text-center"> <h1>{$t('surveys.complete')}</h1> + {#if finalScore !== null} + <p>{$t('surveys.score')} {finalScore} %</p> + {/if} </div> {/if}