From 3ae644fe13b882e9ab992125415563616f455dce Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Tue, 1 Mar 2022 13:03:16 +0100 Subject: [PATCH] Live: Retrieve previous submission --- atsebayt/src/components/SurveyBadge.svelte | 2 +- atsebayt/src/components/SurveyList.svelte | 6 ++-- atsebayt/src/lib/questions.js | 12 +++++++ .../src/routes/surveys/[sid]/admin.svelte | 4 +-- atsebayt/src/routes/surveys/[sid]/live.svelte | 36 +++++++++++++++++-- responses.go | 13 +++++++ 6 files changed, 64 insertions(+), 9 deletions(-) diff --git a/atsebayt/src/components/SurveyBadge.svelte b/atsebayt/src/components/SurveyBadge.svelte index 2d87b5a..b224a82 100644 --- a/atsebayt/src/components/SurveyBadge.svelte +++ b/atsebayt/src/components/SurveyBadge.svelte @@ -4,7 +4,7 @@ export { className as class }; -{#if survey.direct}Direct +{#if survey.direct != null}Direct {:else if survey.startAvailability() > Date.now()}Prévu {:else if survey.endAvailability() > Date.now()}En cours {:else if !survey.corrected}Terminé diff --git a/atsebayt/src/components/SurveyList.svelte b/atsebayt/src/components/SurveyList.svelte index cd16519..0d8c415 100644 --- a/atsebayt/src/components/SurveyList.svelte +++ b/atsebayt/src/components/SurveyList.svelte @@ -12,7 +12,7 @@ req_surveys.then((surveys) => { for (const survey of surveys) { - if (survey.direct) { + if (survey.direct != null) { direct = survey; } } @@ -39,7 +39,7 @@ {:then surveys} {#each surveys as survey, sid (survey.id)} - {#if survey.shown && (!$user || (!$user.was_admin || $user.promo == survey.promo) || $user.is_admin)} + {#if (survey.shown || survey.direct != null) && (!$user || (!$user.was_admin || $user.promo == survey.promo) || $user.is_admin)} {#if $user && $user.is_admin && (sid == 0 || surveys[sid-1].promo != survey.promo)} @@ -47,7 +47,7 @@ {/if} - goto(survey.direct?`surveys/${survey.id}/live`:$user.is_admin?`surveys/${survey.id}/responses`:`surveys/${survey.id}`)}> + goto(survey.direct != null ?`surveys/${survey.id}/live`:$user.is_admin?`surveys/${survey.id}/responses`:`surveys/${survey.id}`)}> {survey.title} diff --git a/atsebayt/src/lib/questions.js b/atsebayt/src/lib/questions.js index 81ba6b5..f6903d4 100644 --- a/atsebayt/src/lib/questions.js +++ b/atsebayt/src/lib/questions.js @@ -74,6 +74,18 @@ export class Question { } } + async getMyResponse() { + const res = await fetch(`api/questions/${this.id}/response`, { + method: 'GET', + headers: {'Accept': 'application/json'}, + }); + if (res.status == 200) { + return new Response(await res.json()); + } else { + throw new Error((await res.json()).errmsg); + } + } + async getResponses() { const res = await fetch(`api/surveys/${this.id_survey}/questions/${this.id}/responses`, { method: 'GET', diff --git a/atsebayt/src/routes/surveys/[sid]/admin.svelte b/atsebayt/src/routes/surveys/[sid]/admin.svelte index 3abb015..8978448 100644 --- a/atsebayt/src/routes/surveys/[sid]/admin.svelte +++ b/atsebayt/src/routes/surveys/[sid]/admin.svelte @@ -181,7 +181,7 @@ disabled={!current_question || !ws_up} on:click={() => { ws.send('{"action":"pause"}')} } > - Pause + @@ -213,7 +213,7 @@ disabled={question.id === current_question || !ws_up} on:click={() => { ws.send('{"action":"new_question", "question":' + question.id + '}')} } > - Lancer cette question + diff --git a/atsebayt/src/routes/surveys/[sid]/live.svelte b/atsebayt/src/routes/surveys/[sid]/live.svelte index 1aa027f..d408b48 100644 --- a/atsebayt/src/routes/surveys/[sid]/live.svelte +++ b/atsebayt/src/routes/surveys/[sid]/live.svelte @@ -25,6 +25,26 @@ let show_question = null; let value; + let req_question; + let nosend = false; + + function afterQUpdate(q) { + value = undefined; + if (q) { + q.getMyResponse().then((response) => { + if (response && response.value) + value = response.value; + }) + } + } + + $: { + if (show_question) { + req_question = getQuestion(show_question); + req_question.then(afterQUpdate); + } + } + function wsconnect() { const ws = new WebSocket((window.location.protocol == 'https'?'wss://':'ws://') + window.location.host + `/api/surveys/${sid}/ws`); @@ -59,7 +79,7 @@ wsconnect(); function sendValue() { - if (show_question && value) { + if (show_question && value && !nosend) { survey.submitAnswers([{"id_question": show_question, "value": value}], $user.id_user).then((response) => { console.log("Vos réponses ont bien étés sauvegardées."); }, (error) => { @@ -91,8 +111,11 @@
{#if show_question} - {#await getQuestion(show_question)} - Please wait + {#await req_question} +
+
+ Chargement d'une nouvelle question … +
{:then question} + {#if question.kind != 'mcq' && question.kind != 'ucq'} + + {/if} {/await} {:else if ws_up}

diff --git a/responses.go b/responses.go index 7fa8055..d76368e 100644 --- a/responses.go +++ b/responses.go @@ -64,6 +64,10 @@ func init() { func(s Survey, u *User, _ []byte) HTTPResponse { return formatApiResponse(s.GetMyResponses(u, s.Corrected)) }), loggedUser)) + router.GET("/api/questions/:qid/response", apiAuthHandler(questionAuthHandler( + func(q Question, u *User, _ []byte) HTTPResponse { + return formatApiResponse(q.GetMyResponse(u, false)) + }), loggedUser)) router.GET("/api/users/:uid/surveys/:sid/responses", apiAuthHandler(func(u *User, ps httprouter.Params, body []byte) HTTPResponse { return surveyAuthHandler(func(s Survey, u *User, _ []byte) HTTPResponse { return userHandler(func(u User, _ []byte) HTTPResponse { @@ -207,6 +211,15 @@ func (s *Survey) GetMyResponses(u *User, showScore bool) (responses []Response, } } +func (q *Question) GetMyResponse(u *User, showScore bool) (r Response, err error) { + err = DBQueryRow("SELECT R.id_response, R.id_question, R.id_user, R.answer, R.time_submit, R.score, R.score_explanation, R.id_corrector, R.time_scored FROM survey_responses R WHERE R.id_question=? AND R.id_user=? ORDER BY time_submit DESC LIMIT 1", q.Id, u.Id).Scan(&r.Id, &r.IdQuestion, &r.IdUser, &r.Answer, &r.TimeSubmit, &r.Score, &r.ScoreExplaination, &r.IdCorrector, &r.TimeScored) + if !showScore { + r.Score = nil + r.ScoreExplaination = nil + } + return +} + func (q *Question) GetResponses() (responses []Response, err error) { if rows, errr := DBQuery("SELECT id_response, id_question, S.id_user, answer, S.time_submit, score, score_explanation, id_corrector, time_scored FROM (SELECT id_user, MAX(time_submit) AS time_submit FROM survey_responses WHERE id_question=? GROUP BY id_user) R INNER JOIN survey_responses S ON S.id_user = R.id_user AND S.time_submit = R.time_submit AND S.id_question=?", q.Id, q.Id); errr != nil { return nil, errr