Implement question order generator
This commit is contained in:
parent
973ff5a1af
commit
bce666f7f3
5 changed files with 27 additions and 10 deletions
|
@ -177,14 +177,24 @@ func SaveContest(contest interface{}) (interface{}, error) {
|
||||||
return contest, nil
|
return contest, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Contest) generateQuestionsOrder() string {
|
func (c *Contest) generateQuestionsOrder() (string, error) {
|
||||||
var order []string
|
var (
|
||||||
|
order []string
|
||||||
|
questions []*Question
|
||||||
|
)
|
||||||
|
|
||||||
|
if err := DB().Find(&questions, Question{ContestID: c.ID}).Error; err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
count := 0
|
count := 0
|
||||||
generated := make(map[int]bool, 0)
|
generated := make(map[int]bool, 0)
|
||||||
|
|
||||||
for count == len(c.Questions) {
|
rand.Seed(time.Now().UnixNano())
|
||||||
number := rand.Intn(len(c.Questions) + 1)
|
|
||||||
|
for count < len(questions) {
|
||||||
|
number := rand.Intn(len(questions))
|
||||||
|
log.Println(count, number)
|
||||||
if generated[number] {
|
if generated[number] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -193,6 +203,5 @@ func (c *Contest) generateQuestionsOrder() string {
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println(strings.Join(order, " "))
|
return strings.Join(order, " "), nil
|
||||||
return strings.Join(order, " ")
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,7 +113,11 @@ func (model *Participant) AfterSave(tx *gorm.DB) error {
|
||||||
return err
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +203,7 @@ func (model *Participant) Create(args map[string]string, w http.ResponseWriter,
|
||||||
|
|
||||||
var response Response
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -360,7 +364,7 @@ func (model *Participant) Update(args map[string]string, w http.ResponseWriter,
|
||||||
|
|
||||||
var response Response
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 "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 "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 "Participant"}}" href="{{all "Participant"}}">Partecipanti</a>
|
||||||
|
<a class="nav-item nav-link {{.Options|active "Response"}}" href="{{all "Response"}}">Prove</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if $isSchool}}
|
{{if $isSchool}}
|
||||||
<a class="nav-item nav-link {{.Options|active "School"}}" href="{{.Claims|userId|show "School"}}">Scuola</a>
|
<a class="nav-item nav-link {{.Options|active "School"}}" href="{{.Claims|userId|show "School"}}">Scuola</a>
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
{{$options := `
|
{{$options := `
|
||||||
title: "Responses"
|
title: "Prove dei partecipanti"
|
||||||
buttonTitle: "Crea nuova prova partecipante"
|
buttonTitle: "Crea nuova prova partecipante"
|
||||||
`}}
|
`}}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,9 @@
|
||||||
<h2 class="karmen-relation-header">Informazioni generali</h2>
|
<h2 class="karmen-relation-header">Informazioni generali</h2>
|
||||||
<dl class="row">
|
<dl class="row">
|
||||||
{{if $isAdmin}}
|
{{if $isAdmin}}
|
||||||
|
<dt class="col-sm-3">Ordine delle domande</dt>
|
||||||
|
<dd class="col-sm-9">{{.Data.QuestionsOrder}}</dd>
|
||||||
|
|
||||||
{{if $creatorUser:=.Data.CreatedBy}}
|
{{if $creatorUser:=.Data.CreatedBy}}
|
||||||
<dt class="col-sm-3">Creato da</dt>
|
<dt class="col-sm-3">Creato da</dt>
|
||||||
<dd class="col-sm-9">{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}} da {{.Data.CreatorIP}}</dd>
|
<dd class="col-sm-9">{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}} da {{.Data.CreatorIP}}</dd>
|
||||||
|
|
Loading…
Reference in a new issue