Skip to content

Commit

Permalink
Merge pull request #148 from Shnoulle/2.05
Browse files Browse the repository at this point in the history
2.05 : fix 3 issue with numeric question type (javascript and php)
  • Loading branch information
Shnoulle committed Oct 21, 2013
2 parents 910aa2b + 6a3a57b commit 1c644b9
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 104 deletions.
78 changes: 7 additions & 71 deletions application/helpers/SurveyRuntimeHelper.php
Expand Up @@ -975,78 +975,14 @@ function run($surveyid,$args) {

// the runonce element has been changed from a hidden to a text/display:none one
// in order to workaround an not-reproduced issue #4453 (lemeur)
echo "<input type='text' id='runonce' value='0' style='display: none;'/>
<!-- JAVASCRIPT FOR CONDITIONAL QUESTIONS -->
<script type='text/javascript'>
<!--\n";
echo "<input type='text' id='runonce' value='0' style='display: none;'/>";

// lemradix and var for LemRadix
$sLemRadixVarScript=" LEMradix='" . $radix . "';\n"
." numRegex = new RegExp('[^-' + LEMradix + '0-9]','g');\n"
." intRegex = new RegExp('[^-0-9]','g');\n";
Yii::app()->clientScript->registerScript('LemRadixVarScript',$sLemRadixVarScript,CClientScript::POS_HEAD);

echo "var LEMradix='" . $radix . "';\n";
echo "var numRegex = new RegExp('[^-' + LEMradix + '0-9]','g');\n";
echo "var intRegex = new RegExp('[^-0-9]','g');\n";

print <<<END
function fixnum_checkconditions(value, name, type, evt_type, intonly)
{
newval = new String(value);
if (typeof intonly !=='undefined' && intonly==1) {
newval = newval.replace(intRegex,'');
}
else {
newval = newval.replace(numRegex,'');
}
if (LEMradix === ',') {
newval = newval.split(',').join('.');
}
if (newval != '-' && newval != '.' && newval != '-.' && newval != parseFloat(newval)) {
newval = '';
}
displayVal = newval;
if (LEMradix === ',') {
displayVal = displayVal.split('.').join(',');
}
if (name.match(/other$/)) {
$('#answer'+name+'text').val(displayVal);
}
$('#answer'+name).val(displayVal);
if (typeof evt_type === 'undefined')
{
evt_type = 'onchange';
}
checkconditions(newval, name, type, evt_type);
}
function checkconditions(value, name, type, evt_type)
{
if (typeof evt_type === 'undefined')
{
evt_type = 'onchange';
}
if (type == 'radio' || type == 'select-one')
{
$('#java'+name).val(value);
}
else if (type == 'checkbox')
{
if ($('#answer'+name).is(':checked'))
{
$('#java'+name).val('Y');
} else
{
$('#java'+name).val('');
}
}
else if (type == 'text' && name.match(/other$/))
{
$('#java'+name).val(value);
}
ExprMgr_process_relevance_and_tailoring(evt_type,name,type);
END;
print <<<END
}
// -->
</script>
END;
$showpopups=Yii::app()->getConfig('showpopups');
//Display the "mandatory" message on page if necessary
if (!$showpopups && $stepInfo['mandViolation'] && $okToShowErrors)
Expand Down
23 changes: 16 additions & 7 deletions application/helpers/expressions/em_core_helper.php
Expand Up @@ -268,14 +268,22 @@ private function RDP_EvaluateBinary(array $token)
$this->RDP_AddError(gT("Invalid value(s) on the stack"), $token);
return false;
}
// TODO: try to determine datatype?
$bNumericArg1 = is_numeric($arg1[0]) || $arg1[0] == '';
$bNumericArg2 = is_numeric($arg2[0]) || $arg2[0] == '';
// Set bothnumeric only if set to numeric
// Not sure if needed to test if [2] is set. : TODO review
$bNumericArg1 = ((is_numeric($arg1[0]) || $arg1[0] == '') && (!isset($arg1[2]) || $arg1[2]=='NUMBER'));
$bNumericArg2 = ((is_numeric($arg2[0]) || $arg2[0] == '') && (!isset($arg2[2]) || $arg2[2]=='NUMBER'));
$bStringArg1 = !$bNumericArg1 || $arg1[0] == '';
$bStringArg2 = !$bNumericArg2 || $arg2[0] == '';
$bBothNumeric = ($bNumericArg1 && $bNumericArg2);
$bBothString = ($bStringArg1 && $bStringArg2);
$bMismatchType = (!$bBothNumeric && !$bBothString);
$bMismatchType=(!$bBothNumeric && !$bBothString);
if($bMismatchType){// Try same than JS: if can be numeric: convert to numeric else false
if((is_numeric($arg1[0]) || $arg1[0] == '') && (is_numeric($arg2[0]) || $arg2[0] == ''))
{
$bBothNumeric=true;
$bMismatchType=false;
}
}
switch(strtolower($token[0]))
{
case 'or':
Expand All @@ -296,7 +304,7 @@ private function RDP_EvaluateBinary(array $token)
break;
case '<':
case 'lt':
if ($bMismatchType) {
if ($bMismatchType && false) {
$result = array(false,$token[1],'NUMBER');
}
else {
Expand Down Expand Up @@ -335,7 +343,7 @@ private function RDP_EvaluateBinary(array $token)
break;
case '>=';
case 'ge':
if ($bMismatchType) {
if ($bMismatchType && false) {
$result = array(false,$token[1],'NUMBER');
}
else {
Expand Down Expand Up @@ -565,7 +573,8 @@ private function RDP_EvaluateConstantVarOrFunction()
}
if ($relStatus==1)
{
$result = array($this->GetVarAttribute($token[0],NULL,''),$token[1],'NUMBER');
$argtype=($this->GetVarAttribute($token[0],'onlynum'))?"NUMBER":"WORD";
$result = array($this->GetVarAttribute($token[0],NULL,''),$token[1],$argtype);
}
else
{
Expand Down
116 changes: 106 additions & 10 deletions application/helpers/expressions/em_manager_helper.php
Expand Up @@ -1304,6 +1304,33 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
// Default validation for question type
switch ($type)
{
case 'N': //NUMERICAL QUESTION TYPE
case 'K': //NUMERICAL QUESTION TYPE
if ($hasSubqs) {
$subqs = $qinfo['subqs'];
$sq_equs=array();
foreach($subqs as $sq)
{
$sq_name = ($this->sgqaNaming)?$sq['rowdivid'].".NAOK":$sq['varName'].".NAOK";
if(($qinfo['mandatory']=='Y')){
$sq_equs[] = 'is_numeric('.$sq_name.')';
}else{
$sq_equs[] = '( is_numeric('.$sq_name.') || is_empty('.$sq_name.') )';
}
}
if (!isset($validationEqn[$questionNum]))
{
$validationEqn[$questionNum] = array();
}
$validationEqn[$questionNum][] = array(
'qtype' => $type,
'type' => 'default',
'class' => 'default',
'eqn' => implode(' and ',$sq_equs),
'qid' => $questionNum,
);
}
break;
case 'R':
if ($hasSubqs) {
$subqs = $qinfo['subqs'];
Expand Down Expand Up @@ -1927,6 +1954,60 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
$max_num_value_n='';
}

// num_value_int_only
// Validation fixnum(sqN)==int(fixnum(sqN)) : fixnum or not fix num ..... 10.00 == 10
if (isset($qattr['num_value_int_only']) && trim($qattr['num_value_int_only']) == "1")
{
$num_value_int_only="1";
if ($hasSubqs) {
$subqs = $qinfo['subqs'];
$sq_eqns = array();
$subqValidEqns = array();
foreach ($subqs as $sq) {
$sq_eqn=null;
switch ($type)
{
case 'N': //NUMERICAL QUESTION TYPE
case 'K': //MULTI NUMERICAL QUESTION TYPE (Need a attribute, not set in 131014)
$sq_name = ($this->sgqaNaming)?$sq['rowdivid'].".NAOK":$sq['varName'].".NAOK";
if(($qinfo['mandatory']=='Y')){
$sq_eqn = 'is_int('.$sq_name.')';
}else{
$sq_eqn = 'is_int('.$sq_name.') || is_empty('.$sq_name.')';
}
break;
default:
break;
}
if (!is_null($sq_eqn)) {
$sq_eqns[] = $sq_eqn;
$subqValidEqns[$subqValidSelector] = array(
'subqValidEqn' => $sq_eqn,
'subqValidSelector' => $subqValidSelector,
);
}
}
if (count($sq_eqns) > 0) {
if (!isset($validationEqn[$questionNum]))
{
$validationEqn[$questionNum] = array();
}
$validationEqn[$questionNum][] = array(
'qtype' => $type,
'type' => 'num_value_int_only',
'class' => 'value_integer',
'eqn' => implode(' and ', $sq_eqns),
'qid' => $questionNum,
'subqValidEqns' => $subqValidEqns,
);
}
}
}
else
{
$num_value_int_only='';
}

// min_num_value
// Validation:= sum(sq1,...,sqN) >= value (which could be an expression).
if (isset($qattr['min_num_value']) && trim($qattr['min_num_value']) != '')
Expand Down Expand Up @@ -2557,6 +2638,12 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
// Default validation qtip without attribute
switch ($type)
{
case 'N':
$qtips['default']=$this->gT("Only numbers may be entered in this field.");
break;
case 'K':
$qtips['default']=$this->gT("Only numbers may be entered in these fields.");
break;
case 'R':
$qtips['default']=$this->gT("All your answers must be different.");
break;
Expand Down Expand Up @@ -2607,6 +2694,12 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
"{if(!is_empty($_minV) && !is_empty($_maxV) && ($_minV) != ($_maxV), sprintf('".$this->gT("Each answer must be between %s and %s")."', fixnum($_minV), fixnum($_maxV)), '')}";
}

// integer for numeric
if ($num_value_int_only!='')
{
$qtips['value_integer']=$this->gT("Only integer value");
}

// min/max value for each numeric entry - for multi-flexible question type
if ($multiflexible_min!='' || $multiflexible_max!='')
{
Expand Down Expand Up @@ -7733,17 +7826,17 @@ static function ProcessCurrentResponses()
}
$aDateFormatData=getDateFormatDataForQID($aAttributes[$qid],$LEM->surveyOptions);
$oDateTimeConverter = new Date_Time_Converter($value, $aDateFormatData['phpdate']);
$value=$oDateTimeConverter->convert("Y-m-d H:i");
$value=$oDateTimeConverter->convert("Y-m-d H:i"); // TODO : control if inverse function original value
}
break;
case 'N': //NUMERICAL QUESTION TYPE
case 'K': //MULTIPLE NUMERICAL QUESTION
if (trim($value)=="") {
$value = "";
}
else {
$value = sanitize_float($value);
}
# case 'N': //NUMERICAL QUESTION TYPE
# case 'K': //MULTIPLE NUMERICAL QUESTION
# if (trim($value)=="") {
# $value = "";
# }
# else {
# $value = sanitize_float($value);
# }
break;
case '|': //File Upload
if (!preg_match('/_filecount$/', $sq))
Expand Down Expand Up @@ -8116,6 +8209,9 @@ private function _GetVarAttribute($name,$attr,$default,$gseq,$qseq)
$qrel = (isset($_SESSION[$this->sessid]['relevanceStatus'][$qid]) ? $_SESSION[$this->sessid]['relevanceStatus'][$qid] : 0);
$sqrel = (isset($_SESSION[$this->sessid]['relevanceStatus'][$rowdivid]) ? $_SESSION[$this->sessid]['relevanceStatus'][$rowdivid] : 1); // true by default - only want false if a subquestion is irrelevant
return ($grel && $qrel && $sqrel);
case 'onlynum':
return (isset($var[$attr])) ? $var[$attr] : $default;
break;
default:
print 'UNDEFINED ATTRIBUTE: ' . $attr . "<br />\n";
return $default;
Expand Down Expand Up @@ -8224,7 +8320,7 @@ static public function ShowSurveyLogicFile($sid, $gid=NULL, $qid=NULL,$LEMdebugL

$LEM =& LimeExpressionManager::singleton();

$aSurveyInfo=getSurveyInfo($sid, $_SESSION['LEMlang']);
$aSurveyInfo=getSurveyInfo($sid,$_SESSION['LEMlang']);

$allErrors = array();
$warnings = 0;
Expand Down
11 changes: 2 additions & 9 deletions application/helpers/qanda_helper.php
Expand Up @@ -3251,10 +3251,6 @@ function do_multiplenumeric($ia)
$inputnames[]=$myfname;
}
$question_tip = '';
if(!$hidetip)
{
$question_tip .= '<p class="tip default">'.$clang->gT('Only numbers may be entered in these fields')."</p>\n";
}

if (trim($aQuestionAttributes['equals_num_value']) != ''
|| trim($aQuestionAttributes['min_num_value']) != ''
Expand Down Expand Up @@ -3394,7 +3390,7 @@ function do_numerical($ia)
$answertypeclass .= " integeronly";
if(is_numeric($fValue))
{
$fValue=number_format($fValue, 0, '', '');
//$fValue=number_format($fValue, 0, '', '');
}
$integeronly=1;
}
Expand All @@ -3407,6 +3403,7 @@ function do_numerical($ia)

$sSeparator = getRadixPointData($thissurvey['surveyls_numberformat']);
$sSeparator = $sSeparator['separator'];
$fValue = rtrim(trim($fValue,"0"),".");
$fValue = str_replace('.',$sSeparator,$fValue);

if ($thissurvey['nokeyboard']=='Y')
Expand All @@ -3425,10 +3422,6 @@ function do_numerical($ia)
. "<input class='text {$answertypeclass}' type=\"text\" size=\"$tiwidth\" name=\"$ia[1]\" title=\"".$clang->gT('Only numbers may be entered in this field.')."\" "
. "id=\"answer{$ia[1]}\" value=\"{$fValue}\" onkeyup=\"{$checkconditionFunction}(this.value, this.name, this.type,'onchange',{$integeronly})\" "
. " {$maxlength} />\t{$suffix}\n</p>\n";
if ($aQuestionAttributes['hide_tip']==0)
{
$answer .= "<p class=\"tip\">".$clang->gT('Only numbers may be entered in this field.')."</p>\n";
}
// --> END NEW FEATURE - SAVE

$inputnames[]=$ia[1];
Expand Down

0 comments on commit 1c644b9

Please sign in to comment.