admin: Add exercice's tags: sync, api, interface done

This commit is contained in:
nemunaire 2018-11-18 22:44:23 +01:00 committed by Pierre-Olivier Mercier
parent 665fd301c6
commit f183985982
10 changed files with 166 additions and 20 deletions

View file

@ -246,6 +246,15 @@ CREATE TABLE IF NOT EXISTS exercice_tries(
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice),
FOREIGN KEY(id_team) REFERENCES teams(id_team)
) DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS exercice_tags(
id_exercice INTEGER NOT NULL,
tag VARCHAR(255) NOT NULL,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
`); err != nil {
return err
}

52
libfic/exercice_tag.go Normal file
View file

@ -0,0 +1,52 @@
package fic
// GetTags returns tags associated with this exercice.
func (e Exercice) GetTags() (tags []string, err error) {
if rows, errr := DBQuery("SELECT tag FROM exercice_tags WHERE id_exercice = ?", e.Id); errr != nil {
return nil, errr
} else {
defer rows.Close()
tags = make([]string, 0)
for rows.Next() {
var t string
if err = rows.Scan(&t); err != nil {
return
}
tags = append(tags, t)
}
err = rows.Err()
return
}
}
// AddTag assign a new tag to the exercice and registers it into the database.
func (e Exercice) AddTag(tag string) (string, error) {
if _, err := DBExec("INSERT INTO exercice_tags (id_exercice, tag) VALUES (?, ?)", e.Id, tag); err != nil {
return "", err
} else {
return tag, nil
}
}
// DeleteTag delete a tag assigned to the current exercice from the database.
func (e Exercice) DeleteTag(tag string) (int64, error) {
if res, err := DBExec("DELETE FROM exercice_tags WHERE id_exercice = ? AND tag = ?", e.Id, tag); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}
// WipeTags delete all tag assigned to the current exercice from the database.
func (e Exercice) WipeTags() (int64, error) {
if res, err := DBExec("DELETE FROM exercice_tags WHERE id_exercice = ?", e.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}

View file

@ -34,7 +34,7 @@ func ResetGame() (error) {
// ResetExercices wipes out all challenges (both attempts and statements).
func ResetExercices() (error) {
return truncateTable("team_hints", "exercice_files_deps", "exercice_files", "flag_found", "exercice_flags", "exercice_solved", "exercice_tries", "exercice_hints", "mcq_found", "mcq_entries", "exercice_mcq", "exercices", "themes")
return truncateTable("team_hints", "exercice_files_deps", "exercice_files", "flag_found", "exercice_flags", "exercice_solved", "exercice_tries", "exercice_hints", "mcq_found", "mcq_entries", "exercice_mcq", "exercice_tags", "exercices", "themes")
}
// ResetTeams wipes out all teams, incluings members and attempts.

View file

@ -6,12 +6,13 @@ import (
// exportedExercice is a structure representing a challenge, as exposed to players.
type exportedExercice struct {
Title string `json:"title"`
URLId string `json:"urlid"`
Gain int64 `json:"gain"`
Coeff float64 `json:"curcoeff"`
Solved int64 `json:"solved"`
Tried int64 `json:"tried"`
Title string `json:"title"`
URLId string `json:"urlid"`
Tags []string `json:"tags"`
Gain int64 `json:"gain"`
Coeff float64 `json:"curcoeff"`
Solved int64 `json:"solved"`
Tried int64 `json:"tried"`
}
// exportedTheme is a structure representing a Theme, as exposed to players.
@ -35,9 +36,11 @@ func ExportThemes() (interface{}, error) {
} else {
exos := map[string]exportedExercice{}
for _, exercice := range exercices {
tags, _ := exercice.GetTags()
exos[fmt.Sprintf("%d", exercice.Id)] = exportedExercice{
exercice.Title,
exercice.URLId,
tags,
exercice.Gain,
exercice.Coefficient,
exercice.SolvedCount(),