Merge branch 'master' into response_2
This commit is contained in:
commit
24ad134e66
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, " ")
|
||||
}
|
||||
|
|
|
@ -116,6 +116,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