Skip to content

Commit

Permalink
Randomizing questions on a page no longer works in questionPerPage mode
Browse files Browse the repository at this point in the history
fix #9385 (#9386)
  • Loading branch information
andrewtelnov authored Feb 3, 2025
1 parent 0a92df6 commit 5f85cba
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
9 changes: 7 additions & 2 deletions packages/survey-core/src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export class PageModel extends PanelModel implements IPage {
get hasShown(): boolean {
return this.wasShown;
}
public setWasShown(val: boolean) {
public setWasShown(val: boolean): void {
if (val == this.hasShownValue) return;
this.hasShownValue = val;
if (this.isDesignMode || val !== true) return;
Expand All @@ -289,7 +289,12 @@ export class PageModel extends PanelModel implements IPage {
(<PanelModelBase><any>els[i]).randomizeElements(this.areQuestionsRandomized);
}
}
this.randomizeElements(this.areQuestionsRandomized);
if(this.randomizeElements(this.areQuestionsRandomized)) {
const singleQuestion: any = this.survey?.currentSingleQuestion;
if(singleQuestion?.page === this) {
this.survey.currentSingleQuestion = this.getFirstVisibleQuestion();
}
}
}
/**
* Scrolls this page to the top.
Expand Down
12 changes: 10 additions & 2 deletions packages/survey-core/src/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ export class PanelModelBase extends SurveyElement<Question>
return isRandom && (this.questionOrder !== "initial") || this.questionOrder === "random";
}
protected isRandomizing = false;
randomizeElements(isRandom: boolean): void {
if (!this.canRandomize(isRandom) || this.isRandomizing) return;
randomizeElements(isRandom: boolean): boolean {
if (!this.canRandomize(isRandom) || this.isRandomizing) return false;
this.isRandomizing = true;
var oldElements = [];
var elements = this.elements;
Expand All @@ -580,6 +580,7 @@ export class PanelModelBase extends SurveyElement<Question>
this.updateRows();
this.updateVisibleIndexes();
this.isRandomizing = false;
return true;
}
/**
* Returns `true` if elements in this panel/page are arranged in random order.
Expand Down Expand Up @@ -1060,6 +1061,13 @@ export class PanelModelBase extends SurveyElement<Question>
}
return null;
}
getFirstVisibleQuestion(): Question {
const qs = this.questions;
for (let i = 0; i < qs.length; i++) {
if (qs[i].isVisible) return qs[i];
}
return null;
}
/**
* Focuses the first question in this panel/page.
* @see focusFirstErrorQuestion
Expand Down
23 changes: 23 additions & 0 deletions packages/survey-core/tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7812,6 +7812,29 @@ QUnit.test("Randomize questions in page and panels", function (assert) {

Helpers.randomizeArray = oldFunc;
});
QUnit.test("Randomize questions in page and panels & single question per page", function (assert) {
const oldFunc = Helpers.randomizeArray;
Helpers.randomizeArray = HelpTest.randomizeArray;

const survey = new SurveyModel({
questionsOnPageMode: "questionPerPage",
questionOrder: "random",
pages: [
{ elements: [{ type: "text", name: "q1" }, { type: "text", name: "q2" }, { type: "text", name: "q3" }] },
{ elements: [{ type: "text", name: "q4" }, { type: "text", name: "q5" }, { type: "text", name: "q6" }] },
]
});
const page = survey.pages[0];
assert.equal(page.elements[0].name, "q3");
assert.equal(page.elements[2].name, "q1");
assert.equal(survey.currentSingleQuestion.name, "q3", "The first question is q3");
survey.performNext();
survey.performNext();
survey.performNext();
assert.equal(survey.currentSingleQuestion.name, "q6", "The current question is q6");

Helpers.randomizeArray = oldFunc;
});

QUnit.test("Quiz, correct, incorrect answers", function (assert) {
var survey = new SurveyModel({
Expand Down

0 comments on commit 5f85cba

Please sign in to comment.