diff --git a/client/src/helpers/survey-solutions.js b/client/src/helpers/survey-solutions.js deleted file mode 100644 index 4709f254..00000000 --- a/client/src/helpers/survey-solutions.js +++ /dev/null @@ -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, [])]; -}; diff --git a/client/src/helpers/survey-solutions.ts b/client/src/helpers/survey-solutions.ts new file mode 100644 index 00000000..6d9f1361 --- /dev/null +++ b/client/src/helpers/survey-solutions.ts @@ -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, [])]; +}; diff --git a/client/src/pages/survey.vue b/client/src/pages/survey.vue index 5299250e..f4817e41 100644 --- a/client/src/pages/survey.vue +++ b/client/src/pages/survey.vue @@ -86,7 +86,8 @@ export default { if (!answer.answer) { 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 const answerText = answer.answer.map((a) => `
  • ${a}
  • `).join(''); return ` @@ -121,7 +122,7 @@ export default { } }, - destroyed() {}, + unmounted() {}, methods: { initSurvey(data, answers) { let survey = new Model(data);