Add new helper string related to justified MCQ flag

This commit is contained in:
nemunaire 2018-12-02 04:52:15 +01:00
commit c5b65289d3
10 changed files with 121 additions and 69 deletions

View file

@ -54,7 +54,7 @@ func (e Exercice) GetFlags() ([]Flag, error) {
// GetFlagByLabel returns a flag matching the given label.
func (e Exercice) GetFlagByLabel(label string) (k Flag, err error) {
err = DBQueryRow("SELECT id_flag, id_exercice, type, help, ignorecase, validator_regexp, cksum FROM exercice_flags WHERE type = ? AND id_exercice = ?", label, e.Id).Scan(&k.Id, &k.IdExercice, &k.Label, &k.Help, &k.IgnoreCase, &k.ValidatorRegexp, &k.Checksum)
err = DBQueryRow("SELECT id_flag, id_exercice, type, help, ignorecase, validator_regexp, cksum FROM exercice_flags WHERE type LIKE ? AND id_exercice = ?", label, e.Id).Scan(&k.Id, &k.IdExercice, &k.Label, &k.Help, &k.IgnoreCase, &k.ValidatorRegexp, &k.Checksum)
return
}

View file

@ -1,6 +1,7 @@
package fic
import (
"fmt"
"time"
)
@ -175,8 +176,8 @@ func (e Exercice) WipeMCQs() (int64, error) {
}
// GetJustifiedFlag searchs for a flag in the scope of the given exercice.
func (m MCQ) GetJustifiedFlag(e Exercice, c MCQ_entry) (Flag, error) {
return e.GetFlagByLabel("%" + m.Title + "%" + c.Label)
func (c MCQ_entry) GetJustifiedFlag(e Exercice) (Flag, error) {
return e.GetFlagByLabel(fmt.Sprintf("\\%%%d\\%%%%", c.Id))
}
// Check if the given vals are the expected ones to validate this flag.

View file

@ -0,0 +1,31 @@
package fic
import (
"errors"
"strings"
"strconv"
)
type FlagLabel struct {
Label string
IdChoice int64
Checksum []byte
Solved bool
}
// IsMCQJustification tells you if this key represent a justification from a MCQ.
func (k Flag) IsMCQJustification() (bool) {
return len(k.Label) > 0 && k.Label[0] == '%'
}
// GetMCQJustification returns the structure corresponding to the given flag.
func (k Flag) GetMCQJustification() (fl FlagLabel, err error) {
spl := strings.Split(k.Label, "%")
if len(spl) >= 3 && len(spl[0]) == 0 {
fl.IdChoice, err = strconv.ParseInt(spl[1], 10, 64)
fl.Label = strings.Join(spl[2:], "%")
} else {
err = errors.New("This is not a MCQ justification")
}
return
}

View file

@ -21,13 +21,19 @@ type myTeamHint struct {
File string `json:"file,omitempty"`
Cost int64 `json:"cost"`
}
type myTeamMCQJustifiedChoice struct {
Label string `json:"label"`
Solved bool `json:"solved,omitempty"`
Value bool `json:"value,omitempty"`
Help string `json:"help,omitempty"`
}
type myTeamMCQ struct {
Title string `json:"title"`
Justify bool `json:"justify,omitempty"`
Choices map[int64]string `json:"choices,omitempty"`
Solved *time.Time `json:"solved,omitempty"`
PSolved map[int64]int `json:"checks_solved,omitempty"`
Soluce string `json:"soluce,omitempty"`
Title string `json:"title"`
Justify bool `json:"justify,omitempty"`
Choices map[int64]interface{} `json:"choices,omitempty"`
Solved *time.Time `json:"solved,omitempty"`
PSolved *time.Time `json:"part_solved,omitempty"`
Soluce string `json:"soluce,omitempty"`
}
type myTeamFlag struct {
Label string `json:"label"`
@ -156,8 +162,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
// Expose exercice flags
justifiedMCQ := map[string][]byte{}
justifiedMCQ_ids := map[string]int64{}
justifiedMCQ := map[int64]FlagLabel{}
exercice.Flags = map[int64]myTeamFlag{}
if flags, err := e.GetFlags(); err != nil {
@ -166,11 +171,12 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
for _, k := range flags {
var flag myTeamFlag
if k.Label[0] == '%' {
justifiedMCQ[k.Label[1:]] = k.Checksum
if t == nil || t.HasPartiallySolved(k) == nil {
justifiedMCQ_ids[k.Label[1:]] = k.Id
if fl, err := k.GetMCQJustification(); err == nil {
fl.Checksum = k.Checksum
if t != nil && t.HasPartiallySolved(k) != nil {
fl.Solved = true
}
justifiedMCQ[fl.IdChoice] = fl
continue
} else if t == nil {
flag.Soluce = hex.EncodeToString(k.Checksum)
@ -206,65 +212,64 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
for _, mcq := range mcqs {
m := myTeamMCQ{
Title: mcq.Title,
Choices: map[int64]string{},
Choices: map[int64]interface{}{},
}
soluce := ""
solved_justify := true
fullySolved := true
if t != nil {
m.PSolved = t.HasPartiallyRespond(mcq)
}
for _, e := range mcq.Entries {
m.Choices[e.Id] = e.Label
if e.Response {
soluce += "t"
} else {
soluce += "f"
}
if v, ok := justifiedMCQ[m.Title + "%" + e.Label]; ok {
if v, ok := justifiedMCQ[e.Id]; ok {
m.Justify = true
if t == nil {
soluce += hex.EncodeToString(v)
soluce += hex.EncodeToString(v.Checksum)
}
if _, ok := justifiedMCQ_ids[m.Title + "%" + e.Label]; ok {
solved_justify = false
if m.PSolved != nil || v.Solved {
jc := myTeamMCQJustifiedChoice{
Label: e.Label,
Solved: v.Solved,
Value: v.Solved,
Help: v.Label,
}
if !v.Solved {
fullySolved = false
}
if PartialValidation && m.PSolved != nil {
jc.Value = e.Response
}
m.Choices[e.Id] = jc
} else {
m.Choices[e.Id] = e.Label
}
} else {
m.Choices[e.Id] = e.Label
}
}
if t != nil {
if solved_justify {
m.Solved = t.HasPartiallyRespond(mcq)
} else if PartialValidation && t.HasPartiallyRespond(mcq) != nil {
m.PSolved = map[int64]int{}
for _, e := range mcq.Entries {
if _, ok := justifiedMCQ[m.Title + "%" + e.Label]; ok {
if _, ok := justifiedMCQ_ids[m.Title + "%" + e.Label]; !ok {
m.PSolved[e.Id] = 2
} else if e.Response {
m.PSolved[e.Id] = 1
}
}
}
} else if PartialValidation {
m.PSolved = map[int64]int{}
for _, e := range mcq.Entries {
if _, ok := justifiedMCQ[m.Title + "%" + e.Label]; ok {
if _, ok := justifiedMCQ_ids[m.Title + "%" + e.Label]; !ok {
m.PSolved[e.Id] = 2
} else {
m.PSolved[e.Id] = -1
}
} else {
m.PSolved[e.Id] = -1
}
}
}
} else {
if t == nil {
h := getHashedFlag([]byte(soluce))
m.Soluce = hex.EncodeToString(h[:])
}
if fullySolved {
m.Solved = m.PSolved
m.PSolved = nil
}
exercice.MCQs[mcq.Id] = m
}
}