Skip to content

Commit

Permalink
New feature #06477: optionnal comment place with "multi choice with c…
Browse files Browse the repository at this point in the history
…omment"

New feature: can replace javascript file in template
Dev: New feature: can replace expression manager function in template.js
  • Loading branch information
Shnoulle committed Apr 9, 2013
1 parent 87c5268 commit e90c89b
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 79 deletions.
29 changes: 28 additions & 1 deletion application/helpers/common_helper.php
Expand Up @@ -3153,6 +3153,33 @@ function questionAttributes($returnByName=false)
"help"=>$clang->gT('Filter the available answers by this value'),
"caption"=>$clang->gT('Code filter'));

$qattributes["commented_checkbox"]=array(
"types"=>"P",
'category'=>$clang->gT('Logic'),
'sortorder'=>110,
'inputtype'=>'singleselect',
'options'=>array(
"allways"=>$clang->gT('No control on checkbox'),
"checked"=>$clang->gT('Checkbox is checked'),
"unchecked"=>$clang->gT('Checkbox is unchecked'),
),
'default' => "checked",
'help'=>$clang->gT('Choose when user can add a comment'),
'caption'=>$clang->gT('Comment only when'));

$qattributes["commented_checkbox_auto"]=array(
"types"=>"P",
'category'=>$clang->gT('Logic'),
'sortorder'=>111,
'inputtype'=>'singleselect',
'options'=>array(
"0"=>$clang->gT('No'),
"1"=>$clang->gT('Yes'),
),
'default' => "1",
'help'=>$clang->gT('Use javascript function to remove text and uncheck checkbox (or use Expression Manager only).'),
'caption'=>$clang->gT('Remove text or uncheck checkbox automatically'));

$qattributes["display_columns"]=array(
"types"=>"LM",
'category'=>$clang->gT('Display'),
Expand Down Expand Up @@ -4200,7 +4227,7 @@ function questionAttributes($returnByName=false)
$qattributes["random_group"]=array(
"types"=>"15ABCDEFGHIKLMNOPQRSTUWXYZ!:;|",
'category'=>$clang->gT('Logic'),
'sortorder'=>100,
'sortorder'=>180,
'inputtype'=>'text',
"help"=>$clang->gT("Place questions into a specified randomization group, all questions included in the specified group will appear in a random order"),
"caption"=>$clang->gT("Randomization group name"));
Expand Down
75 changes: 73 additions & 2 deletions application/helpers/expressions/em_manager_helper.php
Expand Up @@ -1315,6 +1315,59 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
break;
}

// commented_checkbox : only for checkbox with comment ("P")
if($type=="P" && !isset($qattr['commented_checkbox'])) $qattr['commented_checkbox']='checked'; // EM don't get default value http://bugs.limesurvey.org/view.php?id=7750
$commented_checkbox='';
if (isset($qattr['commented_checkbox']) && trim($qattr['commented_checkbox']) != '')
{
switch ($type)
{
case 'P':
if ($hasSubqs) {
$commented_checkbox=$qattr['commented_checkbox'];
$subqs = $qinfo['subqs'];
switch ($commented_checkbox)
{
case 'checked':
$sq_eqn_commented_checkbox=array();
foreach($subqs as $subq)
{
$sq_eqn_commented_checkbox[] = "(is_empty({$subq['varName']}.NAOK) and !is_empty({$subq['varName']}comment.NAOK))";
}
$eqn="sum(".implode(",",$sq_eqn_commented_checkbox).")==0";
break;
case 'unchecked':
$sq_eqn_commented_checkbox=array();
foreach($subqs as $subq)
{
$sq_eqn_commented_checkbox[] = "(!is_empty({$subq['varName']}.NAOK) and !is_empty({$subq['varName']}comment.NAOK))";
}
$eqn="sum(".implode(",",$sq_eqn_commented_checkbox).")==0";
break;
case 'allways':
default:
break;
}
if($commented_checkbox!="allways")
{
if (!isset($validationEqn[$questionNum]))
{
$validationEqn[$questionNum] = array();
}
$validationEqn[$questionNum][] = array(
'qtype' => $type,
'type' => 'commented_checkbox',
'class' => 'commented_checkbox',
'eqn' => $eqn,
'qid' => $questionNum,
);
}
}
break;
default:
break;
}
}
// equals_num_value
// Validation:= sum(sq1,...,sqN) == value (which could be an expression).
if (isset($qattr['equals_num_value']) && trim($qattr['equals_num_value']) != '')
Expand Down Expand Up @@ -2486,9 +2539,27 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
{
case 'R':
$qtips['default']=$this->gT("All your answers must be different.");
break;
default:
break;
}

if($commented_checkbox && $commented_checkbox!='allways')
{
switch ($commented_checkbox)
{
case 'checked':
$qtips['commented_checkbox']=$this->gT("Comment only when you choose an answer.");
break;
case 'unchecked':
$qtips['commented_checkbox']=$this->gT("Comment only when you don't choose an answer.");
break;
case 'allways':
default:
$qtips['commented_checkbox']=$this->gT("Comment your answers.");
break;
}
}
// min/max answers
if ($min_answers!='' || $max_answers!='')
{
Expand Down Expand Up @@ -5672,7 +5743,7 @@ function _ValidateQuestion($questionSeq)
$stringToParse = '';
foreach ($LEM->qid2validationEqn[$qid]['tips'] as $vclass=>$vtip)
{
$stringToParse .= "<div id='vmsg_" . $qid . '_' . $vclass . "' class='em_" . $vclass . "'>" . $vtip . "</div>\n";
$stringToParse .= "<div id='vmsg_" . $qid . '_' . $vclass . "' class='em_" . $vclass . " emtip'>" . $vtip . "</div>\n";
}
$prettyPrintValidTip = $stringToParse;
$validTip = $LEM->ProcessString($stringToParse, $qid,NULL,false,1,1,false,false);
Expand Down Expand Up @@ -6108,7 +6179,7 @@ static function GetRelevanceAndTailoringJavaScript()
$jsParts=array();
$allJsVarsUsed=array();
$rowdividList=array(); // list of subquestions needing relevance entries
$jsParts[] = '<script type="text/javascript" src="'.Yii::app()->getConfig('generalscripts').'expressions/em_javascript.js"></script>';
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('generalscripts')."expressions/em_javascript.js");;
$jsParts[] = "\n<script type='text/javascript'>\n<!--\n";
$jsParts[] = "var LEMmode='" . $LEM->surveyMode . "';\n";
if ($LEM->surveyMode == 'group')
Expand Down
89 changes: 16 additions & 73 deletions application/helpers/qanda_helper.php
Expand Up @@ -2526,41 +2526,6 @@ function do_multiplechoice_withcomments($ia)
{
$othertext=$clang->gT('Other:');
}
// // Check if the max_answers attribute is set
// $maxansw=0;
// $callmaxanswscriptcheckbox = '';
// $callmaxanswscriptcheckbox2 = '';
$callmaxanswscriptother = '';
// $maxanswscript = '';
// if (trim($aQuestionAttributes['max_answers'])!='') {
// $maxansw=$aQuestionAttributes['max_answers'];
// $callmaxanswscriptcheckbox = "limitmaxansw_{$ia[0]}(this);";
// $callmaxanswscriptcheckbox2= "limitmaxansw_{$ia[0]}";
// $callmaxanswscriptother = "onkeyup=\"limitmaxansw_{$ia[0]}(this)\"";
//
// $maxanswscript = "\t<script type='text/javascript'>\n"
// . "\t<!--\n"
// . "function limitmaxansw_{$ia[0]}(me)\n"
// . "\t{\n"
// . "\tmax=$maxansw\n"
// . "\tcount=0;\n"
// . "\tif (max == 0) { return count; }\n";
// }
//
// // Check if the min_answers attribute is set
// $minansw=0;
// $minanswscript = "";
// if (trim($aQuestionAttributes["min_answers"])!='')
// {
// $minansw=trim($aQuestionAttributes["min_answers"]);
// $minanswscript = "<script type='text/javascript'>\n"
// . "\t<!--\n"
// . "oldonsubmit_{$ia[0]} = document.limesurvey.onsubmit;\n"
// . "function ensureminansw_{$ia[0]}()\n"
// . "{\n"
// . "\tcount=0;\n"
// ;
// }

$qquery = "SELECT other FROM {{questions}} WHERE qid=".$ia[0]." AND language='".$_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['s_lang']."' and parent_qid=0";
$other = Yii::app()->db->createCommand($qquery)->queryScalar(); //Checked
Expand Down Expand Up @@ -2617,13 +2582,10 @@ function do_multiplechoice_withcomments($ia)
$answer_main .= CHECKED;
}
}
$answer_main .=" onclick='cancelBubbleThis(event);$checkconditionFunction(this.value, this.name, this.type);if (!$(this).attr(\"checked\")) { $(\"#answer$myfname2\").val(\"\"); $checkconditionFunction(document.getElementById(\"answer{$myfname2}\").value,\"$myfname2\",\"checkbox\");}' />\n"
$answer_main .=" onclick='$checkconditionFunction(this.value, this.name, this.type);' />\n"
. "\t<label for=\"answer$myfname\" class=\"answertext\">\n"
. $ansrow['question']."</label>\n";

// if ($maxansw > 0) {$maxanswscript .= "\tif (document.getElementById('answer".$myfname."').checked) { count += 1; }\n";}
// if ($minansw > 0) {$minanswscript .= "\tif (document.getElementById('answer".$myfname."').checked) { count += 1; }\n";}

$answer_main .= "<input type='hidden' name='java$myfname' id='java$myfname' value='";
if (isset($_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$myfname]))
{
Expand All @@ -2632,12 +2594,10 @@ function do_multiplechoice_withcomments($ia)
$answer_main .= "' />\n";
$fn++;
$answer_main .= "</span>\n<span class=\"comment\">\n\t<label for='answer$myfname2' class=\"answer-comment hide \">".$clang->gT('Make a comment on your choice here:')."</label>\n"
."<input class='text ".$kpclass."' type='text' size='40' id='answer$myfname2' name='$myfname2' title='".$clang->gT('Make a comment on your choice here:')."' value='";
."<input class='text ".$kpclass."' type='text' size='40' id='answer$myfname2' name='$myfname2' value='";
if (isset($_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$myfname2])) {$answer_main .= htmlspecialchars($_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$myfname2],ENT_QUOTES);}
// --> START NEW FEATURE - SAVE
$answer_main .= "' onkeyup='if (jQuery.trim($(\"#answer{$myfname2}\").val())!=\"\") { document.getElementById(\"answer{$myfname}\").checked=true;$checkconditionFunction(document.getElementById(\"answer{$myfname2}\").value,\"$myfname2\",\"text\");$checkconditionFunction(document.getElementById(\"answer{$myfname}\").value,document.getElementById(\"answer{$myfname}\").name, document.getElementById(\"answer{$myfname}\").type);}' />\n</span>\n"
$answer_main .= "' onkeyup='$checkconditionFunction(this.value,this.name,this.type);' />\n</span>\n"
. "\t</li>\n";
// --> END NEW FEATURE - SAVE

$fn++;
$inputnames[]=$myfname;
Expand All @@ -2650,7 +2610,7 @@ function do_multiplechoice_withcomments($ia)
$anscount = $anscount + 2;
$answer_main .= "\t<li class=\"other question-item answer-item checkbox-text-item other-item\" id=\"javatbd$myfname\">\n<span class=\"option\">\n"
. "\t<label for=\"answer$myfname\" class=\"answertext\">\n".$othertext."\n<input class=\"text other ".$kpclass."\" type=\"text\" name=\"$myfname\" id=\"answer$myfname\" title=\"".$clang->gT('Other').'" size="10"';
$answer_main .= " onkeyup='$oth_checkconditionFunction(this.value, this.name, this.type); if($.trim(this.value)==\"\") { $(\"#answer$myfname2\").val(\"\"); $checkconditionFunction(\"\",\"$myfname2\",\"text\"); }'";
$answer_main .= " onkeyup='$oth_checkconditionFunction(this.value, this.name, this.type);'";
if (isset($_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$myfname]) && $_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$myfname])
{
$dispVal = $_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$myfname];
Expand All @@ -2662,49 +2622,32 @@ function do_multiplechoice_withcomments($ia)
}
$fn++;
// --> START NEW FEATURE - SAVE
$answer_main .= " $callmaxanswscriptother />\n\t</label>\n</span>\n"
$answer_main .= " />\n\t</label>\n</span>\n"
. "<span class=\"comment\">\n\t<label for=\"answer$myfname2\" class=\"answer-comment hide\">".$clang->gT('Make a comment on your choice here:')."\t</label>\n"
. '<input class="text '.$kpclass.'" type="text" size="40" name="'.$myfname2.'" id="answer'.$myfname2.'"'
. " onkeyup='$checkconditionFunction(this.value,this.name,this.type);'"
. ' title="'.$clang->gT('Make a comment on your choice here:').'" value="';
// --> END NEW FEATURE - SAVE

if (isset($_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$myfname2])) {$answer_main .= htmlspecialchars($_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$myfname2],ENT_QUOTES);}
// --> START NEW FEATURE - SAVE
$answer_main .= "\"/>\n";

// if ($maxansw > 0)
// {
// if ($aQuestionAttributes['other_comment_mandatory']==1)
// {
// $maxanswscript .= "\tif (document.getElementById('answer".$myfname."').value != '' && document.getElementById('answer".$myfname2."').value != '') { count += 1; }\n";
// }
// else
// {
// $maxanswscript .= "\tif (document.getElementById('answer".$myfname."').value != '') { count += 1; }\n";
// }
// }
//
// if ($minansw > 0)
// {
// if ($aQuestionAttributes['other_comment_mandatory']==1)
// {
// $minanswscript .= "\tif (document.getElementById('answer".$myfname."').value != '' && document.getElementById('answer".$myfname2."').value != '') { count += 1; }\n";
// }
// else
// {
// $minanswscript .= "\tif (document.getElementById('answer".$myfname."').value != '') { count += 1; }\n";
// }
// }

$answer_main .= "</span>\n\t</li>\n";
// --> END NEW FEATURE - SAVE

$inputnames[]=$myfname;
$inputnames[]=$myfname2;
}
$answer .= "<ul class=\"subquestions-list questions-list checkbox-text-list\">\n".$answer_main."</ul>\n";

if($aQuestionAttributes['commented_checkbox']!="allways" && $aQuestionAttributes['commented_checkbox_auto'])
{
header_includes("multiplechoice_withcomments.js");
# $script= " doMultipleChoiceWithComments({$ia[0]},'{$aQuestionAttributes["commented_checkbox"]}');\n";
# App()->getClientScript()->registerScript("doMultipleChoiceWithComments",$script,CClientScript::POS_HEAD);// Deactivate now: need to be after question, and just after
$answer .= "<script type='text/javascript'>\n"
. " /*<![CDATA[*/\n"
." doMultipleChoiceWithComments({$ia[0]},'{$aQuestionAttributes["commented_checkbox"]}');\n"
." /*]]>*/\n"
."</script>\n";
}
return array($answer, $inputnames);
}

Expand Down
10 changes: 7 additions & 3 deletions application/helpers/replacements_helper.php
Expand Up @@ -189,12 +189,16 @@ function templatereplace($line, $replacements = array(), &$redata = array(), $de
}
else
{
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('generalscripts').$jsinclude);
if(file_exists($templatedir.DIRECTORY_SEPARATOR.$jsinclude)){
App()->getClientScript()->registerScriptFile($templateurl.$jsinclude);
}else{
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('generalscripts').$jsinclude);
}
}
}
}
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('generalscripts')."survey_runtime.js");;
App()->getClientScript()->registerScriptFile($templateurl . 'template.js');
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('generalscripts')."survey_runtime.js");
App()->getClientScript()->registerScriptFile($templateurl . 'template.js',CClientScript::POS_END);
useFirebug();
}

Expand Down

0 comments on commit e90c89b

Please sign in to comment.