Skip to content

Commit

Permalink
Dev Added plugin event to allow plugins to decide which tokenized res…
Browse files Browse the repository at this point in the history
…ponse

to resume.
  • Loading branch information
SamMousa committed Jun 26, 2014
1 parent 5213200 commit 5bb740a
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions application/controllers/survey/index.php
Expand Up @@ -499,25 +499,46 @@ function action()
if (!isset($_SESSION['survey_'.$surveyid]['srid']) && $thissurvey['anonymized'] == "N" && $thissurvey['active'] == "Y" && isset($token) && $token !='')
{
// load previous answers if any (dataentry with nosubmit)
//$oSurveyTokenInstance=SurveyDynamic::model($surveyid)->find(array('select'=>'id,submitdate,lastpage', 'condition'=>'token=:token', 'order'=>'id DESC','params'=>array('token' => $token)));
$oSurveyTokenInstance=SurveyDynamic::model($surveyid)->find(array('condition'=>'token=:token', 'order'=>'id DESC','params'=>array('token' => $token)));
if ( $oSurveyTokenInstance )
$oResponses = Response::model($surveyid)->findAllByAttributes(array(
'token' => $token
), array('order' => 'id DESC'));
if (!empty($oResponses))
{
if((empty($oSurveyTokenInstance->submitdate) || $thissurvey['alloweditaftercompletion'] == 'Y' ) && $thissurvey['tokenanswerspersistence'] == 'Y')
/**
* We fire the response selection event when at least 1 response was found.
* If there is just 1 response the plugin still has to option to choose
* NOT to use it.
*/
$event = new PluginEvent('beforeLoadResponse');
$event->set('responses', $oResponses);
App()->pluginManager->dispatchEvent($event);

$oResponse = $event->get('response');
// If $oResponse is false we act as if no response was found.
// This allows a plugin to deny continuing a response.
if ($oResponse !== false)
{
$_SESSION['survey_'.$surveyid]['srid'] = $oSurveyTokenInstance->id;
if (!empty($oSurveyTokenInstance->lastpage))
// If plugin does not set a response we use the first one found, (this replicates pre-plugin behavior)
if (!isset($oResponse) && (!isset($oResponses[0]->submitdate) || $thissurvey['alloweditaftercompletion'] == 'Y') && $thissurvey['tokenanswerspersistence'] == 'Y')
{
$_SESSION['survey_'.$surveyid]['LEMtokenResume'] = true;
$_SESSION['survey_'.$surveyid]['step'] = $oSurveyTokenInstance->lastpage;
$oResponse = $oResponses[0];
}
if (isset($oResponse))
{
$_SESSION['survey_'.$surveyid]['srid'] = $oResponse->id;
if (!empty($oResponse->lastpage))
{
$_SESSION['survey_'.$surveyid]['LEMtokenResume'] = true;
$_SESSION['survey_'.$surveyid]['step'] = $oResponse->lastpage;
}
buildsurveysession($surveyid);
if(!empty($oResponse->submitdate)) // alloweditaftercompletion
{
$_SESSION['survey_'.$surveyid]['maxstep'] = $_SESSION['survey_'.$surveyid]['totalsteps'];
}
loadanswers();
}
}
buildsurveysession($surveyid);
if(!empty($oSurveyTokenInstance->submitdate)) // alloweditaftercompletion
{
$_SESSION['survey_'.$surveyid]['maxstep'] = $_SESSION['survey_'.$surveyid]['totalsteps'];
}
loadanswers();
}
}
// Preview action : Preview right already tested before
Expand Down

3 comments on commit 5bb740a

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

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

Then need to add beforeLoadResponse for loadall too , no ?

@SamMousa
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is just for tokenized surveys where several responses with the token exist. Not sure what you mean

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

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

The name beforeLoadResponse show the event happen every time a reponse is loaded. Not only for token.

You have a way to use this plugin event, but other user can have another way.

Else we can rename to beforeTokenLoadResponse

Please sign in to comment.