Skip to content

Commit

Permalink
Fixed issue #6210 Inconsistent database after deleting a file by "cle…
Browse files Browse the repository at this point in the history
…ar & exit" button

Dev : review fixed issue #6226 & #6229
Dev : show clear and exit button only if token aren't used for survey with alloweditaftercompletion
Dev : test if we delete the DB line before delete it and remove the files
  • Loading branch information
Shnoulle committed Jun 22, 2012
1 parent dfe176e commit 16be365
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 49 deletions.
65 changes: 33 additions & 32 deletions application/controllers/survey/index.php
Expand Up @@ -470,7 +470,7 @@ function sendreq(surveyid)
}
$tkresult = dbExecuteAssoc($tkquery); //Checked
$tokendata = $tkresult->read();
if ($tkresult->count()==0 || $areTokensUsed)
if ($tkresult->count()==0 || ($areTokensUsed && $thissurvey['alloweditaftercompletion'] != 'Y'))
{
sendCacheHeaders();
doHeader();
Expand Down Expand Up @@ -524,50 +524,51 @@ function sendreq(surveyid)
//Clear session and remove the incomplete response if requested.
if (isset($move) && $move == "clearall")
{
// delete the response but only if not already completed
$s_lang = $_SESSION['survey_'.$surveyid]['s_lang'];
if (isset($_SESSION['survey_'.$surveyid]['srid']))
{
// find out if there are any fuqt questions - checked
$fieldmap = createFieldMap($surveyid,'short',false,false,$s_lang);
foreach ($fieldmap as $field)
{
if ($field['type'] == "|" && !strpos($field['fieldname'], "_filecount"))
// delete the response but only if not already completed
$result= dbExecuteAssoc('DELETE FROM {{survey_'.$surveyid.'}} WHERE id='.$_SESSION['survey_'.$surveyid]['srid']." AND submitdate IS NULL");
if($result->count()>0){
// find out if there are any fuqt questions - checked
$fieldmap = createFieldMap($surveyid,'short',false,false,$s_lang);
foreach ($fieldmap as $field)
{
if (!isset($qid)) { $qid = array(); }
$qid[] = $field['fieldname'];
if ($field['type'] == "|" && !strpos($field['fieldname'], "_filecount"))
{
if (!isset($qid)) { $qid = array(); }
$qid[] = $field['fieldname'];
}
}
}

// if yes, extract the response json to those questions
if (isset($qid))
{
$query = "SELECT * FROM {{survey_".$surveyid."}} WHERE id=".$_SESSION['survey_'.$surveyid]['srid'];
$result = dbExecuteAssoc($query);
foreach($result->readAll() as $row)
// if yes, extract the response json to those questions
if (isset($qid))
{
foreach ($qid as $question)
$query = "SELECT * FROM {{survey_".$surveyid."}} WHERE id=".$_SESSION['survey_'.$surveyid]['srid'];
$result = dbExecuteAssoc($query);
foreach($result->readAll() as $row)
{
$json = $row[$question];
if ($json == "" || $json == NULL)
continue;

// decode them
$phparray = json_decode($json);

foreach ($phparray as $metadata)
foreach ($qid as $question)
{
$target = Yii::app()->getConfig("uploaddir")."/surveys/".$surveyid."/files/";
// delete those files
unlink($target.$metadata->filename);
$json = $row[$question];
if ($json == "" || $json == NULL)
continue;

// decode them
$phparray = json_decode($json);

foreach ($phparray as $metadata)
{
$target = Yii::app()->getConfig("uploaddir")."/surveys/".$surveyid."/files/";
// delete those files
unlink($target.$metadata->filename);
}
}
}
}
// done deleting uploaded files
}
// done deleting uploaded files


// delete the response but only if not already completed
dbExecuteAssoc('DELETE FROM {{survey_'.$surveyid.'}} WHERE id='.$_SESSION['survey_'.$surveyid]['srid']." AND submitdate IS NULL");

// also delete a record from saved_control when there is one
dbExecuteAssoc('DELETE FROM {{saved_control}} WHERE srid='.$_SESSION['survey_'.$surveyid]['srid'].' AND sid='.$surveyid);
Expand Down
20 changes: 6 additions & 14 deletions application/helpers/common_helper.php
Expand Up @@ -5621,26 +5621,18 @@ function getXMLWriter() {


/**
* Returns true when a token can not be used (either doesn't exist, has less then one usage left or allao edit after completion aren't allowed)
* Returns true when a token can not be used (either doesn't exist, has less then one usage left )
*
* @param mixed $tid Token
*/
function usedTokens($token, $surveyid)
{
$utresult = true;
$thissurvey=getSurveyInfo($surveyid);;// ONLY for alloweditaftercompletion
if($thissurvey['alloweditaftercompletion'])
{
$utresult = false;
}
else
{
Tokens_dynamic::sid($surveyid);
$query=Tokens_dynamic::model()->findAllByAttributes(array("token"=>$token));
if (count($query) > 0) {
$row = $query[0];
if ($row->usesleft > 0) $utresult = false;
}
Tokens_dynamic::sid($surveyid);
$query=Tokens_dynamic::model()->findAllByAttributes(array("token"=>$token));
if (count($query) > 0) {
$row = $query[0];
if ($row->usesleft > 0) $utresult = false;
}
return $utresult;
}
Expand Down
2 changes: 1 addition & 1 deletion application/helpers/frontend_helper.php
Expand Up @@ -1526,7 +1526,7 @@ function buildsurveysession($surveyid,$previewGroup=false)

$tkresult = dbExecuteAssoc($tkquery); //Checked
$tkexist = reset($tkresult->read());
if (!$tkexist || $areTokensUsed)
if (!$tkexist || ($areTokensUsed && $thissurvey['alloweditaftercompletion'] != 'Y') )
{
//TOKEN DOESN'T EXIST OR HAS ALREADY BEEN USED. EXPLAIN PROBLEM AND EXIT

Expand Down
4 changes: 2 additions & 2 deletions application/helpers/replacements_helper.php
Expand Up @@ -399,7 +399,8 @@ function templatereplace($line, $replacements = array(), &$redata = array(), $de
$_linkreplace='';
}

if (isset($surveyid)) {
if (isset($surveyid) && !(usedTokens($_token,$surveyid) && $thissurvey['alloweditaftercompletion'] == 'Y'))
{
$_clearall = "<input type='button' name='clearallbtn' value='" . $clang->gT("Exit and Clear Survey") . "' class='clearall' "
. "onclick=\"if (confirm('" . $clang->gT("Are you sure you want to clear all your responses?", 'js') . "')) {\nwindow.open('".Yii::app()->getController()->createUrl("survey/index/sid/$surveyid?move=clearall&amp;lang=" . $s_lang);

Expand All @@ -413,7 +414,6 @@ function templatereplace($line, $replacements = array(), &$redata = array(), $de
{
$_clearall = "";
}

if (isset(Yii::app()->session['datestamp']))
{
$_datestamp = Yii::app()->session['datestamp'];
Expand Down

0 comments on commit 16be365

Please sign in to comment.