[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) <tde@openerp.com>
This commit is contained in:
dht-odoo
2021-09-29 20:48:25 +00:00
parent 03e3c8aa7f
commit 8281b1ecd5
4 changed files with 14 additions and 0 deletions
+1
View File
@@ -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
})
@@ -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
});
@@ -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 {
+1
View File
@@ -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"/>
<t t-if="survey.questions_layout == 'one_page'">