@pie-element/image-cloze-association 8.0.1 → 8.2.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.
package/src/index.js CHANGED
@@ -15,7 +15,14 @@ export default class ImageClozeAssociation extends HTMLElement {
15
15
  }
16
16
 
17
17
  isComplete() {
18
- const { autoplayAudioEnabled, completeAudioEnabled } = this._model || {};
18
+ const {
19
+ autoplayAudioEnabled,
20
+ completeAudioEnabled,
21
+ completeResponses,
22
+ duplicateResponses,
23
+ maxResponsePerZone,
24
+ responseAreasToBeFilled,
25
+ } = this._model || {};
19
26
  const elementContext = this;
20
27
 
21
28
  // check audio completion if audio settings are enabled and audio actually exists
@@ -35,11 +42,41 @@ export default class ImageClozeAssociation extends HTMLElement {
35
42
  return false;
36
43
  }
37
44
 
38
- if (!Array.isArray(this._session.answers)) {
45
+ const { answers } = this._session;
46
+
47
+ if (!Array.isArray(answers)) {
39
48
  return false;
40
49
  }
41
50
 
42
- return Array.isArray(this._session.answers) && this._session.answers.length > 0;
51
+ // filter answers by containerIndex and count the ones with content
52
+ const filledResponseAreas = [...new Map(answers.map((item) => [item.containerIndex, item])).values()].length;
53
+ // check if an answer choice was added to at least as many response areas
54
+ // as the number of populated response areas in the correct answer
55
+ const areResponseAreasFilled = filledResponseAreas >= responseAreasToBeFilled;
56
+
57
+ if (maxResponsePerZone > 1) {
58
+ if (duplicateResponses) {
59
+ // an answer choice can be used multiple times
60
+ return areResponseAreasFilled;
61
+ }
62
+
63
+ const allAnswersValue = answers.map((answer) => answer.value);
64
+
65
+ // check if any correct answer have any unplaced answer choices
66
+ const requiredAnswersPlaced = completeResponses.some((response) =>
67
+ response.every((val) => allAnswersValue.includes(val)),
68
+ );
69
+
70
+ if (!requiredAnswersPlaced) {
71
+ // correct answer have unplaced answer choices
72
+ return areResponseAreasFilled;
73
+ }
74
+
75
+ // all choices (required for a correct response) were placed into a response area
76
+ return requiredAnswersPlaced;
77
+ }
78
+
79
+ return areResponseAreasFilled;
43
80
  }
44
81
 
45
82
  set session(s) {