Skip to content

Commit

Permalink
Fixed issue #14337: Comparaison String and Numeric is different in sa…
Browse files Browse the repository at this point in the history
…me page and other page

Dev: Fixed issue #08324: Broken numeric interpretation of answer codes.
Dev: If can be number : return a number in JS
Dev: JS with number+"" do a compare by string : then same for PHP
Dev: adding a Survey test
  • Loading branch information
Shnoulle committed Jun 11, 2019
1 parent 0c6425b commit 63765d2
Show file tree
Hide file tree
Showing 4 changed files with 609 additions and 9 deletions.
10 changes: 9 additions & 1 deletion application/helpers/expressions/em_core_helper.php
Expand Up @@ -327,6 +327,7 @@ public function RDP_EvaluateBinary(array $token)
$aForceStringArray = array('DQ_STRING', 'DS_STRING', 'STRING'); // Question can return NUMBER or WORD : DQ and DS is string entered by user, STRING is a result of a String function
if ((isset($arg1[2]) && in_array($arg1[2], $aForceStringArray) || (isset($arg2[2]) && in_array($arg2[2], $aForceStringArray)))) {
$bBothNumeric = false;
$bBothString = true;
$bMismatchType = false;
$arg1[0] = strval($arg1[0]);
$arg2[0] = strval($arg2[0]);
Expand All @@ -353,6 +354,8 @@ public function RDP_EvaluateBinary(array $token)
case 'lt':
if ($bMismatchType) {
$result = array(false, $token[1], 'NUMBER');
} elseif(!$bBothNumeric && $bBothString) {
$result = array(strcmp($arg1[0],$arg2[0]) < 0, $token[1], 'NUMBER');
} else {
$result = array(($arg1[0] < $arg2[0]), $token[1], 'NUMBER');
}
Expand All @@ -365,6 +368,8 @@ public function RDP_EvaluateBinary(array $token)
// Need this explicit comparison in order to be in agreement with JavaScript
if (($arg1[0] == '0' && $arg2[0] == '') || ($arg1[0] == '' && $arg2[0] == '0')) {
$result = array(true, $token[1], 'NUMBER');
} elseif(!$bBothNumeric && $bBothString) {
$result = array(strcmp($arg1[0],$arg2[0]) <= 0, $token[1], 'NUMBER');
} else {
$result = array(($arg1[0] <= $arg2[0]), $token[1], 'NUMBER');
}
Expand All @@ -378,6 +383,8 @@ public function RDP_EvaluateBinary(array $token)
// Need this explicit comparison in order to be in agreement with JavaScript : still needed since we use ==='' ?
if (($arg1[0] == '0' && $arg2[0] == '') || ($arg1[0] == '' && $arg2[0] == '0')) {
$result = array(false, $token[1], 'NUMBER');
} elseif(!$bBothNumeric && $bBothString) {
$result = array(strcmp($arg1[0],$arg2[0]) > 0, $token[1], 'NUMBER');
} else {
$result = array(($arg1[0] > $arg2[0]), $token[1], 'NUMBER');
}
Expand All @@ -387,9 +394,10 @@ public function RDP_EvaluateBinary(array $token)
case 'ge':
if ($bMismatchType) {
$result = array(false, $token[1], 'NUMBER');
} elseif(!$bBothNumeric && $bBothString) {
$result = array(strcmp($arg1[0],$arg2[0]) >= 0, $token[1], 'NUMBER');
} else {
$result = array(($arg1[0] >= $arg2[0]), $token[1], 'NUMBER');

}
break;
case '+':
Expand Down
12 changes: 4 additions & 8 deletions assets/scripts/expressions/em_javascript.js
Expand Up @@ -858,22 +858,18 @@ function LEMval(alias)
{
var length = value.length;
var firstLetterIsNull = value.split("").shift() === '0';
// @todo : use . or , according to LEMradix !
try{
var numtest = new Decimal(value);
} catch(e){
var numtest = new Decimal(value.toString().replace(/,/,'.'));
// Error can still happen maybe but don't catch to know (and fix) it
}

// If value is on same page : value use LEMradix, else use . (dot) : bug #10001
// if (LEMradix === ',' && onSamePage )
// {
// value = numtest.toString().replace(/\./,',');
// }
value = numtest.valueOf();
if(value.length < length && firstLetterIsNull){
value = str_repeat('0', length).substr(0,(length - value.length))+''+value.toString();
}
value = Number(value); /* If it's a number : always return a number */
}
if(LSvar.bNumRealValue) {
return value;
Expand Down Expand Up @@ -908,12 +904,12 @@ function LEMval(alias)
}
return value;
}
else if(!isNaN(parseFloat(newval)) && isFinite(newval))
else if(!isNaN(parseFloat(value)) && isFinite(value))
{
// If it's not a decimal number, just return value
try {
var decimal_safe = new Decimal(value);
return decimal_safe.toPrecision(value.length);
return Number(decimal_safe.toPrecision(value.length));
}
catch (ex) {
}
Expand Down

0 comments on commit 63765d2

Please sign in to comment.