@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
162 lines • 6.22 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _get from "@babel/runtime/helpers/esm/get";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
function _callSuper(_this, derived, args) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
return !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
} catch (e) {
return false;
}
}
derived = _getPrototypeOf(derived);
return _possibleConstructorReturn(_this, isNativeReflectConstruct() ? Reflect.construct(derived, args || [], _getPrototypeOf(_this).constructor) : derived.apply(_this, args));
}
import React from 'react';
import times from 'lodash/times';
import { v4 as uuid } from 'uuid';
import { rule, each, onSelf } from 'instructure-validations';
import { Text } from '@instructure/ui-text';
import { CATEGORIZATION_SLUG } from '../../interaction_slugs';
import InteractionType from '../InteractionType';
import { presenceMessage, noDuplicatesMessage } from '../../util/validationHelpers';
import t from '@instructure/quiz-i18n/es/format-message';
var CategorizationInteractionType = /*#__PURE__*/function (_InteractionType) {
function CategorizationInteractionType(obj) {
var _this2;
_classCallCheck(this, CategorizationInteractionType);
_this2 = _callSuper(this, CategorizationInteractionType);
_defineProperty(_this2, "slug", CATEGORIZATION_SLUG);
_defineProperty(_this2, "translatedName", t('Categorization'));
_defineProperty(_this2, "defaultProperties", {});
_defineProperty(_this2, "getDefaultScoringData", function (intData) {
var i = 0;
var categories = Object.keys(intData.categories).map(function (uuid) {
var matchingDistractorId = Object.keys(intData.distractors)[i];
i++;
return {
id: uuid,
scoringAlgorithm: 'AllOrNothing',
scoringData: {
value: [matchingDistractorId]
}
};
});
return {
score_method: 'all_or_nothing',
// snake case for backend
value: categories
};
});
_defineProperty(_this2, "getDefaultInteractionData", function () {
var _times = times(4, uuid),
_times2 = _slicedToArray(_times, 4),
categoryIdA = _times2[0],
categoryIdB = _times2[1],
distractorIdA = _times2[2],
distractorIdB = _times2[3];
return {
categoryOrder: [categoryIdA, categoryIdB],
categories: _defineProperty(_defineProperty({}, categoryIdA, {
id: categoryIdA,
itemBody: ''
}), categoryIdB, {
id: categoryIdB,
itemBody: ''
}),
distractors: _defineProperty(_defineProperty({}, distractorIdA, {
id: distractorIdA,
itemBody: ''
}), distractorIdB, {
id: distractorIdB,
itemBody: ''
})
};
});
_defineProperty(_this2, "getRenderedResponse", function (responseValue, interactionData) {
var distractors = interactionData.distractors;
return /*#__PURE__*/React.createElement(Text, {
color: "primary"
}, responseValue.map(function (category) {
var c = interactionData.categories[category.id];
var categoryValue = c ? c.itemBody : '';
var categoryDistractors = category.value.map(function (distractorId) {
var distractorValue = distractors[distractorId];
return distractorValue ? distractorValue.itemBody : '';
}).join(', ');
return t('Category: {categoryValue} | Distractors: {categoryDistractors}', {
categoryValue: categoryValue,
categoryDistractors: categoryDistractors
});
}).join(', '));
});
_get((_this2, _getPrototypeOf(CategorizationInteractionType.prototype)), "initializeProps", _this2).call(_this2, obj);
return _this2;
}
_inherits(CategorizationInteractionType, _InteractionType);
return _createClass(CategorizationInteractionType, [{
key: "getDefaultUserResponse",
value: function getDefaultUserResponse(intData) {
var categories = Object.keys(intData.categories).map(function (uuid) {
return {
id: uuid,
value: [],
type: 'ArrayText'
};
});
return {
value: categories
};
}
}, {
key: "hasResponse",
value: function hasResponse(resp) {
return resp.some(function (category) {
return category.value.length > 0;
});
}
}]);
}(InteractionType);
_defineProperty(CategorizationInteractionType, "useNewValidations", true);
_defineProperty(CategorizationInteractionType, "validations", function (data) {
var scoringDataValue = data.scoringData.value;
var categoryAnswers = scoringDataValue.reduce(function (list, v) {
return list.concat(v.scoringData.value || []);
}, []);
return {
interactionData: {
categories: [each({
itemBody: [rule('presence', {
message: presenceMessage(t('Category'))
})]
}), rule('noDuplicates', {
field: 'itemBody',
message: noDuplicatesMessage(t('Category'))
})],
distractors: [each({
itemBody: [rule('presence', {
message: presenceMessage(t('Distractor'))
})]
}), rule('noDuplicates', {
field: 'itemBody',
message: noDuplicatesMessage(t('Distractor'))
})]
},
scoringData: {
value: [onSelf(rule('listSize', {
listOverride: categoryAnswers,
min: 1,
minMessage: t('You must have at least one answer in a category')
}))]
}
};
});
export { CategorizationInteractionType as default };