Survey-library: Is it possible to include correct answers count and question count in result?

Created on 10 Feb 2020  路  4Comments  路  Source: surveyjs/survey-library

Are you requesting a feature, reporting a bug or asking a question?

Question

What is the current behavior?

The values can be displayed in completedHtml using {correctedAnswers} and {questionCount}

What is the expected behavior?

I would like to get these two numbers in the result too. Is it possible?

How would you reproduce the current behavior (if this is a bug)?


Provide the test code and the tested page URL (if applicable)

Tested page URL:

Test code

your_code_here

Specify your

  • browser:
  • browser version:
  • surveyjs platform (angular or react or jquery or knockout or vue):
  • surveyjs version:
question

All 4 comments

@jakobjh You saving the data into your database yourself. It means that you can modify it as you need. Here is the docs.

Thank you,
Andrew

@andrewtelnov Yes, i know that. But it would be easier if the number of correct answers AND the number of questions was already in the Result JSON - now that they are calculated in the script.

But I found the following code to be working (despite my poor Javascript skills) :

Survey.Serializer.addProperty("question", "correct_answers:number");
Survey.Serializer.addProperty("question", "no_of_questions:number");

function addExtraInfo (result) {

var questions = survey.getQuizQuestions();
var correct = 0;
var numberOfQuestions = 0;
questions.forEach(function(question) {
    if(!question.isEmpty()) {
        numberOfQuestions ++;
        if(question.isAnswerCorrect())
            correct ++;
    }
});

result["correct_answers"] = correct;
result["no_of_questions"] = numberOfQuestions;

return result;

};

survey
.onComplete
.add(function (result) {
document
.querySelector('#surveyResult')
.textContent = "Result JSON:\n" + JSON.stringify(addExtraInfo(result.data), null, 3);
});

@jakobjh It is really severallines of code:

survey.onComplete.add(function (sender, options) {
  var result = sender.data;
  result["correct_answers"] = sender.getCorrectedAnswerCount();
  result["no_of_questions"] = sender.getQuizQuestionCount();
  //Post result into your database
});

Thank you,
Andrew

Ahh okay - great :)

Was this page helpful?
0 / 5 - 0 ratings