Live: Retrieve previous submission
This commit is contained in:
parent
0e5961c406
commit
3ae644fe13
@ -4,7 +4,7 @@
|
||||
export { className as class };
|
||||
</script>
|
||||
|
||||
{#if survey.direct}<span class="badge bg-danger {className}">Direct</span>
|
||||
{#if survey.direct != null}<span class="badge bg-danger {className}">Direct</span>
|
||||
{:else if survey.startAvailability() > Date.now()}<span class="badge bg-info {className}">Prévu</span>
|
||||
{:else if survey.endAvailability() > Date.now()}<span class="badge bg-warning {className}">En cours</span>
|
||||
{:else if !survey.corrected}<span class="badge bg-primary text-light {className}">Terminé</span>
|
||||
|
@ -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}
|
||||
<tbody style="cursor: pointer;">
|
||||
{#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)}
|
||||
<tr class="bg-info text-light">
|
||||
<th colspan="5" class="fw-bold">
|
||||
@ -47,7 +47,7 @@
|
||||
</th>
|
||||
</tr>
|
||||
{/if}
|
||||
<tr on:click={e => goto(survey.direct?`surveys/${survey.id}/live`:$user.is_admin?`surveys/${survey.id}/responses`:`surveys/${survey.id}`)}>
|
||||
<tr on:click={e => goto(survey.direct != null ?`surveys/${survey.id}/live`:$user.is_admin?`surveys/${survey.id}/responses`:`surveys/${survey.id}`)}>
|
||||
<td>
|
||||
{survey.title}
|
||||
<SurveyBadge {survey} class="float-end" />
|
||||
|
@ -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',
|
||||
|
@ -181,7 +181,7 @@
|
||||
disabled={!current_question || !ws_up}
|
||||
on:click={() => { ws.send('{"action":"pause"}')} }
|
||||
>
|
||||
Pause
|
||||
<i class="bi bi-pause-fill"></i>
|
||||
</button>
|
||||
</th>
|
||||
</tr>
|
||||
@ -213,7 +213,7 @@
|
||||
disabled={question.id === current_question || !ws_up}
|
||||
on:click={() => { ws.send('{"action":"new_question", "question":' + question.id + '}')} }
|
||||
>
|
||||
Lancer cette question
|
||||
<i class="bi bi-play-fill"></i>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
|
@ -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 @@
|
||||
|
||||
<form on:submit|preventDefault={sendValue}>
|
||||
{#if show_question}
|
||||
{#await getQuestion(show_question)}
|
||||
Please wait
|
||||
{#await req_question}
|
||||
<div class="text-center">
|
||||
<div class="spinner-border text-primary mx-3" role="status"></div>
|
||||
<span>Chargement d'une nouvelle question …</span>
|
||||
</div>
|
||||
{:then question}
|
||||
<QuestionForm
|
||||
qid={show_question}
|
||||
@ -104,6 +127,13 @@
|
||||
<div class="progress-bar" role="progressbar" style="width: 25%"></div>
|
||||
</div-->
|
||||
</QuestionForm>
|
||||
{#if question.kind != 'mcq' && question.kind != 'ucq'}
|
||||
<button
|
||||
class="btn btn-primary"
|
||||
>
|
||||
Soumettre la réponse
|
||||
</button>
|
||||
{/if}
|
||||
{/await}
|
||||
{:else if ws_up}
|
||||
<h2 class="text-center">
|
||||
|
13
responses.go
13
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
|
||||
|
Reference in New Issue
Block a user