Skip to content

Commit

Permalink
Fixed issue #16635: Timer not working when survey is active (when exe…
Browse files Browse the repository at this point in the history
…cuting the survey multiple times in the same browser/tab) (#1592)

Each timer registers itself in a local storage item identified by the Survey ID.
This is used by the reset function to know which timers belong to the survey and are to be reset.
The reset method was replaced by a function (in survey.js instead of timer.js) that reset all timers for a given survey.
Also reviewed the reset method for preview mode.
  • Loading branch information
gabrieljenik committed Sep 29, 2020
1 parent 3e596fb commit 89f54ca
Show file tree
Hide file tree
Showing 11 changed files with 996 additions and 23 deletions.
3 changes: 2 additions & 1 deletion application/controllers/survey/index.php
Expand Up @@ -72,6 +72,7 @@ public function action()

if (isset($param['newtest']) && $param['newtest'] == "Y") {
killSurveySession($surveyid);
resetQuestionTimers($surveyid);
}

$surveyExists = ($oSurvey != null);
Expand Down Expand Up @@ -591,7 +592,7 @@ public function action()

// Reset the question timers in preview
if (!$isSurveyActive || $previewmode) {
resetQuestionTimers();
resetQuestionTimers($surveyid);
}

sendCacheHeaders();
Expand Down
7 changes: 4 additions & 3 deletions application/helpers/frontend_helper.php
Expand Up @@ -1953,15 +1953,16 @@ function resetTimers()
}

/**
* Sets the 'resetQuestionTimers' flag, so timer.js can pick it and unset the localstorage
* Removes all question timers for this survey from local storage
*
* @return void
*/
function resetQuestionTimers()
function resetQuestionTimers($surveyid)
{
Yii::app()->clientScript->registerScript(
'resetQuestionTimers',
'LSvar.bResetQuestionTimers=true;',
//'LSvar.bResetQuestionTimers=true;',
"resetQuestionTimers({$surveyid})",
LSYii_ClientScript::POS_BEGIN
);
}
Expand Down
1 change: 1 addition & 0 deletions application/helpers/qanda_helper.php
Expand Up @@ -486,6 +486,7 @@ function return_timer_script($aQuestionAttributes, $ia, $disable = null)
'/survey/questions/question_timer/timer_footer',
array(
'iQid'=>$ia[0],
'iSid'=>Yii::app()->getConfig('surveyID'),
'time_limit'=>$time_limit,
'time_limit_action'=>$time_limit_action,
'time_limit_warning'=>$time_limit_warning,
Expand Down
1 change: 1 addition & 0 deletions application/models/QuestionBaseRenderer.php
Expand Up @@ -205,6 +205,7 @@ protected function getTimeSettingRender()
'/survey/questions/question_timer/timer_footer',
array(
'iQid'=>$oQuestion->qid,
'iSid'=>Yii::app()->getConfig('surveyID'),
'time_limit'=>$time_limit,
'time_limit_action'=>$time_limit_action,
'time_limit_warning'=>$time_limit_warning,
Expand Down
Expand Up @@ -15,7 +15,7 @@
* @var $disable
*/
Yii::app()->getClientScript()->registerScript("TimerQuestion".$iQid, "
countdown($iQid, $time_limit, $time_limit_action, $time_limit_warning, $time_limit_warning_2, $time_limit_warning_display_time, $time_limit_warning_2_display_time, '$disable');
countdown($iQid, $iSid, $time_limit, $time_limit_action, $time_limit_warning, $time_limit_warning_2, $time_limit_warning_display_time, $time_limit_warning_2_display_time, '$disable');
", LSYii_ClientScript::POS_POSTSCRIPT);
?>
</div>
8 changes: 8 additions & 0 deletions assets/packages/limesurvey/survey.js
Expand Up @@ -426,3 +426,11 @@ function activateSoftMandatory(){
});
}

function resetQuestionTimers(sid) {
var surveyTimersItemName = 'limesurvey_timers_by_sid_' + sid;
var timers = JSON.parse(window.localStorage.getItem(surveyTimersItemName) || "[]");
timers.forEach(function(timersessionname, idx){
window.localStorage.removeItem('limesurvey_timers_' + timersessionname);
});
window.localStorage.removeItem(surveyTimersItemName);
}

1 comment on commit 89f54ca

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I muts construct the plugin to get old system (no reset on timer) : i create a feature request :)

Please sign in to comment.