diff --git a/direct.go b/direct.go index a239512..02eb44e 100644 --- a/direct.go +++ b/direct.go @@ -98,16 +98,9 @@ func msgCurrentState(survey *Survey) (msg WSMessage) { Action: "pause", } } else { - var correction map[string]int - if survey.Corrected { - correction = getCorrectionString(*survey.Direct) - } - msg = WSMessage{ - Action: "new_question", - QuestionId: survey.Direct, - Corrected: survey.Corrected, - Corrections: correction, + Action: "new_question", + QuestionId: survey.Direct, } } return @@ -153,15 +146,13 @@ func WSWriteAll(message WSMessage) { } type WSMessage struct { - Action string `json:"action"` - SurveyId *int64 `json:"survey,omitempty"` - QuestionId *int64 `json:"question,omitempty"` - Stats map[string]interface{} `json:"stats,omitempty"` - UserId *int64 `json:"user,omitempty"` - Response string `json:"value,omitempty"` - Corrected bool `json:"corrected,omitempty"` - Corrections map[string]int `json:"corrections,omitempty"` - Timer uint `json:"timer,omitempty"` + Action string `json:"action"` + SurveyId *int64 `json:"survey,omitempty"` + QuestionId *int64 `json:"question,omitempty"` + Stats map[string]interface{} `json:"stats,omitempty"` + UserId *int64 `json:"user,omitempty"` + Response string `json:"value,omitempty"` + Timer uint `json:"timer,omitempty"` } func (s *Survey) WSWriteAll(message WSMessage) { @@ -238,25 +229,6 @@ loopadmin: log.Println(u.Login, "admin disconnected") } -func getCorrectionString(qid int64) (ret map[string]int) { - q, err := getQuestion(int(qid)) - if err != nil { - return - } - - cts, err := q.GetCorrectionTemplates() - if err != nil { - return - } - - ret = map[string]int{} - for _, ct := range cts { - ret[ct.RegExp] = ct.Score - } - - return -} - func SurveyWSAdmin(c *gin.Context) { u := c.MustGet("LoggedUser").(*User) survey := c.MustGet("survey").(*Survey) @@ -302,29 +274,15 @@ func SurveyWSAdmin(c *gin.Context) { if *survey.Direct != 0 { var z int64 = 0 survey.Direct = &z - survey.Corrected = false survey.Update() } - go func(corrected bool) { + go func() { time.Sleep(time.Duration(OffsetQuestionTimer+v.Timer) * time.Millisecond) - - if corrected { - survey.Corrected = v.Corrected - survey.Update() - - survey.WSWriteAll(WSMessage{Action: "new_question", QuestionId: v.QuestionId, Corrected: true, Corrections: getCorrectionString(*v.QuestionId)}) - } else { - survey.WSWriteAll(WSMessage{Action: "pause"}) - WSAdminWriteAll(WSMessage{Action: "pause", SurveyId: &survey.Id}) - } - }(v.Corrected) - v.Corrected = false + survey.WSWriteAll(WSMessage{Action: "pause"}) + WSAdminWriteAll(WSMessage{Action: "pause", SurveyId: &survey.Id}) + }() } else { survey.Direct = v.QuestionId - survey.Corrected = v.Corrected - if v.Corrected { - v.Corrections = getCorrectionString(*v.QuestionId) - } } _, err = survey.Update() if err != nil { diff --git a/help.go b/help.go index 181ae7e..53e85ca 100644 --- a/help.go +++ b/help.go @@ -1,10 +1,8 @@ package main import ( - "fmt" "log" "net/http" - "strconv" "time" "github.com/gin-gonic/gin" @@ -12,7 +10,7 @@ import ( func declareAPIAdminHelpRoutes(router *gin.RouterGroup) { router.GET("/help", func(c *gin.Context) { - nhs, err := getNeedHelps("WHERE date_treated IS NULL") + nhs, err := getNeedHelps() if err != nil { log.Println("Unable to getNeedHelps:", err) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during need helps retrieval. Please retry."}) @@ -21,29 +19,6 @@ func declareAPIAdminHelpRoutes(router *gin.RouterGroup) { c.JSON(http.StatusOK, nhs) }) - - needhelpsRoutes := router.Group("/help/:hid") - needhelpsRoutes.Use(needHelpHandler) - - needhelpsRoutes.PUT("", func(c *gin.Context) { - current := c.MustGet("needhelp").(*NeedHelp) - - var new NeedHelp - if err := c.ShouldBindJSON(&new); err != nil { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()}) - return - } - - new.Id = current.Id - - if err := new.Update(); err != nil { - log.Println("Unable to Update needhelp:", err) - c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("An error occurs during needhelp entry updation: %s", err.Error())}) - return - } else { - c.JSON(http.StatusOK, new) - } - }) } func declareAPIAuthHelpRoutes(router *gin.RouterGroup) { @@ -61,19 +36,6 @@ func declareAPIAuthHelpRoutes(router *gin.RouterGroup) { }) } -func needHelpHandler(c *gin.Context) { - if hid, err := strconv.Atoi(string(c.Param("hid"))); err != nil { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Bad need help identifier."}) - return - } else if nh, err := getNeedHelp(hid); err != nil { - c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Need help entry not found."}) - return - } else { - c.Set("needhelp", nh) - c.Next() - } -} - type NeedHelp struct { Id int64 `json:"id"` IdUser int64 `json:"id_user"` @@ -82,8 +44,8 @@ type NeedHelp struct { DateTreated *time.Time `json:"treated,omitempty"` } -func getNeedHelps(cond string) (nh []NeedHelp, err error) { - if rows, errr := DBQuery("SELECT id_need_help, id_user, date, comment, date_treated FROM user_need_help " + cond); errr != nil { +func getNeedHelps() (nh []NeedHelp, err error) { + if rows, errr := DBQuery("SELECT id_need_help, id_user, date, comment, date_treated FROM user_need_help"); errr != nil { return nil, errr } else { defer rows.Close() @@ -103,12 +65,6 @@ func getNeedHelps(cond string) (nh []NeedHelp, err error) { } } -func getNeedHelp(id int) (n *NeedHelp, err error) { - n = new(NeedHelp) - err = DBQueryRow("SELECT id_need_help, id_user, date, comment, date_treated FROM user_need_help WHERE id_need_help=?", id).Scan(&n.Id, &n.IdUser, &n.Date, &n.Comment, &n.DateTreated) - return -} - func (u *User) NewNeedHelp() (NeedHelp, error) { if res, err := DBExec("INSERT INTO user_need_help (id_user, comment) VALUES (?, ?)", u.Id, ""); err != nil { return NeedHelp{}, err diff --git a/responses.go b/responses.go index 9e0e809..4db50bb 100644 --- a/responses.go +++ b/responses.go @@ -1,8 +1,6 @@ package main import ( - "database/sql" - "errors" "log" "net/http" "strconv" @@ -29,7 +27,7 @@ func declareAPIAuthResponsesRoutes(router *gin.RouterGroup) { } var responses []Response - if err := c.ShouldBindJSON(&responses); err != nil { + if err := c.ShouldBindJSON(responses); err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()}) return } @@ -47,7 +45,7 @@ func declareAPIAuthResponsesRoutes(router *gin.RouterGroup) { } for _, response := range responses { - if !uauth.IsAdmin && !s.Shown && (s.Corrected || s.Direct == nil || *s.Direct != response.IdQuestion) { + if !uauth.IsAdmin && !s.Shown && (s.Direct == nil || *s.Direct != response.IdQuestion) { c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"errmsg": "Cette question n'est pas disponible"}) return } else if len(response.Answer) > 0 { @@ -144,7 +142,7 @@ func declareAPIAuthQuestionResponsesRoutes(router *gin.RouterGroup) { q := c.MustGet("question").(*Question) res, err := q.GetMyResponse(u, false) - if err != nil && !errors.Is(err, sql.ErrNoRows) { + if err != nil { log.Printf("Unable to GetMyResponse(uid=%d;qid=%d;false): %s", u.Id, q.Id, err.Error()) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during response retrieval."}) return diff --git a/ui/src/components/CorrectionPieChart.svelte b/ui/src/components/CorrectionPieChart.svelte index 2a1eed3..f203a23 100644 --- a/ui/src/components/CorrectionPieChart.svelte +++ b/ui/src/components/CorrectionPieChart.svelte @@ -31,41 +31,34 @@ return req; } let req_proposals = null; - export let proposals = null; let req_responses = null; - let mean = null; - export let data = { + if (question.kind == "int") { + req_responses = question.getResponses(); + req_responses.then((responses) => { + const proposal_idx = { }; + for (const res of responses) { + if (proposal_idx[res.value]) { + data.datasets[0].values[proposal_idx[res.value]] += 1; + } else { + data.labels.push(res.value); + data.datasets[0].values.push(1); + proposal_idx[res.value] = new String(data.labels.length - 1); + } + } + }); + } else { + req_proposals = refreshProposals(); + } + + let data = { labels: [], datasets: [ { values: [] } ] - }; - - if (!proposals) { - if (question.kind == "int") { - req_responses = question.getResponses(); - req_responses.then((responses) => { - const values = []; - const proposal_idx = { }; - for (const res of responses) { - if (proposal_idx[res.value]) { - data.datasets[0].values[proposal_idx[res.value]] += 1; - values.push(Number(res.value)); - } else { - data.labels.push(res.value); - data.datasets[0].values.push(1); - proposal_idx[res.value] = new String(data.labels.length - 1); - } - } - mean = Math.trunc(values.reduce((p, e) => p + e) / values.length*10)/10; - }); - } else { - req_proposals = refreshProposals(); - } - } + };
@@ -81,16 +74,7 @@ Récupération des réponses…
{:then} - {#if mean !== null} -
- Moyenne représentative : {mean} -
- {/if} - {#if question.kind === "mcq"} - - {:else} - - {/if} + {/await} {/await} diff --git a/ui/src/components/CorrectionReference.svelte b/ui/src/components/CorrectionReference.svelte index f1c51e1..83b62fd 100644 --- a/ui/src/components/CorrectionReference.svelte +++ b/ui/src/components/CorrectionReference.svelte @@ -19,35 +19,6 @@ templates = templates; } - function genTemplates() { - question.getProposals().then((proposals) => { - let i = 0; - for (const p of proposals) { - // Search proposal in templates - let found = false; - for (const tpl of templates) { - if (tpl.regexp.indexOf(p.id.toString()) !== -1) { - found = true; - break; - } - } - - if (!found) { - const ct = new CorrectionTemplate() - ct.id_question = question.id; - ct.regexp = p.id.toString(); - ct.label = String.fromCharCode(97 + i); - ct.save().then((ct) => { - templates.push(ct); - templates = templates; - }); - } - - i++; - } - }) - } - function delTemplate(tpl) { tpl.delete().then(() => { const idx = templates.findIndex((e) => e.id === tpl.id); @@ -138,13 +109,4 @@ > Ajouter un template - {#if question.kind == "mcq" || question.kind == "ucq"} - - {/if} diff --git a/ui/src/components/QuestionForm.svelte b/ui/src/components/QuestionForm.svelte index 8bddf1a..6a11e9f 100644 --- a/ui/src/components/QuestionForm.svelte +++ b/ui/src/components/QuestionForm.svelte @@ -14,7 +14,6 @@ export let qid; export let response_history = null; export let readonly = false; - export let corrections = {}; export let survey = null; export let value = ""; @@ -92,8 +91,6 @@ kind={question.kind} {proposals} readonly - live={survey.direct !== null} - {corrections} bind:value={value} on:change={() => { dispatch("change"); }} /> @@ -111,8 +108,6 @@ kind={question.kind} {proposals} {readonly} - live={survey.direct !== null} - {corrections} bind:value={value} on:change={() => { dispatch("change"); }} /> @@ -137,7 +132,7 @@ > {/if} - {#if survey && survey.corrected && response_history} + {#if survey && survey.corrected} -
{#each proposals as proposal, pid (proposal.id)}
{#if kind == 'mcq'} {:else}
{/each} -
{#if edit} {#if kind == 'mcq'} { - return new UserNeedingHelp(nh) - }); + return await res.json(); } else { throw new Error((await res.json()).errmsg); } diff --git a/ui/src/routes/index.svelte b/ui/src/routes/index.svelte index 1da957e..de5e9aa 100644 --- a/ui/src/routes/index.svelte +++ b/ui/src/routes/index.svelte @@ -1,24 +1,10 @@
@@ -43,7 +29,7 @@ {#if $user.is_admin}

Demande d'aide :

- {#await users_needing_help} + {#await getUserNeedingHelp()} {:then nhs}
    @@ -54,14 +40,7 @@ {:then u} {u.login} {/await} - () - + ({user.date}) {/each}
diff --git a/ui/src/routes/surveys/[sid]/admin.svelte b/ui/src/routes/surveys/[sid]/admin.svelte index 7c8c60b..64e45b8 100644 --- a/ui/src/routes/surveys/[sid]/admin.svelte +++ b/ui/src/routes/surveys/[sid]/admin.svelte @@ -11,7 +11,6 @@ -{#await surveyP then unused} +{#await surveyP then survey} {#if $user && $user.is_admin} @@ -189,10 +185,8 @@
{:then question} = 100 || survey.corrected} - {corrections} + readonly={timer >= 100} bind:value={value} on:change={sendValue} > @@ -206,7 +200,7 @@ {/if} {/await} @@ -232,7 +226,7 @@ class="form-control" bind:value={myQuestion} autofocus - placeholder="Remarques, soucis, choses pas claires ? Levez la main ou écrivez ici !" + placeholder="Remarques, soucis, choses pas claires ? Demandez !" >