From 40529693041955b3a414f4a87c1e98e0595416a0 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 17 Dec 2017 16:28:31 +0100 Subject: [PATCH] libfic/mcq: remove Kind, as we can only handle checkbox; another kind of record should be created to handle select/radio --- admin/sync/exercice_keys.go | 12 +++--------- libfic/db.go | 1 - libfic/mcq.go | 13 ++++++------- libfic/team_my.go | 5 ++--- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/admin/sync/exercice_keys.go b/admin/sync/exercice_keys.go index a209783f..7fe770dc 100644 --- a/admin/sync/exercice_keys.go +++ b/admin/sync/exercice_keys.go @@ -71,7 +71,7 @@ func SyncExerciceMCQ(i Importer, exercice fic.Exercice) []string { // Unique Choice Questions (checkbox) if ucq, err := getFileContent(i, path.Join(exercice.Path, "flags-ucq.txt")); err != nil { errs = append(errs, fmt.Sprintf("%q: unable to read ucq: %s", path.Base(exercice.Path), err)) - } else if flag, err := exercice.AddMCQ("", "checkbox"); err != nil { + } else if flag, err := exercice.AddMCQ(""); err != nil { errs = append(errs, fmt.Sprintf("%q: unable to add ucq: %s", path.Base(exercice.Path), err)) } else { for nline, quest := range strings.Split(ucq, "\n") { @@ -107,7 +107,7 @@ func SyncExerciceMCQ(i Importer, exercice fic.Exercice) []string { continue } - if flag, err := exercice.AddMCQ(quest_splt[0], "radio"); err != nil { + if flag, err := exercice.AddMCQ(quest_splt[0]); err != nil { errs = append(errs, fmt.Sprintf("%q: error in mcq file at line %d: %s", path.Base(exercice.Path), nline + 1, err)) continue } else { @@ -130,13 +130,7 @@ func SyncExerciceMCQ(i Importer, exercice fic.Exercice) []string { } if choice[0] == 49 { - if hasOne { - flag.Kind = "checkbox" - flag.Update() - errs = append(errs, fmt.Sprintf("%q: warning in mcq file at line %d: multiple expected response, switching to ucq-like quiz; is this really expected?", path.Base(exercice.Path), nline + 1)) - } else { - hasOne = true - } + hasOne = true } } if !hasOne { diff --git a/libfic/db.go b/libfic/db.go index eb406acc..af44baaa 100644 --- a/libfic/db.go +++ b/libfic/db.go @@ -147,7 +147,6 @@ 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 { diff --git a/libfic/mcq.go b/libfic/mcq.go index 3487b71a..5915b0fe 100644 --- a/libfic/mcq.go +++ b/libfic/mcq.go @@ -8,7 +8,6 @@ type MCQ struct { Id int64 `json:"id"` IdExercice int64 `json:"idExercice"` Title string `json:"title"` - Kind string `json:"kind"` Entries []MCQ_entry `json:"entries"` } @@ -19,7 +18,7 @@ type MCQ_entry struct { } 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 { + if rows, err := DBQuery("SELECT id_mcq, id_exercice, title FROM exercice_mcq WHERE id_exercice = ?", e.Id); err != nil { return nil, err } else { defer rows.Close() @@ -29,7 +28,7 @@ func (e Exercice) GetMCQ() ([]MCQ, error) { var m MCQ m.IdExercice = e.Id - if err := rows.Scan(&m.Id, &m.IdExercice, &m.Title, &m.Kind); err != nil { + if err := rows.Scan(&m.Id, &m.IdExercice, &m.Title); err != nil { return nil, err } @@ -59,18 +58,18 @@ func (e Exercice) GetMCQ() ([]MCQ, error) { } } -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 { +func (e Exercice) AddMCQ(title string) (MCQ, error) { + if res, err := DBExec("INSERT INTO exercice_mcq (id_exercice, title) VALUES (?, ?)", e.Id, title); 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 + return MCQ{qid, e.Id, title, []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 { + if res, err := DBExec("UPDATE exercice_mcq SET id_exercice = ?, title = ? WHERE id_mcq = ?", m.IdExercice, m.Title, m.Id); err != nil { return 0, err } else if nb, err := res.RowsAffected(); err != nil { return 0, err diff --git a/libfic/team_my.go b/libfic/team_my.go index 4bd02be9..56483d42 100644 --- a/libfic/team_my.go +++ b/libfic/team_my.go @@ -23,7 +23,6 @@ type myTeamHint struct { } type myTeamMCQ struct { Title string `json:"title"` - Kind string `json:"kind"` Choices map[int64]string `json:"choices,omitempty"` Solved *time.Time `json:"solved,omitempty"` } @@ -145,9 +144,9 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) { choices[e.Id] = e.Label } if t == nil { - exercice.MCQs = append(exercice.MCQs, myTeamMCQ{mcq.Title, mcq.Kind, choices, nil}) + exercice.MCQs = append(exercice.MCQs, myTeamMCQ{mcq.Title, choices, nil}) } else { - exercice.MCQs = append(exercice.MCQs, myTeamMCQ{mcq.Title, mcq.Kind, choices, t.HasPartiallyRespond(mcq)}) + exercice.MCQs = append(exercice.MCQs, myTeamMCQ{mcq.Title, choices, t.HasPartiallyRespond(mcq)}) } } }