@pie-element/charting 10.3.4-next.3 → 11.0.0-beta.1

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.
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
@@ -10,119 +9,82 @@ exports.model = model;
10
9
  exports.normalize = void 0;
11
10
  exports.outcome = outcome;
12
11
  exports.validate = exports.setCorrectness = void 0;
13
-
14
- var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
15
-
16
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
17
-
18
12
  var _debug = _interopRequireDefault(require("debug"));
19
-
20
13
  var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
21
-
22
14
  var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));
23
-
24
15
  var _pick = _interopRequireDefault(require("lodash/pick"));
25
-
26
16
  var _defaults = _interopRequireDefault(require("./defaults"));
27
-
28
17
  var _controllerUtils = require("@pie-lib/controller-utils");
29
-
30
- var _excluded = ["deletable"],
31
- _excluded2 = ["correctness"];
32
-
33
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
34
-
35
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
36
-
37
- var log = (0, _debug["default"])('@pie-element:graphing:controller');
38
-
39
- var lowerCase = function lowerCase(string) {
40
- return (string || '').toLowerCase();
41
- };
42
-
43
- var checkLabelsEquality = function checkLabelsEquality(givenAnswerLabel, correctAnswerLabel) {
44
- return lowerCase(givenAnswerLabel) === lowerCase(correctAnswerLabel);
45
- };
46
-
18
+ const log = (0, _debug.default)('@pie-element:graphing:controller');
19
+ const lowerCase = string => (string || '').toLowerCase();
20
+ const checkLabelsEquality = (givenAnswerLabel, correctAnswerLabel) => lowerCase(givenAnswerLabel) === lowerCase(correctAnswerLabel);
47
21
  exports.checkLabelsEquality = checkLabelsEquality;
48
-
49
- var setCorrectness = function setCorrectness(answers, partialScoring) {
50
- return answers ? answers.map(function (answer) {
51
- return _objectSpread(_objectSpread({}, answer), {}, {
52
- correctness: {
53
- value: partialScoring ? 'incorrect' : 'correct',
54
- label: partialScoring ? 'incorrect' : 'correct'
55
- }
56
- });
57
- }) : [];
58
- };
59
-
22
+ const setCorrectness = (answers, partialScoring) => answers ? answers.map(answer => ({
23
+ ...answer,
24
+ correctness: {
25
+ value: partialScoring ? 'incorrect' : 'correct',
26
+ label: partialScoring ? 'incorrect' : 'correct'
27
+ }
28
+ })) : [];
60
29
  exports.setCorrectness = setCorrectness;
61
-
62
- var normalize = function normalize(question) {
63
- return _objectSpread(_objectSpread({}, _defaults["default"]), question);
64
- };
65
-
30
+ const normalize = question => ({
31
+ ..._defaults.default,
32
+ ...question
33
+ });
66
34
  exports.normalize = normalize;
67
-
68
- var getScore = function getScore(question, session) {
69
- var env = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
70
- var correctAnswer = question.correctAnswer,
71
- _question$data = question.data,
72
- initialData = _question$data === void 0 ? [] : _question$data,
73
- scoringType = question.scoringType;
74
- var correctResponses = [];
75
-
76
- var isPartialScoring = _controllerUtils.partialScoring.enabled({
35
+ const getScore = (question, session, env = {}) => {
36
+ const {
37
+ correctAnswer,
38
+ data: initialData = [],
39
+ scoringType
40
+ } = question;
41
+ let correctResponses = [];
42
+ const isPartialScoring = _controllerUtils.partialScoring.enabled({
77
43
  partialScoring: scoringType !== undefined ? scoringType === 'partial scoring' : scoringType
78
44
  }, env);
79
-
80
- var _ref = correctAnswer || {},
81
- _ref$data = _ref.data,
82
- correctAnswers = _ref$data === void 0 ? [] : _ref$data;
83
-
84
- var defaultAnswers = filterCategories(initialData);
85
- var answers = setCorrectness(session && session.answer || defaultAnswers, isPartialScoring);
86
- var result = 0;
87
-
45
+ const {
46
+ data: correctAnswers = []
47
+ } = correctAnswer || {};
48
+ const defaultAnswers = filterCategories(initialData);
49
+ let answers = setCorrectness(session && session.answer || defaultAnswers, isPartialScoring);
50
+ let result = 0;
88
51
  if (isPartialScoring) {
89
52
  // if score type is "partial scoring"
90
53
  // maxScore is calculated based on the correct response
91
54
  // score is calculated based on the given response
92
- var maxScore = 0;
93
- var _score = 0;
94
-
95
- var scoreForLabelAndValueEditable = function scoreForLabelAndValueEditable(answer, corrAnswer) {
96
- var value = answer.value,
97
- label = answer.label,
98
- index = answer.index;
99
- var valueIsCorrect = value === corrAnswer.value;
100
- var labelIsCorrect = checkLabelsEquality(label, corrAnswer.label);
55
+ let maxScore = 0;
56
+ let score = 0;
57
+ const scoreForLabelAndValueEditable = (answer, corrAnswer) => {
58
+ const {
59
+ value,
60
+ label,
61
+ index
62
+ } = answer;
63
+ const valueIsCorrect = value === corrAnswer.value;
64
+ const labelIsCorrect = checkLabelsEquality(label, corrAnswer.label);
101
65
  maxScore += 2;
102
-
103
66
  if (valueIsCorrect) {
104
- _score += 1;
67
+ score += 1;
105
68
  answer.correctness.value = 'correct';
106
69
  }
107
-
108
70
  if (labelIsCorrect) {
109
- _score += 1;
71
+ score += 1;
110
72
  answer.correctness.label = 'correct';
111
73
  }
112
-
113
74
  if (valueIsCorrect && labelIsCorrect) {
114
75
  correctResponses.push({
115
76
  label: label,
116
77
  index: index
117
78
  });
118
79
  }
119
- }; // if given answer has more categories than the correct answers, the "extra" will be ignored
120
-
80
+ };
121
81
 
122
- correctAnswers.forEach(function (corrAnswer, index) {
123
- var defaultAnswer = defaultAnswers[index];
124
- var answer = answers[index]; // if there is a corresponding category at the same position in the given answer
82
+ // if given answer has more categories than the correct answers, the "extra" will be ignored
83
+ correctAnswers.forEach((corrAnswer, index) => {
84
+ const defaultAnswer = defaultAnswers[index];
85
+ const answer = answers[index];
125
86
 
87
+ // if there is a corresponding category at the same position in the given answer
126
88
  if (answer) {
127
89
  // if there is a corresponding category at the same position in the default answer
128
90
  if (defaultAnswer) {
@@ -130,17 +92,17 @@ var getScore = function getScore(question, session) {
130
92
  // it means that this category values only one point (only the value can be changed)
131
93
  if (!defaultAnswer.editable && answer.interactive) {
132
94
  maxScore += 1;
133
-
134
95
  if (answer.value === corrAnswer.value) {
135
- _score += 1;
96
+ score += 1;
136
97
  answer.correctness.value = 'correct';
137
98
  correctResponses.push({
138
99
  label: answer.label,
139
100
  index: index
140
101
  });
141
102
  }
103
+ answer.correctness.label = 'correct';
142
104
 
143
- answer.correctness.label = 'correct'; // if category's label (in default answer) was editable
105
+ // if category's label (in default answer) was editable
144
106
  // it means that this category values 2 points (both label and value can be changed)
145
107
  } else if (defaultAnswer.editable && answer.interactive) {
146
108
  scoreForLabelAndValueEditable(answer, corrAnswer);
@@ -162,131 +124,120 @@ var getScore = function getScore(question, session) {
162
124
  maxScore += 2;
163
125
  }
164
126
  });
165
- result = maxScore ? _score / maxScore : 0;
127
+ result = maxScore ? score / maxScore : 0;
166
128
  } else {
167
129
  // all-or-nothing scoring: overall score is 1 only if lengths and all values/labels match
168
- result = correctAnswers.length === answers.length ? 1 : 0; // regardless of overall result, mark each answer individually for user feedback
169
-
170
- answers = answers.map(function (answer, index) {
171
- var correctAnswer = correctAnswers[index];
172
- var valueIsCorrect = correctAnswer ? answer.value === correctAnswer.value : false;
173
- var labelIsCorrect = correctAnswer ? lowerCase(answer.label) === lowerCase(correctAnswer.label) : false;
130
+ result = correctAnswers.length === answers.length ? 1 : 0;
174
131
 
132
+ // regardless of overall result, mark each answer individually for user feedback
133
+ answers = answers.map((answer, index) => {
134
+ const correctAnswer = correctAnswers[index];
135
+ const valueIsCorrect = correctAnswer ? answer.value === correctAnswer.value : false;
136
+ const labelIsCorrect = correctAnswer ? lowerCase(answer.label) === lowerCase(correctAnswer.label) : false;
175
137
  if (!valueIsCorrect || !labelIsCorrect) {
176
138
  result = 0;
177
139
  }
178
-
179
140
  if (valueIsCorrect && labelIsCorrect) {
180
141
  correctResponses.push({
181
142
  label: answer.label,
182
- index: index
143
+ index
183
144
  });
184
145
  }
185
-
186
- return _objectSpread(_objectSpread({}, answer), {}, {
146
+ return {
147
+ ...answer,
187
148
  correctness: {
188
149
  value: valueIsCorrect ? 'correct' : 'incorrect',
189
150
  label: labelIsCorrect ? 'correct' : 'incorrect'
190
151
  }
191
- });
152
+ };
192
153
  });
193
154
  }
194
-
195
- var score = {
155
+ const score = {
196
156
  score: parseFloat(result.toFixed(2)),
197
- answers: answers
157
+ answers
198
158
  };
199
-
200
159
  if (env.extraProps && env.extraProps.correctResponseEnabled) {
201
160
  score.correctResponses = correctResponses;
202
161
  }
203
-
204
162
  return score;
205
- }; // eslint-disable-next-line no-unused-vars
206
-
207
-
208
- exports.getScore = getScore;
209
-
210
- var filterCategories = function filterCategories(categories) {
211
- return categories ? categories.map(function (_ref2) {
212
- var deletable = _ref2.deletable,
213
- rest = (0, _objectWithoutProperties2["default"])(_ref2, _excluded);
214
- return rest;
215
- }) : [];
216
163
  };
217
164
 
165
+ // eslint-disable-next-line no-unused-vars
166
+ exports.getScore = getScore;
167
+ const filterCategories = categories => categories ? categories.map(({
168
+ deletable,
169
+ ...rest
170
+ }) => rest) : [];
218
171
  exports.filterCategories = filterCategories;
219
-
220
172
  function model(question, session, env) {
221
- return new Promise(function (resolve) {
222
- var normalizedQuestion = normalize(question);
223
- var addCategoryEnabled = normalizedQuestion.addCategoryEnabled,
224
- chartType = normalizedQuestion.chartType,
225
- data = normalizedQuestion.data,
226
- domain = normalizedQuestion.domain,
227
- graph = normalizedQuestion.graph,
228
- prompt = normalizedQuestion.prompt,
229
- promptEnabled = normalizedQuestion.promptEnabled,
230
- range = normalizedQuestion.range,
231
- rationale = normalizedQuestion.rationale,
232
- title = normalizedQuestion.title,
233
- rationaleEnabled = normalizedQuestion.rationaleEnabled,
234
- teacherInstructions = normalizedQuestion.teacherInstructions,
235
- teacherInstructionsEnabled = normalizedQuestion.teacherInstructionsEnabled,
236
- correctAnswer = normalizedQuestion.correctAnswer,
237
- scoringType = normalizedQuestion.scoringType,
238
- studentNewCategoryDefaultLabel = normalizedQuestion.studentNewCategoryDefaultLabel,
239
- language = normalizedQuestion.language,
240
- extraCSSRules = normalizedQuestion.extraCSSRules;
241
- var correctInfo = {
173
+ return new Promise(resolve => {
174
+ const normalizedQuestion = normalize(question);
175
+ const {
176
+ addCategoryEnabled,
177
+ chartType,
178
+ data,
179
+ domain,
180
+ graph,
181
+ prompt,
182
+ promptEnabled,
183
+ range,
184
+ rationale,
185
+ title,
186
+ rationaleEnabled,
187
+ teacherInstructions,
188
+ teacherInstructionsEnabled,
189
+ correctAnswer,
190
+ scoringType,
191
+ studentNewCategoryDefaultLabel,
192
+ language,
193
+ extraCSSRules
194
+ } = normalizedQuestion;
195
+ const correctInfo = {
242
196
  correctness: 'incorrect',
243
197
  score: '0%'
244
198
  };
245
- var base = {
246
- addCategoryEnabled: addCategoryEnabled,
247
- chartType: chartType,
199
+ const base = {
200
+ addCategoryEnabled,
201
+ chartType,
248
202
  data: filterCategories(data),
249
- domain: domain,
250
- graph: graph,
203
+ domain,
204
+ graph,
251
205
  prompt: promptEnabled ? prompt : null,
252
- range: range,
253
- rationale: rationale,
254
- title: title,
206
+ range,
207
+ rationale,
208
+ title,
255
209
  size: graph,
256
210
  showToggle: false,
257
211
  correctness: correctInfo,
258
212
  disabled: env.mode !== 'gather',
259
- scoringType: scoringType,
260
- studentNewCategoryDefaultLabel: studentNewCategoryDefaultLabel,
261
- language: language,
262
- env: env,
263
- extraCSSRules: extraCSSRules
213
+ scoringType,
214
+ studentNewCategoryDefaultLabel,
215
+ language,
216
+ env,
217
+ extraCSSRules
264
218
  };
265
- var scoreObject = getScore(normalizedQuestion, session, env);
266
- var answers = filterCategories(scoreObject.answers);
267
-
219
+ const scoreObject = getScore(normalizedQuestion, session, env);
220
+ const answers = filterCategories(scoreObject.answers);
268
221
  if (env.mode === 'view') {
269
222
  // eslint-disable-next-line no-unused-vars
270
- base.correctedAnswer = answers.map(function (_ref3) {
271
- var correctness = _ref3.correctness,
272
- rest = (0, _objectWithoutProperties2["default"])(_ref3, _excluded2);
273
- return _objectSpread(_objectSpread({}, rest), {}, {
223
+ base.correctedAnswer = answers.map(({
224
+ correctness,
225
+ ...rest
226
+ }) => {
227
+ return {
228
+ ...rest,
274
229
  interactive: false
275
- });
230
+ };
276
231
  });
277
232
  base.addCategoryEnabled = false;
278
233
  }
279
-
280
234
  if (env.mode === 'evaluate') {
281
- var _correctAnswer$data;
282
-
283
235
  base.correctedAnswer = answers;
284
236
  base.correctAnswer = correctAnswer;
285
- base.showToggle = !!(correctAnswer !== null && correctAnswer !== void 0 && (_correctAnswer$data = correctAnswer.data) !== null && _correctAnswer$data !== void 0 && _correctAnswer$data.length) && scoreObject.score !== 1;
237
+ base.showToggle = !!correctAnswer?.data?.length && scoreObject.score !== 1;
286
238
  base.addCategoryEnabled = false;
287
239
  base.showKeyLegend = true;
288
240
  }
289
-
290
241
  if (env.role === 'instructor' && (env.mode === 'view' || env.mode === 'evaluate')) {
291
242
  base.rationale = rationaleEnabled ? rationale : null;
292
243
  base.teacherInstructions = teacherInstructionsEnabled ? teacherInstructions : null;
@@ -294,43 +245,45 @@ function model(question, session, env) {
294
245
  base.rationale = null;
295
246
  base.teacherInstructions = null;
296
247
  }
297
-
298
248
  log('base: ', base);
299
249
  resolve(base);
300
250
  });
301
251
  }
302
-
303
252
  function outcome(model, session, env) {
304
- return new Promise(function (resolve) {
305
- var scoreObject = getScore(model, session, env);
306
- var result = {
253
+ return new Promise(resolve => {
254
+ const scoreObject = getScore(model, session, env);
255
+ const result = {
307
256
  score: scoreObject.score,
308
- empty: !session || (0, _isEmpty["default"])(session)
257
+ empty: !session || (0, _isEmpty.default)(session)
309
258
  };
310
-
311
259
  if (env.extraProps && env.extraProps.correctResponseEnabled) {
312
260
  result.extraProps = {
313
261
  correctResponse: scoreObject.correctResponses
314
262
  };
315
263
  }
316
-
317
264
  resolve(result);
318
265
  });
319
266
  }
320
-
321
- var createCorrectResponseSession = function createCorrectResponseSession(question, env) {
322
- return new Promise(function (resolve) {
267
+ const createCorrectResponseSession = (question, env) => {
268
+ return new Promise(resolve => {
323
269
  if (env.mode !== 'evaluate' && env.role === 'instructor') {
324
- var correctAnswer = question.correctAnswer;
325
- var answers = correctAnswer && correctAnswer.data; // for IBX preview mode
270
+ const {
271
+ correctAnswer
272
+ } = question;
273
+ let answers = correctAnswer && correctAnswer.data;
326
274
 
275
+ // for IBX preview mode
327
276
  if (env.mode === 'gather') {
328
- var data = question.data;
329
- answers = (correctAnswer && correctAnswer.data || []).map(function (answer, index) {
330
- return _objectSpread(_objectSpread({}, data[index]), answer);
277
+ const {
278
+ data
279
+ } = question;
280
+ answers = (correctAnswer && correctAnswer.data || []).map((answer, index) => {
281
+ return {
282
+ ...data[index],
283
+ ...answer
284
+ };
331
285
  });
332
286
  }
333
-
334
287
  resolve({
335
288
  answer: answers,
336
289
  id: '1'
@@ -339,78 +292,56 @@ var createCorrectResponseSession = function createCorrectResponseSession(questio
339
292
  return resolve(null);
340
293
  }
341
294
  });
342
- }; // remove all html tags
343
-
344
-
345
- exports.createCorrectResponseSession = createCorrectResponseSession;
346
-
347
- var getInnerText = function getInnerText(html) {
348
- return (html || '').replaceAll(/<[^>]*>/g, '');
349
- }; // remove all html tags except img, iframe and source tag for audio
350
-
351
-
352
- var getContent = function getContent(html) {
353
- return (html || '').replace(/(<(?!img|iframe|source)([^>]+)>)/gi, '');
354
295
  };
355
296
 
356
- var validate = function validate() {
357
- var model = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
358
- var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
359
-
360
- var _ref4 = model || {},
361
- correctAnswer = _ref4.correctAnswer,
362
- data = _ref4.data;
363
-
364
- var _ref5 = correctAnswer || {},
365
- correctData = _ref5.data;
366
-
367
- var categories = correctData || [];
368
- var errors = {};
369
- var correctAnswerErrors = {};
370
- var categoryErrors = {};
371
- ['teacherInstructions', 'prompt', 'rationale'].forEach(function (field) {
372
- var _config$field;
373
-
374
- if ((_config$field = config[field]) !== null && _config$field !== void 0 && _config$field.required && !getContent(model[field])) {
297
+ // remove all html tags
298
+ exports.createCorrectResponseSession = createCorrectResponseSession;
299
+ const getInnerText = html => (html || '').replaceAll(/<[^>]*>/g, '');
300
+
301
+ // remove all html tags except img, iframe and source tag for audio
302
+ const getContent = html => (html || '').replace(/(<(?!img|iframe|source)([^>]+)>)/gi, '');
303
+ const validate = (model = {}, config = {}) => {
304
+ const {
305
+ correctAnswer,
306
+ data
307
+ } = model || {};
308
+ const {
309
+ data: correctData
310
+ } = correctAnswer || {};
311
+ const categories = correctData || [];
312
+ const errors = {};
313
+ const correctAnswerErrors = {};
314
+ const categoryErrors = {};
315
+ ['teacherInstructions', 'prompt', 'rationale'].forEach(field => {
316
+ if (config[field]?.required && !getContent(model[field])) {
375
317
  errors[field] = 'This field is required.';
376
318
  }
377
319
  });
378
- categories.forEach(function (category, index) {
379
- var label = category.label;
380
-
320
+ categories.forEach((category, index) => {
321
+ const {
322
+ label
323
+ } = category;
381
324
  if (!getInnerText(label)) {
382
325
  categoryErrors[index] = 'Content should not be empty. ';
383
326
  } else {
384
- var identicalAnswer = categories.some(function (c, i) {
385
- return c.label === label && index !== i;
386
- });
387
-
327
+ const identicalAnswer = categories.some((c, i) => c.label === label && index !== i);
388
328
  if (identicalAnswer) {
389
329
  categoryErrors[index] = 'Category names should be unique. ';
390
330
  }
391
331
  }
392
332
  });
393
-
394
333
  if (categories.length < 1 || categories.length > 20) {
395
334
  correctAnswerErrors.categoriesError = 'The correct answer should include between 1 and 20 categories.';
396
- } else if ((0, _isEqual["default"])(data.map(function (category) {
397
- return (0, _pick["default"])(category, 'value', 'label');
398
- }), correctData.map(function (category) {
399
- return (0, _pick["default"])(category, 'value', 'label');
400
- }))) {
335
+ } else if ((0, _isEqual.default)(data.map(category => (0, _pick.default)(category, 'value', 'label')), correctData.map(category => (0, _pick.default)(category, 'value', 'label')))) {
401
336
  correctAnswerErrors.identicalError = 'Correct answer should not be identical to the chart’s initial state';
402
337
  }
403
-
404
- if (!(0, _isEmpty["default"])(categoryErrors)) {
338
+ if (!(0, _isEmpty.default)(categoryErrors)) {
405
339
  errors.categoryErrors = categoryErrors;
406
340
  }
407
-
408
- if (!(0, _isEmpty["default"])(correctAnswerErrors)) {
341
+ if (!(0, _isEmpty.default)(correctAnswerErrors)) {
409
342
  errors.correctAnswerErrors = correctAnswerErrors;
410
343
  }
411
-
412
344
  return errors;
413
345
  };
414
-
415
346
  exports.validate = validate;
416
347
  //# sourceMappingURL=index.js.map