Skip to content

Commit

Permalink
Dev EM-based validation and navigation sub-system:
Browse files Browse the repository at this point in the history
Dev Remove need for save.php - have  EM manage all saving - complete except for final page's submit

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_dev_tms@11562 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
TMSWhite committed Dec 1, 2011
1 parent 3ae96ce commit 3d03321
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 41 deletions.
117 changes: 82 additions & 35 deletions classes/eval/LimeExpressionManager.php
Expand Up @@ -1894,7 +1894,7 @@ static function StartProcessingPage($navigationIndex=false,$allOnOnePage=false)
static function StartSurvey($surveyid,$surveyMode='group',$options=NULL,$forceRefresh=false,$debugLevel=0)
{
$LEM =& LimeExpressionManager::singleton();
$LEM->sid=$surveyid; // TMSW - santize this?
$LEM->sid=sanitize_int($surveyid);

if (is_null($options)) {
$options = array();
Expand All @@ -1904,6 +1904,8 @@ static function StartSurvey($surveyid,$surveyMode='group',$options=NULL,$forceRe
$LEM->surveyOptions['anonymized'] = (isset($options['anonymized']) ? $options['anonymized'] : false);
$LEM->surveyOptions['datestamp'] = (isset($options['datestamp']) ? $options['datestamp'] : false);
$LEM->surveyOptions['ipaddr'] = (isset($options['ipaddr']) ? $options['ipaddr'] : false);
$LEM->surveyOptions['tablename'] = (isset($options['tablename']) ? $options['tablename'] : db_table_name('survey_' . $LEM->sid));
$LEM->surveyOptions['timeadjust'] = (isset($options['timeadjust']) ? $options['timeadjust'] : 0);

$LEM->debugLevel=$debugLevel;
switch ($surveyMode) {
Expand Down Expand Up @@ -1946,7 +1948,6 @@ static function NavigateBackwards()

$LEM->RelevanceResultCache=array(); // to avoid running same test more than once for a given group

// $LEM->ProcessCurrentResponses();
switch ($LEM->surveyMode)
{
case 'survey':
Expand Down Expand Up @@ -1990,13 +1991,14 @@ static function NavigateBackwards()
case 'group':
// First validate the current group
$LEM->StartProcessingPage();
$LEM->ProcessCurrentResponses();
$updatedValues=$LEM->ProcessCurrentResponses();
$message = '';
while (true)
{
$LEM->currentQset = array(); // reset active list of questions
if (--$LEM->currentGroupSeq < 0)
{
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'at_start'=>true,
Expand All @@ -2017,6 +2019,7 @@ static function NavigateBackwards()
else
{
// display new group
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'at_start'=>false,
Expand All @@ -2034,13 +2037,14 @@ static function NavigateBackwards()
break;
case 'question':
$LEM->StartProcessingPage();
$LEM->ProcessCurrentResponses();
$updatedValues=$LEM->ProcessCurrentResponses();
$message = '';
while (true)
{
$LEM->currentQset = array(); // reset active list of questions
if (--$LEM->currentQuestionSeq < 0)
{
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'at_start'=>true,
Expand Down Expand Up @@ -2074,6 +2078,7 @@ static function NavigateBackwards()
else
{
// display new question
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'at_start'=>false,
Expand Down Expand Up @@ -2104,7 +2109,6 @@ static function NavigateForwards($force=false) {

$LEM->RelevanceResultCache=array(); // to avoid running same test more than once for a given group

// $LEM->ProcessCurrentResponses();
switch ($LEM->surveyMode)
{
case 'survey':
Expand Down Expand Up @@ -2148,8 +2152,7 @@ static function NavigateForwards($force=false) {
case 'group':
// First validate the current group
$LEM->StartProcessingPage();
$LEM->ProcessCurrentResponses();
$updatedValues=array();
$updatedValues=$LEM->ProcessCurrentResponses();
$message = '';
if (!$force && $LEM->currentGroupSeq != -1)
{
Expand All @@ -2159,7 +2162,7 @@ static function NavigateForwards($force=false) {
if (!is_null($result) && ($result['mandViolation'] || !$result['valid']))
{
// redisplay the current group
$message .= $LEM->UpdateValuesInDatabase($updatedValues);
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'finished'=>false,
Expand All @@ -2178,7 +2181,7 @@ static function NavigateForwards($force=false) {
$LEM->currentQset = array(); // reset active list of questions
if (++$LEM->currentGroupSeq >= $LEM->numGroups)
{
$message .= $LEM->UpdateValuesInDatabase($updatedValues);
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'finished'=>true,
Expand All @@ -2203,7 +2206,7 @@ static function NavigateForwards($force=false) {
else
{
// display new group
$message .= $LEM->UpdateValuesInDatabase($updatedValues);
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'finished'=>false,
Expand All @@ -2220,8 +2223,7 @@ static function NavigateForwards($force=false) {
break;
case 'question':
$LEM->StartProcessingPage();
$LEM->ProcessCurrentResponses();
$updatedValues=array();
$updatedValues=$LEM->ProcessCurrentResponses();
$message = '';
if (!$force && $LEM->currentQuestionSeq != -1)
{
Expand All @@ -2231,7 +2233,7 @@ static function NavigateForwards($force=false) {
if (!is_null($result) && ($result['mandViolation'] || !$result['valid']))
{
// redisplay the current question
$message .= $LEM->UpdateValuesInDatabase($updatedValues);
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'finished'=>false,
Expand All @@ -2251,7 +2253,7 @@ static function NavigateForwards($force=false) {
$LEM->currentQset = array(); // reset active list of questions
if (++$LEM->currentQuestionSeq >= $LEM->numQuestions)
{
$message .= $LEM->UpdateValuesInDatabase($updatedValues);
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'finished'=>true,
Expand Down Expand Up @@ -2290,7 +2292,7 @@ static function NavigateForwards($force=false) {
else
{
// display new question
$message .= $LEM->UpdateValuesInDatabase($updatedValues);
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'finished'=>false,
Expand All @@ -2313,12 +2315,41 @@ static function NavigateForwards($force=false) {
* Write values to database.
* @param <type> $updatedValues
*/
function UpdateValuesInDatabase($updatedValues)
private function _UpdateValuesInDatabase($updatedValues)
{
// Update these values in the database
global $connect;

$message = '';
$_SESSION['datestamp']=date_shift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $this->surveyOptions['timeadjust']);
if ($this->surveyOptions['active'] && !isset($_SESSION['srid']))
{
// Create initial insert row for this record
$today = date_shift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $this->surveyOptions['timeadjust']);
// TODO - anonymize as needed
$sdata = array(
"datestamp"=>$today,
"ipaddr"=>$_SERVER['REMOTE_ADDR'],
"startlanguage"=>$_SESSION['s_lang'],
"refurl"=>getenv("HTTP_REFERER"),
"datestamp"=>$_SESSION['datestamp'],
"startdate"=>$_SESSION['datestamp'],
);
//One of the strengths of ADOdb's AutoExecute() is that only valid field names for $table are updated
if ($connect->AutoExecute($this->surveyOptions['tablename'], $sdata,'INSERT')) // Checked
{
$srid = $connect->Insert_ID($this->surveyOptions['tablename'],"sid");
$_SESSION['srid'] = $srid;
}
else if ($this->debugLevel >= 2)
{
$message .= "Unable to insert record into survey table.<br />".$connect->ErrorMsg() . "<br/>";
}
}

if (count($updatedValues) > 0)
{
$query = 'UPDATE '.db_table_name('survey_' . $this->sid) . " SET ";
$query = 'UPDATE '.$this->surveyOptions['tablename'] . " SET ";
switch ($this->surveyMode)
{
case 'question':
Expand Down Expand Up @@ -2347,7 +2378,12 @@ function UpdateValuesInDatabase($updatedValues)
}
else
{
$setter[] = db_quote_id($key) . "=" . db_quoteall($value);
switch($value['type'])
{
default:
$setter[] = db_quote_id($key) . "=" . db_quoteall($value['value']);
break;
}
}
}
$query .= implode(', ', $setter);
Expand All @@ -2356,17 +2392,21 @@ function UpdateValuesInDatabase($updatedValues)
if (isset($_SESSION['srid']) && $this->surveyOptions['active'])
{
$query .= $_SESSION['srid'];
db_execute_assoc($query);

if (!db_execute_assoc($query) && ($this->debugLevel >= 2)) {
$message .= 'Error in SQL update: '. $connect->ErrorMsg() . '<br/>';
}
}
if ($this->debugLevel >= 2) {
return $query;
$message .= $query;
}
}
return '';
return $message;
}

/**
* Jump to a specific question or group sequence.
* TODO - if jumping forward, should it re-validate everything in between?
* @param <type> $seq
* @param <type> $force - if true, then skip validation of current group (e.g. will jump even if there are errors)
* @param <type> $preview - if true, then treat this group/question as relevant, even if it is not, so that it can be displayed
Expand All @@ -2380,16 +2420,14 @@ static function JumpTo($seq,$force=false,$preview=false) {
$LEM->RelevanceResultCache=array(); // to avoid running same test more than once for a given group
--$seq; // convert to 0-based numbering

// $LEM->ProcessCurrentResponses();
switch ($LEM->surveyMode)
{
case 'survey':
break;
case 'group':
// First validate the current group
$LEM->StartProcessingPage();
$LEM->ProcessCurrentResponses();
$updatedValues=array();
$updatedValues=$LEM->ProcessCurrentResponses();
$message = '';
if (!$force && $LEM->currentGroupSeq != -1 && $seq > $LEM->currentGroupSeq) // only re-validate if jumping forward
{
Expand All @@ -2399,7 +2437,7 @@ static function JumpTo($seq,$force=false,$preview=false) {
if (!is_null($result) && ($result['mandViolation'] || !$result['valid']))
{
// redisplay the current group
$message .= $LEM->UpdateValuesInDatabase($updatedValues);
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'finished'=>false,
Expand All @@ -2419,7 +2457,7 @@ static function JumpTo($seq,$force=false,$preview=false) {
$LEM->currentQset = array(); // reset active list of questions
if (++$LEM->currentGroupSeq >= $LEM->numGroups)
{
$message .= $LEM->UpdateValuesInDatabase($updatedValues);
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'finished'=>true,
Expand All @@ -2444,7 +2482,7 @@ static function JumpTo($seq,$force=false,$preview=false) {
else
{
// display new group
$message .= $LEM->UpdateValuesInDatabase($updatedValues);
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'finished'=>false,
Expand All @@ -2461,8 +2499,7 @@ static function JumpTo($seq,$force=false,$preview=false) {
break;
case 'question':
$LEM->StartProcessingPage();
$LEM->ProcessCurrentResponses();
$updatedValues=array();
$updatedValues=$LEM->ProcessCurrentResponses();
$message = '';
if (!$force && $LEM->currentQuestionSeq != -1 && $seq > $LEM->currentQuestionSeq)
{
Expand All @@ -2472,7 +2509,7 @@ static function JumpTo($seq,$force=false,$preview=false) {
if ($result['mandViolation'] || !$result['valid'])
{
// redisplay the current question
$message .= $LEM->UpdateValuesInDatabase($updatedValues);
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'finished'=>false,
Expand All @@ -2493,7 +2530,7 @@ static function JumpTo($seq,$force=false,$preview=false) {
$LEM->currentQset = array(); // reset active list of questions
if (++$LEM->currentQuestionSeq >= $LEM->numQuestions)
{
$message .= $LEM->UpdateValuesInDatabase($updatedValues);
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'finished'=>true,
Expand Down Expand Up @@ -2532,7 +2569,7 @@ static function JumpTo($seq,$force=false,$preview=false) {
else
{
// display new question
$message .= $LEM->UpdateValuesInDatabase($updatedValues);
$message .= $LEM->_UpdateValuesInDatabase($updatedValues);
$LEM->runtimeTimings[] = array(__METHOD__,(microtime(true) - $now));
return array(
'finished'=>false,
Expand Down Expand Up @@ -3263,7 +3300,10 @@ function _ValidateQuestion($questionSeq)
$sgqa = $LEM->qid2code[$qid]; // there will be only one, since Equation
// Store the result of the Equation in the SESSION
$_SESSION[$sgqa] = $result;
$updatedValues[$sgqa] = $result;
$updatedValues[$sgqa] = array(
'type'=>'*',
'value'=>$result,
);
if ($LEM->debugLevel>=3)
{
$prettyPrintEqn = $LEM->em->GetPrettyPrintString();
Expand Down Expand Up @@ -4371,8 +4411,9 @@ static function ProcessCurrentResponses()
{
$LEM =& LimeExpressionManager::singleton();
if (!isset($LEM->currentQset)) {
return;
return array();
}
$updatedValues=array();
foreach ($LEM->currentQset as $qinfo)
{
$relevant=false;
Expand All @@ -4384,7 +4425,8 @@ static function ProcessCurrentResponses()
if ($relevant && isset($_POST[$sq]))
{
$value = $_POST[$sq];
switch($qinfo['info']['type'])
$type = $qinfo['info']['type'];
switch($type)
{
case 'D': //DATE
break;
Expand All @@ -4399,6 +4441,10 @@ static function ProcessCurrentResponses()
break;
}
$_SESSION[$sq] = $value;
$updatedValues[$sq] = array (
'type'=>$type,
'value'=>$value,
);
}
else {
$_SESSION[$sq] = "";
Expand All @@ -4409,6 +4455,7 @@ static function ProcessCurrentResponses()
{
$_SESSION[$_POST['timerquestion']]=sanitize_float($_POST[$_POST['timerquestion']]);
}
return $updatedValues;
}
}
?>
1 change: 1 addition & 0 deletions group1.php
Expand Up @@ -25,6 +25,7 @@
'anonymized'=>($thissurvey['anonymized']!='N'),
'datestamp'=>($thissurvey['datestamp']=='Y'),
'ipaddr'=>($thissurvey['ipaddr']=='Y'),
'timeadjust'=>(isset($timeadjust) ? $timeadjust : 0),
);

//Security Checked: POST, GET, SESSION, REQUEST, returnglobal, DB
Expand Down
12 changes: 6 additions & 6 deletions index.php
Expand Up @@ -870,12 +870,12 @@
// SAVE POSTED ANSWERS TO DATABASE IF MOVE (NEXT,PREV,LAST, or SUBMIT) or RETURNING FROM SAVE FORM
if (isset($move) || isset($_POST['saveprompt']))
{
if ($thissurvey['format'] == 'G') {
require_once("save.php"); // save1.php
}
else {
require_once("save.php");
}
// if ($thissurvey['format'] == 'G') {
// require_once("save.php"); // save1.php
// }
// else {
// require_once("save.php");
// }

// RELOAD THE ANSWERS INCASE SOMEONE ELSE CHANGED THEM
if ($thissurvey['active'] == "Y" &&
Expand Down

0 comments on commit 3d03321

Please sign in to comment.