Implement question order generator
This commit is contained in:
parent
2aa98c1bbb
commit
06030bb0c7
2 changed files with 28 additions and 0 deletions
|
@ -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, " ")
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue