Improve live admin

This commit is contained in:
nemunaire 2022-09-02 12:00:13 +02:00
parent 0f8d77b05f
commit 4f13efbab1
2 changed files with 66 additions and 15 deletions

View File

@ -359,6 +359,7 @@ func SurveyWSAdmin(c *gin.Context) {
if survey, err := getSurvey(sid); err != nil {
log.Println("Unable to retrieve survey:", err)
} else {
survey.EndAvailability = time.Now()
survey.Direct = nil
_, err = survey.Update()
if err != nil {

View File

@ -12,10 +12,11 @@
<script>
import { user } from '../../../stores/user';
import CorrectionPieChart from '../../../components/CorrectionPieChart.svelte';
import QuestionForm from '../../../components/QuestionForm.svelte';
import SurveyAdmin from '../../../components/SurveyAdmin.svelte';
import SurveyBadge from '../../../components/SurveyBadge.svelte';
import { getSurvey } from '../../../lib/surveys';
import { getQuestions } from '../../../lib/questions';
import { getQuestion, getQuestions, Question } from '../../../lib/questions';
import { getUsers } from '../../../lib/users';
export let surveyP;
@ -33,18 +34,32 @@
function updateSurvey() {
surveyP = getSurvey(survey.id);
surveyP.then((s) => {
survey = s;
updateQuestions();
if (survey.direct !== null) {
wsconnect();
}
});
}
function updateQuestions() {
req_questions = getQuestions(survey.id);
}
function deleteQuestion(question) {
edit_question = null;
question.delete();
}
let ws = null;
let ws_up = false;
let wsstats = null;
let current_question = null;
let edit_question = null;
let responses = {};
let corrected = false;
let next_corrected = false;
let timer = 20;
let timer_end = null;
let timer_remain = 0;
@ -85,7 +100,7 @@
responsesbyid = tmp;
}
let graph_data = {labels:[]};
let graph_data = {labels:[], datasets:[]};
async function reset_graph_data(questionid) {
if (questionid) {
const labels = [];
@ -147,23 +162,22 @@
ws.addEventListener("close", (e) => {
ws_up = false;
console.log('Socket is closed. Reconnect will be attempted in 1 second.');
setTimeout(function() {
ws = null;
updateSurvey();
setTimeout(function() {
wsconnect();
}, 1500);
});
ws.addEventListener("error", (err) => {
ws_up = false;
console.log('Socket closed due to error.', err);
ws = null;
});
ws.addEventListener("message", (message) => {
const data = JSON.parse(message.data);
if (data.action && data.action == "new_question") {
current_question = data.question;
corrected = data.corrected == true;
if (timer_cancel) {
clearInterval(timer_cancel);
timer_cancel = null;
@ -185,6 +199,9 @@
} else if (data.action && data.action == "new_ask") {
asks.push({"id": data.question, "content": data.value, "userid": data.user});
asks = asks;
} else if (data.action && data.action == "end") {
ws.close();
updateSurvey();
} else {
current_question = null;
timer_end = null;
@ -209,6 +226,15 @@
>
<i class="bi bi-align-end"></i>
</button>
{:else}
<button
type="button"
class="btn btn-primary ms-1 float-end"
title="Commencer le direct"
on:click={() => {survey.shown = true; survey.direct = 0; survey.start_availability = new Date(); survey.end_availability = new Date(Date.now() + 43200000); survey.save().then(() => updateSurvey());}}
>
<i class="bi bi-align-start"></i>
</button>
{/if}
<a href="surveys/{survey.id}/responses" class="btn btn-success ms-1 float-end" title="Voir les réponses"><i class="bi bi-files"></i></a>
{/if}
@ -306,13 +332,21 @@
<button
type="button"
class="btn btn-sm"
class:btn-outline-success={!corrected}
class:btn-success={corrected}
on:click={() => { corrected = !corrected } }
class:btn-outline-success={!next_corrected}
class:btn-success={next_corrected}
on:click={() => { next_corrected = !next_corrected } }
title="La prochaine question est affichée corrigée"
>
<i class="bi bi-eye"></i>
</button>
<button
type="button"
class="btn btn-sm btn-info"
on:click={() => { edit_question = new Question({ id_survey: survey.id }) } }
title="Ajouter une question"
>
<i class="bi bi-plus"></i>
</button>
</th>
</tr>
</thead>
@ -340,10 +374,10 @@
<button
type="button"
class="btn btn-sm"
class:btn-primary={!corrected}
class:btn-success={corrected}
disabled={question.id === current_question || !ws_up}
on:click={() => { ws.send('{"action":"new_question", "corrected": ' + corrected + ', "timer": 0, "question":' + question.id + '}')} }
class:btn-primary={!next_corrected}
class:btn-success={next_corrected}
disabled={(question.id === current_question && next_corrected == corrected) || !ws_up}
on:click={() => { ws.send('{"action":"new_question", "corrected": ' + next_corrected + ', "timer": 0, "question":' + question.id + '}')} }
>
<i class="bi bi-play-fill"></i>
</button>
@ -351,7 +385,7 @@
type="button"
class="btn btn-sm btn-danger"
disabled={question.id === current_question || !ws_up}
on:click={() => { ws.send('{"action":"new_question", "corrected": ' + corrected + ', "timer": ' + timer * 1000 + ',"question":' + question.id + '}')} }
on:click={() => { ws.send('{"action":"new_question", "corrected": ' + next_corrected + ', "timer": ' + timer * 1000 + ',"question":' + question.id + '}')} }
>
<i class="bi bi-stopwatch-fill"></i>
</button>
@ -363,6 +397,14 @@
>
<i class="bi bi-files"></i>
</a>
<button
type="button"
class="btn btn-sm btn-info"
disabled={question.id === current_question}
on:click={() => { getQuestion(question.id).then((q) => {edit_question = q})} }
>
<i class="bi bi-pencil"></i>
</button>
</td>
</tr>
{/each}
@ -370,6 +412,14 @@
</table>
</div>
{/await}
{#if edit_question !== null}
<QuestionForm
{survey}
edit
question={edit_question}
on:delete={() => deleteQuestion(edit_question)}
/>
{/if}
<hr>
<button
@ -588,7 +638,7 @@
<small class="text-muted">{wsstats.nb_clients} utilisateurs</small>
{/if}
</h3>
{#if wsstats}
{#if wsstats && wsstats.users}
<div class="row row-cols-5 py-3">
{#each wsstats.users as login, lid (lid)}
<div class="col">