Force correct order and newlines for matrix solutions

This commit is contained in:
Ramon Wenger 2020-10-05 15:07:16 +02:00
parent a9de3fe403
commit efd1c7e8f7
3 changed files with 19 additions and 13 deletions

View File

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

View File

@ -1,5 +1,21 @@
const extractAnswerFromQuestion = (previous, question) => { 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) => { export const extractSurveySolutions = (prev, element) => {

View File

@ -63,18 +63,7 @@
if (!answer.answer) { if (!answer.answer) {
return previous; return previous;
} }
let answerText; const answerText = answer.answer.replace(/\n/g, '<br>'); // replace all newlines with line breaks
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;
}
return ` return `
${previous} ${previous}
<h2 class="solution-text__heading">${answer.title}</h2> <h2 class="solution-text__heading">${answer.title}</h2>