Skip to content

Commit

Permalink
onIsAnswerCorrect is raised erratically fix #6919 (#6927)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov authored Sep 10, 2023
1 parent 5e163fd commit 2a4bbc8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 68 deletions.
8 changes: 5 additions & 3 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1608,16 +1608,18 @@ export class Question extends SurveyElement<Question>
return 1;
}
protected getCorrectAnswerCount(): number {
return this.checkIfAnswerCorrect()? 1 : 0;
return this.checkIfAnswerCorrect() ? 1 : 0;
}
protected checkIfAnswerCorrect(): boolean {
const isEqual = this.isTwoValueEquals(this.value, this.correctAnswer, !settings.comparator.caseSensitive, true);
const options = { result: isEqual, correctAnswer: isEqual ? 1 : 0 };
const isEqual = Helpers.isTwoValueEquals(this.value, this.correctAnswer, this.getAnswerCorrectIgnoreOrder(), settings.comparator.caseSensitive, true);
const correct = isEqual ? 1 : 0;
const options = { result: isEqual, correctAnswer: correct, correctAnswers: correct, incorrectAnswers: this.quizQuestionCount - correct };
if(!!this.survey) {
this.survey.onCorrectQuestionAnswer(this, options);
}
return options.result;
}
protected getAnswerCorrectIgnoreOrder(): boolean { return false; }
/**
* Returns `true` if a question answer matches the `correctAnswer` property value.
*
Expand Down
4 changes: 2 additions & 2 deletions src/question_checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ export class QuestionCheckboxModel extends QuestionCheckboxBase {
const val = this.renderedValue as Array<any>;
return val.map((item: any) => this.createItemValue(item));
}

protected getAnswerCorrectIgnoreOrder(): boolean { return true; }
protected onCheckForErrors(
errors: Array<SurveyError>,
isOnValueChanged: boolean
) {
):void {
super.onCheckForErrors(errors, isOnValueChanged);
if (isOnValueChanged) return;

Expand Down
33 changes: 8 additions & 25 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6622,7 +6622,7 @@ export class SurveyModel extends SurveyElementCore
return options.html;
}
public getCorrectedAnswerCount(): number {
return this.getCorrectedAnswerCountCore(true);
return this.getCorrectAnswerCount();
}
/**
* Returns the number of correct answers in a quiz.
Expand Down Expand Up @@ -6653,7 +6653,7 @@ export class SurveyModel extends SurveyElementCore
return res;
}
public getInCorrectedAnswerCount(): number {
return this.getCorrectedAnswerCountCore(false);
return this.getInCorrectAnswerCount();
}
/**
* Returns the number of incorrect answers in a quiz.
Expand All @@ -6673,30 +6673,13 @@ export class SurveyModel extends SurveyElementCore
private getCorrectedAnswerCountCore(isCorrect: boolean): number {
var questions = this.getQuizQuestions();
var counter = 0;
const options: IsAnswerCorrectEvent = {
question: <Question>null,
result: false,
correctAnswers: 0,
incorrectAnswers: 0,
};
for (var i = 0; i < questions.length; i++) {
var q = <Question>questions[i];
var quizQuestionCount = q.quizQuestionCount;
options.question = q;
options.correctAnswers = q.correctAnswerCount;
options.incorrectAnswers = quizQuestionCount - options.correctAnswers;
options.result = options.question.isAnswerCorrect();
this.onIsAnswerCorrect.fire(this, options);
if (isCorrect) {
if (options.result || options.correctAnswers < quizQuestionCount) {
var addCount = options.correctAnswers;
if (addCount == 0 && options.result) addCount = 1;
counter += addCount;
}
for (let i = 0; i < questions.length; i++) {
const q = <Question>questions[i];
const correctCount = q.correctAnswerCount;
if(isCorrect) {
counter += correctCount;
} else {
if (!options.result || options.incorrectAnswers < quizQuestionCount) {
counter += options.incorrectAnswers;
}
counter += q.quizQuestionCount - correctCount;
}
}
return counter;
Expand Down
4 changes: 4 additions & 0 deletions tests/helperstests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ QUnit.test("isTwoValueEquals, undefined vs 'undefined', Bug# ", function(
QUnit.test("isTwoValueEquals, Arrays with empty objects", function(assert) {
assert.equal(Helpers.isTwoValueEquals([{ a: "a" }], [{ a: "a" }, {}]), false, "arrays are not equal");
});
QUnit.test("isTwoValueEquals, Arrays ignore orders", function(assert) {
assert.equal(Helpers.isTwoValueEquals([1, 2, 3], [3, 2, 1], true), true, "arrays ignore order");
assert.equal(Helpers.isTwoValueEquals([1, 2, 3], [3, 2, 1], false), false, "arrays doesn't ignore order");
});
QUnit.test("Helpers.isNumber", function(assert) {
assert.equal(Helpers.isNumber("1"), true, "1 is a number");
assert.equal(Helpers.isNumber("0xabcd"), true, "0xabcd is a number");
Expand Down
62 changes: 24 additions & 38 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7639,48 +7639,24 @@ QUnit.test(
q1.choices = [1, 2, 3, 4];
q1.correctAnswer = [2, 3];
q1.value = [1];
assert.equal(
survey.getCorrectedAnswerCount(),
0,
"The answer is incorrected"
);
assert.equal(
survey.getCorrectAnswerCount(),
0,
"The answer is incorrect"
);
assert.equal(survey.getQuizQuestions().length, 1, "survey.getQuizQuestions().length");
assert.equal(survey.getCorrectAnswerCount(), 0, "The answer is incorrect, #1");
q1.value = [3, 2];
assert.equal(
survey.getCorrectedAnswerCount(),
1,
"The answer is corrected now"
);
assert.equal(
survey.getCorrectAnswerCount(),
1,
"The answer is correct now"
);
assert.equal(q1.correctAnswerCount, 1, "q1.correctAnswerCount, #1");
assert.equal(survey.getCorrectAnswerCount(), 1, "The answer is correct now, #2");
let counter = 0;
survey.onIsAnswerCorrect.add(function (survey, options) {
var x = options.question.value;
var y = options.question.correctAnswer;
var res = x.length == y.length;
if (res) {
for (var i = 0; i < x.length; i++) {
if (x[i] != y[i]) {
res = false;
break;
}
}
}
options.result = res;
counter ++;
const q = options.question;
options.result = Helpers.isTwoValueEquals(q.value, q.correctAnswer, false);
});
assert.equal(
survey.getCorrectedAnswerCount(),
0,
"The order is important now"
);
assert.equal(q1.correctAnswerCount, 0, "q1.correctAnswerCount, #2");
assert.equal(counter, 1, "counter #1");
assert.equal(survey.getCorrectedAnswerCount(), 0, "The order is important now, #3");
assert.equal(counter, 2, "counter #2");
q1.value = [2, 3];
assert.equal(survey.getCorrectedAnswerCount(), 1, "The order is correct");
assert.equal(survey.getCorrectedAnswerCount(), 1, "The order is correct, #4");
assert.equal(counter, 3, "counter #3");
}
);
QUnit.test("Quiz, correct, incorrect answers and onIsAnswerCorrect event", function (assert) {
Expand Down Expand Up @@ -7738,21 +7714,31 @@ QUnit.test("question.isCorrectAnswer() and onIsAnswerCorrect event", function (a
{ type: "text", name: "q2", correctAnswer: 2 }
]
});
let counter = 0;
survey.onIsAnswerCorrect.add((sender, options) => {
counter++;
const q = options.question;
options.result = Math.abs(q.value - q.correctAnswer) < 2;
});
const q1 = survey.getQuestionByName("q1");
const q2 = survey.getQuestionByName("q2");
assert.equal(counter, 0, "counter #1");
assert.equal(q1.isAnswerCorrect(), false, "Value is empty");
assert.equal(counter, 0, "counter #2");
q1.value = 1;
assert.equal(q1.isAnswerCorrect(), true, "q1.value = 1");
assert.equal(counter, 1, "counter #3");
q1.value = 5;
assert.equal(q1.isAnswerCorrect(), false, "q1.value = 5");
assert.equal(counter, 2, "counter #4");
q1.value = 2;
assert.equal(q1.isAnswerCorrect(), true, "q1.value = 2");
assert.equal(counter, 3, "counter #5");
q2.value = 3;
assert.equal(q2.isAnswerCorrect(), true, "q2.value = 3");
assert.equal(counter, 4, "counter #6");
assert.equal(q2.isAnswerCorrect(), true, "q2.value = 3");
assert.equal(counter, 5, "counter #7");
});
QUnit.test(
"Quiz, correct, trim value on checking correct answers, https://surveyjs.answerdesk.io/ticket/details/T6569",
Expand Down

0 comments on commit 2a4bbc8

Please sign in to comment.