Merge branch 'response_2' of ssh://git.andreafazzi.eu:10022/andrea/oef into response_2

This commit is contained in:
Andrea Fazzi 2019-12-18 10:46:44 +01:00
commit 3a5e99c3da
3 changed files with 67 additions and 14 deletions

View file

@ -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,32 @@ func (model *Response) Read(args map[string]string, w http.ResponseWriter, r *ht
return nil, err
}
if response.AnswersIDs != "" {
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 +149,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)

View file

@ -25,12 +25,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>

View file

@ -13,7 +13,14 @@
{{if $isAdmin}}
<dt class="col-sm-3">Ordine delle domande</dt>
<dd class="col-sm-9">{{.Data.QuestionsOrder}}</dd>
<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>