admin: display {hint,flag,mcq} dependancies on interface

This commit is contained in:
nemunaire 2020-01-23 14:23:02 +01:00
parent ac9361b4ce
commit 9d93331868
3 changed files with 111 additions and 5 deletions

View file

@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
@ -30,6 +31,7 @@ func init() {
router.GET("/api/exercices/:eid/hints/:hid", apiHandler(hintHandler(showExerciceHint)))
router.PUT("/api/exercices/:eid/hints/:hid", apiHandler(hintHandler(updateExerciceHint)))
router.DELETE("/api/exercices/:eid/hints/:hid", apiHandler(hintHandler(deleteExerciceHint)))
router.GET("/api/exercices/:eid/hints/:hid/dependancies", apiHandler(hintHandler(showExerciceHintDeps)))
router.GET("/api/exercices/:eid/flags", apiHandler(exerciceHandler(listExerciceFlags)))
router.POST("/api/exercices/:eid/flags", apiHandler(exerciceHandler(createExerciceFlag)))
@ -37,6 +39,7 @@ func init() {
router.PUT("/api/exercices/:eid/flags/:kid", apiHandler(flagKeyHandler(updateExerciceFlag)))
router.POST("/api/exercices/:eid/flags/:kid/try", apiHandler(flagKeyHandler(tryExerciceFlag)))
router.DELETE("/api/exercices/:eid/flags/:kid", apiHandler(flagKeyHandler(deleteExerciceFlag)))
router.GET("/api/exercices/:eid/flags/:kid/dependancies", apiHandler(flagKeyHandler(showExerciceFlagDeps)))
router.GET("/api/exercices/:eid/flags/:kid/choices/", apiHandler(flagKeyHandler(listFlagChoices)))
router.GET("/api/exercices/:eid/flags/:kid/choices/:cid", apiHandler(choiceHandler(showFlagChoice)))
router.POST("/api/exercices/:eid/flags/:kid/choices/", apiHandler(flagKeyHandler(createFlagChoice)))
@ -47,6 +50,7 @@ func init() {
router.GET("/api/exercices/:eid/quiz/:qid", apiHandler(quizHandler(showExerciceQuiz)))
router.PUT("/api/exercices/:eid/quiz/:qid", apiHandler(quizHandler(updateExerciceQuiz)))
router.DELETE("/api/exercices/:eid/quiz/:qid", apiHandler(quizHandler(deleteExerciceQuiz)))
router.GET("/api/exercices/:eid/quiz/:qid/dependancies", apiHandler(quizHandler(showExerciceQuizDeps)))
router.GET("/api/exercices/:eid/tags", apiHandler(exerciceHandler(listExerciceTags)))
router.POST("/api/exercices/:eid/tags", apiHandler(exerciceHandler(addExerciceTag)))
@ -111,6 +115,34 @@ func exportResolutionMovies(_ httprouter.Params, body []byte) (interface{}, erro
}
}
func loadFlags(n func () ([]fic.Flag, error)) (interface{}, error) {
if flags, err := n(); err != nil {
return nil, err
} else {
var ret []fic.Flag
for _, flag := range flags {
if f, ok := flag.(fic.FlagKey); ok {
if k, err := fic.GetFlagKey(f.Id); err != nil {
return nil, err
} else {
ret = append(ret, k)
}
} else if f, ok := flag.(fic.MCQ); ok {
if m, err := fic.GetMCQ(f.Id); err != nil {
return nil, err
} else {
ret = append(ret, m)
}
} else {
return nil, errors.New(fmt.Sprintf("Flag type %T not implemented for this flag.", f))
}
}
return ret, nil
}
}
func listExerciceHints(exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetHints()
}
@ -298,6 +330,10 @@ func showExerciceHint(hint fic.EHint, body []byte) (interface{}, error) {
return hint, nil
}
func showExerciceHintDeps(hint fic.EHint, body []byte) (interface{}, error) {
return loadFlags(hint.GetDepends)
}
func updateExerciceHint(hint fic.EHint, body []byte) (interface{}, error) {
var uh fic.EHint
if err := json.Unmarshal(body, &uh); err != nil {
@ -354,6 +390,10 @@ func showExerciceFlag(flag fic.FlagKey, _ fic.Exercice, body []byte) (interface{
return flag, nil
}
func showExerciceFlagDeps(flag fic.FlagKey, _ fic.Exercice, body []byte) (interface{}, error) {
return loadFlags(flag.GetDepends)
}
func tryExerciceFlag(flag fic.FlagKey, _ fic.Exercice, body []byte) (interface{}, error) {
var uk uploadedFlag
if err := json.Unmarshal(body, &uk); err != nil {
@ -457,6 +497,10 @@ func showExerciceQuiz(quiz fic.MCQ, _ fic.Exercice, body []byte) (interface{}, e
return quiz, nil
}
func showExerciceQuizDeps(quiz fic.MCQ, _ fic.Exercice, body []byte) (interface{}, error) {
return loadFlags(quiz.GetDepends)
}
func updateExerciceQuiz(quiz fic.MCQ, exercice fic.Exercice, body []byte) (interface{}, error) {
var uq fic.MCQ
if err := json.Unmarshal(body, &uq); err != nil {