DYNAMIC and outbound-dynamic questions omit question_text; preview_text exposes the learner-facing wording for search and display. Co-authored-by: Cursor <cursoragent@cursor.com>
314 lines
10 KiB
Go
314 lines
10 KiB
Go
package domain
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// ResolveOutboundDynamicPayload returns the dynamic_payload clients should render.
|
|
// Stored dynamic_payload is returned as-is; legacy rows are synthesized from flat columns.
|
|
func ResolveOutboundDynamicPayload(q QuestionWithDetails, catalog []QuestionTypeDefinition) *DynamicQuestionPayload {
|
|
if hasDynamicPayloadContent(q.DynamicPayload) {
|
|
return q.DynamicPayload
|
|
}
|
|
if UsesDynamicQuestionPayload(q.QuestionType) {
|
|
return nil
|
|
}
|
|
def := ResolveQuestionTypeDefinitionForQuestion(q.QuestionTypeDefinitionID, q.QuestionType, catalog)
|
|
payload := buildLegacyDynamicPayload(q, def)
|
|
if !hasDynamicPayloadContent(payload) {
|
|
return nil
|
|
}
|
|
return payload
|
|
}
|
|
|
|
func hasDynamicPayloadContent(payload *DynamicQuestionPayload) bool {
|
|
return payload != nil && (len(payload.Stimulus) > 0 || len(payload.Response) > 0)
|
|
}
|
|
|
|
func buildLegacyDynamicPayload(q QuestionWithDetails, def QuestionTypeDefinition) *DynamicQuestionPayload {
|
|
switch strings.ToUpper(strings.TrimSpace(q.QuestionType)) {
|
|
case "AUDIO":
|
|
return buildLegacyAudioDynamicPayload(q, def)
|
|
case "MCQ", "TRUE_FALSE":
|
|
return buildLegacyMCQDynamicPayload(q, def)
|
|
case "SHORT_ANSWER":
|
|
return buildLegacyShortAnswerDynamicPayload(q, def)
|
|
default:
|
|
return buildLegacyGenericDynamicPayload(q, def)
|
|
}
|
|
}
|
|
|
|
func buildLegacyAudioDynamicPayload(q QuestionWithDetails, def QuestionTypeDefinition) *DynamicQuestionPayload {
|
|
var stimulus []DynamicElementInstance
|
|
|
|
questionTextID := schemaElementID(def.StimulusSchema, string(StimulusQuestionText), "question_text_1")
|
|
voicePromptID := schemaElementID(def.StimulusSchema, string(StimulusAudioPrompt), "voice_prompt_1")
|
|
sampleVoiceID := schemaElementIDBySuffix(def.StimulusSchema, string(StimulusAudioPrompt), "sample_answer_voice_prompt_1", voicePromptID)
|
|
audioResponseID := schemaElementID(def.ResponseSchema, string(ResponseAudioResponse), "audio_response_1")
|
|
|
|
if text := strings.TrimSpace(q.QuestionText); text != "" {
|
|
stimulus = append(stimulus, DynamicElementInstance{
|
|
ID: questionTextID,
|
|
Kind: string(StimulusQuestionText),
|
|
Value: text,
|
|
})
|
|
}
|
|
if q.VoicePrompt != nil {
|
|
if value := strings.TrimSpace(*q.VoicePrompt); value != "" {
|
|
stimulus = append(stimulus, DynamicElementInstance{
|
|
ID: voicePromptID,
|
|
Kind: string(StimulusAudioPrompt),
|
|
Value: value,
|
|
})
|
|
}
|
|
}
|
|
if q.SampleAnswerVoicePrompt != nil {
|
|
if value := strings.TrimSpace(*q.SampleAnswerVoicePrompt); value != "" {
|
|
stimulus = append(stimulus, DynamicElementInstance{
|
|
ID: sampleVoiceID,
|
|
Kind: string(StimulusAudioPrompt),
|
|
Value: value,
|
|
})
|
|
}
|
|
}
|
|
if q.ImageURL != nil {
|
|
if value := strings.TrimSpace(*q.ImageURL); value != "" {
|
|
imageID := schemaElementID(def.StimulusSchema, string(StimulusImage), "image_1")
|
|
stimulus = append(stimulus, DynamicElementInstance{
|
|
ID: imageID,
|
|
Kind: string(StimulusImage),
|
|
Value: value,
|
|
})
|
|
}
|
|
}
|
|
|
|
var response []DynamicElementInstance
|
|
if value := legacyAudioResponseValue(q); value != "" {
|
|
response = append(response, DynamicElementInstance{
|
|
ID: audioResponseID,
|
|
Kind: string(ResponseAudioResponse),
|
|
Value: value,
|
|
})
|
|
}
|
|
|
|
return &DynamicQuestionPayload{Stimulus: stimulus, Response: response}
|
|
}
|
|
|
|
func legacyAudioResponseValue(q QuestionWithDetails) string {
|
|
if q.SampleAnswerVoicePrompt != nil {
|
|
if value := strings.TrimSpace(*q.SampleAnswerVoicePrompt); value != "" {
|
|
return value
|
|
}
|
|
}
|
|
if q.AudioAnswer != nil {
|
|
if value := strings.TrimSpace(q.AudioAnswer.CorrectAnswerText); value != "" {
|
|
return value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func buildLegacyMCQDynamicPayload(q QuestionWithDetails, def QuestionTypeDefinition) *DynamicQuestionPayload {
|
|
stimulus := legacyTextStimulus(q, def)
|
|
if q.ImageURL != nil {
|
|
if value := strings.TrimSpace(*q.ImageURL); value != "" {
|
|
imageID := schemaElementID(def.StimulusSchema, string(StimulusImage), "image_1")
|
|
stimulus = append(stimulus, DynamicElementInstance{
|
|
ID: imageID,
|
|
Kind: string(StimulusImage),
|
|
Value: value,
|
|
})
|
|
}
|
|
}
|
|
|
|
var response []DynamicElementInstance
|
|
if len(q.Options) > 0 {
|
|
mcqID := schemaElementID(def.ResponseSchema, string(ResponseMultipleChoice), "multiple_choice_1")
|
|
options := make([]map[string]interface{}, 0, len(q.Options))
|
|
for _, opt := range q.Options {
|
|
options = append(options, map[string]interface{}{
|
|
"id": opt.ID,
|
|
"option_text": opt.OptionText,
|
|
"option_order": opt.OptionOrder,
|
|
"is_correct": opt.IsCorrect,
|
|
})
|
|
}
|
|
response = append(response, DynamicElementInstance{
|
|
ID: mcqID,
|
|
Kind: string(ResponseMultipleChoice),
|
|
Value: options,
|
|
})
|
|
}
|
|
|
|
return &DynamicQuestionPayload{Stimulus: stimulus, Response: response}
|
|
}
|
|
|
|
func buildLegacyShortAnswerDynamicPayload(q QuestionWithDetails, def QuestionTypeDefinition) *DynamicQuestionPayload {
|
|
stimulus := legacyTextStimulus(q, def)
|
|
if q.ImageURL != nil {
|
|
if value := strings.TrimSpace(*q.ImageURL); value != "" {
|
|
imageID := schemaElementID(def.StimulusSchema, string(StimulusImage), "image_1")
|
|
stimulus = append(stimulus, DynamicElementInstance{
|
|
ID: imageID,
|
|
Kind: string(StimulusImage),
|
|
Value: value,
|
|
})
|
|
}
|
|
}
|
|
|
|
var response []DynamicElementInstance
|
|
if len(q.ShortAnswers) > 0 {
|
|
shortID := schemaElementID(def.ResponseSchema, string(ResponseShortAnswer), "short_answer_1")
|
|
answers := make([]map[string]interface{}, 0, len(q.ShortAnswers))
|
|
for _, sa := range q.ShortAnswers {
|
|
answers = append(answers, map[string]interface{}{
|
|
"id": sa.ID,
|
|
"acceptable_answer": sa.AcceptableAnswer,
|
|
"match_type": sa.MatchType,
|
|
})
|
|
}
|
|
response = append(response, DynamicElementInstance{
|
|
ID: shortID,
|
|
Kind: string(ResponseShortAnswer),
|
|
Value: answers,
|
|
})
|
|
}
|
|
|
|
return &DynamicQuestionPayload{Stimulus: stimulus, Response: response}
|
|
}
|
|
|
|
func buildLegacyGenericDynamicPayload(q QuestionWithDetails, def QuestionTypeDefinition) *DynamicQuestionPayload {
|
|
stimulus := legacyTextStimulus(q, def)
|
|
if q.ImageURL != nil {
|
|
if value := strings.TrimSpace(*q.ImageURL); value != "" {
|
|
imageID := schemaElementID(def.StimulusSchema, string(StimulusImage), "image_1")
|
|
stimulus = append(stimulus, DynamicElementInstance{
|
|
ID: imageID,
|
|
Kind: string(StimulusImage),
|
|
Value: value,
|
|
})
|
|
}
|
|
}
|
|
return &DynamicQuestionPayload{Stimulus: stimulus}
|
|
}
|
|
|
|
func legacyTextStimulus(q QuestionWithDetails, def QuestionTypeDefinition) []DynamicElementInstance {
|
|
text := strings.TrimSpace(q.QuestionText)
|
|
if text == "" {
|
|
return nil
|
|
}
|
|
kind, fallbackID := legacyTextStimulusKindAndID(def)
|
|
return []DynamicElementInstance{{
|
|
ID: schemaElementID(def.StimulusSchema, kind, fallbackID),
|
|
Kind: kind,
|
|
Value: text,
|
|
}}
|
|
}
|
|
|
|
func legacyTextStimulusKindAndID(def QuestionTypeDefinition) (kind string, fallbackID string) {
|
|
for _, k := range []string{string(StimulusQuestionText), string(StimulusInstruction), string(StimulusTextPassage)} {
|
|
if id := schemaElementID(def.StimulusSchema, k, ""); id != "" {
|
|
return k, id
|
|
}
|
|
}
|
|
return string(StimulusQuestionText), "question_text_1"
|
|
}
|
|
|
|
func schemaElementID(schema []DynamicElementDefinition, kind string, fallback string) string {
|
|
kind = strings.TrimSpace(kind)
|
|
for _, el := range schema {
|
|
if strings.TrimSpace(el.Kind) == kind {
|
|
if id := strings.TrimSpace(el.ID); id != "" {
|
|
return id
|
|
}
|
|
}
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func schemaElementIDBySuffix(schema []DynamicElementDefinition, kind, preferred, fallback string) string {
|
|
kind = strings.TrimSpace(kind)
|
|
for _, el := range schema {
|
|
if strings.TrimSpace(el.Kind) != kind {
|
|
continue
|
|
}
|
|
id := strings.TrimSpace(el.ID)
|
|
if id == preferred {
|
|
return id
|
|
}
|
|
}
|
|
if id := schemaElementID(schema, kind, ""); id != "" && id != fallback {
|
|
return id
|
|
}
|
|
return preferred
|
|
}
|
|
|
|
// UsesOutboundDynamicShape reports whether API responses should omit legacy flat fields.
|
|
func UsesOutboundDynamicShape(payload *DynamicQuestionPayload) bool {
|
|
return hasDynamicPayloadContent(payload)
|
|
}
|
|
|
|
// OutboundQuestionContent is the normalized question body for API responses.
|
|
type OutboundQuestionContent struct {
|
|
DynamicPayload *DynamicQuestionPayload
|
|
QuestionText *string
|
|
// PreviewText is always a human-readable prompt for admin lists/search (legacy + DYNAMIC).
|
|
PreviewText string
|
|
VoicePrompt *string
|
|
SampleAnswerVoicePrompt *string
|
|
ImageURL *string
|
|
AudioCorrectAnswerText *string
|
|
Options []QuestionOption
|
|
ShortAnswers []QuestionShortAnswer
|
|
}
|
|
|
|
// 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).
|
|
func ResolveQuestionPreviewText(q QuestionWithDetails, catalog []QuestionTypeDefinition) string {
|
|
payload := ResolveOutboundDynamicPayload(q, catalog)
|
|
if text := QuestionTextJSONField(q.QuestionType, q.QuestionText); text != nil && strings.TrimSpace(*text) != "" {
|
|
return strings.TrimSpace(*text)
|
|
}
|
|
if s := StimulusTextFromPayload(payload); s != "" {
|
|
return s
|
|
}
|
|
if s := strings.TrimSpace(q.QuestionText); s != "" {
|
|
return s
|
|
}
|
|
if q.Tips != nil {
|
|
if s := strings.TrimSpace(*q.Tips); 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)
|
|
preview := ResolveQuestionPreviewText(q, catalog)
|
|
if !UsesOutboundDynamicShape(payload) {
|
|
var audioCorrect *string
|
|
if q.AudioAnswer != nil && strings.TrimSpace(q.AudioAnswer.CorrectAnswerText) != "" {
|
|
text := q.AudioAnswer.CorrectAnswerText
|
|
audioCorrect = &text
|
|
}
|
|
questionText := QuestionTextJSONField(q.QuestionType, q.QuestionText)
|
|
return OutboundQuestionContent{
|
|
DynamicPayload: payload,
|
|
QuestionText: questionText,
|
|
PreviewText: preview,
|
|
VoicePrompt: q.VoicePrompt,
|
|
SampleAnswerVoicePrompt: q.SampleAnswerVoicePrompt,
|
|
ImageURL: q.ImageURL,
|
|
AudioCorrectAnswerText: audioCorrect,
|
|
Options: q.Options,
|
|
ShortAnswers: q.ShortAnswers,
|
|
}
|
|
}
|
|
return OutboundQuestionContent{
|
|
DynamicPayload: payload,
|
|
PreviewText: preview,
|
|
}
|
|
}
|