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,
|
slotApiValueToFieldString,
|
||||||
} from "./practiceDynamicQuestionPayload"
|
} from "./practiceDynamicQuestionPayload"
|
||||||
import {
|
import {
|
||||||
filterQuestionStimulusForBlock,
|
|
||||||
getBlockStimulusByKey,
|
|
||||||
isIeltsSharedStimulusMode,
|
isIeltsSharedStimulusMode,
|
||||||
mapApiStimulusBlocksToForm,
|
mapApiStimulusBlocksToForm,
|
||||||
mapFormStimulusBlocksToApi,
|
mapFormStimulusBlocksToApi,
|
||||||
|
|
@ -682,12 +680,7 @@ function buildFullUpdateQuestion(
|
||||||
item.question_type_definition_id = created.question_type_definition_id
|
item.question_type_definition_id = created.question_type_definition_id
|
||||||
}
|
}
|
||||||
if (created.dynamic_payload) {
|
if (created.dynamic_payload) {
|
||||||
item.dynamic_payload = q.stimulusBlockKey?.trim()
|
item.dynamic_payload = created.dynamic_payload
|
||||||
? filterQuestionStimulusForBlock(
|
|
||||||
created.dynamic_payload,
|
|
||||||
q.dynamicFieldValues ?? {},
|
|
||||||
)
|
|
||||||
: created.dynamic_payload
|
|
||||||
}
|
}
|
||||||
if (q.stimulusBlockKey?.trim()) {
|
if (q.stimulusBlockKey?.trim()) {
|
||||||
item.stimulus_block_key = q.stimulusBlockKey.trim()
|
item.stimulus_block_key = q.stimulusBlockKey.trim()
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import type {
|
||||||
QuestionTypeDefinition,
|
QuestionTypeDefinition,
|
||||||
} from "../types/questionTypeDefinition.types"
|
} from "../types/questionTypeDefinition.types"
|
||||||
import { buildDynamicQuestionPayload } from "./practiceDynamicQuestionPayload"
|
import { buildDynamicQuestionPayload } from "./practiceDynamicQuestionPayload"
|
||||||
import { dynamicPayloadToFieldValues } from "./practiceDynamicQuestionPayload"
|
|
||||||
import {
|
import {
|
||||||
validateDefinitionQuestion,
|
validateDefinitionQuestion,
|
||||||
questionRowHasContent,
|
questionRowHasContent,
|
||||||
|
|
@ -178,78 +177,12 @@ export function mergeEffectivePayload(
|
||||||
blockStimulus: DynamicElementInstance[],
|
blockStimulus: DynamicElementInstance[],
|
||||||
questionPayload?: DynamicQuestionPayload | null,
|
questionPayload?: DynamicQuestionPayload | null,
|
||||||
): DynamicQuestionPayload {
|
): 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 {
|
return {
|
||||||
stimulus,
|
stimulus: [...blockStimulus, ...(questionPayload?.stimulus ?? [])],
|
||||||
response: questionPayload?.response ?? [],
|
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(
|
export function validatePracticeStimulusBlocks(
|
||||||
authoringProfile: AuthoringProfile,
|
authoringProfile: AuthoringProfile,
|
||||||
blocks: PracticeFormStimulusBlock[],
|
blocks: PracticeFormStimulusBlock[],
|
||||||
|
|
@ -273,24 +206,12 @@ export function validatePracticeStimulusBlocks(
|
||||||
if (!def) {
|
if (!def) {
|
||||||
return `Question ${i + 1}: type definition #${q.questionTypeDefinitionId} was not found. Refresh and try again.`
|
return `Question ${i + 1}: type definition #${q.questionTypeDefinitionId} was not found. Refresh and try again.`
|
||||||
}
|
}
|
||||||
const blockStimulus = getBlockStimulusByKey(blocks, q.stimulusBlockKey)
|
const err = validateDefinitionQuestion(def, q, i + 1)
|
||||||
const err = validateDefinitionQuestionWithBlock(def, q, i + 1, blockStimulus)
|
|
||||||
if (err) return err
|
if (err) return err
|
||||||
}
|
}
|
||||||
return null
|
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(
|
export function countQuestionsReferencingBlock(
|
||||||
blockKey: string,
|
blockKey: string,
|
||||||
questions: { stimulusBlockKey?: string | null }[],
|
questions: { stimulusBlockKey?: string | null }[],
|
||||||
|
|
|
||||||
|
|
@ -328,18 +328,16 @@ export function QuestionsStep({
|
||||||
q: any,
|
q: any,
|
||||||
i: number,
|
i: number,
|
||||||
def: QuestionTypeDefinition,
|
def: QuestionTypeDefinition,
|
||||||
options?: { responseOnly?: boolean },
|
|
||||||
) => {
|
) => {
|
||||||
const responseOnly = Boolean(options?.responseOnly);
|
|
||||||
if (definitionUsesDynamicPayload(def)) {
|
if (definitionUsesDynamicPayload(def)) {
|
||||||
return (
|
return (
|
||||||
<div className="space-y-3 rounded-lg border border-violet-200 bg-violet-50/40 p-3">
|
<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">
|
<p className="text-xs leading-snug text-grayScale-600">
|
||||||
{responseOnly ? (
|
{q.stimulusBlockKey ? (
|
||||||
<>
|
<>
|
||||||
Shared stimulus comes from the linked block. Edit{" "}
|
This question also receives the shared stimulus from block{" "}
|
||||||
<span className="font-medium text-grayScale-800">response</span> fields below.
|
<span className="font-medium text-grayScale-800">{q.stimulusBlockKey}</span>.
|
||||||
Use overrides only when this question needs different stimulus than the block.
|
Its own stimulus and response remain separate and are edited below.
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
|
|
@ -349,58 +347,30 @@ export function QuestionsStep({
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</p>
|
</p>
|
||||||
{!responseOnly ? (
|
{def.stimulus_schema.length > 0 ? (
|
||||||
def.stimulus_schema.length > 0 ? (
|
<div className="space-y-2">
|
||||||
<div className="space-y-2">
|
<p className="text-[10px] font-bold uppercase tracking-wide text-violet-800">
|
||||||
<p className="text-[10px] font-bold uppercase tracking-wide text-violet-800">
|
Question stimulus
|
||||||
Stimulus
|
</p>
|
||||||
</p>
|
{def.stimulus_schema.map((row) => (
|
||||||
{def.stimulus_schema.map((row) => (
|
<div
|
||||||
<div
|
key={`stimulus-${row.id}`}
|
||||||
key={`stimulus-${row.id}`}
|
className="rounded-lg border border-grayScale-200 bg-white p-2.5"
|
||||||
className="rounded-lg border border-grayScale-200 bg-white p-2.5"
|
>
|
||||||
>
|
<DynamicSchemaSlotField
|
||||||
<DynamicSchemaSlotField
|
row={row}
|
||||||
row={row}
|
side="stimulus"
|
||||||
side="stimulus"
|
value={q.dynamicFieldValues?.[`stimulus:${row.id}`] ?? ""}
|
||||||
value={q.dynamicFieldValues?.[`stimulus:${row.id}`] ?? ""}
|
onChange={(next) =>
|
||||||
onChange={(next) =>
|
setDynamicValue(i, `stimulus:${row.id}`, next)
|
||||||
setDynamicValue(i, `stimulus:${row.id}`, next)
|
}
|
||||||
}
|
allFieldValues={q.dynamicFieldValues}
|
||||||
allFieldValues={q.dynamicFieldValues}
|
stimulusSchema={def.stimulus_schema}
|
||||||
stimulusSchema={def.stimulus_schema}
|
responseSchema={def.response_schema}
|
||||||
responseSchema={def.response_schema}
|
/>
|
||||||
/>
|
</div>
|
||||||
</div>
|
))}
|
||||||
))}
|
</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>
|
|
||||||
) : null}
|
) : null}
|
||||||
{def.response_schema.length > 0 ? (
|
{def.response_schema.length > 0 ? (
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
|
|
@ -595,7 +565,7 @@ export function QuestionsStep({
|
||||||
<h2 className="text-2xl font-bold text-grayScale-700">Questions</h2>
|
<h2 className="text-2xl font-bold text-grayScale-700">Questions</h2>
|
||||||
<p className="text-grayScale-400 text-lg">
|
<p className="text-grayScale-400 text-lg">
|
||||||
{ieltsMode
|
{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."}{" "}
|
: "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.
|
Group questions into sections so learners complete earlier blocks before later ones unlock.
|
||||||
Collapse cards to compare and drag them into order.
|
Collapse cards to compare and drag them into order.
|
||||||
|
|
@ -942,9 +912,7 @@ export function QuestionsStep({
|
||||||
</p>
|
</p>
|
||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
{def ? renderTypeSpecificFields(q, i, def, {
|
{def ? renderTypeSpecificFields(q, i, def) : null}
|
||||||
responseOnly: ieltsMode && Boolean(q.stimulusBlockKey),
|
|
||||||
}) : null}
|
|
||||||
</div>
|
</div>
|
||||||
</QuestionCollapsibleBody>
|
</QuestionCollapsibleBody>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user