Setting question default value for dropdown list (!) from token Attributes

4 years 5 months ago - 4 years 5 months ago #86102 by september28
Hi there,

Sorry forgot to mention that I am using Latest stable build Version 2.0+ Build 120924

I have been playing around with Limesurvey for a few days now. One of the massive problems that I have encountered is that there doesn't seem to be an easy way to set answers to certain questions from the values (if any) of the custom token attributes. I have a situation where I will be sending out a survey to both customers and non customers. For the customers, I know alot of basic information already such as Country, Business type, business size etc. For non-customers I will need to ask this information.

So, to take the country dropdown list as an example... It should default to the token attribute value (if any) and if not just default to "please choose one".

Following this post I have modified the defaults dropdown so that it displays all of the existing token attributes. This was fairly easy by adding the following around like 39 of application/views/admin/survey/Question/editdefaultvalues_view.php
<?php if($tokenfields) { 
                                        foreach ($tokenfields as $tokenfield)
                                            if (isset($tokenfielddata[$tokenfield]))
                                                $tokenvalues = $tokenfielddata[$tokenfield];
                                                $tokenvalues = array(
                                                    'description' => '',
                                                    'mandatory' => 'N',
                                                    'show_register' => 'N',
                                            $tokenfieldname = '{TOKEN:'.  strtoupper($tokenfield) . '}';
                                    <option value="<?php echo $tokenfieldname; ?>"<?php if ($tokenfieldname == $opts['defaultvalue']){ ?> selected="selected"<?php } ?>>
                                            <?php echo $tokenfield .' ('. ($tokenvalues['description'] ? $tokenvalues['description'] : 'No Description') .')'; ?>
                                    <?php }} ?>

So now, the defaultvalues table will correctly update with the field defaultvalue being set to "{TOKEN:ATTRIBUTE_x}". However it does not work, and the documentation also mentions that this will only work for text fields and not dropdowns. In fact, it seems that even the text type inputs (Short text, long text) don't work. So i wondered where the default values are set...

I have been digging around in the code (I am very familiar with codeigniter, and Yii seems fairly similar). However I am having massive problems trying to find exactly where the code needs editing for this. I have narrowed it down to the fact that the default value for a field is contained in the session array. Clues seem to be in qanda_helper.php around line 1470:
$opt_select = '';
            if ($_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$ia[1]] == $ansrow['code'])
                $opt_select = SELECTED;
            if ($prefixStyle == 1) {
                $_prefix = ++$_rowNum . ') ';
            $answer .= "<option value='{$ansrow['code']}' {$opt_select}>".flattenText($_prefix.$ansrow['answer'])."</option>\n";

So I am trying to find where this session variable is set. I can find plenty of reference to where the fielddate array is set, but the defaults are stored in a separate array which looks like
. Poking around, there seems to be some reference to setting defaults in common_helper.php, but i can't seem to find the exact location i should be looking in. I would like to find where this session variable is set, so that I can add some extra logic to parse the {TOKEN:ATTRIBUTE_x} defaults to get the value.

I will of course share the code with the project to introduce this feature.

I would also be interested in hearing if there is another way to do what I want to do.


Last Edit: 4 years 5 months ago by september28. Reason: Added version info for reference

Please Log in to join the conversation.

4 years 5 months ago #86162 by september28
Does no one have any clues for this?

Help a brother out!


Please Log in to join the conversation.

4 years 5 months ago #86169 by TMSWhite
There are two options, both of which require bypassing the GUI, since the GUI currently only lets you choose a default from among the pick list choices.

(1) Modify the database:
(a) add an entry to the defaultvalues table using the proper qid, scale_id, and sqid.

(2) Use the Excel survey import/export feature:
(a) Export the survey as Tab Separated Values
(b) Open the generated .txt file in Excel, or OpenOffice
(c) Find the row for your list question
(d) Insert the desired default value in the "default_value" column. For example, {TOKEN:ATTRIBUTE_3}
(e) Save the file
(f) Re-import the survey

Limesurvey should then properly handle the default value for the question.

Please Log in to join the conversation.

4 years 5 months ago #86170 by TMSWhite
I just confirmed that that approach works (at least using 1.92 - there seems to be a bug in importing tab separated value files in 2.0). Here is the .lss version of the survey.

File Attachment:

File Name: limesurvey...9581.lss
File Size:25 KB

I tested passing in defaults from {TOKEN:ATTRIBUTE_1}, and also by passing in values from the command line. In this case, the URL was:

So that variable q3 was set to 'red', and variable q4 was set to 'green'. The token value uses 'o' (orange) as its default value.

Here is a screen shot:


Please Log in to join the conversation.

4 years 5 months ago #86284 by september28
Thanks for this, I can verify that your suggested method works in LS 1.92, however does not work as of LS 2.0+, so for now I will continue using 1.92.

If anyone is interested, I have added some lines of code to make the custom token attributes appear in the default values dropdown list. Its for my ease more than anything, so the quality of the code can't be called "production"!. Here is the code that I added into question handling.php around line 159:
//start of mod by dan spencer 01/10/2012 - adapted from code found in tokens.php line 2184 to 2207
                foreach ($tokenfields as $tokenfield=>$tokendescription)
                    $tmpfieldname = '{TOKEN:'.$tokenfield.'}';
                    $tokenoutput.="<option value=\"$tmpfieldname\">$tmpfieldname (".htmlspecialchars($tokendescription,ENT_QUOTES,'UTF-8').")</option>";
                $editdefvalues .= $tokenoutput;
                //end of mod by dan spencer 01/10/2012

I have also attached the full questionhandling.php file if anyone wants to patch. At the moment it is working for dropdown style questions, but could also be adapted for other types as well.


Please Log in to join the conversation.