Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative defaults not triggered in first survey instance #55

Open
JuliaSprenger opened this issue Jun 15, 2021 · 8 comments
Open

Alternative defaults not triggered in first survey instance #55

JuliaSprenger opened this issue Jun 15, 2021 · 8 comments
Assignees

Comments

@JuliaSprenger
Copy link

JuliaSprenger commented Jun 15, 2021

When creating a survey that can be repeated and contains a field with multiple alternative default values, these default values are not displayed when the survey is loaded the first time (filling of first instance).

A minimal example instrument to observe this is

"Variable / Field Name","Form Name","Section Header","Field Type","Field Label","Choices, Calculations, OR Slider Labels","Field Note","Text Validation Type OR Show Slider Number","Text Validation Min","Text Validation Max",Identifier?,"Branching Logic (Show field only if...)","Required Field?","Custom Alignment","Question Number (surveys only)","Matrix Group Name","Matrix Ranking?","Field Annotation"
record_id,form_1,,text,"Record ID",,,,,,,,,,,,,
field1,form_1,,text,Field1,,,,,,,,,,,,," @DEFAULT_0="""" @DEFAULT_1=""instance 0 default value"""

When configuring this instrument as a repeatable survey, I would expect the field1 to be prefilled for all instances of the survey. However, this is only the case for all but the first instance.

The used versions are: Server info: REDCap 11.1.0 · PHP 7.3.27-1~deb10u1 (Linux/Unix OS) · MariaDB 10.3.27, Auto Populate Fields - v2.6.0

@michael-bentz
Copy link
Contributor

@JuliaSprenger Sorry for the delay, can you share your project xml?

@JuliaSprenger
Copy link
Author

JuliaSprenger commented Mar 7, 2022

Sorry for the delay, here's the corresponding corresponding project xml (I changed the file extension to zip to be able to upload the file here)

Two notes on this project file:

  • The project.xml file does not automatically activate the auto_populate_fields external module. This needs to be done manually.
  • The project.xml contains already a record as the survey URL does not load if the project has no complete record with instances registered yet. @mbentz-uf Do you have any idea why this might be the case?

@JuliaSprenger
Copy link
Author

@mbentz-uf Any updates on this? Also if you know a workaround for this issue, this would already be a great help.

@JuliaSprenger
Copy link
Author

@ChemiKyle Do you have any ideas for this?

@ChemiKyle ChemiKyle assigned ChemiKyle and unassigned michael-bentz Mar 23, 2022
@ChemiKyle
Copy link
Contributor

ChemiKyle commented Mar 23, 2022

Hi @JuliaSprenger,

Looks like there's a bit of odd behavior with a hardcoded string in @DEFAULT_0 for the first instance in the survey context. As a workaround meantime, using @DEFAULT="instance 0 default value" results in autofilling as expected:

I'd assume your actual survey is a bit more complicated? I think fixing this bug would benefit from a real example, since a string value set @DEFAULT_0 should be the same as having one in @DEFAULT (the bug is that it is not!), and you likely wouldn't be putting an empty string in any @DEFAULT_<N> field.


Bug:

@DEFAULT and @DEFAULT_0 are not equivalent in the first instance of a survey launched from a public survey link.

Here is what appears when creating a new record (bottom left) and initiating a survey response (bottom right):

image

Note that when using @DEFAULT_0 APF does not fill the survey:
image

After clicking "Recommencer cette enquête" (restart this survey; creates a new instance in the same record) the value @DEFAULT_0 does prepopulate the field as expected.

if (!$this->currentFormHasData()) {

This line does not behave as expected on instance 1 of a survey response.

if (Records::formHasData($record, $_GET['page'], $_GET['event_id'], $_GET['instance'])) {

$_GET['instance'] is integer 1 for the first instance and a string for subsequent values, causing this line to return an erroneous value, however fixing this alone does not rectify the bug.

@JuliaSprenger
Copy link
Author

JuliaSprenger commented Apr 4, 2022

Hi @ChemiKyle!
Thanks for looking into this. Indeed I provided an oversimplified example of my survey. In practice I am trying to have a fixed default value for the first instance of a field and prefill the field with previously entered values in subsequent instances. I tried this via @DEFAULT="[field1][previous-instance]" @DEFAULT_1="instance 0 default value". I think for the first instance "[field1][previous-instance]" should be equivalent to the empty string I provided in the example project above.
Do you think fixing the bug you describe above will fix the issue or is this more complicated than I think?

@ChemiKyle
Copy link
Contributor

ChemiKyle commented Apr 4, 2022

It looked like some logic quirks make APF not activate at all on the first response of a survey. Unfortunately those logic quirks were in REDCap core so it would take more time than I have at the moment to find a workaround in the module.

However, if you're looking for a more immediate solution, you could probably use REDCap JavaScript Injector to fill that particular field if it's blank.
I've solved this problem in other modules, here's a simplified snippet that you could use:

function fillIfBlank(field_name, value) {
    let $target_field = $(`input[name='${field_name}']`);
    // Don't overwrite field if a value is there
    if ($target_field.val() != "") { return; }

    $target_field.val(value);
    // trigger field validation
    $target_field.blur();
}


$(document).ready(function() {
    fillIfBlank("my_field_name", "default value if field is blank")
});

In the 2nd to last line, you'd just change "my_field_name" to the target field's coded name (not its label) and "default value if field is blank" to ... the default value if the field is blank.

@JuliaSprenger
Copy link
Author

@ChemiKyle Thanks for the explanations. If the origin of the bug is in RedCap itself, should I raise this issue also in another repository to be potentially addressed in a future RedCap release?

Unfortunately, injecting JavaScript snippets for each project is not very feasible for me and out of the scope of my work, but thanks for the hint, I didn't know that module before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants