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
|
package orm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"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
|
// 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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ const (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
funcMap = template.FuncMap{
|
funcMap = template.FuncMap{
|
||||||
|
"isResponseIn": isResponseIn,
|
||||||
"query": query,
|
"query": query,
|
||||||
"convertDate": convertDate,
|
"convertDate": convertDate,
|
||||||
"convertTime": convertTime,
|
"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 {
|
func username(claims jwt.MapClaims) string {
|
||||||
return claims["username"].(string)
|
return claims["username"].(string)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,9 +9,9 @@
|
||||||
|
|
||||||
{{if $update}}
|
{{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}}
|
{{else}}
|
||||||
{{template "breadcrumb" toSlice "Responses" (all "Response") "Aggiungi" "current"}}
|
{{template "breadcrumb" toSlice "Prove" (all "Response") "Aggiungi" "current"}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
@ -30,8 +30,12 @@
|
||||||
<p class="lead">{{$question.Text}}</p>
|
<p class="lead">{{$question.Text}}</p>
|
||||||
{{range $answer := $question.Answers}}
|
{{range $answer := $question.Answers}}
|
||||||
<div class="form-check">
|
<div class="form-check">
|
||||||
<input class="form-check-input" type="radio" name="SingleResponses.{{$id}}" id="answer_{{$answer.ID}}" value="{{$answer.ID}}" required>
|
{{$checked := false}}
|
||||||
<label class="form-check-label" for="answer_{{$answer.ID}}">
|
{{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}}
|
{{$answer}}
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue