/**
  * If $qid is set, returns the relevance equation generated from conditions (or NULL if there are no conditions for that $qid)
  * If $qid is NULL, returns an array of relevance equations generated from Condition, keyed on the question ID
  * @param <integer> $surveyId
  * @param <integer> $qid - if passed, only generates relevance equation for that question - otherwise genereates for all questions with conditions
  * @return array of generated relevance strings, indexed by $qid
  */
 public static function ConvertConditionsToRelevance($surveyId = NULL, $qid = NULL)
 {
     $query = LimeExpressionManager::getConditionsForEM($surveyId, $qid);
     $_qid = -1;
     $relevanceEqns = array();
     $scenarios = array();
     $relAndList = array();
     $relOrList = array();
     foreach ($query->readAll() as $row) {
         $row['method'] = trim($row['method']);
         //For Postgres
         if ($row['qid'] != $_qid) {
             // output the values for prior question is there was one
             if ($_qid != -1) {
                 if (count($relOrList) > 0) {
                     $relAndList[] = '(' . implode(' or ', $relOrList) . ')';
                 }
                 if (count($relAndList) > 0) {
                     $scenarios[] = '(' . implode(' and ', $relAndList) . ')';
                 }
                 $relevanceEqn = implode(' or ', $scenarios);
                 $relevanceEqns[$_qid] = $relevanceEqn;
             }
             // clear for next question
             $_qid = $row['qid'];
             $_scenario = $row['scenario'];
             $_cqid = $row['cqid'];
             $_subqid = -1;
             $relAndList = array();
             $relOrList = array();
             $scenarios = array();
             $releqn = '';
         }
         if ($row['scenario'] != $_scenario) {
             if (count($relOrList) > 0) {
                 $relAndList[] = '(' . implode(' or ', $relOrList) . ')';
             }
             $scenarios[] = '(' . implode(' and ', $relAndList) . ')';
             $relAndList = array();
             $relOrList = array();
             $_scenario = $row['scenario'];
             $_cqid = $row['cqid'];
             $_subqid = -1;
         }
         if ($row['cqid'] != $_cqid) {
             $relAndList[] = '(' . implode(' or ', $relOrList) . ')';
             $relOrList = array();
             $_cqid = $row['cqid'];
             $_subqid = -1;
         }
         // fix fieldnames
         if ($row['type'] == '' && preg_match('/^{.+}$/', $row['cfieldname'])) {
             $fieldname = substr($row['cfieldname'], 1, -1);
             // {TOKEN:xxxx}
             $subqid = $fieldname;
             $value = $row['value'];
         } else {
             if ($row['type'] == 'M' || $row['type'] == 'P') {
                 if (substr($row['cfieldname'], 0, 1) == '+') {
                     // if prefixed with +, then a fully resolved name
                     $fieldname = substr($row['cfieldname'], 1) . '.NAOK';
                     $subqid = $fieldname;
                     $value = $row['value'];
                 } else {
                     // else create name by concatenating two parts together
                     $fieldname = $row['cfieldname'] . $row['value'] . '.NAOK';
                     $subqid = $row['cfieldname'];
                     $value = 'Y';
                 }
             } else {
                 $fieldname = $row['cfieldname'] . '.NAOK';
                 $subqid = $fieldname;
                 $value = $row['value'];
             }
         }
         if ($_subqid != -1 && $_subqid != $subqid) {
             $relAndList[] = '(' . implode(' or ', $relOrList) . ')';
             $relOrList = array();
         }
         $_subqid = $subqid;
         // fix values
         if (preg_match('/^@\\d+X\\d+X\\d+.*@$/', $value)) {
             $value = substr($value, 1, -1);
         } else {
             if (preg_match('/^{.+}$/', $value)) {
                 $value = substr($value, 1, -1);
             } else {
                 if ($row['method'] == 'RX') {
                     if (!preg_match('#^/.*/$#', $value)) {
                         $value = '"/' . $value . '/"';
                         // if not surrounded by slashes, add them.
                     }
                 } else {
                     $value = '"' . $value . '"';
                 }
             }
         }
         // add equation
         if ($row['method'] == 'RX') {
             $relOrList[] = "regexMatch(" . $value . "," . $fieldname . ")";
         } else {
             // Condition uses ' ' to mean not answered, but internally it is really stored as ''.  Fix this
             if ($value === '" "' || $value == '""') {
                 if ($row['method'] == '==') {
                     $relOrList[] = "is_empty(" . $fieldname . ")";
                 } else {
                     if ($row['method'] == '!=') {
                         $relOrList[] = "!is_empty(" . $fieldname . ")";
                     } else {
                         $relOrList[] = $fieldname . " " . $row['method'] . " " . $value;
                     }
                 }
             } else {
                 if ($value == '"0"' || !preg_match('/^".+"$/', $value)) {
                     switch ($row['method']) {
                         case '==':
                         case '<':
                         case '<=':
                         case '>=':
                             $relOrList[] = '(!is_empty(' . $fieldname . ') && (' . $fieldname . " " . $row['method'] . " " . $value . '))';
                             break;
                         case '!=':
                             $relOrList[] = '(is_empty(' . $fieldname . ') || (' . $fieldname . " != " . $value . '))';
                             break;
                         default:
                             $relOrList[] = $fieldname . " " . $row['method'] . " " . $value;
                             break;
                     }
                 } else {
                     switch ($row['method']) {
                         case '<':
                         case '<=':
                             $relOrList[] = '(!is_empty(' . $fieldname . ') && (' . $fieldname . " " . $row['method'] . " " . $value . '))';
                             break;
                         default:
                             $relOrList[] = $fieldname . " " . $row['method'] . " " . $value;
                             break;
                     }
                 }
             }
         }
         if ($row['cqid'] == 0 && !preg_match('/^{TOKEN:([^}]*)}$/', $row['cfieldname']) || substr($row['cfieldname'], 0, 1) == '+') {
             $_cqid = -1;
             // forces this statement to be ANDed instead of being part of a cqid OR group (except for TOKEN fields)
         }
     }
     // output last one
     if ($_qid != -1) {
         if (count($relOrList) > 0) {
             $relAndList[] = '(' . implode(' or ', $relOrList) . ')';
         }
         if (count($relAndList) > 0) {
             $scenarios[] = '(' . implode(' and ', $relAndList) . ')';
         }
         $relevanceEqn = implode(' or ', $scenarios);
         $relevanceEqns[$_qid] = $relevanceEqn;
     }
     if (is_null($qid)) {
         return $relevanceEqns;
     } else {
         if (isset($relevanceEqns[$qid])) {
             $result = array();
             $result[$qid] = $relevanceEqns[$qid];
             return $result;
         } else {
             return NULL;
         }
     }
 }