RezinaLMS-BackEnd/internal/domain/question_set_item.go
Yared Yemane 9b504b87a2 feat: add practice-level stimulus blocks for shared IELTS content
Introduce practice_stimulus_blocks, authoring_profile, and stimulus_block_key on question set items. Full practice GET/PUT returns merged effective_dynamic_payload for admin authoring.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-02 03:16:36 -07:00

118 lines
3.3 KiB
Go

package domain
import (
"errors"
"fmt"
)
var ErrInvalidQuestionAssociation = errors.New("invalid question association")
// QuestionSetMembership describes one question's placement and association within a set.
type QuestionSetMembership struct {
QuestionID int64
DisplayOrder int32
AssociatedQuestionID *int64
StimulusBlockKey *string
}
// ValidateQuestionSetAssociations checks association rules for all memberships in a set.
// associated_question_id must reference a section anchor (no association) in the same set
// with a lower display_order.
func ValidateQuestionSetAssociations(memberships []QuestionSetMembership) error {
byID := make(map[int64]QuestionSetMembership, len(memberships))
for _, m := range memberships {
byID[m.QuestionID] = m
}
for _, m := range memberships {
if m.AssociatedQuestionID == nil {
continue
}
assocID := *m.AssociatedQuestionID
if assocID == m.QuestionID {
return fmt.Errorf("question %d cannot be associated with itself", m.QuestionID)
}
assoc, ok := byID[assocID]
if !ok {
return fmt.Errorf("associated_question_id %d is not in the question set", assocID)
}
if assoc.AssociatedQuestionID != nil {
return fmt.Errorf("associated_question_id %d must reference a section anchor question", assocID)
}
if assoc.DisplayOrder >= m.DisplayOrder {
return fmt.Errorf("associated question %d must have a lower display_order than question %d", assocID, m.QuestionID)
}
}
return nil
}
// PrerequisiteQuestionIDs returns question IDs that must be completed before accessing targetQuestionID.
func PrerequisiteQuestionIDs(memberships []QuestionSetMembership, targetQuestionID int64) []int64 {
byID := make(map[int64]QuestionSetMembership, len(memberships))
ordered := make([]QuestionSetMembership, 0, len(memberships))
for _, m := range memberships {
byID[m.QuestionID] = m
ordered = append(ordered, m)
}
target, ok := byID[targetQuestionID]
if !ok {
return nil
}
seen := make(map[int64]struct{})
add := func(id int64) {
if id == targetQuestionID {
return
}
if _, ok := seen[id]; ok {
return
}
seen[id] = struct{}{}
}
if target.AssociatedQuestionID == nil {
for _, m := range ordered {
if m.DisplayOrder < target.DisplayOrder {
add(m.QuestionID)
}
}
} else {
anchorID := *target.AssociatedQuestionID
anchor := byID[anchorID]
for _, m := range ordered {
if m.DisplayOrder < anchor.DisplayOrder {
add(m.QuestionID)
}
}
for _, m := range ordered {
if m.QuestionID == anchorID || (m.AssociatedQuestionID != nil && *m.AssociatedQuestionID == anchorID) {
if m.DisplayOrder < target.DisplayOrder {
add(m.QuestionID)
}
}
}
}
out := make([]int64, 0, len(seen))
for _, m := range ordered {
if _, ok := seen[m.QuestionID]; ok {
out = append(out, m.QuestionID)
}
}
return out
}
// MembershipsFromQuestionSetItems converts set items to membership inputs for validation.
func MembershipsFromQuestionSetItems(items []QuestionSetItemWithQuestion) []QuestionSetMembership {
out := make([]QuestionSetMembership, len(items))
for i, item := range items {
out[i] = QuestionSetMembership{
QuestionID: item.QuestionID,
DisplayOrder: item.DisplayOrder,
AssociatedQuestionID: item.AssociatedQuestionID,
StimulusBlockKey: item.StimulusBlockKey,
}
}
return out
}