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 (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.andreafazzi.eu/andrea/oef/renderer"
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type SingleResponse struct {
|
||||||
|
AnswerID uint
|
||||||
|
}
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
|
|
||||||
|
@ -22,19 +28,42 @@ type Response struct {
|
||||||
ContestID uint
|
ContestID uint
|
||||||
|
|
||||||
QuestionsOrder string
|
QuestionsOrder string
|
||||||
|
AnswersIDs string
|
||||||
|
|
||||||
Questions []*Question
|
Score int `gorm:"-"`
|
||||||
|
|
||||||
// SelectedElement map[uint]string `gorm:"-"`
|
Questions []*Question
|
||||||
// AllElements []*Element `gorm:"-"`
|
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) GetID() uint { return model.ID }
|
||||||
|
|
||||||
func (model *Response) String() string {
|
func (model *Response) String() string {
|
||||||
return model.Name
|
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) {
|
func (model *Response) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
response := new(Response)
|
response := new(Response)
|
||||||
|
@ -71,8 +100,29 @@ func (model *Response) Read(args map[string]string, w http.ResponseWriter, r *ht
|
||||||
return nil, err
|
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
|
// 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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,13 +146,6 @@ func (model *Response) Update(args map[string]string, w http.ResponseWriter, r *
|
||||||
|
|
||||||
response := result.(*Response)
|
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
|
return response, nil
|
||||||
} else {
|
} else {
|
||||||
response, err := model.Read(args, w, r)
|
response, err := model.Read(args, w, r)
|
||||||
|
|
|
@ -23,12 +23,12 @@
|
||||||
role="form"
|
role="form"
|
||||||
id={{$form}}>
|
id={{$form}}>
|
||||||
|
|
||||||
{{range $question := .Data.Questions}}
|
{{range $id,$question := .Data.Questions}}
|
||||||
<div class="form-group p-3">
|
<div class="form-group p-3">
|
||||||
<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="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}}">
|
<label class="form-check-label" for="answer_{{$answer.ID}}">
|
||||||
{{$answer}}
|
{{$answer}}
|
||||||
</label>
|
</label>
|
||||||
|
|
|
@ -11,6 +11,14 @@
|
||||||
<h2 class="karmen-relation-header">Informazioni generali</h2>
|
<h2 class="karmen-relation-header">Informazioni generali</h2>
|
||||||
<dl class="row">
|
<dl class="row">
|
||||||
{{if $isAdmin}}
|
{{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}}
|
{{if $creatorUser:=.Data.CreatedBy}}
|
||||||
<dt class="col-sm-3">Creato da</dt>
|
<dt class="col-sm-3">Creato da</dt>
|
||||||
<dd class="col-sm-9">{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}} da {{.Data.CreatorIP}}</dd>
|
<dd class="col-sm-9">{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}} da {{.Data.CreatorIP}}</dd>
|
||||||
|
|
Loading…
Reference in a new issue