Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Function to correct progressbar hidden question bug

  • BlackSwan
  • BlackSwan's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 days 5 hours ago - 1 day 9 hours ago #267167 by BlackSwan
Testet with selfhosted LimeSurvey CE 6.6.6 and fruity_twentythree (in question by question mode)
==================

Hello everyone, 
I would like to quickly share function to correct the progress bar. There is an annoying and unresolved bug (see: bugs.limesurvey.org/bug_relationship_gra...361&graph=dependency ) in Limesurvey that calculates the progressbar on permanently hidden questions. 
You can call this function in a document ready function of your custom.js. Then try to put all your hidden questions at the beginning and at the end of your survey to get the best result. 

To make this function work, you first need to add this code to a twig file (e.g. footer.twig):
Code:
<div id='SurveyName' style='display:none' hidden=1>{{ aSurveyInfo.name }}</div>
<div id='QuestionNumber' style='display:none' hidden=1>{{ processString('{self.qseq}') }}</div>
<div id='NumberOfAllQuestions' style='display:none' hidden=1>{{ processString('{NUMBEROFQUESTIONS}') }}</div>
<div id='NumberOfVisibleQuestions' style='display:none' hidden=1>{{ aSurveyInfo.iTotalquestions }}</div>

Now just add functions to your custom.js and call them from a document ready function. You can also distinguish between different surveys by the survey name.

Code:
    if ("{SURVEYNAME}" == "Survey 1" )  {
        // All hidden questions at start
        AdjustProgressBar();
        }
    if ("{SURVEYNAME}" == "Survey 2" )  {
        // All hidden questions at end
        AdjustProgressBar(, false);
        }
    if ("{SURVEYNAME}" == "Survey 3" )  {
        // 5 of X hidden questions at start
        AdjustProgressBar(5);
        }


        
Code:
    function AdjustProgressBar(NumberOffHiddenQuestions = 0, HiddenQuestionsAtStart = true) {
        var ToolTipText = "";
        var SurveyName = $('#SurveyName').text();
        var QuestionNumber = $('#QuestionNumber').text();
        var NumberOfAllQuestions = $('#NumberOfAllQuestions').text();
        var NumberOfVisibleQuestions = $('#NumberOfVisibleQuestions').text();
 
        // var NumberOffHiddenQuestions = NumberOfAllQuestions - NumberOfVisibleQuestions;
        if (NumberOffHiddenQuestions == 0)  {
            NumberOffHiddenQuestions = NumberOfAllQuestions - NumberOfVisibleQuestions;
            }
        
        QuestionNumber++; 
        // Adjust QuestionNumber if hidden question are at the beginning of the survey
        if (HiddenQuestionsAtStart)  {
            QuestionNumber = QuestionNumber - NumberOffHiddenQuestions;
            if (QuestionNumber < 0)  {
                QuestionNumber = 0;
                }
            }
            
        var progressValue = Math.round(QuestionNumber / NumberOfVisibleQuestions * 100);
        $('.progress-bar').css({
            'transition': 'width 0s ease 0s',
            'width': progressValue+'%'
            });
        
        ToolTipText = "You have answered ".concat(progressValue, "% of the questions.");
 
        // Replace tooltip-text
        $('.ls-progress-container').attr('title', ToolTipText).tooltip('dispose').tooltip();
    
        // Replace text of div
        $('.ls-progress-container .visually-hidden').text(ToolTipText);
    }


Cheers
Last edit: 1 day 9 hours ago by BlackSwan.
The following user(s) said Thank You: DenisChenu

Please Log in to join the conversation.

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team & Official Partner
  • LimeSurvey Community Team & Official Partner
More
1 day 11 hours ago #267184 by DenisChenu
Great !

Else, this bug need a new report.

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. - Professional support - Plugins, theme and development .
I don't answer to private message.

Please Log in to join the conversation.

  • BlackSwan
  • BlackSwan's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 day 9 hours ago #267191 by BlackSwan

Please Log in to join the conversation.

Moderators: holchtpartner

Lime-years ahead

Online-surveys for every purse and purpose