diff --git a/src/base.ts b/src/base.ts index d2f3b5c996..bb7c9c7d42 100644 --- a/src/base.ts +++ b/src/base.ts @@ -100,6 +100,7 @@ export interface IQuestion extends IElement { getValueName(): string; clearValue(); clearValueIfInvisible(); + isAnswerCorrect(): boolean; } export interface IParentElement { addElement(element: IElement, index: number); diff --git a/src/question.ts b/src/question.ts index b9ae22d682..3e737c7435 100644 --- a/src/question.ts +++ b/src/question.ts @@ -1,4 +1,4 @@ -import { HashTable } from "./helpers"; +import { HashTable, Helpers } from "./helpers"; import { JsonObject } from "./jsonobject"; import { QuestionBase } from "./questionbase"; import { Base, SurveyError, SurveyElement } from "./base"; @@ -401,6 +401,22 @@ export class Question extends QuestionBase implements IValidatorOwner { this.setPropertyValue("defaultValue", val); this.updateValueWithDefaults(); } + /** + * The correct answer on the question. Set this value if you are doing a quiz. + * @see SurveyModel.correctAnswers + * @see SurveyModel.inCorrectAnswers + */ + public get correctAnswer(): any { + return this.getPropertyValue("correctAnswer"); + } + public set correctAnswer(val: any) { + this.setPropertyValue("correctAnswer", val); + } + public isAnswerCorrect(): boolean { + if (this.isValueEmpty(this.value) || this.isValueEmpty(this.correctAnswer)) + return false; + return this.isTwoValueEquals(this.value, this.correctAnswer); + } protected updateValueWithDefaults() { if ( this.isLoadingFromJson || @@ -581,6 +597,7 @@ JsonObject.metaData.addClass( "valueName", "enableIf:condition", "defaultValue:value", + "correctAnswer:value", "isRequired:boolean", { name: "requiredErrorText:text", diff --git a/src/questionbase.ts b/src/questionbase.ts index aa3bb5b5b3..843e0ae8a1 100644 --- a/src/questionbase.ts +++ b/src/questionbase.ts @@ -66,6 +66,7 @@ export class QuestionBase extends SurveyElement public set parent(val: IPanel) { this.setPropertyValue("parent", val); } + public isAnswerCorrect(): boolean { return false; } public getValueName(): string { return this.name; } diff --git a/src/survey.ts b/src/survey.ts index 74b1534acd..877b1b8274 100644 --- a/src/survey.ts +++ b/src/survey.ts @@ -27,6 +27,7 @@ import { CustomError } from "./error"; import { ILocalizableOwner, LocalizableString } from "./localizablestring"; import { StylesManager } from "./stylesmanager"; import { SurveyTimer } from "./surveytimer"; +import { Question } from "./question"; /** * Survey object contains information about the survey. Pages, Questions, flow logic and etc. @@ -1817,6 +1818,24 @@ export class SurveyModel extends Base } return result; } + /** + * Returns quiz questions. All visible questions that has input(s) widgets. + */ + public getQuizQuestions(): Array { + var result = new Array(); + var startIndex = this.firstPageIsStarted ? 1 : 0; + for (var i = startIndex; i < this.pages.length; i++) { + if (!this.pages[i].isVisible) continue; + var questions = this.pages[i].questions; + for (var j = 0; j < questions.length; j++) { + var q = questions[j]; + if (q.isVisible && q.hasInput) { + result.push(q); + } + } + } + return result; + } /** * Returns the list of all panels in the survey */ @@ -2112,6 +2131,21 @@ export class SurveyModel extends Base textValue.value = this.visiblePageCount; return; } + if (name === "correctedanswers") { + textValue.isExists = true; + textValue.value = this.getCorrectedAnswers(); + return; + } + if (name === "incorrectedanswers") { + textValue.isExists = true; + textValue.value = this.getInCorrectedAnswers(); + return; + } + if (name === "questioncount") { + textValue.isExists = true; + textValue.value = this.getQuizQuestions().length; + return; + } var firstName = new ProcessValue().getFirstName(name); var variable = this.getVariable(name); if (variable !== undefined) { @@ -2388,6 +2422,24 @@ export class SurveyModel extends Base res.hasAllValuesOnLastRun = this.textPreProcessor.hasAllValuesOnLastRun; return res; } + /** + * Returns the number of corrected answers on quiz + */ + public getCorrectedAnswers(): number { + var questions = this.getQuizQuestions(); + var counter = 0; + for (var i = 0; i < questions.length; i++) { + if (questions[i].isAnswerCorrect()) counter++; + } + return counter; + } + /** + * Returns the number of incorrected answers on quiz + */ + public getInCorrectedAnswers(): number { + var questions = this.getQuizQuestions(); + return questions.length - this.getCorrectedAnswers(); + } /** * Set it to 'top' or 'bottom' if you want to show the Panel with information about how much time the end-user spent of the survey/page. * If the value doesn't equal 'none' then survey calls startTimer() method on survey rendering. diff --git a/tests/surveytests.ts b/tests/surveytests.ts index 5b1c5adf10..206c64348f 100644 --- a/tests/surveytests.ts +++ b/tests/surveytests.ts @@ -3071,34 +3071,133 @@ QUnit.test("Survey show several pages as one + firstPageIsStarted", function( assert.equal(page.questions.length, 4, "there are 4 questions on the page"); }); -QUnit.test("Survey page hasShown", function( - assert -) { +QUnit.test("Survey page hasShown", function(assert) { var survey = twoPageSimplestSurvey(); assert.equal(survey.pages[0].hasShown, false, "The first page was not shown"); - assert.equal(survey.pages[1].hasShown, false, "The second page was not shown"); - var p = survey.currentPage + assert.equal( + survey.pages[1].hasShown, + false, + "The second page was not shown" + ); + var p = survey.currentPage; assert.equal(survey.pages[0].hasShown, true, "The first page was shown"); - assert.equal(survey.pages[1].hasShown, false, "The second page was not shown"); + assert.equal( + survey.pages[1].hasShown, + false, + "The second page was not shown" + ); survey.nextPage(); assert.equal(survey.pages[1].hasShown, true, "The second page was shown"); survey.clear(); - assert.equal(survey.pages[1].hasShown, false, "The second page hasShown is false again"); + assert.equal( + survey.pages[1].hasShown, + false, + "The second page hasShown is false again" + ); }); -QUnit.test("Questions are randomized", function( - assert -) { +QUnit.test("Questions are randomized", function(assert) { var survey = twoPageSimplestSurvey(); var page = survey.pages[0]; - assert.equal(page.isQuestionsRandomized, false, "By default questions are not randomized"); + assert.equal( + page.isQuestionsRandomized, + false, + "By default questions are not randomized" + ); page.questionsOrder = "random"; - assert.equal(page.isQuestionsRandomized, true, "page.questionsOrder = 'random'"); + assert.equal( + page.isQuestionsRandomized, + true, + "page.questionsOrder = 'random'" + ); page.questionsOrder = "default"; - assert.equal(page.isQuestionsRandomized, false, "page.questionsOrder = 'default'"); + assert.equal( + page.isQuestionsRandomized, + false, + "page.questionsOrder = 'default'" + ); survey.questionsOrder = "random"; - assert.equal(page.isQuestionsRandomized, true, "survey.questionsOrder = 'random' && page.questionsOrder = 'default'"); + assert.equal( + page.isQuestionsRandomized, + true, + "survey.questionsOrder = 'random' && page.questionsOrder = 'default'" + ); page.questionsOrder = "initial"; - assert.equal(page.isQuestionsRandomized, false, "page.questionsOrder = 'initial'"); + assert.equal( + page.isQuestionsRandomized, + false, + "page.questionsOrder = 'initial'" + ); +}); +QUnit.test("Quiz, correct, incorrect answers", function(assert) { + var survey = twoPageSimplestSurvey(); + var page = new PageModel("start"); + page.addNewQuestion("text", "name"); + page.addNewQuestion("text", "email"); + survey.pages.unshift(page); + survey.firstPageIsStarted = true; + for (var i = 1; i <= 4; i++) { + (survey.getQuestionByName("question" + i)).correctAnswer = + "q" + i; + } + survey.completedHtml = + "{correctedAnswers}, {inCorrectedAnswers}, {questionCount}"; + survey.start(); + assert.equal( + survey.getCorrectedAnswers(), + 0, + "The number of corrected answers is 0" + ); + assert.equal( + survey.getInCorrectedAnswers(), + 4, + "The number of corrected answers is 0" + ); + survey.getQuestionByName("question1").visible = false; + assert.equal( + survey.getInCorrectedAnswers(), + 3, + "The number of corrected answers is 0" + ); + (survey.getQuestionByName("question2")).value = "q2"; + assert.equal( + survey.getCorrectedAnswers(), + 1, + "The number of corrected answers is 0" + ); + assert.equal( + survey.getInCorrectedAnswers(), + 2, + "The number of corrected answers is 0" + ); + (survey.getQuestionByName("question3")).value = "q10"; + (survey.getQuestionByName("question4")).value = "q4"; + assert.equal( + survey.getCorrectedAnswers(), + 2, + "The number of corrected answers is 0" + ); + assert.equal( + survey.getInCorrectedAnswers(), + 1, + "The number of corrected answers is 0" + ); + (survey.getQuestionByName("question4")).visible = false; + assert.equal( + survey.getCorrectedAnswers(), + 1, + "The number of corrected answers is 0" + ); + assert.equal( + survey.getInCorrectedAnswers(), + 1, + "The number of corrected answers is 0" + ); + (survey.getQuestionByName("question4")).visible = true; + assert.equal( + survey.processedCompletedHtml, + "2, 1, 3", + "competed html is correct" + ); }); function twoPageSimplestSurvey() {