@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
161 lines • 5.9 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 get from 'lodash/get';
import times from 'lodash/times';
import random from 'lodash/random';
import { rule, each } from 'instructure-validations';
import { Text } from '@instructure/ui-text';
import { MATCHING_SLUG } from '../../interaction_slugs';
import InteractionType from '../InteractionType';
import { presenceMessage, noDuplicatesMessage } from '../../util/validationHelpers';
import t from '@instructure/quiz-i18n/es/format-message';
var MatchingInteractionType = /*#__PURE__*/function (_InteractionType) {
function MatchingInteractionType(obj) {
var _this2;
_classCallCheck(this, MatchingInteractionType);
_this2 = _callSuper(this, MatchingInteractionType);
_defineProperty(_this2, "slug", MATCHING_SLUG);
_defineProperty(_this2, "translatedName", t('Matching'));
_defineProperty(_this2, "defaultProperties", {
shuffleRules: {
questions: {
shuffled: false
}
}
});
_defineProperty(_this2, "getDefaultScoringData", function (intData) {
var matches = intData.questions.map(function (question, idx) {
return {
questionId: question.id,
questionBody: question.itemBody,
answerBody: intData.answers[idx]
};
});
var baseScoringData = {
value: {},
editData: {
matches: matches,
distractors: []
}
};
var defaultScoringData = intData.questions.reduce(function (scoringData, c) {
scoringData.value[c.id] = ''; // eslint-disable-line no-param-reassign
return scoringData;
}, baseScoringData);
return defaultScoringData;
});
_defineProperty(_this2, "getDefaultInteractionData", function () {
var newId = function newId() {
return random(100000).toString();
};
var _times = times(4, newId),
_times2 = _slicedToArray(_times, 4),
choiceIdA = _times2[0],
choiceIdB = _times2[1],
choiceIdC = _times2[2],
choiceIdD = _times2[3];
return {
questions: [{
id: choiceIdA,
itemBody: ''
}, {
id: choiceIdB,
itemBody: ''
}, {
id: choiceIdC,
itemBody: ''
}, {
id: choiceIdD,
itemBody: ''
}],
answers: ['', '', '', '']
};
});
_defineProperty(_this2, "getRenderedResponse", function (responseValue, interactionData) {
return /*#__PURE__*/React.createElement(Text, {
color: "primary"
}, Object.keys(responseValue).map(function (id) {
var questions = interactionData.questions;
var question = questions && questions.find(function (q) {
return q.id === id;
});
return "".concat(question ? question.itemBody : '', " - ").concat(responseValue[id]);
}).join(', '));
});
_get((_this2, _getPrototypeOf(MatchingInteractionType.prototype)), "initializeProps", _this2).call(_this2, obj);
return _this2;
}
_inherits(MatchingInteractionType, _InteractionType);
return _createClass(MatchingInteractionType, [{
key: "getDefaultUserResponse",
value: function getDefaultUserResponse() {
return {
value: {}
};
}
}, {
key: "hasResponse",
value: function hasResponse(resp, intData) {
var numQuestions = intData.questions.length;
var numWithResponse = Object.keys(resp).length;
return numQuestions === numWithResponse;
}
}]);
}(InteractionType);
_defineProperty(MatchingInteractionType, "validations", function () {
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var editData = get(data, 'scoringData.editData', {});
var matches = editData.matches || [];
var answers = matches.map(function (match) {
return match.answerBody;
});
var scoringData = {};
if (matches.length > 0) {
scoringData.editData = {
matches: [each({
questionBody: [rule('presence', {
message: presenceMessage(t('Question'))
})],
answerBody: [rule('presence', {
message: presenceMessage(t('Answer'))
})]
}), rule('noDuplicates', {
field: 'questionBody',
message: noDuplicatesMessage(t('Question'))
})],
distractors: [rule('noDuplicates', {
message: noDuplicatesMessage(t('Distractor'))
}), each(rule('presence', {
message: presenceMessage(t('Distractor'))
})), each(rule('noneOf', {
blacklist: answers,
message: t('Answer is already present')
}))]
};
}
return {
scoringData: scoringData
};
});
export { MatchingInteractionType as default };