Skip to content

Commit

Permalink
Fixed issue #09773: Ranking question with filter are not resetted whe…
Browse files Browse the repository at this point in the history
…n come back to question

Dev: this fix completely the javascript part
Dev: leave EM to fix EM and ranking.js to do the drag & drop
Dev: then drag & drop functionnality can go easily to Question plugin
Dev: dropdown system for EM core
  • Loading branch information
Shnoulle committed Jul 23, 2015
1 parent 1c72333 commit db3f247
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
14 changes: 12 additions & 2 deletions application/helpers/expressions/em_manager_helper.php
Expand Up @@ -1445,9 +1445,11 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
if ($hasSubqs) {
$subqs = $qinfo['subqs'];
$sq_names=array();
$sq_eqPart=array();
foreach($subqs as $subq)
{
$sq_names[] = $subq['varName'].".NAOK";
$sq_eqPart[] = "!is_empty({$subq['varName']})*{$subq['csuffix']}";
}
if (!isset($validationEqn[$questionNum]))
{
Expand All @@ -1457,7 +1459,7 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
'qtype' => $type,
'type' => 'default',
'class' => 'default',
'eqn' => 'unique(' . implode(',',$sq_names) . ')',
'eqn' => 'unique(' . implode(',',$sq_names) . ') and count(' . implode(',',$sq_names) . ')==max('. implode(',',$sq_eqPart) .')',
'qid' => $questionNum,
);
}
Expand Down Expand Up @@ -3087,7 +3089,7 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
$qtips['default']=$this->gT("Only numbers may be entered in these fields.");
break;
case 'R':
$qtips['default']=$this->gT("All your answers must be different.");
$qtips['default']=$this->gT("All your answers must be different and you must rank in order.");
break;
// Helptext is added in qanda_help.php
/* case 'D':
Expand Down Expand Up @@ -7196,6 +7198,14 @@ static function GetRelevanceAndTailoringJavaScript()
$relParts[] = " $('#answer" . $sq['sgqa'] . "NANS').attr('checked',true);\n";
$relParts[] = " }\n";
break;
case 'R':
$listItem = substr($sq['rowdivid'],strlen($sq['sgqa']));
$relParts[] = " $('#question{$arg['qid']} .select-list select').each(function(){ \n";
$relParts[] = " if($(this).val()=='{$listItem}'){ \n";
$relParts[] = " $(this).val('').trigger('change'); \n";
$relParts[] = " }; \n";
$relParts[] = " }); \n";
break;
default:
break;
}
Expand Down
7 changes: 6 additions & 1 deletion scripts/expressions/em_javascript.js
Expand Up @@ -20,7 +20,7 @@

/* Default event to trigger on answer part
* see https://manual.limesurvey.org/Project_ideas_for_GSoC_2015#Expression_Manager_JavaScript_optimizations
* Actually only for list with comment
* Actually only for list with comment and select in ranking
**/
$(document).on("keyup",".text-item textarea:not([onkeyup]),.text-item :text:not([onkeyup])",function(event){
// 'keyup' can be replaced by event.type (but not really needed)
Expand All @@ -34,6 +34,11 @@ $(document).on("keyup",".text-item textarea:not([onkeyup]),.text-item :text:not(
checkconditions($(this).val(), $(this).attr('name'), 'text', 'keyup')
}
});
$(document).on("change",".select-item select:not([onchange])",function(event){
//$('#java'+$(this).attr("name")).val($(this).val()); Not needed for ranking, needed for ? select already have val() and are unique by name
if($.isFunction(window.ExprMgr_process_relevance_and_tailoring ))
ExprMgr_process_relevance_and_tailoring("onchange",$(this).attr("name"),"select-one");
});

function LEMcount()
{
Expand Down
67 changes: 52 additions & 15 deletions scripts/ranking.js
@@ -1,3 +1,14 @@
/*
* This file is part of LimeSurvey
* See COPYRIGHT.php for copyright notices and details.
* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GNU/GPL License v2 or later
*/

/**
* Update answers part for ranking function
*
* @param {number} qId The qid of the question where apply.
*/
function doDragDropRank(qID, showpopups, samechoiceheight, samelistheight) {
// TODO : advanced setting in attributes
if (typeof showpopups === 'undefined'){showpopups=true;}
Expand All @@ -8,16 +19,19 @@ function doDragDropRank(qID, showpopups, samechoiceheight, samelistheight) {
var rankingnamewidth=rankingname.length;
//Add a class to the question
$('#question'+qID+'').addClass('dragDropRanking');
// Hide the default answers list
// Display for media oral, maybe use !window.matchMedia("(oral)").matches but still hidden if user use default browser with screen reader ?
// Hide the default answers list but display for media oral or screen reader
$('#question'+qID+' .answers-list').addClass("hide");
// We are in javascript, then default tip can be replaced
$('#question'+qID+' .em_default').html(aRankingTranslations.rankhelp);

$('#question'+qID+' .em_default').html("<div class='hide'>"+$('#question'+qID+' .em_default').html()+"</div><div aria-hidden='true'>"+aRankingTranslations.rankhelp+"</div>");
$('#question'+qID+' .answers-list').on("change",".select-item",{source:false},function(event,data){
data = data || event.data;
if(data.source!='dragdrop')
loadDragDropRank(qID);;
});

// Add connected sortables elements to the question
// Actually a table : move it to a list is a good idea, but need reviewing template a lot.
var htmlCode = '<div class="dragDropTable"> \
var htmlCode = '<div class="dragDropTable" aria-hidden="true"> \
<div class="columns2">\
<strong class="SortableTitle">'+aRankingTranslations.choicetitle+'</strong>\
<div class="ui-state-default dragDropChoices"> \
Expand Down Expand Up @@ -105,6 +119,11 @@ function doDragDropRank(qID, showpopups, samechoiceheight, samelistheight) {
});
}

/**
* Update answers after updating drag and drop part
*
* @param {number} qId The qid of the question where apply.
*/
function updateDragDropRank(qID){
var maxanswers= parseInt($("#ranking-"+qID+"-maxans").text(),10);
var rankingname= "javatbd"+$("#ranking-"+qID+"-name").text();
Expand All @@ -121,44 +140,64 @@ function updateDragDropRank(qID){
$("[id^=" + relevancename + "]").val('0');
$('#question'+qID+' .select-item select:lt('+maxanswers+')').each(function(index){
number=index+1;
if($(this).val()!=""){
if($(this).val()!="")
{
$("#"+relevancename+number).val("1");
}
checkconditions($(this).val(),$(this).attr("name"),'select-one','onchange');
$(this).trigger("change",{ source : 'dragdrop'});
});
$('#sortable-rank-'+qID+' li').removeClass("error");
$('#sortable-choice-'+qID+' li').removeClass("error");
$('#sortable-rank-'+qID+' li:gt('+(maxanswers*1-1)+')').addClass("error");
}

/**
* Show an alert if needed
*
* @param {number} qId The qid of the question where apply.
* @param {bool} showpopups Show or not the alert
*/
function sortableAlert (qID,showpopups)
{
if(showpopups){
txtAlert=$("#question"+qID+" .em_num_answers").text()
alert(txtAlert);
}
}
/**
* Set the drag and drop according to existing answers
*
* @param {number} qId The qid of the question where apply.
*/
function loadDragDropRank(qID){
var maxanswers= parseInt($("#ranking-"+qID+"-maxans").text(),10);
var rankingname= "javatbd"+$("#ranking-"+qID+"-name").text();
var relevancename= "relevance"+$("#ranking-"+qID+"-name").text();
var rankingnamewidth=rankingname.length;
// Update #relevance
$("[id^=" + relevancename + "]").val('0');
$('#sortable-rank-'+qID+' li').each(function(){
$(this).appendTo('#sortable-choice-'+qID+'');
});
$('#question'+qID+' .select-item select').each(function(index){
if($(this).val()!=''){
number=index+1;
$("#"+relevancename+number).val("1");
$('#sortable-choice-'+qID+' li#'+rankingname+$(this).val()).appendTo('#sortable-rank-'+qID);
number=index+1;
$("#"+relevancename+number).val("1");
$('#sortable-choice-'+qID+' li#'+rankingname+$(this).val()).appendTo('#sortable-rank-'+qID);
}
});

updateDragDropRank(qID);// Update to reorder select
$('#sortable-rank-'+qID+' li').removeClass("error");
$('#sortable-choice-'+qID+' li').removeClass("error");
$('#sortable-rank-'+qID+' li:gt('+(maxanswers*1-1)+')').addClass("error");
}

// Fix choix and list heigth according to parameter
/**
* Fix height of drag and drop according to question settings
*
* @param {number} qId The qid of the question where apply.
* @param {bool} samechoiceheight
* @param {bool} samelistheight
*/
function fixChoiceListHeight(qID,samechoiceheight,samelistheight){
if(samechoiceheight)
{
Expand All @@ -179,5 +218,3 @@ function fixChoiceListHeight(qID,samechoiceheight,samelistheight){
$('.connectedSortable'+qID).css('min-height',totalHeight+'px');
}
}


0 comments on commit db3f247

Please sign in to comment.