From 8281b1ecd55fb9ca532d47dbc70190765413a147 Mon Sep 17 00:00:00 2001 From: dht-odoo Date: Tue, 14 Sep 2021 11:07:49 +0000 Subject: [PATCH] [FIX] survey: display the absolute counter in time limited survey Before this commit: Let's consider the following two scenarios while answering time limited survey: 1) attendee's system clock is de-synchronized compared to the real time 2) Odoo server clock is de-synchronized compared to the real time In any/both of the above cases, it will lead to a wrong timer display, and will confuse the user and/or make him fail the test because he will think he still has more time to submit when he does not. With this commit: To fix this, we simply get the offset of server UTC time and moment time, and apply the offset while calculating the time remaining. Task-2612972 closes odoo/odoo#77419 X-original-commit: 9e9cd045b2899abc5c8e4a92c43ed6eb2eea158a Signed-off-by: Thibault Delavallee (tde) --- addons/survey/controllers/main.py | 1 + addons/survey/static/src/js/survey_form.js | 2 ++ addons/survey/static/src/js/survey_timer.js | 10 ++++++++++ addons/survey/views/survey_templates.xml | 1 + 4 files changed, 14 insertions(+) diff --git a/addons/survey/controllers/main.py b/addons/survey/controllers/main.py index e59123fed74..3bd3d021e81 100644 --- a/addons/survey/controllers/main.py +++ b/addons/survey/controllers/main.py @@ -263,6 +263,7 @@ class Survey(http.Controller): if not answer_sudo.is_session_answer and survey_sudo.is_time_limited and answer_sudo.start_datetime: data.update({ + 'server_time': fields.Datetime.now(), 'timer_start': answer_sudo.start_datetime.isoformat(), 'time_limit_minutes': survey_sudo.time_limit }) diff --git a/addons/survey/static/src/js/survey_form.js b/addons/survey/static/src/js/survey_form.js index 78055cad5f1..fe750c435e5 100644 --- a/addons/survey/static/src/js/survey_form.js +++ b/addons/survey/static/src/js/survey_form.js @@ -886,6 +886,7 @@ publicWidget.registry.SurveyFormWidget = publicWidget.Widget.extend({ var questionTimeLimitReached = $timerData.data('questionTimeLimitReached'); var timeLimitMinutes = $timerData.data('timeLimitMinutes'); var hasAnswered = $timerData.data('hasAnswered'); + const serverTime = $timerData.data('serverTime'); if (!questionTimeLimitReached && !hasAnswered && timeLimitMinutes) { var timer = $timerData.data('timer'); @@ -894,6 +895,7 @@ publicWidget.registry.SurveyFormWidget = publicWidget.Widget.extend({ }); this.$('.o_survey_timer_container').append($timer); this.surveyTimerWidget = new publicWidget.registry.SurveyTimerWidget(this, { + 'serverTime': serverTime, 'timer': timer, 'timeLimitMinutes': timeLimitMinutes }); diff --git a/addons/survey/static/src/js/survey_timer.js b/addons/survey/static/src/js/survey_timer.js index 2bfd78fedf8..23339bc21fd 100644 --- a/addons/survey/static/src/js/survey_timer.js +++ b/addons/survey/static/src/js/survey_timer.js @@ -16,11 +16,18 @@ publicWidget.registry.SurveyTimerWidget = publicWidget.Widget.extend({ this.timer = params.timer; this.timeLimitMinutes = params.timeLimitMinutes; this.surveyTimerInterval = null; + this.timeDifference = null; + if (params.serverTime) { + this.timeDifference = moment.utc().diff(moment.utc(params.serverTime), 'milliseconds'); + } }, /** * Two responsabilities : Validate that time limit is not exceeded and Run timer otherwise. + * If end-user's clock OR the system clock is de-synchronized before the survey is started, we apply the + * difference in timer (if time difference is more than 5 seconds) so that we can + * display the 'absolute' counter * * @override */ @@ -28,6 +35,9 @@ publicWidget.registry.SurveyTimerWidget = publicWidget.Widget.extend({ var self = this; return this._super.apply(this, arguments).then(function () { self.countDownDate = moment.utc(self.timer).add(self.timeLimitMinutes, 'minutes'); + if (Math.abs(self.timeDifference) >= 5000) { + self.countDownDate = self.countDownDate.add(self.timeDifference, 'milliseconds'); + } if (self.timeLimitMinutes <= 0 || self.countDownDate.diff(moment.utc(), 'seconds') < 0) { self.trigger_up('time_up'); } else { diff --git a/addons/survey/views/survey_templates.xml b/addons/survey/views/survey_templates.xml index 7eb1337065e..4f6d1b3d7bd 100644 --- a/addons/survey/views/survey_templates.xml +++ b/addons/survey/views/survey_templates.xml @@ -160,6 +160,7 @@ t-att-data-question-time-limit-reached="answer.question_time_limit_reached" t-att-data-has-answered="bool(has_answered)" t-att-data-is-page-description="bool(question and question.is_page and not is_html_empty(question.description))" + t-att-data-server-time="server_time" t-att-data-timer="timer_start" t-att-data-time-limit-minutes="time_limit_minutes"/>