Skip to content

Commit

Permalink
Further refactor (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverfoster authored Aug 1, 2023
1 parent 3e86a82 commit c721758
Showing 1 changed file with 48 additions and 78 deletions.
126 changes: 48 additions & 78 deletions js/models/questionModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,95 +242,65 @@ class QuestionModel extends ComponentModel {

}

getFeedback(_feedback = this.get('_feedback')) {
if (!_feedback) return {};

// global feedback altTitle / title / _classes
let {
altTitle,
title,
_classes
} = _feedback;

altTitle = altTitle || Adapt.course.get('_globals')._accessibility.altFeedbackTitle || '';
title = title || this.get('displayTitle') || this.get('title') || '';

const feedbackBase = {
altTitle,
title,
_classes
};

const feedbackOverrides = this.getFeedbackOverrides() || {};

const feedbackConfig = {
...feedbackBase,
...feedbackOverrides
};
getFeedback(feedback = this.get('_feedback')) {
if (!feedback) return {};

if (feedbackConfig?._graphic?._src && !feedbackConfig?._imageAlignment) {
feedbackConfig._imageAlignment = 'right';
}

return feedbackConfig;
}

getFeedbackOverrides() {
const _feedback = this.get('_feedback');
const isFinal = (this.get('_attemptsLeft') === 0);

const correctness = this.get('_isCorrect')
const isCorrect = this.get('_isCorrect');
const correctness = isCorrect
? 'correct'
: this.isPartlyCorrect()
? 'partlyCorrect'
: 'incorrect';

switch (correctness) {
case 'correct': {
if (typeof _feedback.correct === 'string') {
// old style
return {
body: _feedback.correct
};
}

// new style
return _feedback._correct;
}

case 'partlyCorrect': {
if (typeof _feedback._partlyCorrect === 'object') {
// old style
const fallbackBody = _feedback._partlyCorrect?.final || _feedback._incorrect?.final || '';
const body = !isFinal ? _feedback._partlyCorrect?.notFinal || fallbackBody : fallbackBody;
const isLegacyConfig = (typeof feedback.correct === 'string') ||
(typeof feedback._partlyCorrect === 'object') ||
(typeof feedback._incorrect === 'object');

const getLegacyConfigObject = () => {
const subPart = isFinal ? 'final' : 'notFinal';
return {
body: (
isCorrect
? feedback.correct
: feedback[`_${correctness}`]?.[subPart] ||
feedback[`_${correctness}`]?.final ||
feedback._incorrect?.final
) || ''
};
};

return {
body
};
}
const getConfigObject = () => {
const subPart = isFinal ? 'Final' : 'NotFinal';
return (
isCorrect
? feedback._correct
: feedback[`_${correctness}${subPart}`] ||
feedback[`_${correctness}Final`] ||
feedback._incorrectFinal
) || {};
};

// new style
const fallbackFeedback = _feedback._partlyCorrectFinal || _feedback._incorrectFinal || '';
const feedbackPartlyCorrect = !isFinal ? _feedback._partlyCorrectNotFinal || fallbackFeedback : fallbackFeedback;
return feedbackPartlyCorrect;
}
case 'incorrect': {
if (typeof _feedback._incorrect === 'object') {
// old style
const fallbackBody = _feedback._incorrect.final;
const body = !isFinal ? (_feedback._incorrect.notFinal || fallbackBody) : fallbackBody;

return {
body
};
}
const feedbackConfig = {
altTitle: feedback.altTitle ||
Adapt.course.get('_globals')._accessibility.altFeedbackTitle ||
'',
title: feedback.title ||
this.get('displayTitle') ||
this.get('title') ||
'',
_classes: feedback._classes,
...(isLegacyConfig
? getLegacyConfigObject()
: getConfigObject()
)
};

// new style
const fallbackFeedback = _feedback._incorrectFinal;
const feedbackIncorrect = !isFinal ? (_feedback._incorrectNotFinal || fallbackFeedback) : fallbackFeedback;
return feedbackIncorrect;
}
if (feedbackConfig?._graphic?._src && !feedbackConfig?._imageAlignment) {
feedbackConfig._imageAlignment = 'right';
}

return feedbackConfig;
}

// Used to setup the correct, incorrect and partly correct feedback
Expand Down

0 comments on commit c721758

Please sign in to comment.