Implement question order generator

This commit is contained in:
Andrea Fazzi 2019-12-16 18:05:12 +01:00
parent 973ff5a1af
commit bce666f7f3
5 changed files with 27 additions and 10 deletions

View file

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

View file

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

View file

@ -41,6 +41,7 @@
<a class="nav-item nav-link {{.Options|active "Answer"}}" href="{{all "Answer"}}">Risposte</a>
<a class="nav-item nav-link {{.Options|active "School"}}" href="{{all "School"}}">Scuole</a>
<a class="nav-item nav-link {{.Options|active "Participant"}}" href="{{all "Participant"}}">Partecipanti</a>
<a class="nav-item nav-link {{.Options|active "Response"}}" href="{{all "Response"}}">Prove</a>
{{end}}
{{if $isSchool}}
<a class="nav-item nav-link {{.Options|active "School"}}" href="{{.Claims|userId|show "School"}}">Scuola</a>

View file

@ -3,7 +3,7 @@
<div class="container">
{{$options := `
title: "Responses"
title: "Prove dei partecipanti"
buttonTitle: "Crea nuova prova partecipante"
`}}

View file

@ -11,6 +11,9 @@
<h2 class="karmen-relation-header">Informazioni generali</h2>
<dl class="row">
{{if $isAdmin}}
<dt class="col-sm-3">Ordine delle domande</dt>
<dd class="col-sm-9">{{.Data.QuestionsOrder}}</dd>
{{if $creatorUser:=.Data.CreatedBy}}
<dt class="col-sm-3">Creato da</dt>
<dd class="col-sm-9">{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}} da {{.Data.CreatorIP}}</dd>