views:

37

answers:

1

We have a requirement to count down based on a user taking a test. What would be a best way to tackle tracking the time taken by a user while taking the test.

We do capture start time, end time. But the calculations go awry if the application server or the OS goes down during the test. We were thinking of using another variable to store the current time after the user submits an answer to the question. So (end time - current time) would reasonably account for the amount of time left.

Is there an effective way to calculate the "time left" in such cases other than the one mentioned above?

We would like the solution to be database agnostic as possible

A: 

To be specific, I'll continue with MYSQL.

As you may stated, you have captured start time. When the test loaded by the user, write this timestamp in a DATETIME field. Another option is that using UNIX_STAMP. And then, when user submits the answer, you may easily put this data to another DATETIME field.

As well as other rdbms systems, mysql got the date-time manipulation functions.

SELECT CURRENT_TIMESTAMP(); query returns current timestamp. eg. '2007-12-15 23:50:26'

SELECT UNIX_TIMESTAMP(); query returns current unix timestamp which may be easy to calculate difference. eg. 1111885200

Also we have got DATE_SUB() and DATE_ADD() functions for addition, subtraction operations.

Please visit date-time manual page for details. I guess this information will lead you to a proper solution.

-- Added on Sep 18:

You may use javascript to track user behaviour. For instance, a function calls a server side script with a salt or something you have in session. That server side script records the current timestamp as "last update". Database parts same as above.

enesismail
Actually you do not need a "current time" field. For future checks, you need two need fields: 1-testStarted 2-testEnded. Just set these fields' values when the action done. The rest is calculating the difference. Which can easily done using date_sub(). To figure out a more effective way, please be more clear. For instance, define whats wrong with this solution. Cheers.
enesismail
@enesismail If I just have 2 dates and if the OS crashes, when I restart the app server. I wouldn't know from where to re-start the counter, this is the problem I am currently facing (maybe the user was 10 minutes into a 20 minute test or 5 minutes into a 20 minute test)
Samuel
I see. I'll add my suggestion about that.
enesismail