include id_theme in Exercice struct

This commit is contained in:
nemunaire 2018-12-08 21:17:04 +01:00
parent 69979ced1d
commit 78b6211b94
4 changed files with 11 additions and 27 deletions

View File

@ -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 <strong>%d<sup>e</sup></strong> défi %s&nbsp;!", team.Name, lvl, theme.Name), "alert-info"); err != nil {
log.Println("[WRN] Unable to create event:", err)

View File

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

View File

@ -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 {

View File

@ -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)