@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
193 lines (190 loc) • 7.95 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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
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 findIndex from 'lodash/findIndex';
import { each, rule, onSelf } from 'instructure-validations';
import { Text } from '@instructure/ui-text';
import InteractionType from '../InteractionType';
import { RICH_FILL_BLANK_SLUG } from '../../interaction_slugs';
import { noDuplicatesMessage, presenceMessage } from '../../util/validationHelpers';
import t from '@instructure/quiz-i18n/es/format-message';
// ================
// ================
// 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 RichFillBlankInteractionType = /*#__PURE__*/function (_InteractionType) {
function RichFillBlankInteractionType(obj) {
var _this2;
_classCallCheck(this, RichFillBlankInteractionType);
_this2 = _callSuper(this, RichFillBlankInteractionType);
_defineProperty(_this2, "slug", RICH_FILL_BLANK_SLUG);
_defineProperty(_this2, "hideResultStem", true);
_defineProperty(_this2, "translatedName", t('Fill in the Blank'));
_defineProperty(_this2, "getDefaultScoringData", function () {
return {
workingItemBody: '',
value: []
};
});
_defineProperty(_this2, "getDefaultInteractionData", function () {
return {
blanks: []
};
});
_defineProperty(_this2, "getRenderedResponse", function (responseValue) {
var interactionData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
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.choices) {
var blankChoice = blank.choices.find(function (choice) {
return choice.id === value.value;
});
valueText = blankChoice ? blankChoice.itemBody : '';
} else if (blank) {
valueText = value.value;
}
return t('Blank {blankPosition, number}: {valueText}', {
blankPosition: blankPosition >= 0 ? blankPosition + 1 : '',
valueText: valueText
});
}).join(', '));
});
_get((_this2, _getPrototypeOf(RichFillBlankInteractionType.prototype)), "initializeProps", _this2).call(_this2, obj);
return _this2;
}
_inherits(RichFillBlankInteractionType, _InteractionType);
return _createClass(RichFillBlankInteractionType, [{
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: "baseValidations",
value: function baseValidations() {
// This overrides the need for itemBody to exist (it gets derived on quiz_api from workingItemBody).
// Instead we validate that workingItemBody exists within scoringData.
return {};
}
}]);
}(InteractionType);
_defineProperty(RichFillBlankInteractionType, "validations", function () {
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
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 eachWordBankChoiceValidator = {
itemBody: [rule('presence', {
message: presenceMessage(t('Distractor'))
})]
};
var allBlanksValidator = onSelf(rule('listSize', {
minMessage: t('You must have a blank. e.g.: "Roses are `red`, violets are `blue`"'),
min: 1
}));
var allWordBanksValidator = onSelf(rule('listSize', {
minMessage: t('The word bank must have more than one item'),
min: 2
}));
return {
interactionData: _objectSpread({
blanks: [allBlanksValidator, each(eachBlankValidator)]
}, data.interactionData.wordBankChoices && {
wordBankChoices: [allWordBanksValidator, each(eachWordBankChoiceValidator)]
}),
scoringData: {
workingItemBody: [rule('presence', {
message: presenceMessage(t('Question Stem'))
})],
value: [each(scoringDatumValidator)]
}
};
});
export { RichFillBlankInteractionType as default };