diff --git a/backend/hint.go b/backend/hint.go index 5c2969f4..2af3bf25 100644 --- a/backend/hint.go +++ b/backend/hint.go @@ -34,7 +34,7 @@ func treatOpeningHint(pathname string, team fic.Team) { log.Println("[WRN]", err) } else if lvl, err := exercice.GetLevel(); err != nil { log.Println("[WRN]", err) - } else if theme, err := exercice.GetTheme(); err != nil { + } else if theme, err := fic.GetTheme(exercice.IdTheme); err != nil { log.Println("[WRN]", err) } else if _, err := fic.NewEvent(fmt.Sprintf("L'équipe %s a dévoilé un indice pour le %de défi %s !", team.Name, lvl, theme.Name), "alert-info"); err != nil { log.Println("[WRN] Unable to create event:", err) diff --git a/backend/submission.go b/backend/submission.go index 358367b9..0a75ffe1 100644 --- a/backend/submission.go +++ b/backend/submission.go @@ -44,7 +44,7 @@ func treatSubmission(pathname string, team fic.Team, exercice_id string) { } // Find the corresponding theme - theme, err := exercice.GetTheme() + theme, err := fic.GetTheme(exercice.IdTheme) if err != nil { log.Printf("%s [ERR] Unable to retrieve theme for exercice %d: %s\n", id, eid, err) return diff --git a/libfic/exercice.go b/libfic/exercice.go index 6c05dc96..335a4e49 100644 --- a/libfic/exercice.go +++ b/libfic/exercice.go @@ -18,6 +18,7 @@ var PartialMCQValidation bool // Exercice represents a challenge inside a Theme. type Exercice struct { Id int64 `json:"id"` + IdTheme int64 `json:"id_theme"` Title string `json:"title"` // URLid is used to reference the challenge from the URL path URLId string `json:"urlid"` @@ -50,7 +51,7 @@ type Exercice struct { // GetExercice retrieves the challenge with the given id. func GetExercice(id int64) (Exercice, error) { var e Exercice - if err := DBQueryRow("SELECT id_exercice, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { + if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { return Exercice{}, err } @@ -60,7 +61,7 @@ func GetExercice(id int64) (Exercice, error) { // GetExercice retrieves the challenge with the given id. func (t Theme) GetExercice(id int) (Exercice, error) { var e Exercice - if err := DBQueryRow("SELECT id_exercice, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { + if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { return Exercice{}, err } @@ -70,7 +71,7 @@ func (t Theme) GetExercice(id int) (Exercice, error) { // GetExerciceByTitle retrieves the challenge with the given title. func (t Theme) GetExerciceByTitle(title string) (Exercice, error) { var e Exercice - if err := DBQueryRow("SELECT id_exercice, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ? AND title = ?", t.Id, title).Scan(&e.Id, &e.Title, &t.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { + if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ? AND title = ?", t.Id, title).Scan(&e.Id, &e.IdTheme, &e.Title, &t.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { return Exercice{}, err } @@ -79,7 +80,7 @@ func (t Theme) GetExerciceByTitle(title string) (Exercice, error) { // GetExercices returns the list of all challenges present in the database. func GetExercices() ([]Exercice, error) { - if rows, err := DBQuery("SELECT id_exercice, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices"); err != nil { + if rows, err := DBQuery("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices"); err != nil { return nil, err } else { defer rows.Close() @@ -87,7 +88,7 @@ func GetExercices() ([]Exercice, error) { var exos = make([]Exercice, 0) for rows.Next() { var e Exercice - if err := rows.Scan(&e.Id, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { + if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { return nil, err } exos = append(exos, e) @@ -102,7 +103,7 @@ func GetExercices() ([]Exercice, error) { // GetExercices returns the list of all challenges in the Theme. func (t Theme) GetExercices() ([]Exercice, error) { - if rows, err := DBQuery("SELECT id_exercice, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ?", t.Id); err != nil { + if rows, err := DBQuery("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ?", t.Id); err != nil { return nil, err } else { defer rows.Close() @@ -110,7 +111,7 @@ func (t Theme) GetExercices() ([]Exercice, error) { var exos = make([]Exercice, 0) for rows.Next() { var e Exercice - if err := rows.Scan(&e.Id, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { + if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { return nil, err } exos = append(exos, e) @@ -216,21 +217,6 @@ func (e *Exercice) FixURLId() bool { return false } -// GetThemeId returns the theme's id for the Exercice. -func (e Exercice) GetThemeId() (tid int64, err error) { - err = DBQueryRow("SELECT id_theme FROM exercices WHERE id_exercice=?", e.Id).Scan(&tid) - return -} - -// GetTheme returns the parent Theme where the Exercice lives. -func (e Exercice) GetTheme() (Theme, error) { - if tid, err := e.GetThemeId(); err != nil { - return Theme{}, err - } else { - return GetTheme(tid) - } -} - // Delete the challenge from the database. func (e Exercice) Delete() (int64, error) { if res, err := DBExec("DELETE FROM exercices WHERE id_exercice = ?", e.Id); err != nil { diff --git a/libfic/team_my.go b/libfic/team_my.go index b59dbb7c..f13c3517 100644 --- a/libfic/team_my.go +++ b/libfic/team_my.go @@ -97,9 +97,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) { for _, e := range exos { if t == nil || t.HasAccess(e) { exercice := myTeamExercice{} - if tid, err := e.GetThemeId(); err == nil { - exercice.ThemeId = tid - } + exercice.ThemeId = e.IdTheme exercice.Statement = strings.Replace(e.Statement, "$FILES$", FilesDir, -1)