[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) <tde@openerp.com>
This commit is contained in:
David Beguin
2020-09-28 16:58:13 +00:00
parent db76ed28cd
commit 1f9cd08234
2 changed files with 12 additions and 0 deletions
+4
View File
@@ -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'])
@@ -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();