Implement question order generator

This commit is contained in:
Andrea Fazzi 2019-12-13 13:25:33 +01:00
parent 2aa98c1bbb
commit 06030bb0c7
2 changed files with 28 additions and 0 deletions

View file

@ -3,6 +3,9 @@ package orm
import (
"fmt"
"log"
"math/rand"
"strconv"
"strings"
"net/http"
"time"
@ -173,3 +176,23 @@ func SaveContest(contest interface{}) (interface{}, error) {
}
return contest, nil
}
func (c *Contest) generateQuestionsOrder() string {
var order []string
count := 0
generated := make(map[int]bool, 0)
for count == len(c.Questions) {
number := rand.Intn(len(c.Questions) + 1)
if generated[number] {
continue
}
generated[number] = true
order = append(order, strconv.Itoa(number))
count++
}
log.Println(strings.Join(order, " "))
return strings.Join(order, " ")
}

View file

@ -111,6 +111,11 @@ func (model *Participant) AfterSave(tx *gorm.DB) error {
}).Error; err != nil {
return err
}
if err := tx.Model(&response).Update("QuestionsOrder", contest.generateQuestionsOrder()).Error; err != nil {
return err
}
}
return nil
}