Skip to content

Commit

Permalink
Filter URL parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub-jedrusiak committed Oct 22, 2024
1 parent 801435c commit 234e3b6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## future release

* Hot reloading now only reloads the survey, not the whole page.
* Added `UrlParameters` argument to the `Survey` class to specify what parameters should be saved in the database.

## 0.3.2

Expand Down
2 changes: 2 additions & 0 deletions velesresearch/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ class SurveyModel(BaseModel):
title (str | None): The title of the survey.
tocLocation (str): The location of the table of contents. Can be 'left' (default), 'right'. See `showTOC`.
triggers (str | None): Triggers for the survey. Usually not necessary. See <https://surveyjs.io/form-library/documentation/design-survey/conditional-logic#conditional-survey-logic-triggers>.
UrlParameters (list[str] | None): The URL parameters to be expected and saved. Default is None.
validateVisitedEmptyFields (bool): Whether to validate empty fields that had been clicked, and unclicked empty. Default is False.
width (str | None): Width of the survey in CSS units. Default is None (inherit from the container).
widthMode (str): The mode of the width. Can be 'auto' (default; the width is set by the content), 'static', 'responsive'.
Expand Down Expand Up @@ -911,6 +912,7 @@ class SurveyModel(BaseModel):
title: str | None = None
tocLocation: str = "left"
triggers: list[dict] | None = None
UrlParameters: list[str] | None = None
validateVisitedEmptyFields: bool = False
width: str | None = None
widthMode: str = "auto"
Expand Down
13 changes: 11 additions & 2 deletions velesresearch/website_template/src/SurveyComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,23 @@ function createResults(survey) {
variables[variable] = survey.getVariable(variable);
}

const URLparams = Object.fromEntries(new URLSearchParams(window.location.search));
const URLparams = new URLSearchParams(window.location.search);
const filteredParams = {};
if (survey.jsonObj.UrlParameters) {
survey.jsonObj.UrlParameters.forEach(param => {
const value = URLparams.get(param);
if (value !== null) {
filteredParams[param] = value;
}
});
}

return Object.assign(
{
id: survey.participantID
},
survey.data,
URLparams,
filteredParams,
variables
);
}
Expand Down
5 changes: 5 additions & 0 deletions velesresearch/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def survey(
title: str | None = None,
tocLocation: str = "left",
triggers: list[dict] | None = None,
UrlParameters: str | list[str] | None = None,
validateVisitedEmptyFields: bool = False,
width: str | None = None,
widthMode: str = "auto",
Expand Down Expand Up @@ -157,6 +158,7 @@ def survey(
title (str | None): The title of the survey.
tocLocation (str): The location of the table of contents. Can be 'left' (default), 'right'. See `showTOC`.
triggers (str | None): Triggers for the survey. Usually not necessary. See <https://surveyjs.io/form-library/documentation/design-survey/conditional-logic#conditional-survey-logic-triggers>.
UrlParameters (list[str] | None): The URL parameters to be expected and saved. Default is None.
validateVisitedEmptyFields (bool): Whether to validate empty fields that had been clicked, and unclicked empty. Default is False.
width (str | None): Width of the survey in CSS units. Default is None (inherit from the container).
widthMode (str): The mode of the width. Can be 'auto' (default; the width is set by the content), 'static', 'responsive'.
Expand All @@ -165,6 +167,8 @@ def survey(
customFunctions (str | None): Custom JS functions definitions to be added to the survey. To be used with `customCode`.
"""
if not isinstance(UrlParameters, list):
UrlParameters = [UrlParameters] if UrlParameters else None
args = {
"addScoreToResults": addScoreToResults,
"allowCompleteSurveyAutomatic": allowCompleteSurveyAutomatic,
Expand Down Expand Up @@ -234,6 +238,7 @@ def survey(
"title": title,
"tocLocation": tocLocation,
"triggers": triggers,
"UrlParameters": UrlParameters,
"validateVisitedEmptyFields": validateVisitedEmptyFields,
"width": width,
"widthMode": widthMode,
Expand Down

0 comments on commit 234e3b6

Please sign in to comment.