diff --git a/orm/contest.go b/orm/contest.go index 8687f120..48fa9d76 100644 --- a/orm/contest.go +++ b/orm/contest.go @@ -110,13 +110,13 @@ func (c *Contest) Read(db *Database, args map[string]string, w http.ResponseWrit } for _, r := range contest.Rank { - if err := calcScore(db, r); err != nil { + if err := calcScoreAndDuration(db, r); err != nil { return nil, err } } sort.SliceStable(contest.Rank, func(i, j int) bool { - return contest.Rank[i].Score < contest.Rank[j].Score + return (contest.Rank[i].Score >= contest.Rank[j].Score) && (contest.Rank[i].Duration < contest.Rank[j].Duration) }) return &contest, nil diff --git a/orm/response.go b/orm/response.go index 26ecd671..18c437eb 100644 --- a/orm/response.go +++ b/orm/response.go @@ -38,8 +38,9 @@ type Response struct { EndTime time.Time TimeLeft time.Duration - Score int `gorm:"-"` - ScoreString string `gorm:"-"` + Score int `gorm:"-"` + ScoreString string `gorm:"-"` + Duration time.Duration `gorm:"-"` Questions []*Question SingleResponses []*SingleResponse `gorm:"-"` @@ -104,7 +105,7 @@ func (model *Response) BeforeSave(tx *gorm.DB) error { return nil } -func calcScore(db *Database, response *Response) error { +func calcScoreAndDuration(db *Database, response *Response) error { response.Score = 0 if response.AnswersIDs != "" { srIDs := strings.Split(response.AnswersIDs, " ") @@ -130,7 +131,12 @@ func calcScore(db *Database, response *Response) error { } - response.ScoreString = fmt.Sprintf("%s punti %v", strconv.Itoa(response.Score), response.EndTime.Sub(response.StartTime)) + if response.StartTime.IsZero() { + response.ScoreString = "non ancora visualizzata" + } else { + response.Duration = response.UpdatedAt.Sub(response.StartTime) + response.ScoreString = fmt.Sprintf("%s punti %v", strconv.Itoa(response.Score), response.Duration) + } return nil } diff --git a/templates/responses_show.html.tpl b/templates/responses_show.html.tpl index 9d37aff2..be7eac78 100644 --- a/templates/responses_show.html.tpl +++ b/templates/responses_show.html.tpl @@ -40,7 +40,7 @@