Preserve question-level stimulus alongside shared IELTS blocks.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
2180d64b06
commit
31b1ea5a81
|
|
@ -32,8 +32,6 @@ import {
|
|||
slotApiValueToFieldString,
|
||||
} from "./practiceDynamicQuestionPayload"
|
||||
import {
|
||||
filterQuestionStimulusForBlock,
|
||||
getBlockStimulusByKey,
|
||||
isIeltsSharedStimulusMode,
|
||||
mapApiStimulusBlocksToForm,
|
||||
mapFormStimulusBlocksToApi,
|
||||
|
|
@ -682,12 +680,7 @@ function buildFullUpdateQuestion(
|
|||
item.question_type_definition_id = created.question_type_definition_id
|
||||
}
|
||||
if (created.dynamic_payload) {
|
||||
item.dynamic_payload = q.stimulusBlockKey?.trim()
|
||||
? filterQuestionStimulusForBlock(
|
||||
created.dynamic_payload,
|
||||
q.dynamicFieldValues ?? {},
|
||||
)
|
||||
: created.dynamic_payload
|
||||
item.dynamic_payload = created.dynamic_payload
|
||||
}
|
||||
if (q.stimulusBlockKey?.trim()) {
|
||||
item.stimulus_block_key = q.stimulusBlockKey.trim()
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import type {
|
|||
QuestionTypeDefinition,
|
||||
} from "../types/questionTypeDefinition.types"
|
||||
import { buildDynamicQuestionPayload } from "./practiceDynamicQuestionPayload"
|
||||
import { dynamicPayloadToFieldValues } from "./practiceDynamicQuestionPayload"
|
||||
import {
|
||||
validateDefinitionQuestion,
|
||||
questionRowHasContent,
|
||||
|
|
@ -178,78 +177,12 @@ export function mergeEffectivePayload(
|
|||
blockStimulus: DynamicElementInstance[],
|
||||
questionPayload?: DynamicQuestionPayload | null,
|
||||
): DynamicQuestionPayload {
|
||||
const stimulus = [...blockStimulus]
|
||||
const byId = new Map(stimulus.map((el, i) => [el.id?.trim(), i]))
|
||||
for (const el of questionPayload?.stimulus ?? []) {
|
||||
const id = el.id?.trim()
|
||||
if (id && byId.has(id)) {
|
||||
stimulus[byId.get(id)!] = el
|
||||
} else {
|
||||
stimulus.push(el)
|
||||
}
|
||||
}
|
||||
return {
|
||||
stimulus,
|
||||
stimulus: [...blockStimulus, ...(questionPayload?.stimulus ?? [])],
|
||||
response: questionPayload?.response ?? [],
|
||||
}
|
||||
}
|
||||
|
||||
export function mergeBlockStimulusIntoFieldValues(
|
||||
def: QuestionTypeDefinition,
|
||||
blockStimulus: DynamicElementInstance[],
|
||||
questionFieldValues: Record<string, string>,
|
||||
): Record<string, string> {
|
||||
const merged = { ...(questionFieldValues ?? {}) }
|
||||
const blockByKind = new Map<string, DynamicElementInstance>()
|
||||
const blockFieldValues = dynamicPayloadToFieldValues({ stimulus: blockStimulus, response: [] })
|
||||
|
||||
for (const el of blockStimulus) {
|
||||
const kind = el.kind.trim().toUpperCase()
|
||||
if (!blockByKind.has(kind)) blockByKind.set(kind, el)
|
||||
}
|
||||
|
||||
for (const row of def.stimulus_schema) {
|
||||
const key = `stimulus:${row.id}`
|
||||
if (merged[key]?.trim()) continue
|
||||
const kind = row.kind.trim().toUpperCase()
|
||||
const blockEl = blockByKind.get(kind)
|
||||
if (blockEl) {
|
||||
merged[key] = blockFieldValues[`stimulus:${blockEl.id}`] ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
for (const el of blockStimulus) {
|
||||
const key = `stimulus:${el.id}`
|
||||
const qVal = merged[key]?.trim()
|
||||
if (!qVal) {
|
||||
merged[key] = blockFieldValues[key] ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
return merged
|
||||
}
|
||||
|
||||
export function validateDefinitionQuestionWithBlock(
|
||||
def: QuestionTypeDefinition,
|
||||
q: LearnEnglishDefinitionQuestionInput,
|
||||
index1Based: number,
|
||||
blockStimulus: DynamicElementInstance[] | null,
|
||||
): string | null {
|
||||
if (!blockStimulus?.length) {
|
||||
return validateDefinitionQuestion(def, q, index1Based)
|
||||
}
|
||||
const mergedFieldValues = mergeBlockStimulusIntoFieldValues(
|
||||
def,
|
||||
blockStimulus,
|
||||
q.dynamicFieldValues ?? {},
|
||||
)
|
||||
return validateDefinitionQuestion(
|
||||
def,
|
||||
{ ...q, dynamicFieldValues: mergedFieldValues },
|
||||
index1Based,
|
||||
)
|
||||
}
|
||||
|
||||
export function validatePracticeStimulusBlocks(
|
||||
authoringProfile: AuthoringProfile,
|
||||
blocks: PracticeFormStimulusBlock[],
|
||||
|
|
@ -273,24 +206,12 @@ export function validatePracticeStimulusBlocks(
|
|||
if (!def) {
|
||||
return `Question ${i + 1}: type definition #${q.questionTypeDefinitionId} was not found. Refresh and try again.`
|
||||
}
|
||||
const blockStimulus = getBlockStimulusByKey(blocks, q.stimulusBlockKey)
|
||||
const err = validateDefinitionQuestionWithBlock(def, q, i + 1, blockStimulus)
|
||||
const err = validateDefinitionQuestion(def, q, i + 1)
|
||||
if (err) return err
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function filterQuestionStimulusForBlock(
|
||||
payload: DynamicQuestionPayload,
|
||||
questionFieldValues: Record<string, string>,
|
||||
): DynamicQuestionPayload {
|
||||
const stimulus = payload.stimulus.filter((slot) => {
|
||||
const raw = questionFieldValues[`stimulus:${slot.id}`]
|
||||
return Boolean(raw?.trim())
|
||||
})
|
||||
return { ...payload, stimulus }
|
||||
}
|
||||
|
||||
export function countQuestionsReferencingBlock(
|
||||
blockKey: string,
|
||||
questions: { stimulusBlockKey?: string | null }[],
|
||||
|
|
|
|||
|
|
@ -328,18 +328,16 @@ export function QuestionsStep({
|
|||
q: any,
|
||||
i: number,
|
||||
def: QuestionTypeDefinition,
|
||||
options?: { responseOnly?: boolean },
|
||||
) => {
|
||||
const responseOnly = Boolean(options?.responseOnly);
|
||||
if (definitionUsesDynamicPayload(def)) {
|
||||
return (
|
||||
<div className="space-y-3 rounded-lg border border-violet-200 bg-violet-50/40 p-3">
|
||||
<p className="text-xs leading-snug text-grayScale-600">
|
||||
{responseOnly ? (
|
||||
{q.stimulusBlockKey ? (
|
||||
<>
|
||||
Shared stimulus comes from the linked block. Edit{" "}
|
||||
<span className="font-medium text-grayScale-800">response</span> fields below.
|
||||
Use overrides only when this question needs different stimulus than the block.
|
||||
This question also receives the shared stimulus from block{" "}
|
||||
<span className="font-medium text-grayScale-800">{q.stimulusBlockKey}</span>.
|
||||
Its own stimulus and response remain separate and are edited below.
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
|
|
@ -349,58 +347,30 @@ export function QuestionsStep({
|
|||
</>
|
||||
)}
|
||||
</p>
|
||||
{!responseOnly ? (
|
||||
def.stimulus_schema.length > 0 ? (
|
||||
<div className="space-y-2">
|
||||
<p className="text-[10px] font-bold uppercase tracking-wide text-violet-800">
|
||||
Stimulus
|
||||
</p>
|
||||
{def.stimulus_schema.map((row) => (
|
||||
<div
|
||||
key={`stimulus-${row.id}`}
|
||||
className="rounded-lg border border-grayScale-200 bg-white p-2.5"
|
||||
>
|
||||
<DynamicSchemaSlotField
|
||||
row={row}
|
||||
side="stimulus"
|
||||
value={q.dynamicFieldValues?.[`stimulus:${row.id}`] ?? ""}
|
||||
onChange={(next) =>
|
||||
setDynamicValue(i, `stimulus:${row.id}`, next)
|
||||
}
|
||||
allFieldValues={q.dynamicFieldValues}
|
||||
stimulusSchema={def.stimulus_schema}
|
||||
responseSchema={def.response_schema}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : null
|
||||
) : def.stimulus_schema.length > 0 ? (
|
||||
<details className="rounded-lg border border-violet-100 bg-white/80 p-3">
|
||||
<summary className="cursor-pointer text-xs font-semibold text-violet-900">
|
||||
Override block stimulus for this question (optional)
|
||||
</summary>
|
||||
<div className="mt-3 space-y-2">
|
||||
{def.stimulus_schema.map((row) => (
|
||||
<div
|
||||
key={`stimulus-${row.id}`}
|
||||
className="rounded-lg border border-grayScale-200 bg-white p-2.5"
|
||||
>
|
||||
<DynamicSchemaSlotField
|
||||
row={row}
|
||||
side="stimulus"
|
||||
value={q.dynamicFieldValues?.[`stimulus:${row.id}`] ?? ""}
|
||||
onChange={(next) =>
|
||||
setDynamicValue(i, `stimulus:${row.id}`, next)
|
||||
}
|
||||
allFieldValues={q.dynamicFieldValues}
|
||||
stimulusSchema={def.stimulus_schema}
|
||||
responseSchema={def.response_schema}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</details>
|
||||
{def.stimulus_schema.length > 0 ? (
|
||||
<div className="space-y-2">
|
||||
<p className="text-[10px] font-bold uppercase tracking-wide text-violet-800">
|
||||
Question stimulus
|
||||
</p>
|
||||
{def.stimulus_schema.map((row) => (
|
||||
<div
|
||||
key={`stimulus-${row.id}`}
|
||||
className="rounded-lg border border-grayScale-200 bg-white p-2.5"
|
||||
>
|
||||
<DynamicSchemaSlotField
|
||||
row={row}
|
||||
side="stimulus"
|
||||
value={q.dynamicFieldValues?.[`stimulus:${row.id}`] ?? ""}
|
||||
onChange={(next) =>
|
||||
setDynamicValue(i, `stimulus:${row.id}`, next)
|
||||
}
|
||||
allFieldValues={q.dynamicFieldValues}
|
||||
stimulusSchema={def.stimulus_schema}
|
||||
responseSchema={def.response_schema}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
{def.response_schema.length > 0 ? (
|
||||
<div className="space-y-2">
|
||||
|
|
@ -595,7 +565,7 @@ export function QuestionsStep({
|
|||
<h2 className="text-2xl font-bold text-grayScale-700">Questions</h2>
|
||||
<p className="text-grayScale-400 text-lg">
|
||||
{ieltsMode
|
||||
? "Create shared stimulus blocks, link questions to a block, then fill in each question's response fields."
|
||||
? "Create shared stimulus blocks, link questions to a block, then fill in each question's own stimulus and response fields."
|
||||
: "Choose a question type for each item, then fill in the fields that type requires."}{" "}
|
||||
Group questions into sections so learners complete earlier blocks before later ones unlock.
|
||||
Collapse cards to compare and drag them into order.
|
||||
|
|
@ -942,9 +912,7 @@ export function QuestionsStep({
|
|||
</p>
|
||||
) : null}
|
||||
|
||||
{def ? renderTypeSpecificFields(q, i, def, {
|
||||
responseOnly: ieltsMode && Boolean(q.stimulusBlockKey),
|
||||
}) : null}
|
||||
{def ? renderTypeSpecificFields(q, i, def) : null}
|
||||
</div>
|
||||
</QuestionCollapsibleBody>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user