Skip to content

Commit

Permalink
Fixed issue #8693: When using non-numeric answer codes Limesurvey aut…
Browse files Browse the repository at this point in the history
…omatically adds non-unique follow up codes
  • Loading branch information
c-schmitz committed Feb 13, 2014
1 parent bacf9a1 commit 2d7ee5d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
4 changes: 4 additions & 0 deletions scripts/admin/admin_core.js
Expand Up @@ -770,4 +770,8 @@ function addHiddenElement(theform,thename,thevalue)
myel.value = thevalue;
return myel;
}
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}

// @license-end
77 changes: 55 additions & 22 deletions scripts/admin/answers.js
Expand Up @@ -6,7 +6,7 @@ $(document).ready(function(){
distance:3});
$('.btnaddanswer').click(addinput);
$('.btndelanswer').click(deleteinput);
$('#editanswersform').submit(code_duplicates_check)
$('#editanswersform').submit(checkForDuplicateCodes)
$('#labelsetbrowser').dialog({ autoOpen: false,
modal: true,
width:800,
Expand Down Expand Up @@ -118,11 +118,7 @@ function addinput()
languages=langs.split(';');

sNextCode=getNextCode($(this).parent().parent().find('.code').val());
while ($(this).parent().parent().parent().find('input[value="'+sNextCode+'"]').length>0 && sNextCode!=$(this).parent().parent().find('.code').val())
{
sNextCode=getNextCode(sNextCode);
}


for (x in languages)
{
tablerow=$('#tabpage_'+languages[x]).find('#answers_'+languages[x]+'_'+scale_id+' .row_'+position);
Expand Down Expand Up @@ -252,32 +248,39 @@ function updatecodes()

}

function getNextCode(sourcecode)
function getNextCode(sSourceCode)
{
i=1;
found=true;
foundnumber=-1;
while (i<=sourcecode.length && found)
mNumberFound=-1;
while (i<=sSourceCode.length && found)
{
found=is_numeric(sourcecode.substr(-i));
found=is_numeric(sSourceCode.substr(-i));
if (found)
{
foundnumber=sourcecode.substr(-i);
mNumberFound=sSourceCode.substr(-i);
i++;
}
}
if (foundnumber==-1)
{
return(sourcecode);
if (mNumberFound==-1)
{
sBaseCode=sSourceCode;
mNumberFound=0
}
else
{
foundnumber++;
foundnumber=foundnumber+'';
result=sourcecode.substr(0,sourcecode.length-foundnumber.length)+foundnumber;
return(result);
{
sBaseCode=sSourceCode.substr(0,sSourceCode.length-mNumberFound.length);
}

var iNumberFound=+mNumberFound;
do
{
iNumberFound=iNumberFound+1;
sNewNumber=iNumberFound+'';
sResult=sBaseCode+sNewNumber;
sResult=sResult.substr(sResult.length - 5);
}
while (areCodesUnique(sResult)==false);
return(sResult);
}

function is_numeric (mixed_var) {
Expand All @@ -290,7 +293,33 @@ function popupeditor()
start_popup_editor(input_id);
}

function code_duplicates_check()
/**
* Checks for duplicate codes and shows an error message & returns false if there are any duplicates
*
* @returns {Boolean}
*/
function checkForDuplicateCodes()
{
if (areCodesUnique('')==false)
{
alert(duplicateanswercode);
return false
}
else
{
return true;
}
}

/**
* Check if all existing codes are unique
* If sNewValue is not empty then only sNewValue is checked for uniqueness against the existing codes
*
* @param sNewValue
*
* @returns {Boolean} False if codes are not unique
*/
function areCodesUnique(sNewValue)
{
languages=langs.split(';');
var dupefound=false;
Expand All @@ -299,9 +328,13 @@ function code_duplicates_check()
$(this).find('tr .code').each(function(){
codearray.push($(this).val());
})
if (sNewValue!='')
{
codearray=codearray.filter( onlyUnique );
codearray.push(sNewValue);
}
if (arrHasDupes(codearray))
{
alert(duplicateanswercode);
dupefound=true;
return;
}
Expand Down

0 comments on commit 2d7ee5d

Please sign in to comment.