Professional > Interview scripting > Writing interview scripts > Ending, suspending, and rolling back interviews > Calculating the total time taken to complete interviews
 
Calculating the total time taken to complete interviews
This topic gives an example of how to calculate the total time taken to complete an interview, excluding time between any stops and restarts of the interview.
The metadata section contains the following statements:
InterviewLength "Length of interview" long [0..];
SessionStartTime "Used to calculate time after a restart"
    date nocasedata;
InterviewLength is used to write the interview duration into the case data, and SessionStartTime is used in the calculation of time between stopping and restarting the interview.
The calculations are carried out in the routing section using the OnInterviewStart and OnInterviewEnd events.
Sub OnInterviewStart(IOM)
Dim IntDuration
IOM.Questions["SessionStartTime"] = Now("UTC")

' Assign question object to temporary variable to simplify
' structure oflater statements that use this object
Set IntDuration = IOM.Questions["InterviewLength"]

' Store current duration in interview property for writing to
' log file
If IntDuration = NULL Then
IOM.Info.User1 = 0
Else
IOM.Info.User1 = IntDuration
End If
Debug.Log("Survey Started: Current duration recorded as " + _
CText(IOM.Info.User1) + " seconds")
End Sub
Sub OnInterviewEnd(IOM)
Dim StartTime, IntDuration, EndTime

' Assign question objects to temporary variables to simplify
' structure of later statements that use these objects
Set StartTime = IOM.Questions["SessionStartTime"]
Set IntDuration = IOM.Questions["InterviewLength"]
' Retrieve original start time and duration if interviewer/respondent
' backtracks to first question
If Not StartTime.Info.IsOnPath Then
StartTime = StartTime.Info.OffPathResponse
End If
If Not IntDuration.Info.IsOnPath Then
IntDuration = IntDuration.Info.OffPathResponse
End If

' Update the duration
EndTime = IIf(IOM.InterviewStatus = InterviewStatus.isTimedOut, _
IOM.Info.LastAskedTime, Now("UTC"))
IntDuration = IntDuration + DateDiff(StartTime, EndTime, "s")
Debug.Log("Survey Ended: Interview duration recorded as " + _
CText(IntDuration) + " seconds")
End Sub
Both events use the Set function (see Assignment statements) to assign an interview object to a temporary variable. This makes later statements in the events shorter because they can refer to the object using the name of the temporary variable rather than having to use the full object reference. For example, OnInterviewStart sets the IntDuration variable to represent IOM.Questions["InterviewLength"]. This means that instead of writing:
If IOM.Questions["InterviewLength"] = NULL
you can use:
If IntDuration = NULL
The OnInterviewStart event performs the following tasks:
Writes the current time into the SessionStartTime item.
Copies the current interview duration (or zero if this is a new interview) into the User1 interview property.
Writes a message to the IVW log file reporting the current duration.
The OnInterviewEnd event performs the following tasks:
Updates the interview duration to include the time spent during the current session and writes a note about this to the IVW log file.
The IIf statement tests whether the interview has ended because it has timed out; that is, whether its current status is TimedOut. If the interview has timed out, the event sets EndTime to be the time at which the last question was asked. If the interview has not timed out (it may have completed, or have been stopped by the Stop button or a statement in the script), EndTime is set to the current time.
The interview's duration is calculated by adding the last recorded duration value to the difference between the start and end times of the current session. The duration is calculated in seconds.
If the interviewer or respondent has backtracked to the start of the interview, the interviewing program resets the interview start time and duration to null. OnInterviewEnd caters for this by checking whether StartTime and IntDuration are off-path and, if so, retrieving their original values for use in the calculations.
Here's an example with times to clarify this behavior.
Respondent actions
Script actions
Starts a new interview at 10:35am and answers questions for five minutes.
OnInterviewStart sets User1 to 0 and writes a message with this value to the log file.
Interrupted at Q10 and is unable to continue the interview before it times out.
OnInterviewEnd does the following:
sets IntDuration and SessionStartTime to on-path so that they will be saved in the value cache
sets EndTime to the time at which Q10 was asked (10.40am)
sets IntDuration to 300 seconds
writes a message containing this value to the log file.
Restarts the interview two hours later, at 12:40pm.
OnInterviewStart sets User1 to the currently saved interview duration (300 seconds) and writes a message with this value to the log file.
Clicks Stop at Q25. It is now 1pm.
OnInterviewEnd does the following:
sets IntDuration and SessionStartTime on-path if necessary
sets EndTime to the current time
sets IntDuration to the original 300 seconds plus the 1200 seconds for the current session
writes a message to the log file showing the current duration as 1500 seconds.
Restarts the interview at 2pm.
OnInterviewStart sets User1 to the currently saved interview duration (1500 seconds) and writes a message with this value to the log file.
Completes the interview at 2:15pm. It is now three hours and 40 minutes since the interview was started, but only 40 of those minutes (5 + 20 + 15) have been spent answering questions.
OnInterviewEnd does the following:
sets IntDuration and SessionStartTime on-path if necessary
sets EndTime to the current time
sets IntDuration to the previously saved 1500 seconds plus the 900 seconds for the current session
writes a message to the log file showing the current duration as 2400 seconds (40 minutes).
See also
Ending, suspending, and rolling back interviews