Include shared stimulus in practice detail responses.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
7f49723661
commit
d6e727152a
|
|
@ -15,6 +15,11 @@ func (s *Service) GetLmsPracticeStimulusBlocks(ctx context.Context, practiceID i
|
|||
return s.loadPracticeStimulusBlocks(ctx, domain.PracticeKindLMS, practiceID)
|
||||
}
|
||||
|
||||
// GetExamPrepPracticeStimulusBlocks returns shared stimulus blocks scoped to one exam-prep practice.
|
||||
func (s *Service) GetExamPrepPracticeStimulusBlocks(ctx context.Context, practiceID int64) ([]domain.PracticeStimulusBlock, error) {
|
||||
return s.loadPracticeStimulusBlocks(ctx, domain.PracticeKindExamPrep, practiceID)
|
||||
}
|
||||
|
||||
func blockKeysSet(blocks []domain.PracticeStimulusBlock) map[string]struct{} {
|
||||
out := make(map[string]struct{}, len(blocks))
|
||||
for _, block := range blocks {
|
||||
|
|
|
|||
|
|
@ -132,10 +132,10 @@ func (h *Handler) ListPracticesByCourse(c *fiber.Ctx) error {
|
|||
return c.JSON(domain.Response{
|
||||
Message: "Practices retrieved successfully",
|
||||
Data: fiber.Map{
|
||||
"practices": items,
|
||||
"practices": items,
|
||||
"total_count": total,
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
},
|
||||
Success: true,
|
||||
StatusCode: fiber.StatusOK,
|
||||
|
|
@ -168,10 +168,10 @@ func (h *Handler) ListPracticesByModule(c *fiber.Ctx) error {
|
|||
return c.JSON(domain.Response{
|
||||
Message: "Practices retrieved successfully",
|
||||
Data: fiber.Map{
|
||||
"practices": items,
|
||||
"practices": items,
|
||||
"total_count": total,
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
},
|
||||
Success: true,
|
||||
StatusCode: fiber.StatusOK,
|
||||
|
|
@ -204,16 +204,21 @@ func (h *Handler) ListPracticesByLesson(c *fiber.Ctx) error {
|
|||
return c.JSON(domain.Response{
|
||||
Message: "Practices retrieved successfully",
|
||||
Data: fiber.Map{
|
||||
"practices": items,
|
||||
"practices": items,
|
||||
"total_count": total,
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
},
|
||||
Success: true,
|
||||
StatusCode: fiber.StatusOK,
|
||||
})
|
||||
}
|
||||
|
||||
type practiceWithStimulusRes struct {
|
||||
domain.Practice
|
||||
StimulusBlocks []domain.PracticeStimulusBlock `json:"stimulus_blocks,omitempty"`
|
||||
}
|
||||
|
||||
// GetPractice godoc
|
||||
// @Tags practices
|
||||
// @Param id path int true "Practice ID"
|
||||
|
|
@ -244,7 +249,22 @@ func (h *Handler) GetPractice(c *fiber.Ctx) error {
|
|||
p.Access = &domain.PracticeAccess{IsAccessible: false, Reason: "Question set not found"}
|
||||
}
|
||||
}
|
||||
return c.JSON(domain.Response{Message: "Practice retrieved successfully", Data: p, Success: true, StatusCode: fiber.StatusOK})
|
||||
stimulusBlocks, err := h.practiceContentSvc.GetLmsPracticeStimulusBlocks(c.Context(), p.ID)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
||||
Message: "Failed to load shared stimulus data",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
return c.JSON(domain.Response{
|
||||
Message: "Practice retrieved successfully",
|
||||
Data: practiceWithStimulusRes{
|
||||
Practice: p,
|
||||
StimulusBlocks: stimulusBlocks,
|
||||
},
|
||||
Success: true,
|
||||
StatusCode: fiber.StatusOK,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) applyPracticeAccess(ctx context.Context, c *fiber.Ctx, items []domain.Practice) error {
|
||||
|
|
|
|||
|
|
@ -56,12 +56,22 @@ type shortAnswerRes struct {
|
|||
MatchType string `json:"match_type"`
|
||||
}
|
||||
|
||||
type questionSharedStimulusRes struct {
|
||||
PracticeKind string `json:"practice_kind"`
|
||||
PracticeID int64 `json:"practice_id"`
|
||||
BlockKey string `json:"block_key"`
|
||||
DisplayOrder int32 `json:"display_order"`
|
||||
Stimulus []domain.DynamicElementInstance `json:"stimulus"`
|
||||
EffectiveDynamicPayload *domain.DynamicQuestionPayload `json:"effective_dynamic_payload,omitempty"`
|
||||
}
|
||||
|
||||
type questionRes struct {
|
||||
ID int64 `json:"id"`
|
||||
DisplayOrder *int32 `json:"display_order,omitempty"`
|
||||
AssociatedQuestionID *int64 `json:"associated_question_id,omitempty"`
|
||||
PrerequisiteQuestionIDs []int64 `json:"prerequisite_question_ids,omitempty"`
|
||||
StimulusBlockKey *string `json:"stimulus_block_key,omitempty"`
|
||||
SharedStimulus []questionSharedStimulusRes `json:"shared_stimulus,omitempty"`
|
||||
EffectiveDynamicPayload *domain.DynamicQuestionPayload `json:"effective_dynamic_payload,omitempty"`
|
||||
QuestionText *string `json:"question_text,omitempty"`
|
||||
QuestionType string `json:"question_type"`
|
||||
|
|
@ -1492,9 +1502,53 @@ func (h *Handler) GetQuestionTypesInSet(c *fiber.Ctx) error {
|
|||
})
|
||||
}
|
||||
|
||||
type questionSetStimulusContext struct {
|
||||
PracticeKind string
|
||||
PracticeID int64
|
||||
Blocks []domain.PracticeStimulusBlock
|
||||
}
|
||||
|
||||
func (h *Handler) questionSetStimulusContexts(ctx context.Context, setID int64) ([]questionSetStimulusContext, error) {
|
||||
contexts := make([]questionSetStimulusContext, 0, 2)
|
||||
|
||||
lmsPractice, found, err := h.practiceSvc.TryGetByQuestionSetID(ctx, setID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if found {
|
||||
blocks, err := h.practiceContentSvc.GetLmsPracticeStimulusBlocks(ctx, lmsPractice.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
contexts = append(contexts, questionSetStimulusContext{
|
||||
PracticeKind: domain.PracticeKindLMS,
|
||||
PracticeID: lmsPractice.ID,
|
||||
Blocks: blocks,
|
||||
})
|
||||
}
|
||||
|
||||
examPractice, found, err := h.examPrepSvc.TryGetExamPrepPracticeByQuestionSetID(ctx, setID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if found {
|
||||
blocks, err := h.practiceContentSvc.GetExamPrepPracticeStimulusBlocks(ctx, examPractice.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
contexts = append(contexts, questionSetStimulusContext{
|
||||
PracticeKind: domain.PracticeKindExamPrep,
|
||||
PracticeID: examPractice.ID,
|
||||
Blocks: blocks,
|
||||
})
|
||||
}
|
||||
|
||||
return contexts, nil
|
||||
}
|
||||
|
||||
// GetQuestionSetItems godoc
|
||||
// @Summary Get questions in set
|
||||
// @Description Returns all questions in a question set with details
|
||||
// @Description Returns all questions in a question set with contextual shared stimulus data for linked LMS and exam-prep practices
|
||||
// @Tags question-set-items
|
||||
// @Produce json
|
||||
// @Param setId path int true "Question Set ID"
|
||||
|
|
@ -1547,6 +1601,14 @@ func (h *Handler) GetQuestionsInSet(c *fiber.Ctx) error {
|
|||
})
|
||||
}
|
||||
|
||||
stimulusContexts, err := h.questionSetStimulusContexts(c.Context(), setID)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
||||
Message: "Failed to load shared stimulus data",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
questionResponses := make([]questionRes, 0, len(items))
|
||||
memberships := domain.MembershipsFromQuestionSetItems(items)
|
||||
for _, item := range items {
|
||||
|
|
@ -1563,6 +1625,31 @@ func (h *Handler) GetQuestionsInSet(c *fiber.Ctx) error {
|
|||
res.DisplayOrder = &order
|
||||
res.AssociatedQuestionID = item.AssociatedQuestionID
|
||||
res.PrerequisiteQuestionIDs = domain.PrerequisiteQuestionIDs(memberships, item.QuestionID)
|
||||
res.StimulusBlockKey = item.StimulusBlockKey
|
||||
res.EffectiveDynamicPayload = domain.MergeEffectiveDynamicPayload(nil, res.DynamicPayload)
|
||||
if item.StimulusBlockKey != nil {
|
||||
blockKey := strings.TrimSpace(*item.StimulusBlockKey)
|
||||
for _, stimulusContext := range stimulusContexts {
|
||||
for _, block := range stimulusContext.Blocks {
|
||||
if block.BlockKey != blockKey {
|
||||
continue
|
||||
}
|
||||
effectivePayload := domain.MergeEffectiveDynamicPayload(block.Stimulus, res.DynamicPayload)
|
||||
res.SharedStimulus = append(res.SharedStimulus, questionSharedStimulusRes{
|
||||
PracticeKind: stimulusContext.PracticeKind,
|
||||
PracticeID: stimulusContext.PracticeID,
|
||||
BlockKey: block.BlockKey,
|
||||
DisplayOrder: block.DisplayOrder,
|
||||
Stimulus: block.Stimulus,
|
||||
EffectiveDynamicPayload: effectivePayload,
|
||||
})
|
||||
if res.EffectiveDynamicPayload == nil || len(res.SharedStimulus) == 1 {
|
||||
res.EffectiveDynamicPayload = effectivePayload
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
questionResponses = append(questionResponses, res)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user