From abd7fc6bef996706a91b558ee9e178f5e2cee7d2 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 2 Dec 2018 05:05:59 +0100 Subject: [PATCH] sync: randomize imports: themes order, MCQ and UCQ choices --- admin/sync/exercice_defines.go | 6 ++++-- admin/sync/exercice_keys.go | 15 +++++++++++++++ admin/sync/themes.go | 5 +++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/admin/sync/exercice_defines.go b/admin/sync/exercice_defines.go index d5237856..6281f6c2 100644 --- a/admin/sync/exercice_defines.go +++ b/admin/sync/exercice_defines.go @@ -44,8 +44,9 @@ type ExerciceFlagMCQChoice struct { // ExerciceFlagMCQ holds information about a MCQ flag. type ExerciceFlagMCQ struct { - Label string `toml:",omitempty"` - Choice []ExerciceFlagMCQChoice + Label string `toml:",omitempty"` + Choice []ExerciceFlagMCQChoice + NoShuffle bool } // ExerciceFlagUCQChoice holds a choice for an UCQ flag. @@ -65,6 +66,7 @@ type ExerciceFlagUCQ struct { Choices_Cost int64 `toml:",omitempty"` Choice []ExerciceFlagUCQChoice LockedFile []ExerciceUnlockFile `toml:"unlock_file,omitempty"` + NoShuffle bool } // ExerciceParams contains values parsed from defines.txt. diff --git a/admin/sync/exercice_keys.go b/admin/sync/exercice_keys.go index e586b080..1f95feb0 100644 --- a/admin/sync/exercice_keys.go +++ b/admin/sync/exercice_keys.go @@ -2,6 +2,7 @@ package sync import ( "fmt" + "math/rand" "path" "unicode" @@ -93,6 +94,13 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) { // Import choices hasOne := false + + if !flag.NoShuffle { + rand.Shuffle(len(flag.Choice), func(i, j int) { + flag.Choice[i], flag.Choice[j] = flag.Choice[j], flag.Choice[i] + }) + } + for cid, choice := range flag.Choice { if len(choice.Label) == 0 { choice.Label = choice.Value @@ -121,6 +129,13 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) { } else { hasOne := false isJustified := false + + if !quest.NoShuffle { + rand.Shuffle(len(quest.Choice), func(i, j int) { + quest.Choice[i], quest.Choice[j] = quest.Choice[j], quest.Choice[i] + }) + } + for cid, choice := range quest.Choice { var val bool var justify string diff --git a/admin/sync/themes.go b/admin/sync/themes.go index b32e4acf..a200b0bd 100644 --- a/admin/sync/themes.go +++ b/admin/sync/themes.go @@ -2,6 +2,7 @@ package sync import ( "fmt" + "math/rand" "path" "regexp" "strings" @@ -54,6 +55,10 @@ func SyncThemes(i Importer) []string { if themes, err := getThemes(i); err != nil { errs = append(errs, err.Error()) } else { + rand.Shuffle(len(themes), func(i, j int) { + themes[i], themes[j] = themes[j], themes[i] + }) + for _, tdir := range themes { var authors []string var intro string