@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
244 lines (243 loc) • 11.2 kB
JavaScript
/** @jsx jsx */ function _assert_this_initialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _call_super(_this, derived, args) {
derived = _get_prototype_of(derived);
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
}
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for(var i = 0; i < props.length; i++){
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _get_prototype_of(o) {
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _get_prototype_of(o);
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _set_prototype_of(subClass, superClass);
}
function _possible_constructor_return(self, call) {
if (call && (_type_of(call) === "object" || typeof call === "function")) {
return call;
}
return _assert_this_initialized(self);
}
function _set_prototype_of(o, p) {
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _set_prototype_of(o, p);
}
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function _is_native_reflect_construct() {
try {
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
} catch (_) {}
return (_is_native_reflect_construct = function() {
return !!result;
})();
}
function _ts_decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
import { Component } from 'react';
import PropTypes from 'prop-types';
import { camelize } from 'humps';
import { ScreenReaderContent } from '@instructure/ui-a11y-content';
import { RadioInput } from '@instructure/ui-radio-input';
import { jsx } from '@instructure/emotion';
import t from '@instructure/quiz-i18n/format-message';
import { ItemBodyWrapper, RichContentRenderer } from '@instructure/quiz-rce';
import { FeedbackWrapper, NoResponse } from '@instructure/quiz-results-feedback';
import generateStyle from './styles';
import generateComponentTheme from './theme';
import { withStyleOverrides } from '@instructure/quiz-common';
export var correctChoice = function correctChoice(param) {
var interactionData = param.interactionData, scoredData = param.scoredData;
return interactionData.choices.find(function(item) {
var choiceData = scoredData.value && scoredData.value[item.id] || {};
return choiceData.hasOwnProperty('correct') ? choiceData.correct : choiceData.resultScore;
}) || {};
};
export var getChoiceData = function(scoredData, id) {
if (!(scoredData === null || scoredData === void 0 ? void 0 : scoredData.value)) return {};
// Some imported quizzes have non-standard IDs (e.g., "ans-6-4") that get
// transformed by camelizeHelpers (e.g., "ans64" or "ans6_4"). To make sure
// student selections still match correctly, we check both the original,
// dash-stripped, and camelized versions of the ID.
return scoredData.value[id] || scoredData.value[id.replace(/-/g, '')] || scoredData.value[camelize(id)] || {};
};
var MultipleChoiceResult = /*#__PURE__*/ function(Component) {
"use strict";
_inherits(MultipleChoiceResult, Component);
function MultipleChoiceResult() {
_class_call_check(this, MultipleChoiceResult);
var _this;
_this = _call_super(this, MultipleChoiceResult, arguments), _define_property(_this, "renderFeedbackChoice", function(param, status) {
var id = param.id, itemBody = param.itemBody;
return /*#__PURE__*/ jsx("div", {
key: id,
css: _this.props.styles.responseWrapper
}, /*#__PURE__*/ jsx(FeedbackWrapper, {
correctAnswer: correctChoice(_this.props).itemBody,
hiddenCorrectAnswerText: t('Correct answer: '),
hiddenIncorrectAnswerText: t('Incorrect answer: '),
status: status,
richContent: true
}, /*#__PURE__*/ jsx(RadioInput, {
value: id,
name: "interaction_".concat(_this.props.itemId),
label: /*#__PURE__*/ jsx(RichContentRenderer, {
content: itemBody
}),
checked: true,
readOnly: true
})));
}), _define_property(_this, "renderUngradedChoice", function(param, checked) {
var id = param.id, itemBody = param.itemBody;
return /*#__PURE__*/ jsx("div", {
key: id,
css: [
_this.props.styles.responseWrapper,
_this.props.styles.radioWrapper
]
}, /*#__PURE__*/ jsx(RadioInput, {
value: id,
name: "interaction_".concat(_this.props.itemId),
label: /*#__PURE__*/ jsx("div", null, /*#__PURE__*/ jsx(RichContentRenderer, {
content: itemBody
}), !checked && /*#__PURE__*/ jsx(ScreenReaderContent, null, t(', Not Selected'))),
checked: checked,
readOnly: true
}));
});
return _this;
}
_create_class(MultipleChoiceResult, [
{
key: "render",
value: function render() {
var _this = this;
var _this_props_interactionData_choices, _this_props_interactionData, _this_props;
var scoredData = this.props.scoredData;
var choices = (_this_props = this.props) === null || _this_props === void 0 ? void 0 : (_this_props_interactionData = _this_props.interactionData) === null || _this_props_interactionData === void 0 ? void 0 : (_this_props_interactionData_choices = _this_props_interactionData.choices) === null || _this_props_interactionData_choices === void 0 ? void 0 : _this_props_interactionData_choices.map(function(choice) {
var choiceData = getChoiceData(scoredData, choice === null || choice === void 0 ? void 0 : choice.id);
var correct = choiceData.hasOwnProperty('correct') ? choiceData.correct : choiceData.resultScore;
var checked = choiceData.userResponded;
return {
choice: choice,
correct: correct,
checked: checked
};
});
var noResponse = choices.every(function(param) {
var checked = param.checked;
return !checked;
});
var correctnessKnown = typeof scoredData.correct !== 'undefined';
return /*#__PURE__*/ jsx(ItemBodyWrapper, {
itemBody: this.props.itemBody
}, /*#__PURE__*/ jsx("div", null, choices.map(function(param) {
var choice = param.choice, correct = param.correct, checked = param.checked;
var status;
if (correct === true || correct > 0) {
status = 'correct';
} else if (correctnessKnown && checked) {
status = 'incorrect';
}
if (!status || !checked) {
// Choice has no feedback
return _this.renderUngradedChoice(choice, checked);
}
return _this.renderFeedbackChoice(choice, status);
})), noResponse && /*#__PURE__*/ jsx(NoResponse, {
css: this.props.styles.noResponseWrapper,
correctAnswerLabel: correctChoice(this.props).itemBody,
responseHidden: !scoredData.value,
richContent: true,
status: correctnessKnown ? 'incorrect' : 'unknown'
}));
}
}
]);
return MultipleChoiceResult;
}(Component);
_define_property(MultipleChoiceResult, "displayName", 'MultipleChoiceResult');
_define_property(MultipleChoiceResult, "componentId", "Quizzes".concat(MultipleChoiceResult.displayName));
_define_property(MultipleChoiceResult, "propTypes", {
interactionData: PropTypes.shape({
choices: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.string,
itemBody: PropTypes.string,
position: PropTypes.number
}))
}).isRequired,
itemBody: PropTypes.string.isRequired,
itemId: PropTypes.string,
scoredData: PropTypes.shape({
correct: PropTypes.bool,
value: PropTypes.objectOf(PropTypes.shape({
correct: PropTypes.bool,
resultScore: PropTypes.number,
userResponded: PropTypes.bool
}))
}).isRequired,
styles: PropTypes.object
});
_define_property(MultipleChoiceResult, "defaultProps", {
itemId: void 0
});
export { MultipleChoiceResult as default };
MultipleChoiceResult = _ts_decorate([
withStyleOverrides(generateStyle, generateComponentTheme)
], MultipleChoiceResult);