UNPKG

quiz-interactions

Version:

A React UI component Library for quiz interaction types.

191 lines (156 loc) 6.65 kB
"use strict"; 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 _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get")); 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 _uiElements = require("@instructure/ui-elements"); var _interaction_slugs = require("../../interaction_slugs"); var _InteractionType2 = _interopRequireDefault(require("../InteractionType")); var _validationHelpers = require("../../util/validationHelpers"); var _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/lib/format-message")); var MatchingInteractionType = /*#__PURE__*/ function (_InteractionType) { (0, _inherits2.default)(MatchingInteractionType, _InteractionType); function MatchingInteractionType(obj) { var _this; (0, _classCallCheck2.default)(this, MatchingInteractionType); _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(MatchingInteractionType).call(this)); _this.slug = _interaction_slugs.MATCHING_SLUG; _this.translatedName = (0, _formatMessage.default)('Matching'); _this.defaultProperties = { shuffleRules: { questions: { shuffled: false } } }; _this.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; }; _this.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: ['', '', '', ''] }; }; _this.getRenderedResponse = function (responseValue, interactionData) { return _react.default.createElement(_uiElements.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)((0, _getPrototypeOf2.default)(MatchingInteractionType.prototype), "initializeProps", (0, _assertThisInitialized2.default)(_this)).call((0, _assertThisInitialized2.default)(_this), obj); return _this; } (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; } }]); return MatchingInteractionType; }(_InteractionType2.default); exports.default = MatchingInteractionType; MatchingInteractionType.validations = function () { var data = arguments.length > 0 && arguments[0] !== void 0 ? 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 }; };