From bce666f7f38021a74c3961c2879d8a87f6dad3bb Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Mon, 16 Dec 2019 18:05:12 +0100 Subject: [PATCH] Implement question order generator --- orm/contest.go | 21 +++++++++++++++------ orm/participant.go | 10 +++++++--- templates/layout/base.html.tpl | 1 + templates/responses.html.tpl | 2 +- templates/responses_show.html.tpl | 3 +++ 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/orm/contest.go b/orm/contest.go index 09157cf7..d626fd43 100644 --- a/orm/contest.go +++ b/orm/contest.go @@ -177,14 +177,24 @@ func SaveContest(contest interface{}) (interface{}, error) { return contest, nil } -func (c *Contest) generateQuestionsOrder() string { - var order []string +func (c *Contest) generateQuestionsOrder() (string, error) { + var ( + order []string + questions []*Question + ) + + if err := DB().Find(&questions, Question{ContestID: c.ID}).Error; err != nil { + return "", err + } count := 0 generated := make(map[int]bool, 0) - for count == len(c.Questions) { - number := rand.Intn(len(c.Questions) + 1) + rand.Seed(time.Now().UnixNano()) + + for count < len(questions) { + number := rand.Intn(len(questions)) + log.Println(count, number) if generated[number] { continue } @@ -193,6 +203,5 @@ func (c *Contest) generateQuestionsOrder() string { count++ } - log.Println(strings.Join(order, " ")) - return strings.Join(order, " ") + return strings.Join(order, " "), nil } diff --git a/orm/participant.go b/orm/participant.go index 95af6232..3498eaa7 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -113,7 +113,11 @@ func (model *Participant) AfterSave(tx *gorm.DB) error { return err } - if err := tx.Model(&response).Update("QuestionsOrder", contest.generateQuestionsOrder()).Error; err != nil { + order, err := contest.generateQuestionsOrder() + if err != nil { + return err + } + if err := tx.Model(&response).Update("QuestionsOrder", order).Error; err != nil { return err } @@ -199,7 +203,7 @@ func (model *Participant) Create(args map[string]string, w http.ResponseWriter, var response Response - if err := DB().Debug().First(&response, &Response{ParticipantID: participant.ID}).Error; err != nil { + if err := DB().First(&response, &Response{ParticipantID: participant.ID}).Error; err != nil { return nil, err } @@ -360,7 +364,7 @@ func (model *Participant) Update(args map[string]string, w http.ResponseWriter, var response Response - if err := DB().Debug().First(&response, &Response{ParticipantID: participant.(*Participant).ID}).Error; err != nil { + if err := DB().First(&response, &Response{ParticipantID: participant.(*Participant).ID}).Error; err != nil { return nil, err } diff --git a/templates/layout/base.html.tpl b/templates/layout/base.html.tpl index f9cc50b4..10b0905f 100644 --- a/templates/layout/base.html.tpl +++ b/templates/layout/base.html.tpl @@ -41,6 +41,7 @@ Risposte Scuole Partecipanti + Prove {{end}} {{if $isSchool}} Scuola diff --git a/templates/responses.html.tpl b/templates/responses.html.tpl index 9301a62c..5a975ced 100644 --- a/templates/responses.html.tpl +++ b/templates/responses.html.tpl @@ -3,7 +3,7 @@
{{$options := ` - title: "Responses" + title: "Prove dei partecipanti" buttonTitle: "Crea nuova prova partecipante" `}} diff --git a/templates/responses_show.html.tpl b/templates/responses_show.html.tpl index 02075011..8622ef0a 100644 --- a/templates/responses_show.html.tpl +++ b/templates/responses_show.html.tpl @@ -11,6 +11,9 @@

Informazioni generali

{{if $isAdmin}} +
Ordine delle domande
+
{{.Data.QuestionsOrder}}
+ {{if $creatorUser:=.Data.CreatedBy}}
Creato da
{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}} da {{.Data.CreatorIP}}