Check out the LimeSurvey source code on GitHub!

Question type - Date

More
5 years 11 months ago #56365 by DPajak
Is it possible to change the dropdown representation of the date question type to show only the month and the year dropdown boxes?

I don't need a specific date, since I will be asking for an event back in the past. So the month and the year would be enough, to avoid missing values because of poor memory.

Kind regards
Danny

Please Log in to join the conversation.

More
5 years 11 months ago #56387 by DPajak
I found the code for the dropdown date menu in quanda.php. Can I simple hide the code for "Show day select box"?
if (trim($qidattributes['dropdown_dates'])!=0) {
        if (!empty($_SESSION[$ia[1]]))
        {
            list($currentyear, $currentmonth, $currentdate) = explode('-', $_SESSION[$ia[1]]);
        } else {
            $currentdate='';
            $currentmonth='';
            $currentyear='';
        }
 
        $dateorder = preg_split('/[-\.\/ ]/', $dateformatdetails['phpdate']);
        $answer='<p class="question">';
        foreach($dateorder as $datepart)
        {
            switch($datepart)
            {
                // Show day select box
                case 'j':
                case 'd':   $answer .= ' <select id="day'.$ia[1].'" class="day">
                                                <option value="">'.$clang->gT('Day')."</option>\n";
                for ($i=1; $i<=31; $i++) {
                    if ($i == $currentdate)
                    {
                        $i_date_selected = SELECTED;
                    }
                    else
                    {
                        $i_date_selected = '';
                    }
                    $answer .= '    <option value="'.sprintf('%02d', $i).'"'.$i_date_selected.'>'.sprintf('%02d', $i)."</option>\n";
                }
                $answer .='</select>';
                break;
                // Show month select box
                case 'n':
                case 'm':   $answer .= ' <select id="month'.$ia[1].'" class="month">
                                            <option value="">'.$clang->gT('Month')."</option>\n";
                $montharray=array(
                $clang->gT('Jan'),
                $clang->gT('Feb'),
                $clang->gT('Mar'),
                $clang->gT('Apr'),
                $clang->gT('May'),
                $clang->gT('Jun'),
                $clang->gT('Jul'),
                $clang->gT('Aug'),
                $clang->gT('Sep'),
                $clang->gT('Oct'),
                $clang->gT('Nov'),
                $clang->gT('Dec'));
                for ($i=1; $i<=12; $i++) {
                    if ($i == $currentmonth)
                    {
                        $i_date_selected = SELECTED;
                    }
                    else
                    {
                        $i_date_selected = '';
                    }
 
                    $answer .= '    <option value="'.sprintf('%02d', $i).'"'.$i_date_selected.'>'.$montharray[$i-1].'</option>';
                }
                $answer .= '    </select>';
                break;
                // Show year select box
                case 'Y':   $answer .= ' <select id="year'.$ia[1].'" class="year">
                                            <option value="">'.$clang->gT('Year').'</option>';
 
                /*
                 *  New question attributes used only if question attribute
                 * "dropdown_dates" is used (see IF(...) above).
                 *
                 * yearmin = Minimum year value for dropdown list, if not set default is 1900
                 * yearmax = Maximum year value for dropdown list, if not set default is 2020
                 */
                if (trim($qidattributes['dropdown_dates_year_min'])!='')
                {
                    $yearmin = $qidattributes['dropdown_dates_year_min'];
                }
                else
                {
                    $yearmin = 1900;
                }
 
                if (trim($qidattributes['dropdown_dates_year_max'])!='')
                {
                    $yearmax = $qidattributes['dropdown_dates_year_max'];
                }
                else
                {
                    $yearmax = 2020;
                }
 
                if ($yearmin > $yearmax)
                {
                    $yearmin = 1900;
                    $yearmax = 2020;
                }
 
                if ($qidattributes['reverse']==1)
                {
                    $tmp = $yearmin;
                    $yearmin = $yearmax;
                    $yearmax = $tmp;
                    $step = 1;
                    $reverse = true;
                }
                else
                {
                    $step = -1;
                    $reverse = false;
                }
 
                for ($i=$yearmax; ($reverse? $i<=$yearmin: $i>=$yearmin); $i+=$step) {
                    if ($i == $currentyear)
                    {
                        $i_date_selected = SELECTED;
                    }
                    else
                    {
                        $i_date_selected = '';
                    }
                    $answer .= '  <option value="'.$i.'"'.$i_date_selected.'>'.$i.'</option>';
                }
                $answer .= '</select>';
 
                break;
            }
        }

Please Log in to join the conversation.

More
5 years 11 months ago #56391 by DenisChenu
Yes ,

You can modify the limesurvey code, but i think there is another solution in javascript.

Try something like that with QQQ your question number. ( in the question text).
$(document).ready(function(){
  $('#questionQQQ select.day option[value='1']').attr("selected", "selected");
  $('#questionQQQ select.day').hide();  
})

First try without the hide, to control day 1 are selected. (if not date cannot be integrated to database).

:)

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).

Please Log in to join the conversation.

More
5 years 11 months ago #56420 by DPajak
Ok, so I tried to add the following javascript code, in the question text field. My question id is 426. Unfortunately it didn't work, the day dropdown-box is still visible.
<script type="text/javascript">
$(document).ready(function(){
  $('#question426 select.day option[value='1']').attr("selected", "selected");
  $('#question426 select.day').hide();  
})
</script>
Any other suggestion, what I have done wrong?

If it is not working with javascript, would it be possible to change the code in quanda.php? I will have only one survey running, where I need the date question format. After the end of that survey I could change it back. But I am not sure what I have to change, so the date is saved correctly in the database. Any help is greatly appreciated.

Please Log in to join the conversation.

More
5 years 11 months ago #56429 by tpartner
I think there's a couple of small typos. Try:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
		$('#question426 select.day option[value="01"]').attr("selected", "selected");
		$('#question426 select.day').hide();  
	})
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 11 months ago #56457 by DPajak
Now its working in the preview of that single question (id 426). But when I try the preview of the group of questions or the whole survey, all three dropdown boxes are visible again.

Please Log in to join the conversation.

More
5 years 11 months ago #56461 by tpartner
Is the day first day of the month being selected? Are there any Javascript errors in the error console?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 11 months ago #56475 by DPajak
When I run the preview of the single question, everything looks fine and the day filed is hided. When I run the group or survey preview, all three boxes are visible and also the first day of the month is not selected.

I the firefox error console I found a few warnings and the following error:

Fehler: autoArray.list is undefined
Quelldatei: ima-research.eu/survey/scripts/survey_runtime.js
Zeile: 68


Kind regards
Danny

Please Log in to join the conversation.

More
5 years 11 months ago #56479 by tpartner
I think that something in your template may be causing the JS to fail. Test it with the default template.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 11 months ago #56500 by DPajak
Same problems with the default template.

Please Log in to join the conversation.

More
5 years 11 months ago #56503 by tpartner
I can't explain that. I have tested in IE and Firefox and it works fine.

Can you put a simple alert in to see if the script is running?
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
		$('#question426 select.day option[value="01"]').attr("selected", "selected");
		$('#question426 select.day').hide();
		alert ('Script runs!');  
	})
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 11 months ago #56506 by DPajak
Same issue. The script is running in the single question preview but not in the whole survey preview.

Is it possible to send you my login informations, so you can have a look by yourself?

By the way, I have tested in Safari and Firefox.

Please Log in to join the conversation.

More
5 years 11 months ago #56507 by tpartner
Sure, send me a PM.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: DPajak

Please Log in to join the conversation.

More
5 years 11 months ago - 5 years 11 months ago #56523 by DPajak
Thanks to Tony the problem is solved.

There was a JavaScript bug (see bugs.limesurvey.org/view.php?id=4938 ). It will be fixed in a future release.

In the meantime one can apply the fix in the installation - in /scripts/survey_runtime.js.

Change line 68 from :
if ((autoArray.list.length > 0)){

to:
if (autoArray.list != "undefined"){
Last Edit: 5 years 11 months ago by DPajak.

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form