Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 | 2x 2x 2x 2x 76x 76x 76x 7x 7x 7x 14x 69x 19x 76x 76x 76x 39x 39x 39x 1x 1x 1x 74x 3x 2x 1x 3x 6x 12x 1x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 1x 62x 3x 6x 1x 360x 180x 108x 1x 15x 86x 44x 44x 28x 44x 44x 13x 36x | import * as React from "react";
import { useDispatch, useSelector } from "react-redux";
import Box from "@mui/material/Box";
import Paper from "@mui/material/Paper";
import Stack from "@mui/material/Stack";
import Typography from "@mui/material/Typography";
import Chip from "@mui/material/Chip";
import ContentCopyIcon from "@mui/icons-material/ContentCopy";
import Alert from "@mui/material/Alert";
import AlertTitle from "@mui/material/AlertTitle";
import Button from "@mui/material/Button";
import TextField from "@mui/material/TextField";
import MenuItem from "@mui/material/MenuItem";
import IconButton from "@mui/material/IconButton";
import Tooltip from "@mui/material/Tooltip";
import Radio from "@mui/material/Radio";
import Dialog from "@mui/material/Dialog";
import DialogContent from "@mui/material/DialogContent";
import Divider from "@mui/material/Divider";
import CheckCircleOutlineIcon from "@mui/icons-material/CheckCircleOutline";
import HelpOutlineIcon from "@mui/icons-material/HelpOutline";
import ErrorOutlineIcon from "@mui/icons-material/ErrorOutline";
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutline";
import EditOutlinedIcon from "@mui/icons-material/EditOutlined";
import ImageOutlinedIcon from "@mui/icons-material/ImageOutlined";
import RichTextContent from "@/utils/components/question-bank/question-display/utils/RichTextContent";
import { gradeLevels, categoryOptions, difficultyOptions } from "@/utils/components/question-bank/utils/constants";
import { updateReviewedQuestion, removeReviewedQuestion } from "@/store/slices/teacher/TeacherQuestionImport";
/**
* Review screen for a bulk question import.
*
* This is the safety gate: nothing has been written to the question bank at this point,
* and nothing will be until the teacher presses Save.
*
* Three things this screen has to get right, each of which it previously got wrong:
*
* * A QUESTION IS RICH TEXT. It can hold a diagram, a table or a formula. This screen
* used to show it as plain text in a text box, which made all of that invisible — and
* one keystroke wrote the stripped version back, destroying the picture for good.
* Display now goes through RichTextContent, and editing through the normal question
* editor, so what a reviewer sees is what a student will see.
*
* * A QUESTION CAN ARRIVE INCOMPLETE. A STAAR PDF states no grade, and text recognition
* can miss an option, leaving the answer unresolved. The batch is all-or-nothing on
* purpose, so without a way to fill those in, one imperfect question meant deleting it
* to save the rest. Every field that can block a save is editable here.
*
* * NOTHING HERE WAS READ BY A PERSON. Everything came out of a picture, so the picture
* travels with the question and is shown beside it. It is the only way to check.
*
* Accessibility note, and it is deliberate: colour is NEVER the only signal. Every
* confidence state carries an icon AND words, so the screen works for colour-blind
* teachers and for screen readers.
*
* Developer: Allan Ninal
*/
// Loaded only when a reviewer actually opens it. The editor pulls in TinyMCE, which is a
// large bundle and a real iframe; importing it at module scope would cost every reviewer
// who never edits anything, and would drag it into tests that only render the list.
const TextEditorDialog = React.lazy(
() => import("@/utils/components/question-bank/question-content/utils/TextEditorDialog")
);
const CONFIDENCE = {
read: {
label: "Read from your file",
colour: "success",
Icon: CheckCircleOutlineIcon,
},
inferred: {
label: "We guessed — please check",
colour: "warning",
Icon: HelpOutlineIcon,
},
missing: {
label: "Not found — please fill in",
colour: "error",
Icon: ErrorOutlineIcon,
},
};
/** Types whose answers live in `parts`, not on the question itself. */
const MULTI_PART = "Multi-Part-Question";
/** What each unresolved field is called in words a teacher can act on. */
const MISSING_WORDING = {
correct_answers: "No answer was found — choose which option is correct.",
teks_code: "TEKS code is missing.",
grade_level: "Grade is missing.",
category: "Reporting category is missing.",
};
/**
* What still stops this question being saved, worked out from the question itself.
*
* NOT the `unresolved` list the service sent: that is a snapshot from before the reviewer
* touched anything, so it still says "no answer" after they have just chosen one. It is
* used as the starting point and then recomputed here on every render, which is what lets
* filling a field switch Save on immediately.
*/
export function stillMissing(question) {
const missing = [];
const type = question.question_type;
if (type === MULTI_PART) {
const parts = question.parts ?? [];
Iif (parts.length < 2) missing.push("parts");
parts.forEach((part) => {
if (!(part.correct_answers ?? []).length) missing.push(`part_${part.name}_answer`);
});
} else if (!(question.correct_answers ?? []).length) {
missing.push("correct_answers");
}
if (!question.teks_code) missing.push("teks_code");
if (!question.grade_level) missing.push("grade_level");
return missing;
}
export function DuplicateChip() {
return (
<Chip
size="small"
color="default"
variant="outlined"
icon={<ContentCopyIcon fontSize="small" />}
label="Already in your bank — will be skipped"
data-testid="duplicate-chip"
/>
);
}
export function ConfidenceChip({ level }) {
const meta = CONFIDENCE[level] ?? CONFIDENCE.missing;
const { Icon } = meta;
return (
<Chip
size="small"
color={meta.colour}
variant="outlined"
icon={<Icon fontSize="small" />}
label={meta.label}
data-testid={`confidence-${level}`}
/>
);
}
/**
* The untouched item as it was printed, beside the question read out of it.
*
* Everything else on the card was recognised rather than read, so this is the only thing
* a reviewer can check the rest against. Small by default because a review screen holds
* many of them; full size on click, because the detail is the point.
*/
export function SourcePicture({ url, index }) {
const [open, setOpen] = React.useState(false);
Iif (!url) return null;
return (
<>
<Box
component="button"
type="button"
onClick={() => setOpen(true)}
aria-label={`Show the original item for question ${index + 1} full size`}
data-testid={`source-image-${index}`}
sx={{
p: 0,
border: "1px solid",
borderColor: "divider",
borderRadius: 1,
background: "none",
cursor: "zoom-in",
display: "block",
width: "100%",
}}
>
<Box component="img" src={url} alt="" sx={{ width: "100%", display: "block" }} />
</Box>
<Typography variant="caption" color="text.secondary">
<ImageOutlinedIcon fontSize="inherit" sx={{ verticalAlign: "middle", mr: 0.5 }} />
The original item — click to enlarge
</Typography>
<Dialog open={open} onClose={() => setOpen(false)} maxWidth="lg" fullWidth>
<DialogContent sx={{ p: 1 }}>
<Box
component="img"
src={url}
alt={`The original printed item for question ${index + 1}`}
sx={{ width: "100%", display: "block" }}
/>
</DialogContent>
</Dialog>
</>
);
}
/** One answer option: the text as a student will see it, and whether it is the answer. */
function ChoiceRow({ text, isAnswer, onPick, onEdit, name, testId }) {
return (
<Stack direction="row" spacing={1} alignItems="flex-start" sx={{ pl: 0.5 }}>
<Radio
size="small"
checked={isAnswer}
onChange={onPick}
name={name}
inputProps={{
"aria-label": "Mark this option as the correct answer",
"data-testid": `${testId}-pick`,
}}
/>
<Box sx={{ flexGrow: 1, pt: 0.75 }}>
<RichTextContent content={text} />
</Box>
{isAnswer && <Chip size="small" color="success" label="answer key" data-testid={testId} />}
{onEdit && (
<Tooltip title="Edit this option">
<IconButton size="small" onClick={onEdit} aria-label="Edit this option">
<EditOutlinedIcon fontSize="small" />
</IconButton>
</Tooltip>
)}
</Stack>
);
}
/** Part A / Part B, each with its own question, options and answer. */
function Parts({ parts, index, onChange }) {
const setPartAnswer = (partIndex, text) => {
const next = parts.map((part, i) => (i === partIndex ? { ...part, correct_answers: [text] } : part));
onChange(next);
};
return (
<Box sx={{ mb: 2 }} data-testid={`parts-${index}`}>
{parts.map((part, partIndex) => (
<Paper key={part.name} variant="outlined" sx={{ p: 1.5, mb: 1 }}>
<Typography variant="subtitle2" gutterBottom>
Part {part.name}
</Typography>
<RichTextContent content={part.question_text} />
<Box mt={1}>
{(part.choices ?? []).map((choice, choiceIndex) => (
<ChoiceRow
key={choiceIndex}
text={choice}
isAnswer={(part.correct_answers ?? []).includes(choice)}
onPick={() => setPartAnswer(partIndex, choice)}
name={`answer-${index}-part-${part.name}`}
testId={`answer-key-${index}-${part.name}-${choiceIndex}`}
/>
))}
</Box>
</Paper>
))}
</Box>
);
}
function QuestionCard({ question, index }) {
const dispatch = useDispatch();
const [editing, setEditing] = React.useState(null);
const [draft, setDraft] = React.useState("");
const missing = stillMissing(question);
const needsAttention = missing.length > 0;
const isMultiPart = question.question_type === MULTI_PART;
const setField = (field, value) => dispatch(updateReviewedQuestion({ index, changes: { [field]: value } }));
const openEditor = (target, content) => {
setDraft(content ?? "");
setEditing(target);
};
const saveEdit = () => {
if (editing?.field === "question") {
setField("question", draft);
} else if (editing?.field === "choice") {
// The whole list, because the store merges changes shallowly: sending one
// choice would drop the others. And if this option was the answer, the answer
// moves with it — they have to stay character-for-character identical or the
// question grades nobody correctly.
const previous = question.choices[editing.choiceIndex].text;
const choices = question.choices.map((choice, i) =>
i === editing.choiceIndex ? { ...choice, text: draft } : choice
);
const answers = (question.correct_answers ?? []).map((answer) => (answer === previous ? draft : answer));
dispatch(updateReviewedQuestion({ index, changes: { choices, correct_answers: answers } }));
}
setEditing(null);
};
return (
<Paper
variant="outlined"
sx={{ p: 2, mb: 2, borderColor: needsAttention ? "warning.main" : "divider" }}
data-testid={`import-question-${index}`}
>
<Stack direction="row" justifyContent="space-between" alignItems="center" mb={1}>
<Typography variant="subtitle2">
Question {index + 1} — {question.question_type}
{question.question_id ? ` · ${question.question_id}` : ""}
</Typography>
<Stack direction="row" spacing={1} alignItems="center">
<ConfidenceChip level={question.correct_answer_confidence ?? "missing"} />
{question.duplicate && <DuplicateChip />}
<Tooltip title="Remove this question from the import">
<IconButton
size="small"
aria-label={`Remove question ${index + 1}`}
onClick={() => dispatch(removeReviewedQuestion(index))}
>
<DeleteOutlineIcon fontSize="small" />
</IconButton>
</Tooltip>
</Stack>
</Stack>
<Stack direction={{ xs: "column", md: "row" }} spacing={2}>
{question.source_image && (
<Box sx={{ width: { xs: "100%", md: 260 }, flexShrink: 0 }}>
<SourcePicture url={question.source_image} index={index} />
</Box>
)}
<Box sx={{ flexGrow: 1, minWidth: 0 }}>
<Stack direction="row" alignItems="flex-start" spacing={1}>
<Box sx={{ flexGrow: 1, minWidth: 0 }} data-testid={`question-text-${index}`}>
<RichTextContent content={question.question} />
</Box>
<Tooltip title="Edit the question">
<IconButton
size="small"
aria-label={`Edit question ${index + 1}`}
onClick={() => openEditor({ field: "question" }, question.question)}
data-testid={`edit-question-${index}`}
>
<EditOutlinedIcon fontSize="small" />
</IconButton>
</Tooltip>
</Stack>
{isMultiPart && (
<Parts
parts={question.parts ?? []}
index={index}
onChange={(parts) => setField("parts", parts)}
/>
)}
{!isMultiPart && question.choices?.length > 0 && (
<Box mt={1} mb={2}>
<Typography variant="caption" color="text.secondary">
Choices — select the correct answer
</Typography>
{question.choices.map((choice, i) => (
<ChoiceRow
key={i}
text={choice.text}
isAnswer={(question.correct_answers ?? []).includes(choice.text)}
// The answer IS the choice's own text, never a copy of
// it: grading compares them character for character.
onPick={() => setField("correct_answers", [choice.text])}
onEdit={() => openEditor({ field: "choice", choiceIndex: i }, choice.text)}
name={`answer-${index}`}
testId={`answer-key-${index}-${i}`}
/>
))}
</Box>
)}
<Divider sx={{ my: 1.5 }} />
<Stack direction="row" spacing={1} flexWrap="wrap" useFlexGap>
<TextField
size="small"
label="TEKS"
value={question.teks_code ?? ""}
onChange={(e) => setField("teks_code", e.target.value)}
error={missing.includes("teks_code")}
sx={{ width: 140 }}
id={`teks-${index}`}
inputProps={{ "data-testid": `teks-${index}` }}
/>
<TextField
select
size="small"
label="Grade"
value={question.grade_level ? String(question.grade_level) : ""}
onChange={(e) => setField("grade_level", Number(e.target.value))}
error={missing.includes("grade_level")}
sx={{ width: 110 }}
id={`grade-${index}`}
inputProps={{ "data-testid": `grade-${index}` }}
>
{gradeLevels.map((grade) => (
<MenuItem key={grade.value} value={grade.value}>
{grade.label}
</MenuItem>
))}
</TextField>
<TextField
select
size="small"
label="Category"
value={question.category ?? ""}
onChange={(e) => setField("category", e.target.value)}
sx={{ width: 120 }}
id={`category-${index}`}
inputProps={{ "data-testid": `category-${index}` }}
>
{categoryOptions.map((option) => (
<MenuItem key={option.value} value={option.value}>
{option.label}
</MenuItem>
))}
</TextField>
<TextField
select
size="small"
label="Difficulty"
value={question.difficulty ?? ""}
onChange={(e) => setField("difficulty", e.target.value)}
sx={{ width: 130 }}
id={`difficulty-${index}`}
inputProps={{ "data-testid": `difficulty-${index}` }}
>
{difficultyOptions.map((option) => (
<MenuItem key={option.value} value={option.value}>
{option.label}
</MenuItem>
))}
</TextField>
</Stack>
</Box>
</Stack>
{(question.notes ?? []).length > 0 && (
<Alert severity="info" sx={{ mt: 2 }} data-testid={`notes-${index}`}>
{question.notes.map((note, i) => (
<Typography key={i} variant="body2">
{note}
</Typography>
))}
</Alert>
)}
{needsAttention && (
<Alert severity="warning" sx={{ mt: 2 }} icon={<ErrorOutlineIcon fontSize="inherit" />}>
<AlertTitle>Needs you before this can be saved</AlertTitle>
{missing.map((field) => (
<Typography key={field} variant="body2">
{MISSING_WORDING[field] ?? `${field.replace(/_/g, " ")} is missing.`}
</Typography>
))}
</Alert>
)}
{editing && (
<React.Suspense fallback={null}>
<TextEditorDialog
open
editorType={editing.field === "choice" ? "choice" : "question"}
isEditMode
editContent={draft}
setEditContent={setDraft}
onSave={saveEdit}
onCancel={() => setEditing(null)}
// The pictures on an imported question already live in our own
// store; adding more from here would upload to a different place
// and is not what this screen is for.
disabledToolbarButtons={["insertimage"]}
/>
</React.Suspense>
)}
</Paper>
);
}
export default function ImportReview({ onSave, onCancel }) {
const { questions, rowErrors, persisted, committing, filename } = useSelector((s) => s.TeacherQuestionImport);
// Recomputed from the questions themselves, not read from the service's count: the
// teacher edits and removes questions here, so anything the server said goes stale
// the moment they touch it.
const needing = questions.filter((q) => stillMissing(q).length > 0).length;
// DISTINCT rows, not the error count. One bad row routinely produces several errors —
// a missing `choices` AND a missing `correctAnswers` — and counting errors told a
// teacher "2 rows could not be read" about a file with one bad row. The staff portal
// learnt this and this one never did; both now behave the same way.
const affectedRowCount = new Set(
rowErrors.map((error) => error.row).filter((row) => row !== null && row !== undefined)
).size;
const canSave = questions.length > 0 && needing === 0 && !committing;
return (
<Box data-testid="import-review">
<Stack direction="row" justifyContent="space-between" alignItems="center" mb={2}>
<Typography variant="h6">
Review {questions.length} question{questions.length === 1 ? "" : "s"}
{filename ? ` from ${filename}` : ""}
</Typography>
<Typography variant="body2" color={needing ? "warning.main" : "success.main"}>
{needing > 0 ? `${needing} need you` : "All ready to save"}
</Typography>
</Stack>
{/* Stated plainly and permanently: this screen has changed nothing yet. */}
{!persisted && (
<Alert severity="info" sx={{ mb: 2 }} data-testid="not-saved-notice">
Nothing has been added to your question bank yet. These are proposals — press Save to add them.
</Alert>
)}
{rowErrors.length > 0 && (
<Alert severity="warning" sx={{ mb: 2 }} data-testid="row-errors">
<AlertTitle>
{affectedRowCount > 0
? `${affectedRowCount} item${affectedRowCount === 1 ? "" : "s"} could not be imported`
: "Some items could not be imported"}
</AlertTitle>
{rowErrors.slice(0, 10).map((error, i) => (
<Typography key={i} variant="body2">
{error.row ? `Row ${error.row}` : ""}
{error.column ? `${error.row ? ", " : ""}${error.column}` : ""}
{error.row || error.column ? ": " : ""}
{error.message}
</Typography>
))}
{rowErrors.length > 10 && (
<Typography variant="body2">…and {rowErrors.length - 10} more.</Typography>
)}
</Alert>
)}
{questions.map((question, index) => (
<QuestionCard key={index} question={question} index={index} />
))}
<Stack direction="row" spacing={2} justifyContent="flex-end" mt={2}>
<Button onClick={onCancel} disabled={committing}>
Cancel
</Button>
<Tooltip
title={
needing > 0
? "Finish the highlighted questions first"
: "Add these questions to your question bank"
}
>
{/* span so the tooltip still shows while the button is disabled */}
<span>
<Button
variant="contained"
disabled={!canSave}
onClick={() => onSave(questions)}
data-testid="save-import"
>
{committing ? "Saving…" : `Save ${questions.length}`}
</Button>
</span>
</Tooltip>
</Stack>
</Box>
);
}
|