@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
167 lines (166 loc) • 7.4 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _get3 = _interopRequireDefault(require("lodash/get"));
var _times3 = _interopRequireDefault(require("lodash/times"));
var _random = _interopRequireDefault(require("lodash/random"));
var _instructureValidations = require("instructure-validations");
var _uiText = require("@instructure/ui-text");
var _interaction_slugs = require("../../interaction_slugs");
var _InteractionType2 = _interopRequireDefault(require("../InteractionType"));
var _validationHelpers = require("../../util/validationHelpers");
var _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/es/format-message"));
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 = (0, _getPrototypeOf2["default"])(derived);
return (0, _possibleConstructorReturn2["default"])(_this, isNativeReflectConstruct() ? Reflect.construct(derived, args || [], (0, _getPrototypeOf2["default"])(_this).constructor) : derived.apply(_this, args));
}
var MatchingInteractionType = exports["default"] = /*#__PURE__*/function (_InteractionType) {
function MatchingInteractionType(obj) {
var _this2;
(0, _classCallCheck2["default"])(this, MatchingInteractionType);
_this2 = _callSuper(this, MatchingInteractionType);
(0, _defineProperty2["default"])(_this2, "slug", _interaction_slugs.MATCHING_SLUG);
(0, _defineProperty2["default"])(_this2, "translatedName", (0, _formatMessage["default"])('Matching'));
(0, _defineProperty2["default"])(_this2, "defaultProperties", {
shuffleRules: {
questions: {
shuffled: false
}
}
});
(0, _defineProperty2["default"])(_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;
});
(0, _defineProperty2["default"])(_this2, "getDefaultInteractionData", function () {
var newId = function newId() {
return (0, _random["default"])(100000).toString();
};
var _times = (0, _times3["default"])(4, newId),
_times2 = (0, _slicedToArray2["default"])(_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: ['', '', '', '']
};
});
(0, _defineProperty2["default"])(_this2, "getRenderedResponse", function (responseValue, interactionData) {
return /*#__PURE__*/_react["default"].createElement(_uiText.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(', '));
});
(0, _get2["default"])((_this2, (0, _getPrototypeOf2["default"])(MatchingInteractionType.prototype)), "initializeProps", _this2).call(_this2, obj);
return _this2;
}
(0, _inherits2["default"])(MatchingInteractionType, _InteractionType);
return (0, _createClass2["default"])(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;
}
}]);
}(_InteractionType2["default"]);
(0, _defineProperty2["default"])(MatchingInteractionType, "validations", function () {
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var editData = (0, _get3["default"])(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: [(0, _instructureValidations.each)({
questionBody: [(0, _instructureValidations.rule)('presence', {
message: (0, _validationHelpers.presenceMessage)((0, _formatMessage["default"])('Question'))
})],
answerBody: [(0, _instructureValidations.rule)('presence', {
message: (0, _validationHelpers.presenceMessage)((0, _formatMessage["default"])('Answer'))
})]
}), (0, _instructureValidations.rule)('noDuplicates', {
field: 'questionBody',
message: (0, _validationHelpers.noDuplicatesMessage)((0, _formatMessage["default"])('Question'))
})],
distractors: [(0, _instructureValidations.rule)('noDuplicates', {
message: (0, _validationHelpers.noDuplicatesMessage)((0, _formatMessage["default"])('Distractor'))
}), (0, _instructureValidations.each)((0, _instructureValidations.rule)('presence', {
message: (0, _validationHelpers.presenceMessage)((0, _formatMessage["default"])('Distractor'))
})), (0, _instructureValidations.each)((0, _instructureValidations.rule)('noneOf', {
blacklist: answers,
message: (0, _formatMessage["default"])('Answer is already present')
}))]
};
}
return {
scoringData: scoringData
};
});