Skip to content

Commit

Permalink
Fixed issue #18407: On-page conditions not working when pre-filling q…
Browse files Browse the repository at this point in the history
…uestion with equation on the same page (#2676)

Dev: ASSIGN didn't work really with javascript,
Dev: Minimal fix to don't throw JS error
  • Loading branch information
Shnoulle committed Feb 21, 2023
1 parent a217fab commit 5dc8d08
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions application/helpers/expressions/em_core_helper.php
Expand Up @@ -1194,6 +1194,8 @@ public function GetJavaScriptEquivalentOfExpression()
}
$tokens = $this->RDP_tokens;
$stringParts = array();
/* @var string|null used for ASSIGN expression */
$idToSet = null;
$numTokens = count($tokens);
for ($i = 0; $i < $numTokens; ++$i) {
$token = $tokens[$i];
Expand All @@ -1217,7 +1219,12 @@ public function GetJavaScriptEquivalentOfExpression()
$stringParts[] = $funcInfo[1]; // the PHP function name
} elseif ($i + 1 < $numTokens && $tokens[$i + 1][2] == 'ASSIGN') {
$jsName = $this->GetVarAttribute($token[0], 'jsName', '');
$stringParts[] = "document.getElementById('".$jsName."').value";
/* Value is not in the page : can not set */
if (empty($jsName)) {
$this->jsExpression = '';
return '';
}
$idToSet = $jsName;
if ($tokens[$i + 1][0] == '+=') {
// Javascript does concatenation unless both left and right side are numbers, so refactor the equation
$varName = $this->GetVarAttribute($token[0], 'varName', $token[0]);
Expand Down Expand Up @@ -1248,6 +1255,7 @@ public function GetJavaScriptEquivalentOfExpression()
default:
// don't need to check type of $token[2] here since already handling SQ_STRING and DQ_STRING above
switch (strtolower($token[0])) {
case '=': /* ASSIGN : usage jquery, */; break;
case 'and': $stringParts[] = ' && '; break;
case 'or': $stringParts[] = ' || '; break;
case 'lt': $stringParts[] = ' < '; break;
Expand All @@ -1264,14 +1272,18 @@ public function GetJavaScriptEquivalentOfExpression()
// for each variable that does not have a default value, add clause to throw error if any of them are NA
$nonNAvarsUsed = array();
foreach ($this->GetVarsUsed() as $var) {
// this function wants to see the NAOK suffix
// this function wants to see the NAOK suffix
if (!preg_match("/^.*\.(NAOK|relevanceStatus)$/", $var)) {
if ($this->GetVarAttribute($var, 'jsName', '') != '') {
$nonNAvarsUsed[] = $var;
}
}
}
$mainClause = implode('', $stringParts);
if ($idToSet) {
/* This set value to the hidden part only */
$mainClause = "$('#{$idToSet}').val({$mainClause})";
}
$varsUsed = implode("', '", $nonNAvarsUsed);
if ($varsUsed != '') {
$this->jsExpression = "LEMif(LEManyNA('".$varsUsed."'),'',(".$mainClause."))";
Expand Down

0 comments on commit 5dc8d08

Please sign in to comment.