Skip to content

Commit eaee525

Browse files
Muhammad Faraz  MaqsoodFaraz32123
authored andcommitted
fix: disable tolerance for multiple answers
As tolerance was being only applied to first correct answer. So, disable tolerance and do not apply it in case of multiple correct answers for Numerical input problem type according to the given documentation: https://docs.openedx.org/en/latest/educators/how-tos/course_development/exercise_tools/manage_numerical_input_problem.html#add-a-tolerance:~:text=hints%20to%20problems.-,Add%20Multiple%20Correct%20Responses%20via%20the%20Advanced%20Editor,text%20string%20as%20correct%20answers.
1 parent 154b411 commit eaee525

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/index.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const SettingsWidget = ({
7777
updateSettings={updateSettings}
7878
answers={answers}
7979
tolerance={settings.tolerance}
80+
correctAnswerCount={correctAnswerCount}
8081
/>
8182
</div>
8283
)}

src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/Tolerance/index.jsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,20 @@ const ToleranceCard = ({
5050
tolerance,
5151
answers,
5252
updateSettings,
53+
correctAnswerCount,
5354
// inject
5455
intl,
5556
}) => {
5657
const isAnswerRange = isAnswerRangeSet({ answers });
58+
const hasMultipleCorrectAnswers = correctAnswerCount > 1;
5759
let summary = getSummary({ tolerance, intl });
5860
useEffect(() => { summary = getSummary({ tolerance, intl }); }, [tolerance]);
61+
useEffect(() => {
62+
if (hasMultipleCorrectAnswers) {
63+
updateSettings({ tolerance: { value: null, type: ToleranceTypes.none.type } });
64+
}
65+
}, [tolerance, hasMultipleCorrectAnswers]);
66+
5967
return (
6068
<SettingsOption
6169
title={intl.formatMessage(messages.toleranceSettingTitle)}
@@ -70,6 +78,16 @@ const ToleranceCard = ({
7078
<FormattedMessage {...messages.toleranceAnswerRangeWarning} />
7179
</Alert>
7280
)}
81+
{
82+
hasMultipleCorrectAnswers
83+
&& (
84+
<Alert
85+
variant="info"
86+
>
87+
<FormattedMessage {...messages.toleranceMultipleAnswersWarning} />
88+
</Alert>
89+
)
90+
}
7391
<div className="mb-3">
7492
<span>
7593
<FormattedMessage {...messages.toleranceSettingText} />
@@ -79,7 +97,7 @@ const ToleranceCard = ({
7997
<Form.Control
8098
as="select"
8199
onChange={handleToleranceTypeChange({ updateSettings, tolerance, answers })}
82-
disabled={isAnswerRange}
100+
disabled={isAnswerRange || hasMultipleCorrectAnswers}
83101
value={tolerance.type}
84102
>
85103
{Object.keys(ToleranceTypes).map((toleranceType) => (
@@ -91,7 +109,7 @@ const ToleranceCard = ({
91109
</option>
92110
))}
93111
</Form.Control>
94-
{ tolerance?.type !== ToleranceTypes.none.type && !isAnswerRange
112+
{ tolerance?.type !== ToleranceTypes.none.type && (!isAnswerRange || !hasMultipleCorrectAnswers)
95113
&& (
96114
<Form.Control
97115
className="mt-4"
@@ -114,6 +132,7 @@ ToleranceCard.propTypes = {
114132
type: PropTypes.string,
115133
value: PropTypes.oneOfType([PropTypes.number, PropTypes.any]),
116134
}).isRequired,
135+
correctAnswerCount: PropTypes.number.isRequired,
117136
answers: PropTypes.arrayOf(PropTypes.shape({
118137
correct: PropTypes.bool,
119138
id: PropTypes.string,

src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/Tolerance/index.test.jsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,40 @@ describe('ToleranceCard', () => {
110110
expect(NumberText).toBeDefined();
111111
expect(screen.getByTestId('select').getAttributeNames().includes('disabled')).toBeTruthy();
112112
});
113+
it('If there are multiple correct answers, show multiple correct answers warning message and disable dropdown.', () => {
114+
const rangeprops = {
115+
answers: [{
116+
id: 'A',
117+
correct: true,
118+
selectedFeedback: '',
119+
title: 'An Answer A',
120+
isAnswerRange: false,
121+
unselectedFeedback: '',
122+
},
123+
{
124+
id: 'B',
125+
correct: true,
126+
selectedFeedback: '',
127+
title: 'An Answer B',
128+
isAnswerRange: false,
129+
unselectedFeedback: '',
130+
},
131+
],
132+
updateSettings: jest.fn(),
133+
intl: {
134+
formatMessage,
135+
},
136+
};
137+
138+
render(<ToleranceCard
139+
tolerance={mockToleranceNumber}
140+
correctAnswerCount={2}
141+
{...rangeprops}
142+
/>);
143+
const warningMessage = screen.getByText(messages.toleranceMultipleAnswersWarning.defaultMessage);
144+
expect(warningMessage).toBeDefined();
145+
expect(screen.getByTestId('select').getAttributeNames().includes('disabled')).toBeTruthy();
146+
});
113147
});
114148
describe('Type Select', () => {
115149
it('Renders the types for selection', async () => {

src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/Tolerance/messages.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ const messages = defineMessages({
2424
toleranceAnswerRangeWarning: {
2525
id: 'problemEditor.settings.tolerance.answerrangewarning',
2626
defaultMessage: 'Tolerance cannot be applied to an answer range',
27-
description: 'a warning to users that tolerance cannot be aplied to an answer range.',
27+
description: 'a warning to users that tolerance cannot be applied to an answer range.',
28+
},
29+
toleranceMultipleAnswersWarning: {
30+
id: 'problemEditor.settings.tolerance.toleranceMultipleAnswersWarning',
31+
defaultMessage: 'Tolerance cannot be applied to multiple correct answers',
32+
description: 'a warning to users that tolerance cannot be applied to multiple correct answers.',
2833
},
2934
typesPercentage: {
3035
id: 'problemEditor.settings.tolerance.type.percent',

0 commit comments

Comments
 (0)