@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
187 lines (183 loc) • 6.54 kB
JavaScript
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 { v4 as uuid } from 'uuid';
import findIndex from 'lodash/findIndex';
import { each, rule, onSelf } from 'instructure-validations';
import { Text } from '@instructure/ui-text';
import InteractionType from '../InteractionType';
import { FILL_BLANK_SLUG } from '../../interaction_slugs';
import { noDuplicatesMessage, presenceMessage } from '../../util/validationHelpers';
import t from '@instructure/quiz-i18n/es/format-message';
// ================
// ================
// HELPERS
// ================
// ================
function newId() {
return uuid();
}
// ================
// ================
// VALIDATIONS
// ================
// ================
function scoringDatumValidator(val) {
var editDistanceRules = [];
if (val.scoringAlgorithm === 'TextCloseEnough') {
editDistanceRules = [rule('numeric', {
message: t('Levenshtein Distance must be a number'),
minMessage: t('Levenshtein Distance must be greater than zero'),
maxMessage: t('Levenshtein Distance must be less than the length of the blank text'),
requireInteger: true,
integerMessage: t('Levenshtein Distance must be an integer'),
min: 1,
max: val.scoringData.blankText.length
})];
}
var textInChoicesRules = val.scoringAlgorithm === 'TextInChoices' ?
// each is for multiple specified correct answers
[each(rule('presence', {
message: presenceMessage(t('Correct value'))
})), onSelf(rule('listSize', {
minMessage: t('You must have an answer'),
min: 1,
allowNullOrUndefined: true
}))] : [onSelf(rule('presence', {
message: presenceMessage(t('Correct value'))
}))];
return {
scoringData: {
editDistance: editDistanceRules,
value: textInChoicesRules
}
};
}
// ================
// ================
// RECORD
// ================
// ================
var FillBlankInteractionType = /*#__PURE__*/function (_InteractionType) {
function FillBlankInteractionType(obj) {
var _this2;
_classCallCheck(this, FillBlankInteractionType);
_this2 = _callSuper(this, FillBlankInteractionType);
_defineProperty(_this2, "slug", FILL_BLANK_SLUG);
_defineProperty(_this2, "hideResultStem", true);
_defineProperty(_this2, "translatedName", t('Fill in the Blank'));
_defineProperty(_this2, "getDefaultScoringData", function () {
return {
value: []
};
});
_defineProperty(_this2, "getDefaultInteractionData", function () {
return {
stemItems: [{
id: newId(),
type: 'text',
value: '',
position: 1
}],
blanks: []
};
});
_defineProperty(_this2, "getRenderedResponse", function (responseValue) {
var interactionData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
stemItems: [],
blanks: []
};
return /*#__PURE__*/React.createElement(Text, {
color: "primary"
}, responseValue.map(function (value) {
var blankPosition = findIndex(interactionData.blanks, function (blank) {
return blank.id === value.id;
});
var blank = blankPosition >= 0 && interactionData.blanks[blankPosition];
var valueText = '';
if (blank && blank.answerType === 'openEntry') {
valueText = value.value;
} else if (blank) {
var blankChoice = blank.choices.find(function (choice) {
return choice.id === value.value;
});
valueText = blankChoice ? blankChoice.itemBody : '';
}
return t('Blank {blankPosition, number}: {valueText}', {
blankPosition: blankPosition >= 0 ? blankPosition + 1 : '',
valueText: valueText
});
}).join(', '));
});
_get((_this2, _getPrototypeOf(FillBlankInteractionType.prototype)), "initializeProps", _this2).call(_this2, obj);
return _this2;
}
_inherits(FillBlankInteractionType, _InteractionType);
return _createClass(FillBlankInteractionType, [{
key: "getDefaultUserResponse",
value: function getDefaultUserResponse() {
return {
value: []
};
}
}, {
key: "hasResponse",
value: function hasResponse(resp, intData) {
var numBlanks = intData.blanks.length;
var numWithResponse = resp.filter(function (blank) {
return blank.value;
}).length;
return numBlanks === numWithResponse;
}
}], [{
key: "validations",
value: function validations() {
var eachBlankValidator = {
choices: [onSelf(rule('listSize', {
minMessage: t('You must have more than one choice'),
min: 2,
allowNullOrUndefined: true
})), rule('noDuplicates', {
field: 'itemBody',
message: noDuplicatesMessage(t('Choice'))
}), each({
itemBody: [rule('presence', {
message: presenceMessage(t('Choice'))
})]
})]
};
var allBlanksValidator = onSelf(rule('listSize', {
minMessage: t('You must have a blank. e.g.: "Roses are `red`, violets are `blue`"'),
min: 1
}));
return {
interactionData: {
blanks: [allBlanksValidator, each(eachBlankValidator)]
},
scoringData: {
value: [each(scoringDatumValidator)]
}
};
}
}]);
}(InteractionType);
export { FillBlankInteractionType as default };