libfic: start working on MCQ: structures done
This commit is contained in:
parent
31b80a5b2a
commit
c36cd202e8
23
libfic/db.go
23
libfic/db.go
|
@ -138,6 +138,28 @@ CREATE TABLE IF NOT EXISTS exercice_keys(
|
|||
cksum BINARY(64) NOT NULL,
|
||||
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
|
||||
);
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS exercice_mcq(
|
||||
id_mcq INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
id_exercice INTEGER NOT NULL,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
kind ENUM('checkbox', 'radio', 'select') NOT NULL,
|
||||
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
|
||||
);
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS mcq_entries(
|
||||
id_mcq_entry INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
id_mcq INTEGER NOT NULL,
|
||||
label VARCHAR(255) NOT NULL,
|
||||
response BOOLEAN NOT NULL,
|
||||
FOREIGN KEY(id_mcq) REFERENCES exercice_mcq(id_mcq)
|
||||
);
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -146,6 +168,7 @@ CREATE TABLE IF NOT EXISTS key_found(
|
|||
id_key INTEGER NOT NULL,
|
||||
id_team INTEGER NOT NULL,
|
||||
time TIMESTAMP NOT NULL,
|
||||
CONSTRAINT uc_found UNIQUE (id_key,id_team),
|
||||
FOREIGN KEY(id_key) REFERENCES exercice_keys(id_key),
|
||||
FOREIGN KEY(id_team) REFERENCES teams(id_team)
|
||||
);
|
||||
|
|
121
libfic/mcq.go
Normal file
121
libfic/mcq.go
Normal file
|
@ -0,0 +1,121 @@
|
|||
package fic
|
||||
|
||||
import (
|
||||
)
|
||||
|
||||
type MCQ struct {
|
||||
Id int64 `json:"id"`
|
||||
IdExercice int64 `json:"idExercice"`
|
||||
Title string `json:"title"`
|
||||
Kind string `json:"kind"`
|
||||
Entries []MCQ_entry `json:"entries"`
|
||||
}
|
||||
|
||||
type MCQ_entry struct {
|
||||
Id int64 `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Response bool `json:"response"`
|
||||
}
|
||||
|
||||
func (e Exercice) GetMCQ() ([]MCQ, error) {
|
||||
if rows, err := DBQuery("SELECT id_mcq, id_exercice, title, kind FROM exercice_mcq WHERE id_exercice = ?", e.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
var mcqs = make([]MCQ, 0)
|
||||
for rows.Next() {
|
||||
var m MCQ
|
||||
m.IdExercice = e.Id
|
||||
|
||||
if err := rows.Scan(&m.Id, &m.IdExercice, &m.Title, &m.Kind); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if entries_rows, err := DBQuery("SELECT id_mcq, id_exercice, label, response FROM mcq_entries WHERE id_mcq = ?", m.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer entries_rows.Close()
|
||||
|
||||
mcqs = append(mcqs, m)
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mcqs, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (e Exercice) AddMCQ(title string, kind string) (MCQ, error) {
|
||||
if res, err := DBExec("INSERT INTO exercice_mcq (id_exercice, title, kind) VALUES (?, ?, ?)", e.Id, title, kind); err != nil {
|
||||
return MCQ{}, err
|
||||
} else if qid, err := res.LastInsertId(); err != nil {
|
||||
return MCQ{}, err
|
||||
} else {
|
||||
return MCQ{qid, e.Id, title, kind, []MCQ_entry{}}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m MCQ) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE exercice_mcq SET id_exercice = ?, title = ?, kind = ? WHERE id_mcq = ?", m.IdExercice, m.Title, m.Kind, m.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m MCQ) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM exercice_mcq WHERE id_mcq = ?", m.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m MCQ) AddEntry(label string, response bool) (MCQ_entry, error) {
|
||||
if res, err := DBExec("INSERT INTO mcq_entries (id_mcq, label, response) VALUES (?, ?, ?)", m.Id, label, response); err != nil {
|
||||
return MCQ_entry{}, err
|
||||
} else if nid, err := res.LastInsertId(); err != nil {
|
||||
return MCQ_entry{}, err
|
||||
} else {
|
||||
return MCQ_entry{nid, label, response}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (n MCQ_entry) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE mcq_entries SET label = ?, response = ? WHERE id_mcq = ?", n.Label, n.Response, n.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func (n MCQ_entry) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM mcq_entries WHERE id_mcq_entry = ?", n.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m MCQ) Check(vals map[int64]bool) int {
|
||||
diff := 0
|
||||
for _, n := range m.Entries {
|
||||
if v, ok := vals[n.Id]; ok && v == n.Response {
|
||||
continue
|
||||
}
|
||||
diff += 1
|
||||
}
|
||||
|
||||
return diff
|
||||
}
|
|
@ -22,7 +22,7 @@ func ResetGame() (error) {
|
|||
}
|
||||
|
||||
func ResetExercices() (error) {
|
||||
return truncateTable("team_hints", "exercice_files", "key_found", "exercice_keys", "exercice_solved", "exercice_tries", "exercice_hints", "exercices", "themes")
|
||||
return truncateTable("team_hints", "exercice_files", "key_found", "exercice_keys", "exercice_solved", "exercice_tries", "exercice_hints", "mcq_entries", "exercice_mcq", "exercices", "themes")
|
||||
}
|
||||
|
||||
func ResetTeams() (error) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user