diff --git a/internal/domain/question_outbound_payload.go b/internal/domain/question_outbound_payload.go index d3c184de..cf340e80 100644 --- a/internal/domain/question_outbound_payload.go +++ b/internal/domain/question_outbound_payload.go @@ -263,7 +263,7 @@ type OutboundQuestionContent struct { } // ResolveQuestionPreviewText returns learner-facing prompt text for list UIs and search keywords. -// Prefer explicit question_text, then stimulus prompt text, then the stored DB column (DYNAMIC search text). +// Prefer explicit question_text, then stimulus prompt text, then response text, then stored DB column. func ResolveQuestionPreviewText(q QuestionWithDetails, catalog []QuestionTypeDefinition) string { payload := ResolveOutboundDynamicPayload(q, catalog) if text := QuestionTextJSONField(q.QuestionType, q.QuestionText); text != nil && strings.TrimSpace(*text) != "" { @@ -272,6 +272,9 @@ func ResolveQuestionPreviewText(q QuestionWithDetails, catalog []QuestionTypeDef if s := StimulusTextFromPayload(payload); s != "" { return s } + if s := firstReadableTextFromElements(payloadResponseElements(payload)); s != "" { + return s + } if s := strings.TrimSpace(q.QuestionText); s != "" { return s } @@ -283,6 +286,56 @@ func ResolveQuestionPreviewText(q QuestionWithDetails, catalog []QuestionTypeDef return "" } +func payloadResponseElements(payload *DynamicQuestionPayload) []DynamicElementInstance { + if payload == nil { + return nil + } + return payload.Response +} + +func firstReadableTextFromElements(elements []DynamicElementInstance) string { + for _, el := range elements { + if s := nestedReadableString(el.Value); s != "" { + return s + } + } + return "" +} + +func nestedReadableString(v interface{}) string { + if v == nil { + return "" + } + switch t := v.(type) { + case string: + s := strings.TrimSpace(t) + if s == "" || strings.HasPrefix(strings.ToLower(s), "http://") || strings.HasPrefix(strings.ToLower(s), "https://") { + return "" + } + return s + case map[string]interface{}: + for _, key := range []string{"text", "content", "value", "label", "prompt", "instruction", "option_text"} { + if nested, ok := t[key]; ok { + if s := nestedReadableString(nested); s != "" { + return s + } + } + } + for _, nested := range t { + if s := nestedReadableString(nested); s != "" { + return s + } + } + case []interface{}: + for _, nested := range t { + if s := nestedReadableString(nested); s != "" { + return s + } + } + } + return "" +} + // ResolveOutboundQuestionContent normalizes legacy rows to dynamic_payload for API responses. func ResolveOutboundQuestionContent(q QuestionWithDetails, catalog []QuestionTypeDefinition) OutboundQuestionContent { payload := ResolveOutboundDynamicPayload(q, catalog)