Add solution handling for multiple choice matrix questions
Resolves MS-726 #complete
This commit is contained in:
parent
28a23c4d02
commit
8cb0172d81
|
|
@ -1,26 +0,0 @@
|
||||||
const extractAnswerFromQuestion = (previous, question) => {
|
|
||||||
if (!question.correctAnswer) {
|
|
||||||
return [...previous];
|
|
||||||
}
|
|
||||||
let answer = question.correctAnswer;
|
|
||||||
if (question.getType() === 'matrix') {
|
|
||||||
const correctAnswer = question.correctAnswer;
|
|
||||||
const questionRows = question.getRows();
|
|
||||||
const keys = questionRows.map((question) => question.value); // get the keys as they appear in the question
|
|
||||||
|
|
||||||
answer = keys.map((key) => {
|
|
||||||
let keyWithoutPunctuation = /[,.!?]/.test(key.slice(-1)) ? key.slice(0, -1) : key; // the last character might be a period, comma, question or exclamation mark. If not, we just return the key as-is
|
|
||||||
let answer = correctAnswer[key] || correctAnswer[keyWithoutPunctuation]; // the key might be with or without the punctuation, can be inconsistent depending on the survey
|
|
||||||
return `${keyWithoutPunctuation}: ${answer}`; // return the key without punctuation, as we add the colon
|
|
||||||
}); // return an array, it gets converted to a string further up
|
|
||||||
}
|
|
||||||
return [...previous, { title: question.title, answer, type: question.getType() }];
|
|
||||||
};
|
|
||||||
|
|
||||||
export const extractSurveySolutions = (prev, element) => {
|
|
||||||
if (!element || !element.elements) {
|
|
||||||
// element does not exist or does not have children, so just return the previous result
|
|
||||||
return prev;
|
|
||||||
}
|
|
||||||
return [...prev, ...element.elements.reduce(extractAnswerFromQuestion, [])];
|
|
||||||
};
|
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
interface RowKey {
|
||||||
|
value: string;
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Question {
|
||||||
|
correctAnswer: any;
|
||||||
|
getType: () => string;
|
||||||
|
rows: any[];
|
||||||
|
columns: any[];
|
||||||
|
title: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const extractAnswerFromQuestion = (previous: any[], question: Question) => {
|
||||||
|
if (!question.correctAnswer) {
|
||||||
|
return [...previous];
|
||||||
|
}
|
||||||
|
let answer = question.correctAnswer;
|
||||||
|
console.log(question.getType());
|
||||||
|
const type = question.getType();
|
||||||
|
if (type === 'matrix' || type === 'matrixdropdown') {
|
||||||
|
const correctAnswer = question.correctAnswer;
|
||||||
|
const { rows, columns } = question;
|
||||||
|
console.log('columns', columns);
|
||||||
|
const col1 = columns[0];
|
||||||
|
const col2 = columns[1];
|
||||||
|
console.log('col1', col1);
|
||||||
|
console.log(col1.value);
|
||||||
|
console.log(col1.name);
|
||||||
|
console.log(col1.locTextValue);
|
||||||
|
console.log(col2);
|
||||||
|
console.log(col2.choices);
|
||||||
|
|
||||||
|
answer = rows.map(({ text, value }: RowKey) => {
|
||||||
|
const labelWitoutPunctuation = /[,.!?]/.test(text.slice(-1)) ? text.slice(0, -1) : text; // the last character might be a period, comma, question or exclamation mark. If not, we just return the key as-is
|
||||||
|
console.log('correctAnswer', correctAnswer);
|
||||||
|
let answer;
|
||||||
|
if (type === 'matrix') {
|
||||||
|
answer = correctAnswer[value] || correctAnswer[labelWitoutPunctuation]; // the key might be with or without the punctuation, can be inconsistent depending on the survey
|
||||||
|
} else {
|
||||||
|
const answers = correctAnswer[value] || correctAnswer[labelWitoutPunctuation]; // the key might be with or without the punctuation, can be inconsistent depending on the survey
|
||||||
|
// console.log(answers);
|
||||||
|
// console.log(answers.getOwnPropertyNames());
|
||||||
|
// const forcedObject = { ...answers };
|
||||||
|
// console.log(forcedObject.getOwnPropertyNames());
|
||||||
|
// console.log(forcedObject.keys());
|
||||||
|
// answer = answers.keys();
|
||||||
|
answer = Object.keys(answers);
|
||||||
|
console.log(answer);
|
||||||
|
}
|
||||||
|
return `${labelWitoutPunctuation}: ${answer}`; // return the key without punctuation, as we add the colon
|
||||||
|
}); // return an array, it gets converted to a string further up
|
||||||
|
}
|
||||||
|
return [...previous, { title: question.title, answer, type }];
|
||||||
|
};
|
||||||
|
|
||||||
|
export const extractSurveySolutions = (prev: any[], element: any) => {
|
||||||
|
if (!element || !element.elements) {
|
||||||
|
// element does not exist or does not have children, so just return the previous result
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
return [...prev, ...element.elements.reduce(extractAnswerFromQuestion, [])];
|
||||||
|
};
|
||||||
|
|
@ -86,7 +86,8 @@ export default {
|
||||||
if (!answer.answer) {
|
if (!answer.answer) {
|
||||||
return previous;
|
return previous;
|
||||||
}
|
}
|
||||||
if (answer.type === 'matrix' || answer.type === 'checkbox') {
|
const type = answer.type;
|
||||||
|
if (type === 'matrix' || type === 'matrixdropdown' || type === 'checkbox') {
|
||||||
// wrap all the answers inside li tags and convert to a single string
|
// wrap all the answers inside li tags and convert to a single string
|
||||||
const answerText = answer.answer.map((a) => `<li class="solution-text__list-item">${a}</li>`).join('');
|
const answerText = answer.answer.map((a) => `<li class="solution-text__list-item">${a}</li>`).join('');
|
||||||
return `
|
return `
|
||||||
|
|
@ -121,7 +122,7 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
destroyed() {},
|
unmounted() {},
|
||||||
methods: {
|
methods: {
|
||||||
initSurvey(data, answers) {
|
initSurvey(data, answers) {
|
||||||
let survey = new Model(data);
|
let survey = new Model(data);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue