diff --git a/admin/api_team.go b/admin/api_team.go index da32dee4..3c9f449f 100644 --- a/admin/api_team.go +++ b/admin/api_team.go @@ -85,6 +85,8 @@ func listTeam(args []string, body []byte) (interface{}, error) { return fic.MyJSONTeam(team, true) } else if args[1] == "wait.json" { return fic.MyJSONTeam(team, false) + } else if args[1] == "tries" { + return fic.GetTries(team, nil) } else if team != nil && args[1] == "members" { return team.GetMembers() } else if args[1] == "certificate" && team != nil { @@ -95,6 +97,8 @@ func listTeam(args []string, body []byte) (interface{}, error) { } else if len(args) == 1 { if args[0] == "teams.json" { return fic.ExportTeams() + } else if args[0] == "tries" { + return fic.GetTries(nil, nil) } else if args[0] == "nginx" { return nginxGenTeam() } else if args[0] == "binding" { diff --git a/libfic/team.go b/libfic/team.go index 10d67b72..4be647a4 100644 --- a/libfic/team.go +++ b/libfic/team.go @@ -1,6 +1,7 @@ package fic import ( + "database/sql" "fmt" "time" ) @@ -152,6 +153,61 @@ func (t Team) HasAccess(e Exercice) bool { } } +func NbTry(t *Team, e Exercice) int { + var cnt *int + + if t != nil { + DBQueryRow("SELECT COUNT(*) FROM exercice_tries WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&cnt) + } else { + DBQueryRow("SELECT COUNT(*) FROM exercice_tries WHERE id_exercice = ?", e.Id).Scan(&cnt) + } + + if cnt == nil { + return 0 + } else { + return *cnt + } +} + +func GetTries(t *Team, e *Exercice) ([]time.Time, error) { + var rows *sql.Rows + var err error + + if t == nil { + if e == nil { + rows, err = DBQuery("SELECT time FROM exercice_tries ORDER BY time ASC") + } else { + rows, err = DBQuery("SELECT time FROM exercice_tries WHERE id_exercice = ? ORDER BY time ASC", e.Id) + } + } else { + if e == nil { + rows, err = DBQuery("SELECT time FROM exercice_tries WHERE id_team = ? ORDER BY time ASC", t.Id) + } else { + rows, err = DBQuery("SELECT time FROM exercice_tries WHERE id_team = ? AND id_exercice = ? ORDER BY time ASC", t.Id, e.Id) + } + } + + if err != nil { + return nil, err + } else { + defer rows.Close() + + times := make([]time.Time, 0) + for rows.Next() { + var tm time.Time + if err := rows.Scan(&tm); err != nil { + return nil, err + } + times = append(times, tm) + } + if err := rows.Err(); err != nil { + return nil, err + } + + return times, nil + } +} + func (t Team) HasSolved(e Exercice) (bool, time.Time, int64) { var nb *int64 var tm *time.Time