Skip to content

Commit

Permalink
Dev: refactor and cleaning of em_manager_helper to reduce double post…
Browse files Browse the repository at this point in the history
…ings
  • Loading branch information
lacrioque committed Mar 13, 2018
1 parent 412486c commit 22ee0e9
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions application/helpers/expressions/em_manager_helper.php
Expand Up @@ -5445,7 +5445,6 @@ private function _UpdateValuesInDatabase($finished=false)
}
if (count($updatedValues) > 0 || $finished)
{
$query = 'UPDATE ' . $this->surveyOptions['tablename'] . ' SET ';
$setter = array();
switch ($this->surveyMode)
{
Expand All @@ -5463,14 +5462,15 @@ private function _UpdateValuesInDatabase($finished=false)
$thisstep = 0;
break;
}
$setter[] = App()->db->quoteColumnName('lastpage') . "=" . App()->db->quoteValue($thisstep);
$setter['lastpage'] = $thisstep;


if ($this->surveyOptions['datestamp'] && isset($_SESSION[$this->sessid]['datestamp'])) {
$_SESSION[$this->sessid]['datestamp']=dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $this->surveyOptions['timeadjust']);
$setter[] = App()->db->quoteColumnName('datestamp') . "=" . App()->db->quoteValue(dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $this->surveyOptions['timeadjust']));
$setter['datestamp'] = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $this->surveyOptions['timeadjust']);
}
if ($this->surveyOptions['ipaddr']) {
$setter[] = App()->db->quoteColumnName('ipaddr') . "=" . App()->db->quoteValue(getIPAddress());
$setter['ipaddr'] = getIPAddress();
}

foreach ($updatedValues as $key=>$value)
Expand Down Expand Up @@ -5503,23 +5503,24 @@ private function _UpdateValuesInDatabase($finished=false)
// @todo : control length of DB string, if answers in single choice is valid too (for example) ?
break;
}
if (is_null($val))
{
$setter[] = App()->db->quoteColumnName($key) . "=NULL";
}
else
{
$setter[] = App()->db->quoteColumnName($key) . "=" . App()->db->quoteValue(stripCtrlChars($val));
}

$setter[$key] = is_null($val) ? NULL :stripCtrlChars($val);
}
$query .= implode(', ', $setter);
$query .= " WHERE ID=";

if (isset($_SESSION[$this->sessid]['srid']) && $this->surveyOptions['active'])
{
$query .= $_SESSION[$this->sessid]['srid'];
$oSurveyResponse = SurveyDynamic::model($this->sid)->findByAttributes(['id' => $_SESSION[$this->sessid]['srid']]);

//If the responses already have been submitted once they are marked as completed already, so they shouldn't be changed.
if($oSurveyResponse->lastpage != 1){
array_walk($setter, function($value, $key) use (&$oSurveyResponse) {
$oSurveyResponse->setAttribute($key, $value);
});
}

$saveResult = $oSurveyResponse->save();

if (!dbExecuteAssoc($query))
if ( !$saveResult || $oSurveyResponse == null)
{
// TODO: This kills the session if adminemail is defined, so the queries below won't work.
$message = submitfailed('', $query); // TODO - report SQL error?

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Mar 13, 2018

Collaborator

Think $query break if debug=2 and DB broke (can be tested with hacking ranking question type max column attribute)

But we can have error with $oSurveyResponse->getErrors and it's better :)

Expand All @@ -5529,10 +5530,10 @@ private function _UpdateValuesInDatabase($finished=false)
}

LimeExpressionManager::addFrontendFlashMessage('error', $message, $this->sid);

}

// Save Timings if needed
elseif ($this->surveyOptions['savetimings']) {
else if ($this->surveyOptions['savetimings']) {
Yii::import("application.libraries.Save");
$cSave = new Save();
$cSave->set_answer_time();
Expand Down Expand Up @@ -5567,19 +5568,12 @@ private function _UpdateValuesInDatabase($finished=false)
}
else
{
if ($finished) {
$sQuery = 'UPDATE '.$this->surveyOptions['tablename'] . " SET ";
if($this->surveyOptions['datestamp'])
{
// Replace with date("Y-m-d H:i:s") ? See timeadjust
$sQuery .= App()->db->quoteColumnName('submitdate') . "=" . App()->db->quoteValue(dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $this->surveyOptions['timeadjust']));
}
else
{
$sQuery .= App()->db->quoteColumnName('submitdate') . "=" . App()->db->quoteValue(date("Y-m-d H:i:s",mktime(0,0,0,1,1,1980)));
}
$sQuery .= " WHERE ID=".$_SESSION[$this->sessid]['srid'];
dbExecuteAssoc($sQuery); // Checked
if ($finished && $oSurveyResponse->submitdate == null) {
$dateFinished = $this->surveyOptions['datestamp']
? dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $this->surveyOptions['timeadjust'])
: date("Y-m-d H:i:s",mktime(0,0,0,1,1,1980));

$oSurveyResponse->setAttribute('submitdate',$dateFinished);
}
}

Expand Down

0 comments on commit 22ee0e9

Please sign in to comment.