I have been trying for a couple of days to add to my surveyjs questionnaire some datetime values refering to the start and ending timespan of the questionnaire. I use React in frontend and Python Django at the backend. Usually, I would use Python to create the datetime variable at the start and end of the questionnaire and all calculation could be done easily. Now, migrating to React and surveyJS it is a lot of fun and provides great improvements, but I now have to generate these start and end datetime variables in React, linking them directly to the json questionnaire variable. From my understanding this json file is naturally static, so I cannot just use a function and capture and store the datetime as a question value… I read and examined several links using time starts, and end with surveyjs, but I just can't find a way to make it work. I do calculate the datetime at the end of the survey, reading the completed json file… but also need to do this at the beginning of the survey, for example after the first question has been entered… and be able to store and save this datetime information before transferring it to my django api. Hope this makes sense, and you could help or guide me to a solution… Best Regards Guy
Calculating and adding survey start and end time to json file
Answers approved by surveyjs Support
Hello,
You can set start time on the first value change or just after creating the survey, it is up to you. Your code should be something like this:
JavaScriptthis.startTime = undefined;
this.endTime = undefined;
this.survey.onValueChanged.add((sender, options) => {
if(startTime == undefined) {
startTime = Date.now();
}
});
this.survey.onComplete.add((sender, options) => {
this.endTime = Date.now();
this.survey.setValue("startTime", this.startTime);
this.survey.setValue("endTime", this.endTime);
//save the results this.survey.data into your database
});
Thank you,
Andrew
SurveyJS Team
Hello Andrew,
Thank you for your email. I was not able to include the elements you provided in my program. I still get the error message for "startTime" and "endTime" are not defined no-undef…
I tried to include them in the json.js file (named question.js) but it says survey is not defined…
As mentioned, I named this file questions.js. It's a reduce version of my questions. I include the 4 basic files containing only 2 questions. I renamed the extension from ".js" to ".txt", as suggested by your virus protection) I hope with your expertise it will be easier and you can help me or guide me to a solution.
As for the surveyJS, I must say I am impressed. I was able to use it with the translation and it's great. The link with my Python Django API works fine. It's just getting this datetime start and end time that I need. As I mentioned, I am more competent with python programming, but using node with survey.js in my frontend is so powerful. It seems like the perfect combination… if I can make it work. Best regards and thank you once again for your help. Guy
Le 20-12-13 à 02 h 29, Andrew Telnov [surveyjs Support] a écrit :
Andrew,
Victory. I finally found a way around it and all is good. I basically was looking only for a way to generate a starting time and ending time for each survey.
For the starting time, I just create a variable when the survey starts… that is the variable const startTime = moment(Date.now()).format('YYYY-MM-DDTHH:MM:SS')
This variable is located in my component SondageDisplay.js, so where the axios url is.
As for the endtime, I use my data entry in python when the axios url transfert the data to my rest framework api. So this is good. My only question, I did not implement any variable or function directly associated with the questionnaire. I did some timing and it seem the starting time begins when the first question is completed… So that is good. There is probably a more elegant way to do it. I will surely improve it with time. But right now, I have all I need.
Thank you for your help. Best regards.
Guy
Le 20-12-13 à 02 h 29, Andrew Telnov [surveyjs Support] a écrit :
Hello,
I would set this variables both on server or both on client, but this approach can work as well.
Thank you,
Andrew
SurveyJS Team