diff --git a/orm/response.go b/orm/response.go index 93575929..289432b8 100644 --- a/orm/response.go +++ b/orm/response.go @@ -1,6 +1,7 @@ package orm import ( + "fmt" "net/http" "strconv" "strings" @@ -124,8 +125,22 @@ func (model *Response) Read(args map[string]string, w http.ResponseWriter, r *ht } + qOrder := make([]uint, 0) + qIDs := strings.Split(response.QuestionsOrder, " ") + for _, id := range qIDs { + id, err := strconv.Atoi(id) + if err != nil { + return nil, err + } + qOrder = append(qOrder, uint(id)) + } + // Fetch questions in the given order - if err := DB().Where("contest_id = ?", response.Contest.ID).Preload("Answers").Find(&response.Questions).Error; err != nil { + + field := fmt.Sprintf("FIELD(id,%s)", strings.Replace(response.QuestionsOrder, " ", ",", -1)) + if err := DB().Order(field).Where("contest_id = ?", response.Contest.ID).Preload("Answers", func(db *gorm.DB) *gorm.DB { + return db.Order("RAND()") + }).Find(&response.Questions).Error; err != nil { return nil, err } diff --git a/renderer/funcmap.go b/renderer/funcmap.go index 799a301e..5b23b5e2 100644 --- a/renderer/funcmap.go +++ b/renderer/funcmap.go @@ -22,6 +22,7 @@ const ( var ( funcMap = template.FuncMap{ + "isResponseIn": isResponseIn, "query": query, "convertDate": convertDate, "convertTime": convertTime, @@ -62,6 +63,27 @@ var ( } ) +func isResponseIn(id uint, answersIDs string) (bool, error) { + if answersIDs != "" { + ids := make([]uint, 0) + srIDs := strings.Split(answersIDs, " ") + for _, srID := range srIDs { + id, err := strconv.Atoi(srID) + if err != nil { + return false, err + } + ids = append(ids, uint(id)) + } + + for _, v := range ids { + if v == id { + return true, nil + } + } + } + return false, nil +} + func username(claims jwt.MapClaims) string { return claims["username"].(string) } diff --git a/templates/responses_add_update.html.tpl b/templates/responses_add_update.html.tpl index 816a8176..71649082 100644 --- a/templates/responses_add_update.html.tpl +++ b/templates/responses_add_update.html.tpl @@ -9,9 +9,9 @@ {{if $update}} - {{template "breadcrumb" toSlice "Responses" (all "Response") (.Data|string) (.Data.ID|show "Response") "Aggiorna" "current"}} + {{template "breadcrumb" toSlice "Prove" (all "Response") (.Data|string) (.Data.ID|show "Response") "Aggiorna" "current"}} {{else}} - {{template "breadcrumb" toSlice "Responses" (all "Response") "Aggiungi" "current"}} + {{template "breadcrumb" toSlice "Prove" (all "Response") "Aggiungi" "current"}} {{end}} {{end}} @@ -30,8 +30,12 @@

{{$question.Text}}

{{range $answer := $question.Answers}}
- -