feat: edit quiz questions over MCP
- quizzes_create_question, update_question, delete_question and
reorder_questions on /api/v1/quizzes/{slug}/questions
- update_question replaces answers by id (kept, created, deleted), so
answer ids stored in past attempts stay valid; foreign ids are refused
- classes.CheckQuestion holds the importer's per-question rules, shared
with the manage routes; attempt scores are never touched (D-16)
This commit is contained in:
@@ -97,40 +97,7 @@ func ParseImport(raw []byte) (*ImportQuiz, error) {
|
||||
problems = append(problems, "questions must not be empty")
|
||||
}
|
||||
for i, q := range doc.Questions {
|
||||
at := fmt.Sprintf("questions[%d]", i)
|
||||
if strings.TrimSpace(q.Text) == "" {
|
||||
problems = append(problems, at+".text is required")
|
||||
}
|
||||
if !slices.Contains(models.QuestionTypes, q.Type) {
|
||||
problems = append(problems, fmt.Sprintf("%s.type must be one of %s", at, strings.Join(models.QuestionTypes, ", ")))
|
||||
continue
|
||||
}
|
||||
if q.Points != nil && *q.Points < 0 {
|
||||
problems = append(problems, at+".points must not be negative")
|
||||
}
|
||||
correct := 0
|
||||
for j, a := range q.Answers {
|
||||
if strings.TrimSpace(a.Text) == "" {
|
||||
problems = append(problems, fmt.Sprintf("%s.answers[%d].text is required", at, j))
|
||||
}
|
||||
if a.IsCorrect {
|
||||
correct++
|
||||
}
|
||||
}
|
||||
switch q.Type {
|
||||
case models.TypeSingle:
|
||||
if len(q.Answers) < 2 || correct != 1 {
|
||||
problems = append(problems, at+": a single question needs at least two answers and exactly one correct")
|
||||
}
|
||||
case models.TypeMultiple:
|
||||
if len(q.Answers) < 2 || correct < 1 {
|
||||
problems = append(problems, at+": a multiple question needs at least two answers and one or more correct")
|
||||
}
|
||||
case models.TypeSelfCheck:
|
||||
if len(q.Answers) > 0 {
|
||||
problems = append(problems, at+": a self_check question has no answers; use answer_text")
|
||||
}
|
||||
}
|
||||
problems = append(problems, CheckQuestion(fmt.Sprintf("questions[%d]", i), q)...)
|
||||
}
|
||||
if len(problems) > 0 {
|
||||
return nil, fmt.Errorf("invalid quiz: %s", strings.Join(problems, "; "))
|
||||
@@ -138,6 +105,49 @@ func ParseImport(raw []byte) (*ImportQuiz, error) {
|
||||
return &doc, nil
|
||||
}
|
||||
|
||||
// CheckQuestion returns the problems of one question, each prefixed with
|
||||
// at (such as "questions[0]"): the text is required, the type is one of
|
||||
// models.QuestionTypes, points are not negative, answer texts are
|
||||
// required, a single question has at least two answers and exactly one
|
||||
// correct, a multiple question at least two and one or more correct, and a
|
||||
// self_check question none. The importer and the manage routes share it.
|
||||
func CheckQuestion(at string, q ImportQuestion) []string {
|
||||
var problems []string
|
||||
if strings.TrimSpace(q.Text) == "" {
|
||||
problems = append(problems, at+".text is required")
|
||||
}
|
||||
if !slices.Contains(models.QuestionTypes, q.Type) {
|
||||
return append(problems, fmt.Sprintf("%s.type must be one of %s", at, strings.Join(models.QuestionTypes, ", ")))
|
||||
}
|
||||
if q.Points != nil && *q.Points < 0 {
|
||||
problems = append(problems, at+".points must not be negative")
|
||||
}
|
||||
correct := 0
|
||||
for j, a := range q.Answers {
|
||||
if strings.TrimSpace(a.Text) == "" {
|
||||
problems = append(problems, fmt.Sprintf("%s.answers[%d].text is required", at, j))
|
||||
}
|
||||
if a.IsCorrect {
|
||||
correct++
|
||||
}
|
||||
}
|
||||
switch q.Type {
|
||||
case models.TypeSingle:
|
||||
if len(q.Answers) < 2 || correct != 1 {
|
||||
problems = append(problems, at+": a single question needs at least two answers and exactly one correct")
|
||||
}
|
||||
case models.TypeMultiple:
|
||||
if len(q.Answers) < 2 || correct < 1 {
|
||||
problems = append(problems, at+": a multiple question needs at least two answers and one or more correct")
|
||||
}
|
||||
case models.TypeSelfCheck:
|
||||
if len(q.Answers) > 0 {
|
||||
problems = append(problems, at+": a self_check question has no answers; use answer_text")
|
||||
}
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
// ImportResult reports what an import wrote.
|
||||
type ImportResult struct {
|
||||
Quiz models.Quiz
|
||||
|
||||
Reference in New Issue
Block a user