Encode/decode single responses
This commit is contained in:
parent
973ff5a1af
commit
c19631e65b
3 changed files with 64 additions and 13 deletions
|
@ -2,11 +2,17 @@ package orm
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.andreafazzi.eu/andrea/oef/renderer"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
type SingleResponse struct {
|
||||
AnswerID uint
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
gorm.Model
|
||||
|
||||
|
@ -22,19 +28,42 @@ type Response struct {
|
|||
ContestID uint
|
||||
|
||||
QuestionsOrder string
|
||||
AnswersIDs string
|
||||
|
||||
Questions []*Question
|
||||
Score int `gorm:"-"`
|
||||
|
||||
// SelectedElement map[uint]string `gorm:"-"`
|
||||
// AllElements []*Element `gorm:"-"`
|
||||
Questions []*Question
|
||||
SingleResponses []*SingleResponse `gorm:"-"`
|
||||
}
|
||||
|
||||
func (sr *SingleResponse) UnmarshalText(text []byte) error {
|
||||
id, err := strconv.Atoi(string(text))
|
||||
sr.AnswerID = uint(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
func (model *Response) GetID() uint { return model.ID }
|
||||
|
||||
func (model *Response) String() string {
|
||||
return model.Name
|
||||
}
|
||||
|
||||
func (model *Response) generateAnswersIDs() string {
|
||||
ids := make([]string, 0)
|
||||
for _, sr := range model.SingleResponses {
|
||||
ids = append(ids, strconv.Itoa(int(sr.AnswerID)))
|
||||
}
|
||||
return strings.Join(ids, " ")
|
||||
}
|
||||
|
||||
func (model *Response) BeforeSave(tx *gorm.DB) error {
|
||||
model.AnswersIDs = model.generateAnswersIDs()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (model *Response) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
if r.Method == "GET" {
|
||||
response := new(Response)
|
||||
|
@ -71,8 +100,29 @@ func (model *Response) Read(args map[string]string, w http.ResponseWriter, r *ht
|
|||
return nil, err
|
||||
}
|
||||
|
||||
srIDs := strings.Split(response.AnswersIDs, " ")
|
||||
for _, srID := range srIDs {
|
||||
id, err := strconv.Atoi(srID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response.SingleResponses = append(response.SingleResponses, &SingleResponse{uint(id)})
|
||||
}
|
||||
|
||||
for _, sr := range response.SingleResponses {
|
||||
var answer Answer
|
||||
|
||||
if err := DB().First(&answer, sr.AnswerID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if answer.Correct {
|
||||
response.Score++
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch questions in the given order
|
||||
if err := DB().Where("contest_id = ?", response.Contest.ID).Find(&response.Questions).Error; err != nil {
|
||||
if err := DB().Where("contest_id = ?", response.Contest.ID).Preload("Answers").Find(&response.Questions).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -96,13 +146,6 @@ func (model *Response) Update(args map[string]string, w http.ResponseWriter, r *
|
|||
|
||||
response := result.(*Response)
|
||||
|
||||
// if err := DB().Find(&response.AllElements).Error; err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// response.SelectedElement = make(map[uint]string)
|
||||
// response.SelectedElement[response.ElementID] = "selected"
|
||||
|
||||
return response, nil
|
||||
} else {
|
||||
response, err := model.Read(args, w, r)
|
||||
|
|
|
@ -23,12 +23,12 @@
|
|||
role="form"
|
||||
id={{$form}}>
|
||||
|
||||
{{range $question := .Data.Questions}}
|
||||
{{range $id,$question := .Data.Questions}}
|
||||
<div class="form-group p-3">
|
||||
<p class="lead">{{$question.Text}}</p>
|
||||
{{range $answer := $question.Answers}}
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="radio" name="question_{{$question.ID}}" id="answer_{{$answer.ID}}" value="{{$answer.ID}}">
|
||||
<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}}">
|
||||
{{$answer}}
|
||||
</label>
|
||||
|
|
|
@ -11,6 +11,14 @@
|
|||
<h2 class="karmen-relation-header">Informazioni generali</h2>
|
||||
<dl class="row">
|
||||
{{if $isAdmin}}
|
||||
<dt class="col-sm-3">Risposte fornite (IDs)</dt>
|
||||
{{if .Data.AnswersIDs}}
|
||||
<dd class="col-sm-9">{{printf "[%v]" .Data.AnswersIDs}}</dd>
|
||||
{{else}}
|
||||
<dd class="col-sm-9">nessuna risposta fornita</dd>
|
||||
{{end}}
|
||||
<dt class="col-sm-3">Punteggio</dt>
|
||||
<dd class="col-sm-9">{{.Data.Score}}</dd>
|
||||
{{if $creatorUser:=.Data.CreatedBy}}
|
||||
<dt class="col-sm-3">Creato da</dt>
|
||||
<dd class="col-sm-9">{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}} da {{.Data.CreatorIP}}</dd>
|
||||
|
|
Loading…
Reference in a new issue