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

Add config and code to get latest record ID if multiple #38

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

paulj3
Copy link

@paulj3 paulj3 commented Sep 12, 2024

I am working with the PMI team on a solution that uses this plugin where the source project has an ID column called "snapshot_id". Because of a data integrity issue, there can be multiple records with the same snapshot_id, and the client always needs the most recent one. I could fork this EM and make my change for their needs, but I thought I would first consider making it an option on the existing plug-in.

This PR creates a new project-level config option called ``'use-latest-record-id'that will use the latest record-id if multiple records exist for the Unique Match Field. The code logic is doing a simpleksort`, so this only works for auto incr int IDs, but that is the default behavior so I think it is useful.

What do you think?

@paulj3 paulj3 requested review from mmcev106 and moorejr5 and removed request for mmcev106 September 12, 2024 18:30
@moorejr5
Copy link
Contributor

I can definitely see the usefulness of needing to do this. You may want to consider and work with the 'Records::getRecordList' method built into REDCap. Behind the scenes there is a 'redcap_record_list' DB table that I believe tracks records by order of creation. The method allows you to only check the order of a specific group of records. Using this and just grabbing the last entry should give you the last created record chronologically. It should make this work even outside of the auto-ID process.

@paulj3
Copy link
Author

paulj3 commented Sep 19, 2024

@moorejr5 amazing. Thank you for suggesting this. I just updated the code with your recommendation. I tested it and it works as expected, though I'm very new to using this method so any review would be appreciated!

getValue.php Outdated
if (!is_null($useLatestRecordIdSetting) && isset($useLatestRecordIdSetting[0]) && $useLatestRecordIdSetting[0] === true) {
$recordKeys = array_keys($filterData);
$records = \Records::getRecordList($_POST['otherpid'], [], false, false, null, null, 0, $recordKeys);
$recordId = end($records);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'getRecordList' function could possibly return 'false' in some instances. It's not likely, but you may want some default behavior in case '$records' ends up being false. Something like $recordId = end($records ?: [array_key_first($filterData)]); If you use 'array_key_first' you'd have to set the min-php version in config.json to 7.3, because apparently some people are still using this module on PHP 5.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome thanks for bringing this to my attention. I don't want to force 7.3 if I don't need to, so I found and tested another solution.

@paulj3 paulj3 requested a review from moorejr5 September 20, 2024 19:04
@paulj3
Copy link
Author

paulj3 commented Sep 23, 2024

@moorejr5 Thanks for approving this! What are the next steps? Should I add a blurb to the README? Do you merge it or do I?

@moorejr5
Copy link
Contributor

@moorejr5 Thanks for approving this! What are the next steps? Should I add a blurb to the README? Do you merge it or do I?

I think it would be worthwhile to update things to at least explain the use of the new setting. You can do the merge if you're fine with it. I believe this module is on the public repo, so it would need a new release version for this. I'm not sure if you've had to go through that process with modules. We can go over it sometime if you haven't so you can see the workflow.

@paulj3
Copy link
Author

paulj3 commented Sep 24, 2024

hi @moorejr5 - thanks for the suggestion. I've added to the docs, including new screenshot. I am very interested in setting learning how to release a new version, and I would love to learn how to do that from you.

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

Successfully merging this pull request may close these issues.

2 participants