Skip to content

Commit

Permalink
auto merge
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-kurmanov committed Dec 26, 2017
2 parents c569e90 + 091c2ae commit 38d33ad
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface IQuestion extends IElement {
getValueName(): string;
clearValue();
clearValueIfInvisible();
isAnswerCorrect(): boolean;
}
export interface IParentElement {
addElement(element: IElement, index: number);
Expand Down
19 changes: 18 additions & 1 deletion src/question.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -581,6 +597,7 @@ JsonObject.metaData.addClass(
"valueName",
"enableIf:condition",
"defaultValue:value",
"correctAnswer:value",
"isRequired:boolean",
{
name: "requiredErrorText:text",
Expand Down
1 change: 1 addition & 0 deletions src/questionbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
52 changes: 52 additions & 0 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<IQuestion> {
var result = new Array<IQuestion>();
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
*/
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
129 changes: 114 additions & 15 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
(<Question>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"
);
(<Question>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"
);
(<Question>survey.getQuestionByName("question3")).value = "q10";
(<Question>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"
);
(<Question>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"
);
(<Question>survey.getQuestionByName("question4")).visible = true;
assert.equal(
survey.processedCompletedHtml,
"2, 1, 3",
"competed html is correct"
);
});

function twoPageSimplestSurvey() {
Expand Down

0 comments on commit 38d33ad

Please sign in to comment.