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 (#2921)
  • Loading branch information
Shnoulle committed Feb 27, 2023
1 parent 4a5abbc commit 3d974b8
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions application/helpers/expressions/em_core_helper.php
Expand Up @@ -1248,11 +1248,15 @@ public function GetJavaScriptEquivalentOfExpression()
return '';
}
$tokens = $this->RDP_tokens;
/* @var string|null used for ASSIGN expression */
$idToSet = null;
/* @var string[] the final expression line by line (to be join at end) */
$stringParts = array();
$numTokens = count($tokens);

/* Static function management */
/* @var integer bracket count for static function management */
$bracket = 0;
/* @var string static string to be parsed bedfore send to JS */
$staticStringToParse = "";
for ($i = 0; $i < $numTokens; ++$i) {
$token = $tokens[$i]; // When do these need to be quoted?
Expand Down Expand Up @@ -1306,12 +1310,15 @@ public function GetJavaScriptEquivalentOfExpression()
}
} elseif ($i + 1 < $numTokens && $tokens[$i + 1][2] == 'ASSIGN') {
$jsName = $this->GetVarAttribute($token[0], 'jsName', '');
$stringParts[] = "document.getElementById('" . $jsName . "').value";
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]);
$stringParts[] = " = LEMval('" . $varName . "') + ";
++$i;
/* Value is in the page : can not set */
if (!empty($jsName)) {
$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]);
$stringParts[] = " = LEMval('" . $varName . "') + ";
++$i;
}
}
} else {
if (preg_match("/\.(" . $this->getRegexpStaticValidAttributes() . ")$/", $token[0])) {
Expand Down Expand Up @@ -1369,6 +1376,9 @@ public function GetJavaScriptEquivalentOfExpression()
case '!=':
$stringParts[] = ' != ';
break;
case '=':
/* ASSIGN : usage jquery: don't add anything (disable default) */;
break;
default:
$stringParts[] = ' ' . $token[0] . ' ';
break;
Expand All @@ -1392,6 +1402,10 @@ public function GetJavaScriptEquivalentOfExpression()
}
}
$mainClause = implode('', $stringParts);
if ($idToSet) {
/* If there are an id to set (assign) : set it via jquery */
$mainClause = "$('#{$idToSet}').val({$mainClause})";
}
$varsUsed = implode("', '", $nonNAvarsUsed);
if ($varsUsed != '') {
$this->jsExpression = "LEMif(LEManyNA('" . $varsUsed . "'),'',(" . $mainClause . "))";
Expand Down

0 comments on commit 3d974b8

Please sign in to comment.