From 3d03321af8a75e13c66bdc48e81274f2723c9033 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Thu, 1 Dec 2011 23:27:47 +0000 Subject: [PATCH] Dev EM-based validation and navigation sub-system: 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 --- classes/eval/LimeExpressionManager.php | 117 +++++++++++++++++-------- group1.php | 1 + index.php | 12 +-- 3 files changed, 89 insertions(+), 41 deletions(-) diff --git a/classes/eval/LimeExpressionManager.php b/classes/eval/LimeExpressionManager.php index 9e8df8062ef..3402f373e5b 100644 --- a/classes/eval/LimeExpressionManager.php +++ b/classes/eval/LimeExpressionManager.php @@ -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(); @@ -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) { @@ -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': @@ -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, @@ -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, @@ -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, @@ -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, @@ -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': @@ -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) { @@ -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, @@ -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, @@ -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, @@ -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) { @@ -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, @@ -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, @@ -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, @@ -2313,12 +2315,41 @@ static function NavigateForwards($force=false) { * Write values to database. * @param $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.
".$connect->ErrorMsg() . "
"; + } + } + if (count($updatedValues) > 0) { - $query = 'UPDATE '.db_table_name('survey_' . $this->sid) . " SET "; + $query = 'UPDATE '.$this->surveyOptions['tablename'] . " SET "; switch ($this->surveyMode) { case 'question': @@ -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); @@ -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() . '
'; + } } 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 $seq * @param $force - if true, then skip validation of current group (e.g. will jump even if there are errors) * @param $preview - if true, then treat this group/question as relevant, even if it is not, so that it can be displayed @@ -2380,7 +2420,6 @@ 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': @@ -2388,8 +2427,7 @@ static function JumpTo($seq,$force=false,$preview=false) { 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 { @@ -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, @@ -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, @@ -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, @@ -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) { @@ -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, @@ -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, @@ -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, @@ -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(); @@ -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; @@ -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; @@ -4399,6 +4441,10 @@ static function ProcessCurrentResponses() break; } $_SESSION[$sq] = $value; + $updatedValues[$sq] = array ( + 'type'=>$type, + 'value'=>$value, + ); } else { $_SESSION[$sq] = ""; @@ -4409,6 +4455,7 @@ static function ProcessCurrentResponses() { $_SESSION[$_POST['timerquestion']]=sanitize_float($_POST[$_POST['timerquestion']]); } + return $updatedValues; } } ?> diff --git a/group1.php b/group1.php index eea83d79a56..8fd5c6af72e 100644 --- a/group1.php +++ b/group1.php @@ -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 diff --git a/index.php b/index.php index c7bfb9fa12b..61f9847c91e 100644 --- a/index.php +++ b/index.php @@ -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" &&