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 Confirmed that all EM-based validations are working (including combinations of regex and min/max criteria)
Dev Confirmed that correct validation messages generated and that only displayed when needed.

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_dev_tms@11543 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
TMSWhite committed Nov 30, 2011
1 parent a3aed59 commit d84f170
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
42 changes: 31 additions & 11 deletions classes/eval/LimeExpressionManager.php
Expand Up @@ -872,7 +872,8 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
case 'S': //SHORT FREE TEXT
case 'T': //LONG FREE TEXT
case 'U': //HUGE FREE TEXT
$sq_name = '(!regexMatch("' . $preg . '", ' . $sq['varName'] . '.NAOK))';
// TODO - should empty always be an option? or require that empty be an explicit option in the regex?
$sq_name = '(if((strlen('.$sq['varName'].'.NAOK)==0),0,!regexMatch("' . $preg . '", ' . $sq['varName'] . '.NAOK)))';
break;
default:
break;
Expand All @@ -889,7 +890,7 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
$validationEqn[$questionNum][] = array(
'qtype' => $type,
'type' => 'preg',
'eqn' => '(count(' . implode(', ', $sq_names) . ') > 0)',
'eqn' => '(sum(' . implode(', ', $sq_names) . ') == 0)',
'qid' => $questionNum,
'tip' => $this->gT('All entries must conform to this regular expression:') . " " . str_replace(array('{','}'),array('{ ',' }'), $preg),
);
Expand Down Expand Up @@ -2490,7 +2491,7 @@ function _ValidateGroup($groupSeq, $debug=false)
. ($grel ? 'relevant ' : " <span style='color:red'>irrelevant</span> ")
. (($ghidden && $grel) ? " <span style='color:red'>always-hidden</span> " : ' ')
. ($gmandViolation ? " <span style='color:red'>(missing a relevant mandatory)</span> " : ' ')
. ($gvalid ? '' : " <span style='color:red'>(missing a relevant mandatory)</span> ")
. ($gvalid ? '' : " <span style='color:red'>(fails at least one validation rule)</span> ")
. "<br/>\n"
. implode('', $messages);
}
Expand Down Expand Up @@ -2642,6 +2643,8 @@ function _ValidateGroup($groupSeq, $debug=false)
'gid' => $LEM->gseq2info[$groupSeq]['gid'],
'anyUnanswered' => ((strlen($unansweredSQList) > 0) ? true : false),
'anyErrors' => (($gmandViolation || !$gvalid) ? true : false),
'valid' => $gvalid,
'mandViolation' => $gmandViolation,
'show' => (($grel && !$ghidden) ? true : false),
);

Expand Down Expand Up @@ -3035,6 +3038,8 @@ function _ValidateQuestion($questionSeq, $debug=false)
$qvalid=true; // assume valid unless discover otherwise
$hasValidationEqn=false;
$prettyPrintValidEqn=''; // assume no validation eqn by default
$validationJS=''; // assume can't generate JavaScript to validate equation
$validTip=''; // default is none
// TODO - when there are multiple questions which each use the same validation, need to know which sub-questions are invalid
if (isset($LEM->qid2validationEqn[$qid]))
{
Expand All @@ -3047,15 +3052,21 @@ function _ValidateQuestion($questionSeq, $debug=false)
{
$qvalid=false; // default to invalid so that can show the error
}
else
{
$validationJS = $LEM->em->GetJavaScriptEquivalentOfExpression();
}
if ($debug)
{
$prettyPrintValidEqn = $LEM->em->GetPrettyPrintString();
}

// Also preview the Validation Tip
$stringToParse = implode('<br/>',$LEM->qid2validationEqn[$qid]['tips']);
// pretty-print them
$LEM->ProcessString($stringToParse, $qid,NULL,false,1,1,false,false);
$prettyPrintValidTip = $LEM->GetLastPrettyPrintExpression(); }
$stringToParse = implode('<br/>',$LEM->qid2validationEqn[$qid]['tips']);
// pretty-print them
$validTip = $LEM->ProcessString($stringToParse, $qid,NULL,false,1,1,false,false);
if ($debug) {
$prettyPrintValidTip = $LEM->GetLastPrettyPrintExpression();
}
}
else
{
Expand All @@ -3079,7 +3090,7 @@ function _ValidateQuestion($questionSeq, $debug=false)
. ($qrel ? 'relevant' : " <span style='color:red'>irrelevant</span> ")
. ($qhidden ? " <span style='color:red'>always-hidden</span> " : ' ')
. (($qInfo['mandatory'] == 'Y')? ' mandatory' : ' ')
. (($hasValidationEqn) ? (!$qvalid ? " <span style='color:red'>(missing a relevant mandatory)</span> " : ' valid') : '')
. (($hasValidationEqn) ? (!$qvalid ? " <span style='color:red'>(fails validation rules)</span> " : ' valid') : '')
. ($qmandViolation ? " <span style='color:red'>(missing a relevant mandatory)</span> " : ' ')
. $prettyPrintRelEqn
. "<br/>\n";
Expand All @@ -3091,13 +3102,21 @@ function _ValidateQuestion($questionSeq, $debug=false)

if ($prettyPrintValidTip != '')
{
$debug_qmessage .= '----Validation Tip: ' . $prettyPrintValidTip . "<br/>\n";
$debug_qmessage .= '----Pretty Validation Tip: ' . $prettyPrintValidTip . "<br/>\n";
}
if ($validTip != '')
{
$debug_qmessage .= '----Validation Tip: ' . $validTip . "<br/>\n";
}

if ($prettyPrintValidEqn != '')
{
$debug_qmessage .= '----Validation Eqn: ' . $prettyPrintValidEqn . "<br/>\n";
}
if ($validationJS != '')
{
$debug_qmessage .= '----Validation JavaScript: ' . $validationJS . "<br/>\n";
}

// what are the database question codes for this question?
$subQList = '{' . implode('}, {', explode('|',$LEM->qid2code[$qid])) . '}';
Expand Down Expand Up @@ -3151,7 +3170,8 @@ function _ValidateQuestion($questionSeq, $debug=false)
'unansweredSQs' => implode('|',$unansweredSQs),
'valid' => $qvalid,
'validEqn' => $prettyPrintValidEqn,
'validTip' => $prettyPrintValidTip,
'validTip' => $validTip,
'validJS' => $validationJS,
'invalidSQs' => (isset($invalidSQs) ? $invalidSQs : ''),
'relevantSQs' => implode('|',$relevantSQs),
'irrelevantSQs' => implode('|',$irrelevantSQs),
Expand Down
9 changes: 5 additions & 4 deletions group1.php
Expand Up @@ -86,6 +86,7 @@
if ($LEMdebugLevel>=2) {
$LEMmsg = $moveResult['message'];
}
$ginfo = LimeExpressionManager::GetGroupIndexInfo($moveResult['gseq']);
}

// We do not keep the participant session anymore when the same browser is used to answer a second time a survey (let's think of a library PC for instance).
Expand Down Expand Up @@ -501,13 +502,13 @@
// TMSW Mandatory -> EM
// TMSW - get question-level error messages - don't call **_popup() directly
// if (isset($notanswered) && $notanswered && $_SESSION['maxstep'] != $_SESSION['step'])
if (isset($notanswered) && $notanswered && $_SESSION['prevstep'] == $_SESSION['step'])
if ($ginfo['mandViolation'] && $_SESSION['prevstep'] == $_SESSION['step'])
{
list($mandatorypopup, $popup)=mandatory_popup($ia, $notanswered);
}

//Display the "validation" popup if necessary
if (isset($notvalidated) && $notvalidated && $_SESSION['prevstep'] == $_SESSION['step'])
if (!$ginfo['valid'] && $_SESSION['prevstep'] == $_SESSION['step'])
{
list($validationpopup, $vpopup)=validation_popup($ia, $notvalidated);
}
Expand Down Expand Up @@ -1315,13 +1316,13 @@ function checkconditions(value, name, type)

//Display the "mandatory" message on page if necessary
// TMSW Mandatory -> EM
if (isset($showpopups) && $showpopups == 1 && isset($notanswered) && is_array($notanswered) && count($notanswered) > 0 && $_SESSION['prevstep'] == $_SESSION['step'])
if (isset($showpopups) && $showpopups == 1 && $ginfo['mandViolation'] && $_SESSION['prevstep'] == $_SESSION['step'])
{
echo "<p><span class='errormandatory'>" . $clang->gT("One or more mandatory questions have not been answered. You cannot proceed until these have been completed.") . "</span></p>";
}

//Display the "validation" message on page if necessary
if (isset($showpopups) && $showpopups == 1 && isset($notvalidated) && is_array($notvalidated) && count($notvalidated) > 0 && $_SESSION['prevstep'] == $_SESSION['step'])
if (isset($showpopups) && $showpopups == 1 && !$ginfo['valid'] && $_SESSION['prevstep'] == $_SESSION['step'])
{
echo "<p><span class='errormandatory'>" . $clang->gT("One or more questions have not been answered in a valid manner. You cannot proceed until these answers are valid.") . "</span></p>";
}
Expand Down
24 changes: 20 additions & 4 deletions qanda1.php
Expand Up @@ -812,11 +812,21 @@ function retrieveAnswers($ia, $notanswered=null, $notvalidated=null, $filenotval
$qtitle .= $mandatory_msg;
$question_text['man_message'] = $mandatory_msg;

$validation_msg = validation_message($ia);
if (($_SESSION['step'] != $_SESSION['maxstep']) || ($_SESSION['step'] == $_SESSION['prevstep'])) {
$validation_msg = validation_message($ia);
}
else {
$validation_msg = '';
}
$qtitle .= $validation_msg;
$question_text['valid_message'] = $validation_msg;

$file_validation_msg = file_validation_message($ia);
if (($_SESSION['step'] != $_SESSION['maxstep']) || ($_SESSION['step'] == $_SESSION['prevstep'])) {
$file_validation_msg = file_validation_message($ia);
}
else {
$file_validation_msg = '';
}
$qtitle .= $ia[4] == "|" ? $file_validation_msg : "";
$question_text['file_valid_message'] = $ia[4] == "|" ? $file_validation_msg : "";

Expand Down Expand Up @@ -964,10 +974,16 @@ function validation_message($ia)
if (!$qinfo['valid']) {
$help = $qinfo['info']['help'];
if (strlen($help) > 0) {
$help .= '<br/>' . $qinfo['validTip'];
$help .= '<br/>';
}
$help .= $qinfo['validTip'];
if (strlen($help) == 0) {
$help = $clang->gT('This question must be answered correctly');
}
else {
$help=' <span class="questionhelp">'.$help.'</span>';
}
return '<br /><span class="errormandatory">'.$clang->gT('This question must be answered correctly').'.'.$help.'</span><br />';
return '<br /><span class="errormandatory">'.$help.'</span><br />';
}
else {
return "";
Expand Down

0 comments on commit d84f170

Please sign in to comment.