Merged in feature/fix-survey-answers (pull request #76)

Force correct order and newlines for matrix solutions

Approved-by: Christian Cueni
This commit is contained in:
Ramon Wenger 2020-10-05 13:32:50 +00:00
commit 3e4812297a
3 changed files with 19 additions and 13 deletions

View File

@ -13,6 +13,7 @@
<p
class="solution__text solution-text fade"
data-cy="solution-text"
v-if="visible"
v-html="value.text"/>
</transition>

View File

@ -1,5 +1,21 @@
const extractAnswerFromQuestion = (previous, question) => {
return [...previous, {title: question.title, answer: question.correctAnswer}];
let answer = question.correctAnswer;
if (question.getType() === 'matrix') {
const correctAnswer = question.correctAnswer;
const questionRows = question.getRows();
const keys = questionRows.map(question => {
const text = question.value;
if (/[,.!?]/.test(text.slice(-1))) {
return text.slice(0, -1);
}
return text;
}); // get the keys as they appear in the question, without punctuation at the end
const answers = keys.map(key => `${key}: ${correctAnswer[key]}`);
answer = answers.join('\n');
}
return [...previous, {title: question.title, answer}];
};
export const extractSurveySolutions = (prev, element) => {

View File

@ -63,18 +63,7 @@
if (!answer.answer) {
return previous;
}
let answerText;
if (typeof answer.answer === 'object') {
// this means the answer comes from a matrix, where the keys are the labels and the values are the respective answers
let answerObject = answer.answer;
let keysAndValues = [];
for (let prop of Object.keys(answerObject)) {
keysAndValues.push(`${prop}: ${answerObject[prop]}`);
}
answerText = keysAndValues.join(', ');
} else {
answerText = answer.answer;
}
const answerText = answer.answer.replace(/\n/g, '<br>'); // replace all newlines with line breaks
return `
${previous}
<h2 class="solution-text__heading">${answer.title}</h2>