From 01015bde42e26e918f569bf269bb22cd75bf55a5 Mon Sep 17 00:00:00 2001 From: Jason Cleeland Date: Sat, 27 Mar 2010 13:45:53 +0000 Subject: [PATCH] Fixed bug #4176 - Array filter does not know how to handle 'other' field git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey@8523 b72ed6b6-b9f8-46b5-92b4-906544132732 --- common.php | 25 ++++++++++++--- group.php | 54 ++++++++++++++++++++++++++++++-- qanda.php | 92 ++++++++++++++++++++++++++++++++++++------------------ survey.php | 51 ++++++++++++++++++++++++++++-- 4 files changed, 182 insertions(+), 40 deletions(-) diff --git a/common.php b/common.php index 98f78f3d945..ddb32c4948b 100644 --- a/common.php +++ b/common.php @@ -4773,6 +4773,7 @@ function getArrayFiltersForQuestion($qid) $qid=sanitize_int($qid); $query = "SELECT value FROM ".db_table_name('question_attributes')." WHERE attribute='array_filter' AND qid='".$qid."'"; $result = db_execute_assoc($query); //Checked + if ($result->RecordCount() == 1) // We Found a array_filter attribute { $val = $result->FetchRow(); // Get the Value of the Attribute ( should be a previous question's title in same group ) @@ -4790,6 +4791,14 @@ function getArrayFiltersForQuestion($qid) if ((isset($_SESSION[$fields[1].$code['code']]) && $_SESSION[$fields[1].$code['code']] == "Y") || $_SESSION[$fields[1]] == $code['code']) array_push($selected,$code['code']); } + //Now we also need to find out if (a) the question had "other" enabled, and (b) if that was selected + $query = "SELECT other FROM ".db_table_name('questions')." where qid='{$fields[0]}'"; + $qresult = db_execute_assoc($query); + while ($row=$qresult->fetchRow()) {$other=$row['other'];} + if($other == "Y") + { + if($_SESSION[$fields[1].'other'] != "") {array_push($selected, "other");} + } return $selected; } } @@ -4831,7 +4840,7 @@ function getArrayFilterExcludesForQuestion($qid) $query = "SELECT value FROM ".db_table_name('question_attributes')." WHERE attribute='array_filter_exclude' AND qid='".$qid."'"; $result = db_execute_assoc($query); //Checked $excludevals=array(); - if ($result->RecordCount() == 1) // We Found a array_filter attribute + if ($result->RecordCount() == 1) // We Found a array_filter_exclude attribute { $selected=array(); $excludevals[] = $result->FetchRow(); // Get the Value of the Attribute ( should be a previous question's title in same group ) @@ -4848,11 +4857,11 @@ function getArrayFilterExcludesForQuestion($qid) /* For each $val (question title) that applies to this, check what values exist and add them to the $selected array */ foreach ($excludevals as $val) { - foreach ($_SESSION['fieldarray'] as $fields) + foreach ($_SESSION['fieldarray'] as $fields) //iterate through every question in the survey { if ($fields[2] == $val['value']) - { - // we found the target question, now we need to know what the answers where, we know its a multi! + { + // we found the target question, now we need to know what the answers were! $fields[0]=sanitize_int($fields[0]); $query = "SELECT code FROM ".db_table_name('answers')." where qid='{$fields[0]}' AND language='".$_SESSION['s_lang']."' order by sortorder"; $qresult = db_execute_assoc($query); //Checked @@ -4861,6 +4870,14 @@ function getArrayFilterExcludesForQuestion($qid) if ((isset($_SESSION[$fields[1].$code['code']]) && $_SESSION[$fields[1].$code['code']] == "Y") || $_SESSION[$fields[1]] == $code['code']) array_push($selected,$code['code']); } + //Now we also need to find out if (a) the question had "other" enabled, and (b) if that was selected + $query = "SELECT other FROM ".db_table_name('questions')." where qid='{$fields[0]}'"; + $qresult = db_execute_assoc($query); + while ($row=$qresult->fetchRow()) {$other=$row['other'];} + if($other == "Y") + { + if($_SESSION[$fields[1].'other'] != "") {array_push($selected, "other");} + } } } } diff --git a/group.php b/group.php index 82a44cd901c..3229fe8d6e8 100644 --- a/group.php +++ b/group.php @@ -473,7 +473,7 @@ function checkconditions(value, name, type) if (type == 'checkbox') { var hiddenformname='java'+name; - var chkname='answer'+name; + var chkname='answer'+name; if (document.getElementById(chkname).checked) { document.getElementById(hiddenformname).value='Y'; @@ -895,10 +895,12 @@ function checkconditions(value, name, type) $qfbase = $surveyid."X".$gid."X".$attralist['fid']; if ($attralist['type'] == "M") { - $qquery = "SELECT {$dbprefix}answers.code, {$dbprefix}questions.type FROM {$dbprefix}answers, {$dbprefix}questions WHERE {$dbprefix}answers.qid={$dbprefix}questions.qid AND {$dbprefix}answers.qid='".$attralist['qid']."' AND {$dbprefix}answers.language='".$_SESSION['s_lang']."' order by code;"; + $qquery = "SELECT {$dbprefix}answers.code, {$dbprefix}questions.type, {$dbprefix}questions.other FROM {$dbprefix}answers, {$dbprefix}questions WHERE {$dbprefix}answers.qid={$dbprefix}questions.qid AND {$dbprefix}answers.qid='".$attralist['qid']."' AND {$dbprefix}answers.language='".$_SESSION['s_lang']."' order by code;"; $qresult = db_execute_assoc($qquery); //Checked + $other=null; while ($fansrows = $qresult->FetchRow()) { + if($fansrows['other']== "Y") $other="Y"; if(strpos($array_filter_types, $fansrows['type']) === false) {} else { $fquestans = "java".$qfbase.$fansrows['code']; @@ -922,6 +924,28 @@ function checkconditions(value, name, type) $appendj .= "\t}\n"; } } + + if($other=="Y") { + $fquestans = "answer".$qfbase."other"; + $tbody = "javatbd".$qbase."other"; + $dtbody = "tbdisp".$qbase."other"; + $tbodyae = $qbase."other"; + $appendj .= "\n"; + $appendj .= "\tif (document.getElementById('$fquestans').value !== '')\n"; + $appendj .= "\t{\n"; + $appendj .= "\t\tdocument.getElementById('$tbody').style.display='';\n"; + $appendj .= "\t\t$('#$dtbody').val('on');\n"; + $appendj .= "\t}\n"; + $appendj .= "\telse\n"; + $appendj .= "\t{\n"; + $appendj .= "\t\tdocument.getElementById('$tbody').style.display='none';\n"; + $appendj .= "\t\t$('#$dtbody').val('off');\n"; + // This line resets the text fields in the hidden row + $appendj .= "\t\t$('#$tbody input[type=text]').val('');"; + // This line resets any radio group in the hidden row + $appendj .= "\t\t$('#$tbody input[type=radio]').attr('checked', false); "; + $appendj .= "\t}\n"; + } } } $java .= $appendj; @@ -931,15 +955,17 @@ function checkconditions(value, name, type) $qfbase = $surveyid."X".$gid."X".$attralist['fid']; if ($attralist['type'] == "M") { - $qquery = "SELECT {$dbprefix}answers.code, {$dbprefix}questions.type + $qquery = "SELECT {$dbprefix}answers.code, {$dbprefix}questions.type, {$dbprefix}questions.other FROM {$dbprefix}answers, {$dbprefix}questions WHERE {$dbprefix}answers.qid={$dbprefix}questions.qid AND {$dbprefix}answers.qid='".$attralist['qid']."' AND {$dbprefix}answers.language='".$_SESSION['s_lang']."' ORDER BY code;"; $qresult = db_execute_assoc($qquery); //Checked + $other=null; while ($fansrows = $qresult->FetchRow()) { + if($fansrows['other']== "Y") $other="Y"; if(strpos($array_filter_exclude_types, $fansrows['type']) === false) {} else { $fquestans = "java".$qfbase.$fansrows['code']; @@ -953,6 +979,7 @@ function checkconditions(value, name, type) /* If this question is a cascading question, then it also needs to check the status of the question that this one relies on */ if(isset($array_filterXqs_cascades[$attralist['qid']])) { + foreach($array_filterXqs_cascades[$attralist['qid']] as $cascader) { $cascadefqa ="java".$surveyid."X".$gid."X".$cascader.$fansrows['code']; @@ -977,6 +1004,27 @@ function checkconditions(value, name, type) $appendj .= "\t}\n"; } } + if($other=="Y") { + $fquestans = "answer".$qfbase."other"; + $tbody = "javatbd".$qbase."other"; + $dtbody = "tbdisp".$qbase."other"; + $tbodyae = $qbase."other"; + $appendj .= "\n"; + $appendj .= "\tif (document.getElementById('$fquestans').value !== '')\n"; + $appendj .= "\t{\n"; + $appendj .= "\t\tdocument.getElementById('$tbody').style.display='none';\n"; + $appendj .= "\t\t$('#$dtbody').val('on');\n"; + $appendj .= "\t}\n"; + $appendj .= "\telse\n"; + $appendj .= "\t{\n"; + $appendj .= "\t\tdocument.getElementById('$tbody').style.display='';\n"; + $appendj .= "\t\t$('#$dtbody').val('off');\n"; + // This line resets the text fields in the hidden row + $appendj .= "\t\t$('#$tbody input[type=text]').val('');"; + // This line resets any radio group in the hidden row + $appendj .= "\t\t$('#$tbody input[type=radio]').attr('checked', false); "; + $appendj .= "\t}\n"; + } } } $java .= $appendj; diff --git a/qanda.php b/qanda.php index 426f3ef33d0..2668524d6e7 100644 --- a/qanda.php +++ b/qanda.php @@ -1205,17 +1205,17 @@ function countdown(questionid,timer,action,warning,warninghide,disable){ return $output; } -function return_array_filter_strings($ia, $qidattributes, $thissurvey, $ansrow, $rowname, $trbc='', $valuename, $method="tbody") { +function return_array_filter_strings($ia, $qidattributes, $thissurvey, $ansrow, $rowname, $trbc='', $valuename, $method="tbody", $class=null) { /* We're just going to work out whether to do the include or exclude version of the function at this point */ if(isset($qidattributes['array_filter_exclude']) && trim($qidattributes['array_filter_exclude']) != '') { - list($html2body, $hiddenfield) = return_array_filter_exclude_strings($ia, $qidattributes, $thissurvey, $ansrow, $rowname, $trbc, $valuename, $method); + list($html2body, $hiddenfield) = return_array_filter_exclude_strings($ia, $qidattributes, $thissurvey, $ansrow, $rowname, $trbc, $valuename, $method, $class); } else { - list($html2body, $hiddenfield) = return_array_filter_include_strings($ia, $qidattributes, $thissurvey, $ansrow, $rowname, $trbc, $valuename, $method); + list($html2body, $hiddenfield) = return_array_filter_include_strings($ia, $qidattributes, $thissurvey, $ansrow, $rowname, $trbc, $valuename, $method, $class); } return array($html2body, $hiddenfield); } -function return_array_filter_include_strings($ia, $qidattributes, $thissurvey, $ansrow, $rowname, $trbc='', $valuename, $method="tbody") { +function return_array_filter_include_strings($ia, $qidattributes, $thissurvey, $ansrow, $rowname, $trbc='', $valuename, $method="tbody", $class=null) { /* DO ARRAY_FILTER ATTRIBUTE We set the $hiddenfield for each answer, and the value of this is available to java to let javascripts know whether each answer is currently being displayed. $htmltbody2 determines whether the answer row @@ -1230,42 +1230,50 @@ function return_array_filter_include_strings($ia, $qidattributes, $thissurvey, $ if ( (trim($qidattributes['array_filter'])!='' && // the array_filter attribute is set $thissurvey['format'] == 'G' && // and the survey is being presented group by group - getArrayFiltersOutGroup($ia[0]) == false // and this question _is_not_ in the current group for the array filter + getArrayFiltersOutGroup($ia[0]) == false // and the source question is in the same group (ie displayed on same page) ) || // OR (trim($qidattributes['array_filter'])!='' && // the array_filter attribute is set $thissurvey['format'] == 'A' // and the survey is being presented all on one page ) ) { - $htmltbody2 = "\n\n\t<$method id='javatbd$rowname' style='display: none'>\n"; + $htmltbody2 = "\n\n\t<$method id='javatbd$rowname' style='display: none'"; + $htmltbody2 .= ($class !== null) ? " class='$class'": ""; + $htmltbody2 .= ">\n"; $hiddenfield = "\n"; } else if ( - (trim($qidattributes['array_filter'])!='' && - $thissurvey['format'] == 'S' - ) || - (trim($qidattributes['array_filter'])!='' && - $thissurvey['format'] == 'G' && - getArrayFiltersOutGroup($ia[0]) == true + (trim($qidattributes['array_filter'])!='' && // The array filter attribute is set + $thissurvey['format'] == 'S' // and the survey is being presented in question-by-question mode + ) || // OR + (trim($qidattributes['array_filter'])!='' && // The array filter attribute is set + $thissurvey['format'] == 'G' && // and the survey is being presented in group-by-group mode + getArrayFiltersOutGroup($ia[0]) == true // and the source question for the array filter is in a different group than this question ) ) { $selected = getArrayFiltersForQuestion($ia[0]); if (!empty($selected) && !in_array($ansrow['code'],$selected)) { - $htmltbody2 = "\n\n\t<$method id='javatbd$rowname' style='display: none'>\n"; + $htmltbody2 = "\n\n\t<$method id='javatbd$rowname' style='display: none'"; + $htmltbody2 .= ($class !== null) ? " class='$class'": ""; + $htmltbody2 .= ">\n"; $hiddenfield=""; $_SESSION[$valuename] = ''; //Remove any saved results for this since it is no longer being displayed } else { - $htmltbody2 = "\n\n\t<$method id='javatbd$rowname'>"; + $htmltbody2 = "\n\n\t<$method id='javatbd$rowname'"; + $htmltbody2 .= ($class !== null) ? " class='$class'": ""; + $htmltbody2 .= ">"; $hiddenfield="\n"; } } else { - $htmltbody2 = "\n\n\t<$method id='javatbd$rowname'>\n"; + $htmltbody2 = "\n\n\t<$method id='javatbd$rowname'"; + $htmltbody2 .= ($class !== null) ? " class='$class'": ""; + $htmltbody2 .= ">\n"; $hiddenfield = ""; } @@ -1274,7 +1282,7 @@ function return_array_filter_include_strings($ia, $qidattributes, $thissurvey, $ return array($htmltbody2, $hiddenfield); } -function return_array_filter_exclude_strings($ia, $qidattributes, $thissurvey, $ansrow, $rowname, $trbc='', $valuename, $method="tbody") { +function return_array_filter_exclude_strings($ia, $qidattributes, $thissurvey, $ansrow, $rowname, $trbc='', $valuename, $method="tbody", $class=null) { /* DO ARRAY_FILTER_EXCLUDE ATTRIBUTE We set the $hiddenfield for each answer, and the value of this is available to java to let javascripts know whether each answer is currently being displayed. $htmltbody2 determines whether the answer row @@ -1285,11 +1293,11 @@ function return_array_filter_exclude_strings($ia, $qidattributes, $thissurvey, $ $hiddenfield= ''; if ( (trim($qidattributes['array_filter_exclude'])!='' && // the array_filter attribute is set - $thissurvey['format'] == 'G' && // and the survey is being presented group by group - getArrayFiltersExcludesOutGroup($ia[0]) == false // and this question _is_not_ in the current group for the array filter - ) || // OR + $thissurvey['format'] == 'G' && // and the survey is being presented group by group + getArrayFiltersExcludesOutGroup($ia[0]) == false // and this question _is_ in the current group for the array filter (ie it's on the same page) + ) || // OR (trim($qidattributes['array_filter_exclude'])!='' && // the array_filter attribute is set - $thissurvey['format'] == 'A' // and the survey is being presented all on one page + $thissurvey['format'] == 'A' // and the survey is being presented all on one page ) ) { @@ -1307,6 +1315,9 @@ function return_array_filter_exclude_strings($ia, $qidattributes, $thissurvey, $ ) { $selected = getArrayFilterExcludesForQuestion($ia[0]); + //echo "
"; print_r($selected); echo "
-->"; print_r($ansrow['code']); echo "
"; + //if(in_array($ansrow['code'], $selected)) echo "Hi there".$ansrow['code']; + //if(!empty($selected)) echo "notempty ".$ansrow['code']; if (!empty($selected) && !in_array($ansrow['code'],$selected)) { $htmltbody2 = "\n\n\t<$method id='javatbd$rowname'>\n"; @@ -2109,24 +2120,24 @@ function do_list_radio($ia) { $other = $row['other']; } - + //question attribute random order set? if ($qidattributes['random_order']==1) { $ansquery = "SELECT * FROM {$dbprefix}answers WHERE qid=$ia[0] AND language='".$_SESSION['s_lang']."' ORDER BY ".db_random(); } - + //question attribute alphasort set? elseif ($qidattributes['alphasort']==1) { $ansquery = "SELECT * FROM {$dbprefix}answers WHERE qid=$ia[0] AND language='".$_SESSION['s_lang']."' ORDER BY answer"; - } - + } + //no question attributes -> order by sortorder else { $ansquery = "SELECT * FROM {$dbprefix}answers WHERE qid=$ia[0] AND language='".$_SESSION['s_lang']."' ORDER BY sortorder, answer"; } - + $ansresult = db_execute_assoc($ansquery) or safe_die('Couldn\'t get answers
$ansquery
'.$connect->ErrorMsg()); //Checked $anscount = $ansresult->RecordCount(); @@ -2240,8 +2251,18 @@ function do_list_radio($ia) { $answer_other = ' value=""'; } - - $answer .= $wrapper['item-start-other'].' + + list($htmltbody2, $hiddenfield)=return_array_filter_strings($ia, $qidattributes, $thissurvey, array("code"=>"other"), $thisfieldname, $trbc, $myfname, "li", "other"); + + if($wrapper['item-start-other'] == "\t
  • \n") + { + $startitem = "\t$htmltbody2\n"; + } else { + $startitem = $wrapper['item-start-other']; + } + $answer .= $startitem; + $answer .= "\t$hiddenfield\n"; + $answer .= '
  • \n") + { + $startitem = "\t$htmltbody2\n"; + } else { + $startitem = $wrapper['item-start']; + } + $answer .= $startitem.' + $answer .= " onclick='cancelBubbleThis(event);".$callmaxanswscriptcheckbox."if(this.checked===false) document.getElementById(\"answer$myfname\").value=\"\";"; + $answer .= " if(this.checked===true) document.getElementById(\"answer$myfname\").focus();"; + $answer .= " $checkconditionFunction(document.getElementById(\"answer$myfname\").value, document.getElementById(\"answer$myfname\").name, document.getElementById(\"answer$myfname\").type);"; + $answer .= "' /> FetchRow()) { + if($fansrows['other']=="Y") $other="Y"; if(strpos($array_filter_types, $fansrows['type']) === false) {} else { $fquestans = "java".$qfbase.$fansrows['code']; @@ -702,6 +704,28 @@ function checkconditions(value, name, type) $appendj .= "\t}\n"; } } + + if($other=="Y") { + $fquestans = "answer".$qfbase."other"; + $tbody = "javatbd".$qbase."other"; + $dtbody = "tbdisp".$qbase."other"; + $tbodyae = $qbase."other"; + $appendj .= "\n"; + $appendj .= "\tif (document.getElementById('$fquestans').value !== '')\n"; + $appendj .= "\t{\n"; + $appendj .= "\t\tdocument.getElementById('$tbody').style.display='';\n"; + $appendj .= "\t\t$('#$dtbody').val('on');\n"; + $appendj .= "\t}\n"; + $appendj .= "\telse\n"; + $appendj .= "\t{\n"; + $appendj .= "\t\tdocument.getElementById('$tbody').style.display='none';\n"; + $appendj .= "\t\t$('#$dtbody').val('off');\n"; + // This line resets the text fields in the hidden row + $appendj .= "\t\t$('#$tbody input[type=text]').val('');"; + // This line resets any radio group in the hidden row + $appendj .= "\t\t$('#$tbody input[type=radio]').attr('checked', false); "; + $appendj .= "\t}\n"; + } } } $java .= $appendj; @@ -711,15 +735,17 @@ function checkconditions(value, name, type) $qfbase = $surveyid."X".$attralist['gid2']."X".$attralist['fid']; if ($attralist['type'] == "M") { - $qquery = "SELECT {$dbprefix}answers.code, {$dbprefix}questions.type + $qquery = "SELECT {$dbprefix}answers.code, {$dbprefix}questions.type, {$dbprefix}questions.other FROM {$dbprefix}answers, {$dbprefix}questions WHERE {$dbprefix}answers.qid={$dbprefix}questions.qid AND {$dbprefix}answers.qid='".$attralist['qid']."' AND {$dbprefix}answers.language='".$_SESSION['s_lang']."' ORDER BY code;"; $qresult = db_execute_assoc($qquery); //Checked + $other=null; while ($fansrows = $qresult->FetchRow()) { + if($fansrows['other']== "Y") $other="Y"; if(strpos($array_filter_exclude_types, $fansrows['type']) === false) {} else { $fquestans = "java".$qfbase.$fansrows['code']; @@ -758,6 +784,27 @@ function checkconditions(value, name, type) $appendj .= "\t}\n"; } } + if($other=="Y") { + $fquestans = "answer".$qfbase."other"; + $tbody = "javatbd".$qbase."other"; + $dtbody = "tbdisp".$qbase."other"; + $tbodyae = $qbase."other"; + $appendj .= "\n"; + $appendj .= "\tif (document.getElementById('$fquestans').value !== '')\n"; + $appendj .= "\t{\n"; + $appendj .= "\t\tdocument.getElementById('$tbody').style.display='none';\n"; + $appendj .= "\t\t$('#$dtbody').val('on');\n"; + $appendj .= "\t}\n"; + $appendj .= "\telse\n"; + $appendj .= "\t{\n"; + $appendj .= "\t\tdocument.getElementById('$tbody').style.display='';\n"; + $appendj .= "\t\t$('#$dtbody').val('off');\n"; + // This line resets the text fields in the hidden row + $appendj .= "\t\t$('#$tbody input[type=text]').val('');"; + // This line resets any radio group in the hidden row + $appendj .= "\t\t$('#$tbody input[type=radio]').attr('checked', false); "; + $appendj .= "\t}\n"; + } } } $java .= $appendj;