@lessonkit/react 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,9 +8,10 @@ import {
8
8
  readStringField,
9
9
  resetAssessmentWarningsForTests,
10
10
  useAssessmentHandleRegistration,
11
+ useLessonkit,
11
12
  usePluginScoring,
12
13
  useQuizState
13
- } from "./chunk-TDM3ARE7.js";
14
+ } from "./chunk-7TJQJFYR.js";
14
15
 
15
16
  // src/runtime/lessonMountRegistry.ts
16
17
  var mountCounts = /* @__PURE__ */ new Map();
@@ -48,7 +49,8 @@ function QuizInner(props, ref) {
48
49
  const { enclosingLessonId } = props;
49
50
  const checkId = useMemo(() => normalizeComponentId(props.checkId, "checkId"), [props.checkId]);
50
51
  const quiz = useQuizState(enclosingLessonId);
51
- const { getPluginScore, isChoiceCorrect } = usePluginScoring(checkId, enclosingLessonId);
52
+ const { config } = useLessonkit();
53
+ const { scoreResponse } = usePluginScoring(checkId, enclosingLessonId);
52
54
  const [selected, setSelected] = useState(null);
53
55
  const [selectionCorrect, setSelectionCorrect] = useState(null);
54
56
  const [quizPassed, setQuizPassed] = useState(false);
@@ -146,9 +148,9 @@ function QuizInner(props, ref) {
146
148
  if (nextPassed === true || nextPassed === false) {
147
149
  setQuizPassed(nextPassed);
148
150
  completedRef.current = nextPassed;
149
- if (nextPassed) {
150
- const maxScore = nextCompletedMaxScore ?? completedMaxScore ?? 1;
151
- const score = nextCompletedScore ?? completedScore ?? maxScore;
151
+ if (nextPassed && config.tracking?.replayResumeEvents === true) {
152
+ const maxScore = nextCompletedMaxScore ?? 1;
153
+ const score = nextCompletedScore ?? (nextPassed ? maxScore : 0);
152
154
  replayTelemetry(
153
155
  nextSelected ?? null,
154
156
  nextCorrect ?? null,
@@ -164,6 +166,7 @@ function QuizInner(props, ref) {
164
166
  checkId,
165
167
  completedMaxScore,
166
168
  completedScore,
169
+ config.tracking?.replayResumeEvents,
167
170
  props.passingScore,
168
171
  props.question,
169
172
  quiz,
@@ -185,32 +188,40 @@ function QuizInner(props, ref) {
185
188
  name: questionId,
186
189
  value: c,
187
190
  checked: selected === c,
188
- disabled: passed,
191
+ disabled: passed && !props.enableRetry,
189
192
  "aria-invalid": selected === c && selectionCorrect === false ? true : void 0,
190
193
  onChange: () => {
191
- if (passed) return;
194
+ if (passed && !props.enableRetry) return;
192
195
  setSelected(c);
193
- const custom = getPluginScore(c);
194
- const correct = isChoiceCorrect(c, props.answer, custom, props.passingScore);
195
- setSelectionCorrect(correct);
196
+ const defaultCorrect = c === props.answer;
197
+ const scored = scoreResponse(c, defaultCorrect, 1, props.passingScore);
198
+ setSelectionCorrect(scored.passed);
196
199
  quiz.answer({
197
200
  checkId,
198
201
  question: props.question,
199
202
  choice: c,
200
- correct
203
+ correct: scored.passed
201
204
  });
202
- if (correct && !completedRef.current) {
205
+ if (scored.passed && !completedRef.current) {
203
206
  completedRef.current = true;
204
207
  setQuizPassed(true);
205
- const maxScore = custom?.maxScore ?? 1;
206
- const score = custom?.score ?? maxScore;
207
- setCompletedScore(score);
208
- setCompletedMaxScore(maxScore);
208
+ setCompletedScore(scored.score);
209
+ setCompletedMaxScore(scored.maxScore);
209
210
  quiz.complete({
210
211
  checkId,
211
- score,
212
- maxScore,
213
- passingScore: props.passingScore ?? maxScore
212
+ score: scored.score,
213
+ maxScore: scored.maxScore,
214
+ passingScore: props.passingScore ?? scored.maxScore
215
+ });
216
+ } else if (!scored.passed && props.enableRetry === false && !completedRef.current) {
217
+ completedRef.current = true;
218
+ setCompletedScore(scored.score);
219
+ setCompletedMaxScore(scored.maxScore);
220
+ quiz.complete({
221
+ checkId,
222
+ score: scored.score,
223
+ maxScore: scored.maxScore,
224
+ passingScore: props.passingScore ?? scored.maxScore
214
225
  });
215
226
  }
216
227
  }
@@ -219,7 +230,24 @@ function QuizInner(props, ref) {
219
230
  c
220
231
  ] }, `${questionId}-${i}`))
221
232
  ] }),
222
- selected && selectionCorrect !== null ? /* @__PURE__ */ jsx("p", { role: "status", "aria-live": "polite", children: selectionCorrect ? "Correct" : "Try again" }) : null
233
+ selected && selectionCorrect !== null ? /* @__PURE__ */ jsx("p", { role: "status", "aria-live": "polite", children: selectionCorrect ? "Correct" : "Try again" }) : null,
234
+ props.enableRetry && passed ? /* @__PURE__ */ jsx(
235
+ "button",
236
+ {
237
+ type: "button",
238
+ "data-testid": "quiz-retry",
239
+ onClick: () => {
240
+ completedRef.current = false;
241
+ telemetryReplayedRef.current = false;
242
+ setQuizPassed(false);
243
+ setSelected(null);
244
+ setSelectionCorrect(null);
245
+ setCompletedScore(null);
246
+ setCompletedMaxScore(null);
247
+ },
248
+ children: "Try again"
249
+ }
250
+ ) : null
223
251
  ] });
224
252
  }
225
253
  var QuizInnerForwarded = forwardRef(QuizInner);
@@ -227,16 +255,7 @@ var Quiz = forwardRef(function Quiz2(props, ref) {
227
255
  return /* @__PURE__ */ jsx(AssessmentLessonGuard, { blockLabel: "Quiz", checkId: props.checkId, children: (lessonId) => /* @__PURE__ */ jsx(QuizInnerForwarded, { ...props, enclosingLessonId: lessonId, ref }) });
228
256
  });
229
257
  function KnowledgeCheck(props) {
230
- return /* @__PURE__ */ jsx(
231
- Quiz,
232
- {
233
- checkId: props.checkId,
234
- question: props.question,
235
- choices: props.choices,
236
- answer: props.answer,
237
- passingScore: props.passingScore
238
- }
239
- );
258
+ return /* @__PURE__ */ jsx(Quiz, { ...props });
240
259
  }
241
260
  function resetQuizWarningsForTests() {
242
261
  resetAssessmentWarningsForTests();