Handle justified MCQ in admin and sync part

This commit is contained in:
nemunaire 2018-11-21 04:40:32 +01:00 committed by Pierre-Olivier Mercier
parent 488a032eba
commit d7553f0392
4 changed files with 62 additions and 28 deletions

View file

@ -120,13 +120,39 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) {
continue
} else {
hasOne := false
isJustified := false
for cid, choice := range quest.Choice {
if _, err := flag.AddEntry(choice.Label, choice.Value); err != nil {
var val bool
if choice.Value == nil {
val = false
} else if p, ok := choice.Value.(bool); ok {
val = p
if isJustified {
errs = append(errs, fmt.Sprintf("%q: error MCQ #%d: all true items has to be justified in this MCQ.", path.Base(exercice.Path), nline + 1))
continue
}
} else if p, ok := choice.Value.(string); ok {
val = true
if hasOne && !isJustified {
errs = append(errs, fmt.Sprintf("%q: error MCQ #%d: all true items has to be justified in this MCQ.", path.Base(exercice.Path), nline + 1))
continue
}
isJustified = true
if _, err := exercice.AddRawFlag("%" + quest.Label + "%" + choice.Label, "", false, nil, []byte(p)); err != nil {
errs = append(errs, fmt.Sprintf("%q: error MCQ #%d: %s", path.Base(exercice.Path), nline + 1, err))
continue
}
} else {
errs = append(errs, fmt.Sprintf("%q: error in MCQ %d choice %d: has an invalid type. Expected true, false or a string", path.Base(exercice.Path), nline + 1, cid))
continue
}
if _, err := flag.AddEntry(choice.Label, val); err != nil {
errs = append(errs, fmt.Sprintf("%q: error in MCQ %d choice %d: %s", path.Base(exercice.Path), nline + 1, cid, err))
continue
}
if choice.Value {
if val {
hasOne = true
}
}