Skip to content

Commit

Permalink
Fixed issue #06235: Javascript errors when use equations for min/max …
Browse files Browse the repository at this point in the history
…num values in Ranking questions
  • Loading branch information
TMSWhite committed Jun 19, 2012
1 parent 6180af8 commit d9244e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 59 deletions.
12 changes: 3 additions & 9 deletions classes/expressions/LimeExpressionManager.php
Expand Up @@ -1443,6 +1443,7 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
case 'K': //MULTIPLE NUMERICAL QUESTION
case 'Q': //MULTIPLE SHORT TEXT
case 'M': //Multiple choice checkbox
case 'R': //RANKING STYLE
if ($this->sgqaNaming)
{
$sq_name = substr($sq['jsVarName'],4) . '.NAOK';
Expand All @@ -1464,10 +1465,6 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
}
}
break;
case 'R': //RANKING STYLE
// TODO - does not have sub-questions, so how should this be done?
// Current JavaScript works fine, but can't use expression value
break;
default:
break;
}
Expand Down Expand Up @@ -1532,6 +1529,7 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
case 'K': //MULTIPLE NUMERICAL QUESTION
case 'Q': //MULTIPLE SHORT TEXT
case 'M': //Multiple choice checkbox
case 'R': //RANKING STYLE
if ($this->sgqaNaming)
{
$sq_name = substr($sq['jsVarName'],4) . '.NAOK';
Expand All @@ -1553,10 +1551,6 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
}
}
break;
case 'R': //RANKING STYLE
// TODO - does not have sub-questions, so how should this be done?
// Current JavaScript works fine, but can't use expression value
break;
default:
break;
}
Expand Down Expand Up @@ -2984,7 +2978,7 @@ private function setVariableAndTokenMappingsForExpressionManager($surveyid,$forc
$varName = $fielddata['title'] . '_' . $fielddata['aid'];
$question = $fielddata['subquestion'];
// $question = $fielddata['question'] . ': ' . $fielddata['subquestion'];
if ($type != 'H' && $type != 'R') {
if ($type != 'H') {
if ($type == 'P' && preg_match("/comment$/", $sgqa)) {
// $rowdivid = substr($sgqa,0,-7);
}
Expand Down
68 changes: 18 additions & 50 deletions qanda.php
Expand Up @@ -155,12 +155,6 @@ function retrieveAnswers($ia)
if (count($values[1]) > 1 && $qidattributes['hide_tip']==0)
{
$question_text['help'] = $clang->gT("Click on an item in the list on the left, starting with your highest ranking item, moving through to your lowest ranking item.");
if (trim($qidattributes['min_answers'])!='')
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. sprintf($clang->ngT("Check at least %d item","Check at least %d items",$qidattributes['min_answers']),$qidattributes['min_answers'])."</span>";
$question_text['help'] .=' '.sprintf($clang->ngT("Check at least %d item","Check at least %d items",$qidattributes['min_answers']),$qidattributes['min_answers']);
}
}
break;
case 'M': //Multiple choice checkbox
Expand Down Expand Up @@ -1944,16 +1938,28 @@ function do_ranking($ia)
if (trim($qidattributes["max_answers"])!='')
{
$max_answers=trim($qidattributes["max_answers"]);
$max_ans_val = LimeExpressionManager::ProcessString('{'.$max_answers.'}',$ia[0]);
if (!is_numeric($max_ans_val)) // this happens when try to do dynamic max ranking values and the starting value is blank
{
$max_ans_val = $anscount;
}
} else {
$max_answers=$anscount;
$max_ans_val = $anscount;
}
$finished=$anscount-$max_answers;

$answer .= "\t<script type='text/javascript'>\n"
. "\t<!--\n"
. "function rankthis_{$ia[0]}(\$code, \$value)\n"
. "\t{\n"
. "\t\$index=document.getElementById('CHOICES_{$ia[0]}').selectedIndex;\n"
. "\tfor (i=1; i<=$max_answers; i++)\n"
. "\tvar maxval = (LEMempty(LEMval('$max_answers')) ? $anscount : Math.floor(LEMval('$max_answers')));\n"
. "\tif (($anscount - document.getElementById('CHOICES_{$ia[0]}').options.length) >= maxval) {\n"
. "\t\tdocument.getElementById('CHOICES_{$ia[0]}').disabled=true;\n"
. "\t\tdocument.getElementById('CHOICES_{$ia[0]}').selectedIndex=-1;\n"
. "\t\treturn true;\n"
. "\t}\n"
. "\tfor (i=1; i<=maxval; i++)\n"
. "{\n"
. "\$b=i;\n"
. "\$b += '';\n"
Expand All @@ -1973,10 +1979,10 @@ function do_ranking($ia)
. "\t\t\t\t\t\t\t\t\tdocument.getElementById('CHOICES_{$ia[0]}').options[b] = null;\n"
. "\t\t\t\t\t\t\t\t\t}\n"
. "\t\t\t\t\t\t\t\t}\n"
. "\t\t\t\t\t\t\ti=$max_answers;\n"
. "\t\t\t\t\t\t\ti=maxval;\n"
. "\t\t\t\t\t\t\t}\n"
. "\t\t\t\t\t\t}\n"
. "\t\t\t\t\tif (document.getElementById('CHOICES_{$ia[0]}').options.length == $finished)\n"
. "\t\t\t\t\tif (document.getElementById('CHOICES_{$ia[0]}').options.length == ($anscount - maxval))\n"
. "\t\t\t\t\t\t{\n"
. "\t\t\t\t\t\tdocument.getElementById('CHOICES_{$ia[0]}').disabled=true;\n"
. "\t\t\t\t\t\t}\n"
Expand Down Expand Up @@ -2030,7 +2036,7 @@ function do_ranking($ia)
$existing++;
}
}
for ($i=1; $i<=$max_answers; $i++)
for ($i=1; $i<=floor($max_ans_val); $i++)
{
$myfname = $ia[1].$i;
if (isset($_SESSION[$myfname]) && $_SESSION[$myfname])
Expand Down Expand Up @@ -2081,7 +2087,7 @@ function do_ranking($ia)
{
if (!in_array($ans, $chosen))
{
$choicelist .= "\t\t\t\t\t\t\t<option value='{$ans[0]}'>{$ans[1]}</option>\n";
$choicelist .= "\t\t\t\t\t\t\t<option id='javatbd{$ia[1]}{$ans[0]}' value='{$ans[0]}'>{$ans[1]}</option>\n";
}
if (strlen($ans[1]) > $maxselectlength) {$maxselectlength = strlen($ans[1]);}
}
Expand Down Expand Up @@ -2114,44 +2120,6 @@ function do_ranking($ia)
. "</tr>\n"
. "\t</table>\n";

if (trim($qidattributes["min_answers"])!='')
{
$minansw=trim($qidattributes["min_answers"]);
if(!isset($showpopups) || $showpopups == 0)
{
$answer .= "<div id='rankingminanswarning{$ia[0]}' style='display: none; color: red' class='errormandatory'>"
.sprintf($clang->ngT('Please rank at least %d item for question "%s".','Please rank at least %d items for question "%s".',$minansw),$minansw, trim(str_replace(array("\n", "\r"), "", $ia[3])))."</div>";
}
$minanswscript = "<script type='text/javascript'>\n"
. " <!--\n"
. " oldonsubmit_{$ia[0]} = document.limesurvey.onsubmit;\n"
. " function ensureminansw_{$ia[0]}()\n"
. " {\n"
. " count={$anscount} - document.getElementById('CHOICES_{$ia[0]}').options.length;\n"
. " if (count < {$minansw} && $('#relevance{$ia[0]}').val()==1){\n";
if(!isset($showpopups) || $showpopups == 0)
{
$minanswscript .= "\n
document.getElementById('rankingminanswarning{$ia[0]}').style.display='';\n";
} else {
$minanswscript .="
alert('".sprintf($clang->ngT('Please rank at least %d item for question "%s".', 'Please rank at least %d items for question "%s".', $minansw,'js'),$minansw, trim(javascript_escape(str_replace(array("\n", "\r"), "",$ia[3]),true,true)))."');\n";
}
$minanswscript .= ""
. " return false;\n"
. " } else {\n"
. " if (oldonsubmit_{$ia[0]}){\n"
. " return oldonsubmit_{$ia[0]}();\n"
. " }\n"
. " return true;\n"
. " }\n"
. " }\n"
. " document.limesurvey.onsubmit = ensureminansw_{$ia[0]}\n"
. " -->\n"
. " </script>\n";
$answer = $minanswscript . $answer;
}

return array($answer, $inputnames);
}

Expand Down

0 comments on commit d9244e2

Please sign in to comment.