From bcf2e1be4d5b2710d63df90a048da34b717fb9e3 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Fri, 13 Dec 2019 12:55:11 +0100 Subject: [PATCH] Working on participant response --- i18n/i18n.go | 11 ++++++++++ orm/participant.go | 5 +++++ orm/question.go | 4 +++- orm/response.go | 17 +++++++++++++++ renderer/funcmap.go | 15 +++++++++++-- renderer/renderer.go | 1 - templates/layout/relation_list.html.tpl | 7 ++++-- templates/questions_show.html.tpl | 8 +++++-- templates/responses_show.html.tpl | 29 +++++++++++++++++++------ 9 files changed, 82 insertions(+), 15 deletions(-) diff --git a/i18n/i18n.go b/i18n/i18n.go index 05a08166..d9c7814d 100644 --- a/i18n/i18n.go +++ b/i18n/i18n.go @@ -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", + }, + } ) diff --git a/orm/participant.go b/orm/participant.go index c59da3b4..5b4948fd 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -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", diff --git a/orm/question.go b/orm/question.go index 4cce25a9..fda4b6c7 100644 --- a/orm/question.go +++ b/orm/question.go @@ -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 } diff --git a/orm/response.go b/orm/response.go index 1018282b..6815489d 100644 --- a/orm/response.go +++ b/orm/response.go @@ -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 diff --git a/renderer/funcmap.go b/renderer/funcmap.go index fd7e22c6..799a301e 100644 --- a/renderer/funcmap.go +++ b/renderer/funcmap.go @@ -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()) } diff --git a/renderer/renderer.go b/renderer/renderer.go index 24398b3c..105f970d 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -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{}) } diff --git a/templates/layout/relation_list.html.tpl b/templates/layout/relation_list.html.tpl index cab2895f..d34fcd74 100644 --- a/templates/layout/relation_list.html.tpl +++ b/templates/layout/relation_list.html.tpl @@ -17,8 +17,11 @@ {{if $.small}} {{range $s := $.small}}
- {{$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}}
{{end}} {{end}} diff --git a/templates/questions_show.html.tpl b/templates/questions_show.html.tpl index f3358744..a81eb09b 100644 --- a/templates/questions_show.html.tpl +++ b/templates/questions_show.html.tpl @@ -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") }} diff --git a/templates/responses_show.html.tpl b/templates/responses_show.html.tpl index 4cccf38d..02075011 100644 --- a/templates/responses_show.html.tpl +++ b/templates/responses_show.html.tpl @@ -1,23 +1,38 @@ {{ define "content" }} +{{$isAdmin := .Claims|isAdmin}} +{{$isParticipant := .Claims|isParticipant}} +
- {{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")}} -

GENERAL SECTION

+

Informazioni generali

+
+ {{if $isAdmin}} + {{if $creatorUser:=.Data.CreatedBy}} +
Creato da
+
{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}} da {{.Data.CreatorIP}}
+ {{end}} + {{if $updaterUser:=.Data.UpdatedBy}} +
Modificato da
+
{{$updaterUser.Username}}[{{$updaterUser.Role}}] {{$.Data.UpdatedAt|prettyDateTime}} da {{.Data.UpdaterIP}}
+ {{end}} + {{end}} +
{{$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}}