lives: Can display results as charts on all screens
This commit is contained in:
parent
5b2fddddc1
commit
e61a8bd51d
3 changed files with 107 additions and 11 deletions
74
direct.go
74
direct.go
|
@ -2,8 +2,10 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -280,6 +282,62 @@ func getCorrectionString(qid int64) (ret map[string]int) {
|
|||
return
|
||||
}
|
||||
|
||||
func getResponsesStats(qid int64) map[string]interface{} {
|
||||
q, err := getQuestion(int(qid))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
responses, err := q.GetResponses()
|
||||
if err != nil {
|
||||
log.Println("Unable to retrieve responses:", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
labels := []string{}
|
||||
values := []uint{}
|
||||
|
||||
if q.Kind == "mcq" || q.Kind == "ucq" {
|
||||
proposals, err := q.GetProposals()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
proposal_idx := map[string]int{}
|
||||
for _, p := range proposals {
|
||||
proposal_idx[fmt.Sprintf("%d", p.Id)] = len(labels)
|
||||
labels = append(labels, p.Label)
|
||||
values = append(values, 0)
|
||||
}
|
||||
|
||||
for _, r := range responses {
|
||||
for _, v := range strings.Split(r.Answer, ",") {
|
||||
values[proposal_idx[v]]++
|
||||
}
|
||||
}
|
||||
} else {
|
||||
stats := map[string]uint{}
|
||||
|
||||
for _, r := range responses {
|
||||
stats[r.Answer]++
|
||||
}
|
||||
|
||||
for k, v := range stats {
|
||||
labels = append(labels, k)
|
||||
values = append(values, v)
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"labels": labels,
|
||||
"datasets": []map[string][]uint{
|
||||
map[string][]uint{
|
||||
"values": values,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func SurveyWSAdmin(c *gin.Context) {
|
||||
u := c.MustGet("LoggedUser").(*User)
|
||||
survey := c.MustGet("survey").(*Survey)
|
||||
|
@ -342,6 +400,7 @@ func SurveyWSAdmin(c *gin.Context) {
|
|||
|
||||
// Save corrected state for the callback
|
||||
corrected := v.Corrected
|
||||
with_stats := v.Stats != nil
|
||||
|
||||
surveyTimer = time.AfterFunc(time.Duration(OffsetQuestionTimer+v.Timer)*time.Millisecond, func() {
|
||||
surveyTimer = nil
|
||||
|
@ -349,7 +408,12 @@ func SurveyWSAdmin(c *gin.Context) {
|
|||
survey.Corrected = v.Corrected
|
||||
survey.Update()
|
||||
|
||||
survey.WSWriteAll(WSMessage{Action: "new_question", QuestionId: v.QuestionId, Corrected: true, Corrections: getCorrectionString(*v.QuestionId)})
|
||||
var stats map[string]interface{}
|
||||
if with_stats {
|
||||
stats = getResponsesStats(*v.QuestionId)
|
||||
}
|
||||
|
||||
survey.WSWriteAll(WSMessage{Action: "new_question", QuestionId: v.QuestionId, Corrected: true, Stats: stats, Corrections: getCorrectionString(*v.QuestionId)})
|
||||
} else {
|
||||
var z int64 = 0
|
||||
survey.Direct = &z
|
||||
|
@ -360,10 +424,18 @@ func SurveyWSAdmin(c *gin.Context) {
|
|||
}
|
||||
})
|
||||
v.Corrected = false
|
||||
v.Stats = nil
|
||||
} else {
|
||||
survey.Corrected = v.Corrected
|
||||
if v.Corrected {
|
||||
v.Corrections = getCorrectionString(*v.QuestionId)
|
||||
if v.Stats != nil {
|
||||
v.Stats = getResponsesStats(*v.QuestionId)
|
||||
} else {
|
||||
v.Stats = nil
|
||||
}
|
||||
} else {
|
||||
v.Stats = nil
|
||||
}
|
||||
}
|
||||
_, err = survey.Update()
|
||||
|
|
Reference in a new issue