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}}