Major problems with questionnaire date field

Major problems with questionnaire date field

by Aaron Johnson -
Number of replies: 24

I think I am having two separate issues with the date field.

The first problem is that dates are being displayed as 1 day earlier then they are actually being stored in the database... Dates will export properly to csv, but when viewing a response with a filled in date field using the standard 'view responses' screen, the date for all responses is off by 1 day. I have attached a screenshot to help you see what I am talking about. Also, I have checked my local and server side date and timezone settings because this would be the obvious cause but I have not found any inconsistencies...

The problem gets worse because now I have 2 people who say they have entered a date like: 4/14/2003 but instead moodle is displaying 4/9/2003 but in the database it says 4/10/2003 so it's like the questions are being messed with before the user actually submits their response, plus there is a date display issue as well...

Wow, I really wish I would have just used a text box instead of the date field...

Attachment questionnaire02.jpg
Average of ratings: -
In reply to Aaron Johnson

Re: Major problems with questionnaire date field

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Which moodle version are you currently using?

And which language (if not English)?

Joseph

In reply to Joseph Rézeau

Re: Major problems with questionnaire date field

by Aaron Johnson -

Another Update: I just tested this on a brand new moodle installation on a completely different computer and it does the exact same thing!

The only thing about the environment that is the same is that I am using nginx + php-fpm instead of apache + mod_php but other than that the platform is entirely unique.

Steps to reproduce:

  • Install latest 1.9 Moodle (I used 1.9.12+)
  • Install Questionnaire module for 1.9
  • create a questionnaire and enable the save function
  • add a date field and a text box field to the questionnaire form
  • fill in both boxes with the same date and click save.
  • notice how the date field went back by 1 day where as the text box field remained the same.
  • Again enter the same date in both boxes and click submit.
  • notice how the date field when viewing 'Your Responses' is also back 1 day.
  • You can further test by viewing the mdl_questionnaire_response_date table and notice how the dates are actually stored correctly in the database!!!
Also, I have tested this using both Chromium and Firefox with the same results.
In reply to Aaron Johnson

Update

by Aaron Johnson -

Update: I have tested filling out the form and submitting it and indeed the date is being stored in the database correctly, and csv exports show the correct data so the good news is that most likely most of the dates that are stored in the database are being stored correctly (but only if the user did not use the Save button... see below).

Also, I tested filling out a questionnaire, and then clicked save and then when I looked at the date field it indeed went back 1 day! So then I completed and submitted the questionnaire and then my response showed that it went back another day! So basically what is happening is if the site stores/retrieves the date in any maner, it takes a day away from it. If you save, it takes a day away, then when you submit it takes another day away. I have tried clicking save multiple times and every single time I click save, 1 day is subtracted from the date! 

Why is it doing this!?!?!?!

Attachment questionnaire01.jpg
In reply to Aaron Johnson

Re: Update

by Aaron Johnson -

Sorry, I thought I put that in my initial post. I am using an up-to-date version of 1.9.9+ and the site is in en_US

In reply to Aaron Johnson

Re: Update

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Aaron,

I'll investigate your problem ASAP and report here.

Joseph

EDIT.- Cannot reproduce the problem on my local moodle test site. Which kind of database are you using on your moodle site: mysql or ...?

In reply to Joseph Rézeau

Re: Update

by Aaron Johnson -

Okay, I just set up another machine, this one is running Debian 6 64-bit but it still is running nginx + php-fpm and on all machines I'm running mysql. On the latest test machine I am not experiencing the bug just as you are sad

Now I have to figure out if it's a platform issue, web server configuration issue, or something in the code of the version of moodle I am using...

I will keep you posted and if I can get this other machine to do it I will send you a link so you can see it for yourself.

In reply to Joseph Rézeau

Re: Update

by Michael Spall -
Picture of Core developers Picture of Testers

I can reproduce this by choosing any Default timezone (timezone) setting that is less than UTC, e.g, UTC-0.5 or UTC-7 or MST7MDT or "Server's local time" since our server is MST7MDT. If I pick UTC or UTC +0.5 or UTC +13, I don't see this.

In reply to Michael Spall

Re: Update

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Michael,

Thanks for reporting this weird bug. I confirm that it is dependent on the timezone you set on your moodle site. Will investigate and report here.

Joseph

In reply to Joseph Rézeau

Re: Update

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Michael (and anyone who is using a timezone that is negative relative to UTC). I have hopefully found a fix for this date bug. I'd appreciate if you would try the fix before I commit it. This is for Moodle 1.9.

In file mod/questionnaire/locallib.php, function response_select( ... ), section --- response_date ---, around line 1875, change:

$val = gmmktime(0, 0, 0, $dateparts[1], $dateparts[2], $dateparts[0]); // Unix timestamp


to

$timezoneoffset = (get_user_timezone_offset() < 0);
$val = gmmktime(0, 0, 0, $dateparts[1], $dateparts[2] + $timezoneoffset, $dateparts[0]); // Unix timestamp

Joseph

In reply to Joseph Rézeau

Re: Update

by Michael Spall -
Picture of Core developers Picture of Testers

I was able to fix this by changing the first gmmktime back mktime in /contrib/plugins/mod/questionnaire/locallib.php

It is line 1839 of this patch:
http://cvs.moodle.org/contrib/plugins/mod/questionnaire/locallib.php?r1=1.123&r2=1.124

I don't think gmmktime is needed in this case since what is happenning is converting a date like 1999-12-25 to 12/ 25/1999 and ignoring any hrs, mins, and secs.

Your code is adding a day to the date for negative offset timezones and works since the time is being "rounded down to the next earliest date".

In reply to Joseph Rézeau

Re: Update

by Michael Spall -
Picture of Core developers Picture of Testers

I see a problem with using get_user_timezone_offset(). It doesn't take daylight savings time into account. Choose America/Scoresbysund which is UTC-1 but follows daylight savings time. and is UTC during the summer. When I enter 06/12/1998 in a date field your method produces 06/ 13/1998 while mine produces 06/ 12/1998.

This behavior could be OS dependent. I tested on Solaris 10 and will try RHEL 6.1 tomorrow.

In reply to Michael Spall

Re: Update

by Michael Spall -
Picture of Core developers Picture of Testers

I tested on RHEL 6.1 and got the same result for America/Scoresbysund and 06/12/1998. If you use 2/12/1998 which isn't during Scoresbysund DST your method gets the correct answer.

I think dates without times should use mktime. Without a time element you can't know wether to adjust a date +/- 1 for a different timezone.

In reply to Michael Spall

Re: Update

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Using mktime instead of gmmktime does not make any difference. Dates still go backwards one day each time the response form is saved!

I'll wait for more confirmation from moodlers who are in a "negative" time zone relative to GMT that my proposed fix works before committing. In the meantime you are welcome to use it on your moodle site.

Joseph

In reply to Joseph Rézeau

Re: Update

by Michael Spall -
Picture of Core developers Picture of Testers

But your fix doesn't work. If I use your method and America/Scoresbysund, the dates advance by one if you pick 6/8/1998. The problem is get_user_timezone_offset() is off by 1 for DST.

How about this:

$val = gmmktime(0, 0, 0, $dateparts[1], $dateparts[2], $dateparts[0]) - (get_user_timezone_offset() * 3600);
$val = userdate ( $val, $dateformat, get_user_timezone());

This will set the gmt time as 12:00m or 1:00am depending on DST.

 

In reply to Michael Spall

Re: Update

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Upon further experimenting with that date formatting problem it seems that we should use moodle's make_timestamp (  ) function rather than PHP's gmmktime (  ) or mktime (  ). That seems to solve all problems.

Please note that that change must be made in 2 files. I am attaching a patch.

Please test and report. With thanks,

Joseph

Average of ratings: Useful (1)
In reply to Joseph Rézeau

Re: Update

by Aaron Johnson -

Thanks so much for figuring this one out! I havn't been paying attention to this thread for a few days and started to give up hope!

I have tested Joseph Rézeau's patch and it appears to be working. I have not tested it thoroughly but so far so good! Excellent work people!

In reply to Joseph Rézeau

Re: Update

by Michael Spall -
Picture of Core developers Picture of Testers

I have tested this and it works. Thank you for your work on this. I also learned some valubable information from this, e.g., America/Scoresbysund is a great timezone to test with and getting dates correct takes some time.

In reply to Michael Spall

Re: Update

by blake barber -

Thanks for taking the time to help here. I have tried this fix but I am still getting the error when trying to enter in a date. Do I need to change the time zone settings in moodle? Do I need to force users to a certain time zone? 

In reply to blake barber

Re: Update

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

@Blake Barber

Which version of Moodle are you currently using? Are you using the most recent version of Questionnaire for that Moodle version? Normally, the bug has been fixed in available versions - no need to add a particular fix.

What is your time zone?

Joseph

In reply to Joseph Rézeau

Re: Update

by blake barber -

Here is what I have:

questionaire version: 2010110101

Moodle version: 2.0.2+

My time zone is central. So is the server. Users are worldwide. Everywhere.

In reply to blake barber

Re: Update

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Oops! clown

Upon further investigation, it looks like my suggested patch never made it to distributed versions of Questionnaire. I will create an entry in the bug tracker for it.

In the meantime, if you are using Questionnaire on a Moodle 2 version, please try the following changes.

in file <yourmoodle>/mod/questionnaire/locallib.php around line 1915

replace

$val = gmmktime(0, 0, 0, $dateparts[1], $dateparts[2], $dateparts[0]); // Unix timestamp

with

$val = make_timestamp($dateparts[0], $dateparts[1], $dateparts[2]);

in file <yourmoodle>/mod/questionnaire/questiontypes/questiontypes.class .php around line 670

replace

$this->text = gmmktime(0, 0, 0, $dateparts[1], $dateparts[2], $dateparts[0]); // Unix timestamp

with

$this->text = make_timestamp($dateparts[0], $dateparts[1], $dateparts[2]); // Unix timestamp

Please tell me if this works...

Joseph

In reply to Joseph Rézeau

Re: Update

by Ryan Brazell -

Hi Joseph,

Did this ever make it into the tracker? I wasn't able to find an entry for it. We are still on 1.9.12 but having this same problem. Thanks in advance ...

Ryan

In reply to Ryan Brazell

Re: Update

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

@Ryan,

Actually, in my previous message (Tuesday, 24 January 2012, 10:41 PM) I was asking blake barber to try my bug fix and tell me if it was working, before making an entry in the bug tracker... but he never answered.

Can you try my fix on your 1.9 moodle (test site, please) and tell me if it's working for you?

Joseph

In reply to Joseph Rézeau

Re: Update

by Mark Fischer -

I have applied this patch on a Moodle 1.9.16+ (Build: 20120119) and everything appears to be working correctly.

Thanks for the update.