UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

403 lines (400 loc) • 12.7 kB
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 _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; var _dec, _class, _FillBlankExample; 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)); } /** @jsx jsx */ /* eslint-disable react/jsx-no-literals */ import { Component } from 'react'; import PropTypes from 'prop-types'; import { cloneDeep } from 'lodash'; import { Heading } from '@instructure/ui-heading'; import { jsx } from '@instructure/emotion'; import find from 'lodash/find'; import FillBlankEdit from '../Edit'; import FillBlankShow from '../Show'; import FillBlankTake from '../Take'; import FillBlankResult from '../Result'; import generateStyle from '../../common/example/default'; import generateComponentTheme from '../../common/example/defaultTheme'; import { withStyleOverrides } from '@instructure/quiz-common'; /** --- category: FillInTheBlank --- FillInTheBlank Example. ```jsx_example const WrappedExample = DragDropContext(HTML5Backend)(FillBlankExample); <SettingsSwitcher locales={LOCALES}> <WrappedExample /> </SettingsSwitcher> ``` **/ var FillBlankExample = (_dec = withStyleOverrides(generateStyle, generateComponentTheme), _dec(_class = (_FillBlankExample = /*#__PURE__*/function (_Component) { function FillBlankExample(props) { var _this2; _classCallCheck(this, FillBlankExample); _this2 = _callSuper(this, FillBlankExample); _defineProperty(_this2, "handleChangeItemState", function (itemState) { var newState = {}; if (itemState.interactionData) { newState.takeInteractionData = _this2.transformTakeInteractionData(itemState.interactionData); } var updatedState = Object.assign({}, itemState, newState); _this2.setState(updatedState); }); _defineProperty(_this2, "handleResponseUpdate", function (userResponse) { _this2.setState({ userResponse: { value: userResponse } }); }); _this2.state = {}; if (props.useDefaultData) { Object.assign(_this2.state, { interactionData: { stemItems: [{ id: 'stem_uuid-1', position: 1, type: 'text', value: ' ' }, { id: 'stem_uuid0', position: 2, type: 'blank', blankId: 'fitb_uuid1' }, { id: 'stem_uuid1', position: 3, type: 'text', value: ' Columbus sailed in ' }, { id: 'stem_uuid2', position: 4, type: 'blank', blankId: 'fitb_uuid2' }, { id: 'stem_uuid3', position: 5, type: 'text', value: ' from the country of ' }, { id: 'stem_uuid4', position: 6, type: 'blank', blankId: 'fitb_uuid3' }, { id: 'stem_uuid5', position: 7, type: 'text', value: ' on the continent of ' }, { id: 'stem_uuid6', position: 8, type: 'blank', blankId: 'fitb_uuid4' }, { id: 'stem_uuid7', position: 9, type: 'text', value: ' across the ' }, { id: 'stem_uuid8', position: 10, type: 'blank', blankId: 'fitb_uuid5' }, { id: 'stem_uuid9', position: 11, type: 'text', value: ' and found ' }, { id: 'stem_uuid10', position: 12, type: 'blank', blankId: 'fitb_uuid6' }, { id: 'stem_uuid11', position: 13, type: 'text', value: ' . Where the people were ' }, { id: 'stem_uuid12', position: 14, type: 'blank', blankId: 'fitb_uuid7' }, { id: 'stem_uuid13', position: 15, type: 'text', value: ' . ' }], blanks: [{ id: 'fitb_uuid1', answerType: 'openEntry' }, { id: 'fitb_uuid2', answerType: 'openEntry' }, { id: 'fitb_uuid3', answerType: 'openEntry' }, { id: 'fitb_uuid4', answerType: 'openEntry' }, { id: 'fitb_uuid5', answerType: 'openEntry' }, { id: 'fitb_uuid6', choices: [{ id: 'choice_uuid11_brazil', position: 1, itemBody: 'Brazil' }, { id: 'choice_uuid12_austria', position: 2, itemBody: 'Austria' }, { id: 'choice_uuid13_america', position: 3, itemBody: 'America' }], answerType: 'wordbank' }, { id: 'fitb_uuid7', choices: [{ id: 'choice_uuid11_peaceful', position: 1, itemBody: 'peaceful' }, { id: 'choice_uuid12_war-torn', position: 2, itemBody: 'war-torn' }, { id: 'choice_uuid13_confused', position: 3, itemBody: 'confused' }], answerType: 'dropdown' }] }, scoringData: { value: [{ id: 'fitb_uuid1', scoringAlgorithm: 'TextRegex', scoringData: { value: '.*Christopher', blankText: 'Christopher' } }, { id: 'fitb_uuid2', scoringAlgorithm: 'TextCloseEnough', scoringData: { value: '1492', editDistance: 1, blankText: '1492' } }, { id: 'fitb_uuid3', scoringAlgorithm: 'TextInChoices', scoringData: { value: ['Spain', 'Espana', 'Kingdom of Spain'], blankText: 'Spain' } }, { id: 'fitb_uuid4', scoringAlgorithm: 'TextEquivalence', scoringData: { value: 'Europe', blankText: 'Europe' } }, { id: 'fitb_uuid5', scoringAlgorithm: 'TextContainsAnswer', scoringData: { value: 'Atlantic', blankText: 'Atlantic' } }, { id: 'fitb_uuid6', scoringAlgorithm: 'Equivalence', scoringData: { value: 'choice_uuid13_america', blankText: 'America' } }, { id: 'fitb_uuid7', scoringAlgorithm: 'Equivalence', scoringData: { value: 'choice_uuid13_confused', blankText: 'confused' } }] }, userResponse: { value: [{ id: 'fitb_uuid1', value: 'Christopher', type: 'Text' }, { id: 'fitb_uuid2', value: '1493', type: 'Text' }, { id: 'fitb_uuid3', value: 'Span', type: 'Text' }, { id: 'fitb_uuid4', value: 'Europe', type: 'Text' }, { id: 'fitb_uuid5', value: 'Atlantic', type: 'Text' }, { id: 'fitb_uuid6', value: 'choice_uuid12_austria', type: 'Text' }, { id: 'fitb_uuid7', value: 'choice_uuid13_confused', type: 'Text' }] } }); _this2.state.scoredData = _this2.transformScoredData(); _this2.state.takeInteractionData = _this2.transformTakeInteractionData(_this2.state.interactionData); } return _this2; } _inherits(FillBlankExample, _Component); return _createClass(FillBlankExample, [{ key: "transformScoredData", value: function transformScoredData() { var _this3 = this; var scoredData = {}; this.state.scoringData.value.forEach(function (item) { var blank = find(_this3.state.interactionData.blanks, ['id', item.id]); var correctAnswer = item.scoringData.blankText; var userResponseObj = find(_this3.state.userResponse.value, ['id', item.id]) || {}; var userResponse = ''; if (blank.answerType !== 'openEntry' && userResponseObj.value) { var choice = find(blank.choices, ['id', userResponseObj.value]) || {}; userResponse = choice.itemBody || ''; } else { userResponse = userResponseObj.value; } var resultScore = userResponse === correctAnswer ? 1 : 0; scoredData[item.id] = { resultScore: resultScore, userResponse: userResponse, correctAnswer: correctAnswer, userResponded: 1 }; }); return { value: scoredData }; } }, { key: "transformTakeInteractionData", value: function transformTakeInteractionData(interactionData) { return cloneDeep(interactionData); } }, { key: "renderEdit", value: function renderEdit() { return jsx("div", { title: "edit" }, jsx(Heading, { level: "h2" }, "Edit"), jsx(FillBlankEdit, { changeItemState: this.handleChangeItemState, interactionData: this.state.interactionData, itemBody: this.state.itemBody, scoringData: this.state.scoringData })); } }, { key: "renderShow", value: function renderShow() { return jsx("div", { title: "show" }, jsx(Heading, { level: "h2" }, "Show"), jsx(FillBlankShow, { interactionData: this.state.interactionData, itemBody: this.state.itemBody, scoringData: this.state.scoringData })); } }, { key: "renderTake", value: function renderTake() { return jsx("div", { title: "take" }, jsx(Heading, { level: "h2" }, "Take"), jsx(FillBlankTake, { interactionData: this.state.takeInteractionData, itemBody: this.state.itemBody, userResponse: this.state.userResponse, handleResponseUpdate: this.handleResponseUpdate })); } }, { key: "renderResult", value: function renderResult() { return jsx("div", { title: "result" }, jsx(Heading, { level: "h2" }, "Result"), jsx(FillBlankResult, { interactionData: this.state.takeInteractionData, itemBody: this.state.itemBody, scoredData: this.transformScoredData() })); } }, { key: "render", value: function render() { return jsx("div", null, jsx("div", { css: this.props.styles.row }, jsx("div", { css: this.props.styles.panel }, this.renderEdit()), jsx("div", { css: this.props.styles.panel }, this.renderShow())), jsx("div", { css: this.props.styles.row }, jsx("div", { css: this.props.styles.panel }, this.renderTake()), jsx("div", { css: this.props.styles.panel }, this.renderResult()))); } }]); }(Component), _defineProperty(_FillBlankExample, "displayName", 'FillBlankExample'), _defineProperty(_FillBlankExample, "componentId", "Quizzes".concat(_FillBlankExample.displayName)), _defineProperty(_FillBlankExample, "propTypes", { useDefaultData: PropTypes.bool, styles: PropTypes.object }), _defineProperty(_FillBlankExample, "defaultProps", { useDefaultData: true }), _FillBlankExample)) || _class); export { FillBlankExample as default };