diff --git a/models/submition/Submition.js b/models/submition/Submition.js index e13d60f..d7450c0 100644 --- a/models/submition/Submition.js +++ b/models/submition/Submition.js @@ -89,6 +89,11 @@ SubmitionSchema.statics.findSubmitionsByUserData = function (data, callback) { Submition.find({ $and: search_query }, (err, submitions) => { if (err) return callback('database_error'); + const filters = JSON.parse(data.filters)["filters"]; + User.getUsersFromSubmitionsByFilters(submitions, filters, (err, _submitions)=>{ + if (err && err != "missing filters") return callback(err); + if (!err) submitions = _submitions; + async.timesSeries( submitions.length, (time, next) => { @@ -97,16 +102,16 @@ SubmitionSchema.statics.findSubmitionsByUserData = function (data, callback) { if (submition.type == 'target') { User.getUserById(submition.user_id, (err, user) => { if (err) return next(err); - + const data = { no: time + 1, // gender: user.gender, // birth_year: user.birth_year }; - + for (let i = 0; i < project.questions.length; i++) data[project.questions[i].text] = submition.answers[project.questions[i]._id] ? submition.answers[project.questions[i]._id] : ''; - + return next(null, data); }); } else { @@ -123,7 +128,8 @@ SubmitionSchema.statics.findSubmitionsByUserData = function (data, callback) { } }, (err, submitions) => callback(err, submitions) - ); + ); + }) }); }); }; diff --git a/models/user/User.js b/models/user/User.js index 1b1947c..260e14a 100644 --- a/models/user/User.js +++ b/models/user/User.js @@ -178,8 +178,6 @@ UserSchema.statics.getUsersFromSubmitionsByFilters = function (_submitions, _fil const User = this; const submitions = []; -// console.log(_filters.age); - async.forEachOf(_submitions, (submition, key, callback) =>{ User.getUserById(submition.user_id, (err, user) =>{ if(err) callback(); diff --git a/public/js/projects/report/index.js b/public/js/projects/report/index.js index 56ab7bc..e6bbafa 100644 --- a/public/js/projects/report/index.js +++ b/public/js/projects/report/index.js @@ -75,7 +75,7 @@ function setQuery(){ if (q.type == 'yes_no'){ html_code += `${Math.max(q.answers.yes, q.answers.no)}`; - html_code += `${question.answers.yes >= question.answers.no ? 'YES' : 'NO'}`; + html_code += `${q.answers.yes >= q.answers.no ? 'YES' : 'NO'}`; } else if (q.type == 'multiple_choice'){ @@ -110,7 +110,7 @@ function setQuery(){ html_code += `${question.details}`; if (question.type == 'yes_no'){ - html_code += '
'; + html_code += '
'; html_code += `
`; html_code += '
'; html_code += '
'; @@ -137,7 +137,9 @@ function setQuery(){ else if (question.type == 'multiple_choice'){ html_code += `${question.data.total} answers`; html_code += '
'; - for(key in Object.keys(question.answers)){ + for(j in Object.keys(question.answers)){ + j = parseInt(j); + const key = j+1; html_code += '
'; html_code += `${key}`; html_code += `${question.answers[key]}`; @@ -168,22 +170,18 @@ function setQuery(){ html_code += `${question.data.median}`; html_code += '
'; html_code += '
'; - for(j in Object.keys(question.answers)){ - j = parseInt(j); - const key = j+1; + for(key of Object.keys(question.answers)){ html_code += `
`; html_code += ` ${question.answers[key]}`; html_code += '
'; - if (j < Object.keys(question.answers).length-1) + if (key < Object.keys(question.answers).length-1) html_code += '
'; } html_code += '
'; html_code += '
'; - for(j in Object.keys(question.answers)){ - j = parseInt(j); - const key = j+1; + for(key of Object.keys(question.answers)){ html_code += `
${key}
`; - if(j < Object.keys(question.answers).length-1) + if(key < Object.keys(question.answers).length-1) html_code += '
'; } html_code += '
'; @@ -279,9 +277,7 @@ window.onload = () => { if(event.target.classList.contains('pdf-download')){ html2canvas(document.getElementsByClassName('questions-all-wrapper')[0]).then(function(canvas){ - console.log(canvas); var data = canvas.toDataURL(); - console.log(data); var docDefinition = { content: [{ image: data, @@ -292,6 +288,22 @@ window.onload = () => { }) } + if(event.target.classList.contains('csv-download')){ + + const id = window.location.href.split('id=')[1].split('&')[0]; + let data = {}; + + if(selected_filters.age.length != 0 || selected_filters.gender.length != 0){ + data = { + filters: selected_filters + } + } + + console.log(`/projects/report/csv?id=${id}&filters=${JSON.stringify(data)}`) + + window.open(`/projects/report/csv?id=${id}&filters=${JSON.stringify(data)}`) + } + if (event.target.classList.contains('results-download-button-span')) { if (isDownloadButtonOpen) { isDownloadButtonOpen = false; diff --git a/views/projects/report/index.pug b/views/projects/report/index.pug index f3a3527..44b45ce 100644 --- a/views/projects/report/index.pug +++ b/views/projects/report/index.pug @@ -24,7 +24,8 @@ block main .results-download-button span.results-download-button-span Download Results .download-options-wrapper - a.each-download-option(href="/projects/report/csv?id=" + project._id) .CSV + a.each-download-option.csv-download .CSV + //- (href="/projects/report/csv?id=" + project._id) a.each-download-option.pdf-download .PDF //-.filters-wrapper .general-filter-button-wrapper