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 @@