Working on participant response

This commit is contained in:
Andrea Fazzi 2019-12-13 12:55:11 +01:00
parent 2aa98c1bbb
commit bcf2e1be4d
9 changed files with 82 additions and 15 deletions

View file

@ -15,14 +15,25 @@ var (
"it": "L'utente non dispone delle autorizzazioni necessarie a visualizzare questa pagina.",
},
}
Formats = map[string]map[string]string{
"dateTime": map[string]string{
"it": "il %02d/%02d/%d alle ore %02d:%02d",
},
}
Authorization = map[string]map[string]string{
"notAuthorized": map[string]string{
"it": "Non si è autorizzati ad accedere a questa pagina",
},
}
Text = map[string]map[string]string{
"answerCorrect": map[string]string{
"it": "Corretta",
},
"alwaysActiveContest": map[string]string{
"it": "sempre attiva",
},
}
)

View file

@ -87,8 +87,13 @@ func (model *Participant) exists() (*User, error) {
return &user, nil
}
func (model *Participant) AfterCreate(tx *gorm.DB) error {
response.UserModifierCreate = NewUserModifierCreate(r)
}
func (model *Participant) BeforeSave(tx *gorm.DB) error {
var user User
if err := tx.FirstOrCreate(&user, &User{
Username: model.username(),
Role: "participant",

View file

@ -52,7 +52,9 @@ func (q *Question) Read(args map[string]string, w http.ResponseWriter, r *http.R
id := args["id"]
if err := DB().Preload("Answers").Preload("Contest").First(&question, id).Error; err != nil {
if err := DB().Preload("Answers", func(db *gorm.DB) *gorm.DB {
return db.Order("answers.correct DESC")
}).Preload("Contest").First(&question, id).Error; err != nil {
return nil, err
}

View file

@ -10,6 +10,9 @@ import (
type Response struct {
gorm.Model
*UserModifierCreate
*UserModifierUpdate
Name string
Participant *Participant
@ -18,6 +21,8 @@ type Response struct {
Contest *Contest
ContestID uint
QuestionsOrder string
Questions []*Question
// SelectedElement map[uint]string `gorm:"-"`
@ -33,6 +38,7 @@ func (model *Response) String() string {
func (model *Response) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
if r.Method == "GET" {
response := new(Response)
contestID := r.URL.Query().Get("contest_id")
if err := DB().Preload("Answers").Where("contest_id = ?", contestID).Find(&response.Questions).Error; err != nil {
@ -45,6 +51,9 @@ func (model *Response) Create(args map[string]string, w http.ResponseWriter, r *
if err != nil {
return nil, err
}
response.UserModifierCreate = NewUserModifierCreate(r)
response, err = CreateResponse(response)
if err != nil {
return nil, err
@ -62,6 +71,11 @@ func (model *Response) Read(args map[string]string, w http.ResponseWriter, r *ht
return nil, err
}
// Fetch questions in the given order
if err := DB().Where("contest_id = ?", response.Contest.ID).Find(&response.Questions).Error; err != nil {
return nil, err
}
return &response, nil
}
@ -99,6 +113,9 @@ func (model *Response) Update(args map[string]string, w http.ResponseWriter, r *
if err != nil {
return nil, err
}
response.(*Response).UserModifierUpdate = NewUserModifierUpdate(r)
_, err = SaveResponse(response)
if err != nil {
return nil, err

View file

@ -161,10 +161,17 @@ func incr(value int) int {
func callString(value interface{}) string {
if value != nil {
if reflect.ValueOf(value).Kind() == reflect.String {
switch reflect.ValueOf(value).Kind() {
case reflect.String:
return value.(string)
case reflect.Bool:
if value.(bool) {
return i18n.Text["answerCorrect"]["it"]
}
return "false"
default:
return reflect.ValueOf(value).MethodByName("String").Interface().(func() string)()
}
return reflect.ValueOf(value).MethodByName("String").Interface().(func() string)()
} else {
return ""
}
@ -248,6 +255,10 @@ func prettyDate(value interface{}) string {
if !ok {
return ""
}
if zeroTime(&t) {
return i18n.Text["alwaysActiveContest"]["it"]
}
return fmt.Sprintf("%02d/%02d/%d", t.Day(), t.Month(), t.Year())
}

View file

@ -323,7 +323,6 @@ func Decode(dst interface{}, r *http.Request) error {
case "html":
var timeConverter = func(value string) reflect.Value {
log.Println(value)
if value == "" {
return reflect.ValueOf(time.Time{})
}

View file

@ -17,8 +17,11 @@
{{if $.small}}
{{range $s := $.small}}
<div class="text-right">
{{$options := `noElements: "nessun elemento"`}}
{{template "small" dict "options" ($options | yaml) "data" ($el|field $s)}}
{{if $.smallOptions}}
{{template "small" dict "options" ($.smallOptions | yaml) "data" ($el|field $s)}}
{{else}}
{{template "small" dict "data" ($el|field $s)}}
{{end}}
</div>
{{end}}
{{end}}

View file

@ -16,9 +16,13 @@
model: "Answer"
icon: "fa fa-reply"
`}}
{{$smallOptions := `
noElements: "Errata"
`}}
{{$noElements := "Alla domanda non è associata alcuna risposta."}}
{{template "relation_list" dict "options" ($options|yaml) "data" .Data.Answers "noElements" $noElements}}
{{template "relation_list" dict "options" ($options|yaml) "smallOptions" ($smallOptions) "data" .Data.Answers "noElements" $noElements "small" (toSlice "Correct") }}
</div>
</div>

View file

@ -1,23 +1,38 @@
{{ define "content" }}
{{$isAdmin := .Claims|isAdmin}}
{{$isParticipant := .Claims|isParticipant}}
<div class="container">
{{template "breadcrumb" toSlice "ELEMENTS" (all "Response") (.Data|string) "current"}}
{{template "breadcrumb" toSlice "Prove dei partecipanti" (all "Response") (.Data|string) "current"}}
{{template "show_header" dict "title" (.Data|string) "updatePath" (.Data.ID|update "Response") "deletePath" (.Data.ID|delete "Response")}}
<h2 class="karmen-relation-header">GENERAL SECTION</h2>
<h2 class="karmen-relation-header">Informazioni generali</h2>
<dl class="row">
{{if $isAdmin}}
{{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>
{{end}}
{{if $updaterUser:=.Data.UpdatedBy}}
<dt class="col-sm-3">Modificato da</dt>
<dd class="col-sm-9">{{$updaterUser.Username}}[{{$updaterUser.Role}}] {{$.Data.UpdatedAt|prettyDateTime}} da {{.Data.UpdaterIP}}</dd>
{{end}}
{{end}}
</dl>
<div class="row">
<div class="col-md-12">
{{$options := `
title: "RELATIONS"
model: "MODEL"
icon: "ICON_CLASS"
title: "Domande"
model: "Question"
icon: "fa fa-question-circle"
`}}
{{$noElements := "NO ELEMENTS"}}
{{template "relation_list" dict "options" ($options|yaml) "data" .Data.RELATIONS "noElements" $noElements}}
{{$noElements := "La prova del partecipante non contiene alcuna domanda."}}
{{template "relation_list" dict "options" ($options|yaml) "data" .Data.Questions "noElements" $noElements}}
</div>
</div>