Skip to content

Commit

Permalink
Fixed issue #14400: Unable to use self ot that fixed string in Expres…
Browse files Browse the repository at this point in the history
…sion (#1202)

Dev: add self,that and this in reserved var for Question rules
Fixed issue : don't replace question title starting with self or that in expression
  • Loading branch information
Shnoulle committed Jan 9, 2019
1 parent 7677de1 commit ab247ba
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 82 deletions.
45 changes: 34 additions & 11 deletions application/helpers/expressions/em_core_helper.php
Expand Up @@ -1755,23 +1755,46 @@ public function sProcessStringContainingExpressionsHelper($src, $questionNum, $s
}

/**
* If the equation contains refernece to this, expand to comma separated list if needed.
* If the equation contains reference to this, expand to comma separated list if needed.
* @param string $src
*/
function ExpandThisVar($src)
{
$splitter = '(?:\b(?:self|that))(?:\.(?:[A-Z0-9_]+))*';
$parts = preg_split("/(".$splitter.")/i", $src, -1, (PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE));
$result = '';
foreach ($parts as $part) {
if (preg_match("/".$splitter."/", $part)) {
$result .= LimeExpressionManager::GetAllVarNamesForQ($this->questionSeq, $part);
} else {
$result .= $part;
$expandedVar = "";
$tokens = $this->Tokenize($src,1);
foreach ($tokens as $token) {
switch ($token[2]) {
case 'SGQA':
case 'WORD':
$splitter = '(?:\b(?:self|that))(?:\.(?:[A-Z0-9_]+))*'; // self or that, optionnaly followed by dot and alnum
if (preg_match("/".$splitter."/", $token[0])) {
$expandedVar .= LimeExpressionManager::GetAllVarNamesForQ($this->questionSeq, $token[0]);
} else {
$expandedVar .= $token[0];
}
break;
case 'DQ_STRING';
$expandedVar .= "\"{$token[0]}\"";
break;
case 'SQ_STRING';
$expandedVar .= "'{$token[0]}'";
break;
case 'SPACE':
case 'LP':
case 'RP':
case 'COMMA':
case 'AND_OR':
case 'COMPARE':
case 'NUMBER':
case 'NOT':
case 'OTHER':
case 'ASSIGN':
case 'BINARYOP':
default:
$expandedVar .= $token[0];
}
}

return $result;
return $expandedVar;
}

/**
Expand Down
91 changes: 27 additions & 64 deletions application/helpers/expressions/em_manager_helper.php
Expand Up @@ -4790,123 +4790,86 @@ public static function GetLastPrettyPrintExpression()
public static function GetAllVarNamesForQ($qseq,$varname)
{
$LEM =& LimeExpressionManager::singleton();

$parts = explode('.',$varname);
$qroot = '';
$suffix = '';
$sqpatts = array();
$nosqpatts = array();
$comments = '';

if ($parts[0] == 'self')
{
if ($parts[0] == 'self') {
$type = 'self';
}
else
{
} else {
$type = 'that';
array_shift($parts);
if (isset($parts[0]))
{
if (isset($parts[0])) {
$qroot = $parts[0];
}
else
{
} else {
return $varname;
}
}
array_shift($parts);

if (count($parts) > 0)
{
if (preg_match('/^' . ExpressionManager::$RDP_regex_var_attr . '$/',$parts[count($parts)-1]))
{
if (count($parts) > 0) {
if (preg_match('/^' . ExpressionManager::$RDP_regex_var_attr . '$/',$parts[count($parts)-1])) {
$suffix = '.' . $parts[count($parts)-1];
array_pop($parts);
}
}

foreach($parts as $part)
{
if ($part == 'nocomments')
{
foreach($parts as $part) {
if ($part == 'nocomments') {
$comments = 'N';
}
else if ($part == 'comments')
{
} elseif ($part == 'comments') {
$comments = 'Y';
}
else if (preg_match('/^sq_.+$/',$part))
{
} elseif (preg_match('/^sq_.+$/',$part)) {
$sqpatts[] = substr($part,3);
}
else if (preg_match('/^nosq_.+$/',$part))
{
} elseif (preg_match('/^nosq_.+$/',$part)) {
$nosqpatts[] = substr($part,5);
}
else
{
} else {
return $varname; // invalid
}
}
$sqpatt = implode('|',$sqpatts);
$nosqpatt = implode('|',$nosqpatts);
$vars = array();
if(isset($LEM->knownVars))
{
foreach ($LEM->knownVars as $kv)
{
if ($type == 'self')
{
if (!isset($kv['qseq']) || $kv['qseq'] != $qseq || trim($kv['sgqa']) == '')
{
if(isset($LEM->knownVars)) {
foreach ($LEM->knownVars as $kv) {
if ($type == 'self') {
if (!isset($kv['qseq']) || $kv['qseq'] != $qseq || trim($kv['sgqa']) == '') {
continue;
}
}
else
{
if (!isset($kv['rootVarName']) || $kv['rootVarName'] != $qroot)
{
} else {
if (!isset($kv['rootVarName']) || $kv['rootVarName'] != $qroot) {
continue;
}
}
if ($comments != '')
{
if ($comments == 'Y' && !preg_match('/comment$/',$kv['sgqa']))
{
if ($comments != '') {
if ($comments == 'Y' && !preg_match('/comment$/',$kv['sgqa'])) {
continue;
}
if ($comments == 'N' && preg_match('/comment$/',$kv['sgqa']))
{
if ($comments == 'N' && preg_match('/comment$/',$kv['sgqa'])) {
continue;
}
}
$sgq = $LEM->sid . 'X' . $kv['gid'] . 'X' . $kv['qid'];
$ext = (string)substr($kv['sgqa'],strlen($sgq));

if ($sqpatt != '')
{
if (!preg_match('/'.$sqpatt.'/',$ext))
{
if ($sqpatt != '') {
if (!preg_match('/'.$sqpatt.'/',$ext)) {
continue;
}
}
if ($nosqpatt != '')
{
if (preg_match('/'.$nosqpatt.'/',$ext))
{
if ($nosqpatt != '') {
if (preg_match('/'.$nosqpatt.'/',$ext)) {
continue;
}
}

$vars[] = $kv['sgqa'] . $suffix;
}
}
if (count($vars) > 0)
{
if (count($vars) > 0) {
return implode(',',$vars);
}
return $varname; // invalid
return $varname; // invalid
}

/**
Expand Down
11 changes: 4 additions & 7 deletions application/models/Question.php
Expand Up @@ -171,13 +171,10 @@ public function rules()
/* ExpressionManager reserved word (partial) */
$aRules[] = array('title', 'in', 'not' => true,
'range' => array(
'LANG',
'SID',
'QID',
'GID',
'SAVEDID',
'SGQ',
'TOKEN',
'LANG','SID', // Global var
'SAVEDID','TOKEN', // current survey related var
'QID','GID','SGQ', // current question related var
'self','that','this', // EM reserved variables
),
'message'=> sprintf(gT("Code: '%s' is a reserved word."),$this->title), // Usage of {attribute} need attributeLabels, {value} never exist in message
'except' => 'archiveimport'
Expand Down

0 comments on commit ab247ba

Please sign in to comment.