Fetch given single responses from DB and implement random order
This commit is contained in:
parent
3a5e99c3da
commit
95249fe665
3 changed files with 46 additions and 5 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 @@
|
|||
<p class="lead">{{$question.Text}}</p>
|
||||
{{range $answer := $question.Answers}}
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="radio" name="SingleResponses.{{$id}}" id="answer_{{$answer.ID}}" value="{{$answer.ID}}" required>
|
||||
<label class="form-check-label" for="answer_{{$answer.ID}}">
|
||||
{{$checked := false}}
|
||||
{{if isResponseIn $answer.ID $.Data.AnswersIDs}}
|
||||
{{$checked = true}}
|
||||
{{end}}
|
||||
<input class="form-check-input" type="radio" name="SingleResponses.{{$id}}" id="answer_{{$answer.ID}}" value="{{$answer.ID}}" required {{if $checked}}checked{{end}}>
|
||||
<label class="form-check-label {{if $answer.Correct}}text-success{{end}}" for="answer_{{$answer.ID}}">
|
||||
{{$answer}}
|
||||
</label>
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue