@pie-element/image-cloze-association 10.1.2-next.2 → 10.1.2

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.
Files changed (130) hide show
  1. package/CHANGELOG.json +437 -0
  2. package/CHANGELOG.md +1997 -0
  3. package/LICENSE.md +5 -0
  4. package/README.md +1 -0
  5. package/configure/CHANGELOG.json +197 -0
  6. package/configure/CHANGELOG.md +1600 -0
  7. package/configure/lib/defaults.js +86 -0
  8. package/configure/lib/defaults.js.map +1 -0
  9. package/configure/lib/index.js +99 -0
  10. package/configure/lib/index.js.map +1 -0
  11. package/configure/lib/root.js +135 -0
  12. package/configure/lib/root.js.map +1 -0
  13. package/configure/package.json +23 -0
  14. package/configure/src/__tests__/index.test.js +155 -0
  15. package/configure/src/defaults.js +59 -0
  16. package/configure/src/index.js +114 -0
  17. package/configure/src/root.jsx +116 -0
  18. package/controller/CHANGELOG.json +137 -0
  19. package/controller/CHANGELOG.md +1149 -0
  20. package/controller/lib/defaults.js +14 -0
  21. package/controller/lib/defaults.js.map +1 -0
  22. package/controller/lib/index.js +304 -0
  23. package/controller/lib/index.js.map +1 -0
  24. package/controller/lib/utils.js +70 -0
  25. package/controller/lib/utils.js.map +1 -0
  26. package/controller/package.json +19 -0
  27. package/controller/src/__tests__/index.test.js +711 -0
  28. package/controller/src/defaults.js +7 -0
  29. package/controller/src/index.js +322 -0
  30. package/controller/src/utils.js +72 -0
  31. package/docs/config-schema.json +1382 -0
  32. package/docs/config-schema.json.md +1021 -0
  33. package/docs/demo/config.js +8 -0
  34. package/docs/demo/generate.js +74 -0
  35. package/docs/demo/index.html +1 -0
  36. package/docs/demo/session.js +16 -0
  37. package/docs/pie-schema.json +1085 -0
  38. package/docs/pie-schema.json.md +810 -0
  39. package/lib/constants.js +12 -0
  40. package/lib/constants.js.map +1 -0
  41. package/lib/evaluation-icon.js +60 -0
  42. package/lib/evaluation-icon.js.map +1 -0
  43. package/lib/image-container.js +94 -0
  44. package/lib/image-container.js.map +1 -0
  45. package/lib/image-drop-target.js +130 -0
  46. package/lib/image-drop-target.js.map +1 -0
  47. package/lib/index.js +220 -0
  48. package/lib/index.js.map +1 -0
  49. package/lib/interactive-section.js +104 -0
  50. package/lib/interactive-section.js.map +1 -0
  51. package/lib/possible-response.js +161 -0
  52. package/lib/possible-response.js.map +1 -0
  53. package/lib/possible-responses.js +58 -0
  54. package/lib/possible-responses.js.map +1 -0
  55. package/lib/root.js +491 -0
  56. package/lib/root.js.map +1 -0
  57. package/lib/static-html-span.js +35 -0
  58. package/lib/static-html-span.js.map +1 -0
  59. package/lib/utils-correctness.js +89 -0
  60. package/lib/utils-correctness.js.map +1 -0
  61. package/package.json +21 -86
  62. package/src/__tests__/index.test.js +174 -0
  63. package/src/__tests__/root.test.jsx +99 -0
  64. package/src/__tests__/utils.test.js +207 -0
  65. package/src/constants.js +5 -0
  66. package/src/evaluation-icon.jsx +54 -0
  67. package/src/image-container.jsx +90 -0
  68. package/src/image-drop-target.jsx +140 -0
  69. package/src/index.js +245 -0
  70. package/src/interactive-section.jsx +94 -0
  71. package/src/possible-response.jsx +152 -0
  72. package/src/possible-responses.jsx +52 -0
  73. package/src/root.jsx +490 -0
  74. package/src/static-html-span.jsx +30 -0
  75. package/src/utils-correctness.js +95 -0
  76. package/configure.js +0 -2
  77. package/controller.js +0 -1
  78. package/dist/author/defaults.d.ts +0 -88
  79. package/dist/author/defaults.js +0 -58
  80. package/dist/author/index.d.ts +0 -34
  81. package/dist/author/index.js +0 -71
  82. package/dist/author/root.d.ts +0 -14
  83. package/dist/author/root.js +0 -80
  84. package/dist/browser/Check-DL1c-mLM.js +0 -10708
  85. package/dist/browser/Check-DL1c-mLM.js.map +0 -1
  86. package/dist/browser/author/index.js +0 -38597
  87. package/dist/browser/author/index.js.map +0 -1
  88. package/dist/browser/controller/index.js +0 -171
  89. package/dist/browser/controller/index.js.map +0 -1
  90. package/dist/browser/delivery/index.js +0 -2699
  91. package/dist/browser/delivery/index.js.map +0 -1
  92. package/dist/browser/dist-BphSS14E.js +0 -346
  93. package/dist/browser/dist-BphSS14E.js.map +0 -1
  94. package/dist/browser/humps-CZ4RCLab.js +0 -67
  95. package/dist/browser/humps-CZ4RCLab.js.map +0 -1
  96. package/dist/browser/image-cloze-association.css +0 -2
  97. package/dist/controller/defaults.d.ts +0 -16
  98. package/dist/controller/defaults.js +0 -10
  99. package/dist/controller/index.d.ts +0 -23
  100. package/dist/controller/index.js +0 -122
  101. package/dist/controller/utils.d.ts +0 -14
  102. package/dist/controller/utils.js +0 -36
  103. package/dist/delivery/constants.d.ts +0 -14
  104. package/dist/delivery/evaluation-icon.d.ts +0 -28
  105. package/dist/delivery/evaluation-icon.js +0 -38
  106. package/dist/delivery/image-container.d.ts +0 -13
  107. package/dist/delivery/image-container.js +0 -61
  108. package/dist/delivery/image-drop-target.d.ts +0 -45
  109. package/dist/delivery/image-drop-target.js +0 -90
  110. package/dist/delivery/index.d.ts +0 -20
  111. package/dist/delivery/index.js +0 -110
  112. package/dist/delivery/interactive-section.d.ts +0 -15
  113. package/dist/delivery/interactive-section.js +0 -72
  114. package/dist/delivery/possible-response.d.ts +0 -34
  115. package/dist/delivery/possible-response.js +0 -100
  116. package/dist/delivery/possible-responses.d.ts +0 -31
  117. package/dist/delivery/possible-responses.js +0 -41
  118. package/dist/delivery/root.d.ts +0 -21
  119. package/dist/delivery/root.js +0 -278
  120. package/dist/delivery/static-html-span.d.ts +0 -14
  121. package/dist/delivery/static-html-span.js +0 -22
  122. package/dist/delivery/utils-correctness.d.ts +0 -10
  123. package/dist/delivery/utils-correctness.js +0 -43
  124. package/dist/index.d.ts +0 -1
  125. package/dist/index.iife.d.ts +0 -8
  126. package/dist/index.iife.js +0 -152
  127. package/dist/index.js +0 -2
  128. package/dist/node_modules/.bun/clsx@2.1.1/node_modules/clsx/dist/clsx.js +0 -16
  129. package/dist/runtime-support.d.ts +0 -12
  130. package/dist/runtime-support.js +0 -12
@@ -0,0 +1,322 @@
1
+ import debug from 'debug';
2
+ import { camelizeKeys } from 'humps';
3
+ import { partialScoring } from '@pie-lib/controller-utils';
4
+ import { cloneDeep, isEmpty, shuffle } from 'lodash-es';
5
+
6
+ import defaults from './defaults';
7
+ import { getAllUniqueCorrectness, getCompleteResponseDetails } from './utils';
8
+
9
+ const log = debug('pie-elements:image-cloze-association:controller');
10
+
11
+ export const normalize = (question) => ({ ...defaults, ...question });
12
+
13
+ export const model = (question, session, env) => {
14
+ const questionNormalized = normalize(question);
15
+ const questionCamelized = camelizeKeys(questionNormalized);
16
+
17
+ return new Promise((resolve) => {
18
+ const shouldIncludeCorrectResponse = env.mode === 'evaluate';
19
+
20
+ const {
21
+ responseAreasToBeFilled,
22
+ possibleResponses: completeResponses,
23
+ hasUnplacedChoices,
24
+ } = getCompleteResponseDetails(questionCamelized.validation, questionCamelized.possibleResponses);
25
+
26
+ const out = {
27
+ disabled: env.mode !== 'gather',
28
+ mode: env.mode,
29
+ ...questionCamelized,
30
+ responseCorrect: shouldIncludeCorrectResponse ? getScore(questionCamelized, session) === 1 : undefined,
31
+ validation: shouldIncludeCorrectResponse ? questionCamelized.validation : undefined,
32
+ responseAreasToBeFilled,
33
+ completeResponses,
34
+ hasUnplacedChoices,
35
+ };
36
+
37
+ if (questionNormalized.shuffle) {
38
+ out.possibleResponses = shuffle(questionNormalized.possible_responses);
39
+ }
40
+
41
+ if (env.role === 'instructor' && (env.mode === 'view' || env.mode === 'evaluate')) {
42
+ out.teacherInstructions = questionCamelized.teacherInstructionsEnabled
43
+ ? questionCamelized.teacherInstructions
44
+ : null;
45
+ out.rationale = questionCamelized.rationale ? questionCamelized.rationale : null;
46
+ } else {
47
+ out.teacherInstructions = null;
48
+ out.rationale = null;
49
+ }
50
+
51
+ resolve(out);
52
+ });
53
+ };
54
+
55
+ export const isResponseCorrect = (correctResponses, session) => {
56
+ const responses = cloneDeep(correctResponses);
57
+ let isCorrect = true;
58
+ let totalValidResponses = 0;
59
+
60
+ if (!session || isEmpty(session)) {
61
+ return false;
62
+ }
63
+
64
+ responses.forEach((value) => (totalValidResponses += (value.images || []).length));
65
+
66
+ if (session.answers && totalValidResponses === session.answers.length) {
67
+ session.answers.forEach((answer) => {
68
+ const index = (responses[answer.containerIndex]?.images || []).indexOf(answer.value);
69
+
70
+ if (index >= 0) {
71
+ // remove response from correct responses array to ensure that duplicates are evaluated correctly
72
+ responses[answer.containerIndex].images.splice(index, 1);
73
+ } else {
74
+ isCorrect = false;
75
+ }
76
+ });
77
+ } else {
78
+ isCorrect = false;
79
+ }
80
+
81
+ return isCorrect;
82
+ };
83
+
84
+ // This applies for correct responses that have empty values
85
+ const keepNonEmptyResponses = (responses) => {
86
+ const filtered = responses.filter((response) => response.images && response.images.length);
87
+ return cloneDeep(filtered);
88
+ };
89
+
90
+ // This applies for items that don't support partial scoring.
91
+ const isDefaultOrAltResponseCorrect = (question, session) => {
92
+ const {
93
+ validation: { altResponses },
94
+ } = question;
95
+ let {
96
+ validation: {
97
+ validResponse: { value },
98
+ },
99
+ } = question;
100
+
101
+ let isCorrect = isResponseCorrect(value, session);
102
+
103
+ // Look for correct answers in alternate responses.
104
+ if (!isCorrect && altResponses && altResponses.length) {
105
+ altResponses.forEach((altResponse) => {
106
+ if (isResponseCorrect(altResponse.value, session)) {
107
+ isCorrect = true;
108
+ }
109
+ });
110
+ }
111
+ return isCorrect;
112
+ };
113
+
114
+ // Deduct only the items that exceeded the maximum valid response per container.
115
+ const getDeductionPerContainer = (containerIndex, answers, valid) => {
116
+ const totalStack = answers.filter((item) => item.containerIndex === containerIndex);
117
+ const incorrectStack = totalStack.filter((item) => !item.isCorrect);
118
+ const maxValid = (valid.value[containerIndex].images || []).length;
119
+
120
+ if (totalStack.length > maxValid) {
121
+ const ignored = totalStack.length - maxValid;
122
+ return incorrectStack.slice(-ignored);
123
+ }
124
+ return [];
125
+ };
126
+
127
+ export const getPartialScore = (question, session) => {
128
+ const {
129
+ validation: { validResponse },
130
+ maxResponsePerZone,
131
+ } = question;
132
+ let correctAnswers = 0;
133
+ let incorrectAnswers = 0;
134
+ let possibleResponses = 0;
135
+
136
+ if (!session || isEmpty(session)) {
137
+ return 0;
138
+ }
139
+
140
+ validResponse.value.forEach((value) => (possibleResponses += (value.images || []).length));
141
+
142
+ if (session.answers && session.answers.length) {
143
+ const all = getAllUniqueCorrectness(session.answers, validResponse.value);
144
+ correctAnswers = all.filter((item) => item.isCorrect).length;
145
+ incorrectAnswers = all.filter((item) => !item.isCorrect).length;
146
+
147
+ // deduction rules: https://docs.google.com/document/d/1Oprm8Qs5fg_Dwoj2pNpsfu4D63QgCZgvcqTgeaVel7I/edit
148
+ session.answers.forEach((answer) => {
149
+ if (maxResponsePerZone > 1) {
150
+ const deductionList = getDeductionPerContainer(answer.containerIndex, all, validResponse);
151
+
152
+ if (deductionList.length) {
153
+ deductionList.forEach((item) => {
154
+ if (item.id === answer.id) {
155
+ correctAnswers -= 1;
156
+ }
157
+ });
158
+ }
159
+ }
160
+ });
161
+
162
+ if (!maxResponsePerZone || maxResponsePerZone <= 1) {
163
+ correctAnswers -= incorrectAnswers;
164
+ }
165
+ } else {
166
+ correctAnswers = 0;
167
+ }
168
+ // negative values will implicitly make the score equal to zero
169
+ correctAnswers = correctAnswers < 0 ? 0 : correctAnswers;
170
+
171
+ // use length of validResponse since some containers can be left empty
172
+ const nonEmptyResponses = keepNonEmptyResponses(validResponse.value);
173
+ const denominator = maxResponsePerZone > 1 ? possibleResponses : (nonEmptyResponses || []).length;
174
+ const str = (correctAnswers / denominator).toFixed(2);
175
+
176
+ return parseFloat(str);
177
+ };
178
+
179
+ const getScore = (config, session, env = {}) => {
180
+ const isPartialScoring = partialScoring.enabled(config, env);
181
+ const correct = isDefaultOrAltResponseCorrect(config, session);
182
+
183
+ return isPartialScoring ? getPartialScore(config, session) : correct ? 1 : 0;
184
+ };
185
+
186
+ /**
187
+ * Generates detailed trace log for scoring evaluation
188
+ * @param {Object} model - the question model
189
+ * @param {Object} session - the student session
190
+ * @param {Object} env - the environment
191
+ * @returns {Array} traceLog - array of trace messages
192
+ */
193
+ export const getLogTrace = (model, session, env) => {
194
+ const traceLog = [];
195
+ const { answers } = session || {};
196
+
197
+ const validResponse = model.validation?.validResponse?.value || [];
198
+ const totalContainers = validResponse.length;
199
+ traceLog.push(`${totalContainers} placement container(s) defined in this question.`);
200
+
201
+ if (answers && answers.length > 0) {
202
+ traceLog.push(`Student placed ${answers.length} image(s) into placement containers.`);
203
+
204
+ const answersByContainer = {};
205
+ answers.forEach((answer) => {
206
+ if (!answersByContainer[answer.containerIndex]) {
207
+ answersByContainer[answer.containerIndex] = [];
208
+ }
209
+ answersByContainer[answer.containerIndex].push(answer.value);
210
+ });
211
+
212
+ validResponse.forEach((container, containerIndex) => {
213
+ const correctImages = container.images || [];
214
+ const studentImages = answersByContainer[containerIndex] || [];
215
+
216
+ if (correctImages.length > 0) {
217
+ if (studentImages.length === 0) {
218
+ traceLog.push(`Container ${containerIndex + 1}: student left empty (should contain ${correctImages.length} image(s)).`);
219
+ } else {
220
+ const correctCount = studentImages.filter(img => correctImages.includes(img)).length;
221
+ const incorrectCount = studentImages.length - correctCount;
222
+
223
+ if (correctCount > 0 && incorrectCount === 0) {
224
+ traceLog.push(`Container ${containerIndex + 1}: student placed ${correctCount} correct image(s).`);
225
+ } else if (correctCount === 0 && incorrectCount > 0) {
226
+ traceLog.push(`Container ${containerIndex + 1}: student placed ${incorrectCount} incorrect image(s).`);
227
+ } else {
228
+ traceLog.push(`Container ${containerIndex + 1}: student placed ${correctCount} correct and ${incorrectCount} incorrect image(s).`);
229
+ }
230
+ }
231
+ }
232
+ });
233
+ } else {
234
+ traceLog.push('Student did not place any images into placement containers.');
235
+ }
236
+
237
+ const altResponses = model.validation?.altResponses || [];
238
+ if (altResponses.length > 0) {
239
+ traceLog.push(`${altResponses.length} alternate response combination(s) are accepted for this question.`);
240
+ }
241
+
242
+ const partialScoringEnabled = partialScoring.enabled(model, env);
243
+ const scoringMethod = partialScoringEnabled ? 'partial scoring' : 'all-or-nothing scoring';
244
+ traceLog.push(`Score calculated using ${scoringMethod}.`);
245
+
246
+ const score = getScore(model, session, env);
247
+ traceLog.push(`Final score: ${score}.`);
248
+
249
+ return traceLog;
250
+ }
251
+
252
+ export const outcome = (config, session, env = {}) => {
253
+ return new Promise((resolve) => {
254
+ log('outcome...');
255
+ if (!session || isEmpty(session)) {
256
+ resolve({
257
+ score: 0,
258
+ empty: true,
259
+ traceLog: ['Student did not place any images into placement containers. Score is 0.']
260
+ });
261
+ } else {
262
+ const configCamelized = camelizeKeys(config);
263
+ const traceLog = getLogTrace(configCamelized, session, env);
264
+ const score = getScore(configCamelized, session, env);
265
+
266
+ resolve({
267
+ score,
268
+ empty: false,
269
+ traceLog
270
+ });
271
+ }
272
+ });
273
+ };
274
+
275
+ export const createCorrectResponseSession = (question, env) => {
276
+ return new Promise((resolve) => {
277
+ if (env.mode !== 'evaluate' && env.role === 'instructor') {
278
+ const {
279
+ validation: {
280
+ valid_response: { value },
281
+ },
282
+ } = question;
283
+ const answers = [];
284
+
285
+ if (value) {
286
+ value.forEach((container, i) => {
287
+ (container.images || []).forEach((v) => {
288
+ answers.push({
289
+ value: v,
290
+ containerIndex: i,
291
+ });
292
+ });
293
+ });
294
+ }
295
+
296
+ resolve({
297
+ answers,
298
+ id: '1',
299
+ });
300
+ } else {
301
+ resolve(null);
302
+ }
303
+ });
304
+ };
305
+
306
+ // remove all html tags
307
+ const getInnerText = (html) => (html || '').replaceAll(/<[^>]*>/g, '');
308
+
309
+ // remove all html tags except img, iframe and source tag for audio
310
+ const getContent = (html) => (html || '').replace(/(<(?!img|iframe|source)([^>]+)>)/gi, '');
311
+
312
+ export const validate = (model = {}, config = {}) => {
313
+ const errors = {};
314
+
315
+ ['teacherInstructions'].forEach((field) => {
316
+ if (config[field]?.required && !getContent(model[field])) {
317
+ errors[field] = 'This field is required.';
318
+ }
319
+ });
320
+
321
+ return errors;
322
+ };
@@ -0,0 +1,72 @@
1
+ // functions also used in src/utils-correctness.js
2
+
3
+ const getAllCorrectness = (answers, responses) =>
4
+ (answers || []).map((answer) => ({
5
+ ...answer,
6
+ isCorrect: ((responses[answer.containerIndex] && responses[answer.containerIndex].images) || []).includes(
7
+ answer.value,
8
+ ),
9
+ }));
10
+
11
+ const getValidAnswer = (answer, response) =>
12
+ ((response[answer.containerIndex] && response[answer.containerIndex].images) || []).filter(
13
+ (res) => res === answer.value,
14
+ );
15
+
16
+ export const getAllUniqueCorrectness = (answers, validResponses) => {
17
+ let allCorrectness = getAllCorrectness(answers, validResponses);
18
+
19
+ answers.forEach((answer1) => {
20
+ const valuesToParse = answers.filter(
21
+ (answer2) => answer2.value === answer1.value && answer2.containerIndex === answer1.containerIndex,
22
+ );
23
+
24
+ if (valuesToParse.length > 1) {
25
+ // point only to duplicates but first
26
+ valuesToParse.shift();
27
+ // mark duplicates as incorrect
28
+ valuesToParse.forEach((value, index) => {
29
+ allCorrectness = (allCorrectness || []).map((finalAnswer) => {
30
+ if (finalAnswer.id === value.id) {
31
+ let valid = getValidAnswer(finalAnswer, validResponses);
32
+ return {
33
+ ...finalAnswer,
34
+ isCorrect: valid.length > index + 1,
35
+ };
36
+ }
37
+ return finalAnswer;
38
+ });
39
+ });
40
+ }
41
+ });
42
+ return allCorrectness;
43
+ };
44
+
45
+ // calculate the minimum number of populated response areas (categories) in the correct answer or alternates
46
+ // and create an array with the possible responses ids
47
+ export const getCompleteResponseDetails = (validation, allChoices) => {
48
+ const extractImages = (response) => (response?.value || []).map((container) => container.images);
49
+ const countFilledResponseAreas = (container) => (container || []).filter((images) => images.length).length;
50
+
51
+ const { validResponse, altResponses } = validation || {};
52
+ const imagesPerContainer = extractImages(validResponse);
53
+ const possibleResponses = [imagesPerContainer.flat()];
54
+ let responseAreasToBeFilled = countFilledResponseAreas(imagesPerContainer);
55
+
56
+ (altResponses || []).forEach((altResponse) => {
57
+ const altImagesPerContainer = extractImages(altResponse);
58
+ const filledResponseAreas = countFilledResponseAreas(altImagesPerContainer);
59
+ possibleResponses.push(altImagesPerContainer.flat());
60
+
61
+ if (filledResponseAreas < responseAreasToBeFilled) {
62
+ responseAreasToBeFilled = filledResponseAreas;
63
+ }
64
+ });
65
+
66
+ // check if any correct answer have any unplaced answer choices
67
+ const hasUnplacedChoices = possibleResponses.some(
68
+ (response) => !allChoices.every((val) => response.includes(val)),
69
+ );
70
+
71
+ return { responseAreasToBeFilled, possibleResponses, hasUnplacedChoices };
72
+ };