From 1f9cd08234f16381fa17d044d3d1f050a6c3d9de Mon Sep 17 00:00:00 2001 From: David Beguin Date: Thu, 24 Sep 2020 09:18:11 +0000 Subject: [PATCH] [FIX] survey: ensure to retrieve the participant's answer when restarting In order to avoid making it possible to follow the same survey multiple times at the same time, a survey cookie is added with the survey token and answer token. When starting the survey, if the cookie contains a valid answer token for that survey token, the survey session is reloaded where the participant left it. When the user finished the survey, the cookie is deleted to allow to retake the survey later, if possible. Note: A participant can still open multiple tabs with the same survey, but it will be linked to the same survey answer. So if the user tries to submit another answer, the late answer will overwrite the previous one. Task ID: 2345468 closes odoo/odoo#58728 X-original-commit: c97c266313aff864f8c397fb1b42f792f9650e5e Signed-off-by: Thibault Delavallee (tde) --- addons/survey/controllers/main.py | 4 ++++ addons/survey/static/src/js/survey_form.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/addons/survey/controllers/main.py b/addons/survey/controllers/main.py index d657bf000d1..da1ec450cca 100644 --- a/addons/survey/controllers/main.py +++ b/addons/survey/controllers/main.py @@ -204,6 +204,10 @@ class Survey(http.Controller): * a token linked to a survey; * a token linked to an answer or generate a new token if access is allowed; """ + # Get the current answer token from cookie + if not answer_token: + answer_token = request.httprequest.cookies.get('survey_%s' % survey_token) + access_data = self._get_access_data(survey_token, answer_token, ensure_token=False) if access_data['validity_code'] is not True: return self._redirect_with_error(access_data, access_data['validity_code']) diff --git a/addons/survey/static/src/js/survey_form.js b/addons/survey/static/src/js/survey_form.js index 661127221c8..d48523e8324 100644 --- a/addons/survey/static/src/js/survey_form.js +++ b/addons/survey/static/src/js/survey_form.js @@ -7,6 +7,7 @@ var time = require('web.time'); var core = require('web.core'); var Dialog = require('web.Dialog'); var dom = require('web.dom'); +var utils = require('web.utils'); var _t = core._t; @@ -35,6 +36,12 @@ publicWidget.registry.SurveyFormWidget = publicWidget.Widget.extend({ self.options = self.$target.find('form').data(); self.readonly = self.options.readonly; self.selectedAnswers = self.options.selectedAnswers; + + // Add Survey cookie to retrieve the survey if you quit the page and restart the survey. + if (!utils.get_cookie('survey_' + self.options.surveyToken)) { + utils.set_cookie('survey_' + self.options.surveyToken, self.options.answerToken, 60*60*24); + } + // Init fields if (!self.options.isStartScreen && !self.readonly) { self._initTimer(); @@ -422,6 +429,7 @@ publicWidget.registry.SurveyFormWidget = publicWidget.Widget.extend({ var selectorsToFadeout = ['.o_survey_form_content']; if (options.isFinish) { selectorsToFadeout.push('.breadcrumb', '.o_survey_timer'); + utils.set_cookie('survey_' + self.options.surveyToken, '', -1); // delete cookie } self.$(selectorsToFadeout.join(',')).fadeOut(this.fadeInOutDelay, function () { resolveFadeOut();