@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
146 lines (145 loc) • 5.66 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 { addValidator, each, rule, onSelf } from 'instructure-validations';
import { isScientificNotation } from '@instructure/quiz-scientific-notation';
import { Text } from '@instructure/ui-text';
import { FORMULA_SLUG } from '../../interaction_slugs';
import InteractionType from '../InteractionType';
import { formulaSyntaxError, variablesFromItemBody } from '../../util/formula';
import { numericMessage } from '../../util/validationHelpers';
import t from '@instructure/quiz-i18n/es/format-message';
var hasVariables = function hasVariables(itemBody) {
if (variablesFromItemBody(itemBody).length === 0) {
return t('Question Stem must contain variables');
}
};
var formulaSyntax = function formulaSyntax(formula, options) {
var variables = variablesFromItemBody(options.itemBody);
return formulaSyntaxError(variables, formula);
};
addValidator('hasVariables', hasVariables);
addValidator('formulaSyntax', formulaSyntax);
export var variableRules = function variableRules(variable) {
var minRule = rule('numeric', {
message: numericMessage(t('Min'))
});
var maxRule = rule('numeric', {
message: numericMessage(t('Maximum')),
min: variable.min,
minMessage: t('Must be larger than minimum value')
});
var scientificNotationRule = rule('scientificNotation', {
message: t('Must be in scientific notation')
});
// Don't allow just min or max in scientific notation: it's both, or neither
var min = isScientificNotation(variable.max) ? [scientificNotationRule, minRule] : [minRule];
var max = isScientificNotation(variable.min) ? [scientificNotationRule, maxRule] : [maxRule];
var precision = [rule('numeric', {
decMessage: t('Minimum value must have at least the same number of decimals as precision'),
type: 'decimals',
value: variable.min
}), rule('numeric', {
decMessage: t('Maximum value must have at least the same number of decimals as precision'),
type: 'decimals',
value: variable.max
})];
return {
min: min,
max: max,
precision: precision
};
};
var FormulaInteractionType = /*#__PURE__*/function (_InteractionType) {
function FormulaInteractionType(obj) {
var _this2;
_classCallCheck(this, FormulaInteractionType);
_this2 = _callSuper(this, FormulaInteractionType);
_defineProperty(_this2, "slug", FORMULA_SLUG);
_defineProperty(_this2, "translatedName", t('Formula'));
_defineProperty(_this2, "getDefaultScoringData", function (intData) {
return {
value: {
answerCount: '200',
formula: '',
generatedSolutions: [],
variables: [],
// TODO: set this from the UI, rather than being stuck with the default
numeric: {
type: 'marginOfError',
marginType: 'absolute',
margin: '0'
}
}
};
});
_defineProperty(_this2, "getDefaultInteractionData", function () {
return {};
});
_defineProperty(_this2, "getRenderedResponse", function (responseValue) {
return /*#__PURE__*/React.createElement(Text, {
color: "primary"
}, responseValue);
});
_get((_this2, _getPrototypeOf(FormulaInteractionType.prototype)), "initializeProps", _this2).call(_this2, obj);
return _this2;
}
_inherits(FormulaInteractionType, _InteractionType);
return _createClass(FormulaInteractionType, [{
key: "getDefaultUserResponse",
value: function getDefaultUserResponse() {
return {
value: ''
};
}
}]);
}(InteractionType);
_defineProperty(FormulaInteractionType, "validations", function () {
var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var numeric = obj.scoringData.value.numeric && obj.scoringData.value.numeric.type === 'marginOfError' ? {
margin: [rule('numeric', {
message: numericMessage(t('Margin')),
min: 0
})]
} : [];
return {
itemBody: [rule('presence', {
message: t('Question Stem cannot be blank')
}), rule('hasVariables')],
scoringData: {
value: {
variables: [each(variableRules)],
formula: [rule('presence', {
message: t('Formula cannot be blank')
}), rule('formulaSyntax', {
itemBody: obj.itemBody
})],
numeric: numeric,
generatedSolutions: [onSelf(rule('listSize', {
min: 1,
minMessage: t('Must have at least one solution')
}))]
}
}
};
});
export { FormulaInteractionType as default };