Skip to content

Commit 15412e1

Browse files
author
“Carmel
committed
Write survey links to ldot and flip completion status
1 parent 7687612 commit 15412e1

9 files changed

Lines changed: 64 additions & 37 deletions

app.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ def button1():
4141

4242
ldot_study_id = ldot_vars.get("ldot_study_id")
4343
link_creation_eaid = ldot_vars.get("eaid_qualtrics_survey_link_creation_to_do_date")
44+
link_completed_eaid = ldot_vars.get("eaid_qualtrics_survey_link_completed")
4445

45-
new_subjects_ids = get_new_subjects(ldot_study_id, link_creation_eaid)
46+
new_subjects_ids = get_new_subjects(ldot_study_id, link_creation_eaid, link_completed_eaid)
4647

4748
message = f"Found {len(new_subjects_ids)} new subjects in this study"
4849
return jsonify({"success": True, "message": message, "new_subject_ids": new_subjects_ids})
@@ -69,6 +70,8 @@ def button2():
6970
id_deelnemer_entity = ldot_vars.get("id_deelnemer_entity")
7071
id_location = ldot_vars.get("id_location")
7172
custom_var_qualtrics_link = ldot_vars.get("custom_var_qualtrics_link")
73+
link_completed_eaid = ldot_vars.get("eaid_qualtrics_survey_link_creation_completed")
74+
7275
qualtrics_survey_id = qualtrics_vars.get("survey_id")
7376
mailing_list_id = qualtrics_vars.get("mailing_list_id")
7477
embedded_data_field = qualtrics_vars.get("embedded_data_field")
@@ -92,7 +95,7 @@ def button2():
9295
}
9396

9497
subject_id_to_link_dict = add_individuals_to_survey(new_subject_ids, ldot_study_id, id_deelnemer_entity, embedded_data_field, distribution_id, qualtrics_survey_id, mailing_list_id, directory_id)
95-
send_links_to_ldot(ldot_study_id, id_deelnemer_entity, id_location, custom_var_qualtrics_link, subject_id_to_link_dict)
98+
send_links_to_ldot(ldot_study_id, id_deelnemer_entity, id_location, custom_var_qualtrics_link, link_completed_eaid, subject_id_to_link_dict)
9699

97100
return jsonify({
98101
"success": True,
@@ -118,7 +121,6 @@ def button3():
118121

119122
try:
120123
result = f"Button 3 executed for study {study_id}"
121-
# Return both message and subject_ids list
122124
subjects_not_completed_survey = get_incomplete_subjects(ldot_study_id, eaid_survey_invitation_completed, eaid_survey_progress_completed)
123125
return jsonify({"success": True, "message": result, "subject_ids": subjects_not_completed_survey})
124126
except Exception as e:
@@ -129,11 +131,11 @@ def button3():
129131
def button4():
130132
"""Fourth API call - uses subjectIDs from button3"""
131133
data = request.json
132-
# study_id = data.get("study_id")
133-
# subject_ids = data.get("subject_ids") # List of subjectIDs from button3
134+
study_id = data.get("study_id")
135+
subject_ids = data.get("subject_ids") # List of subjectIDs from button3
134136

135-
# if not study_id:
136-
# return jsonify({"success": False, "message": "Missing study_id in request"}), 400
137+
if not study_id:
138+
return jsonify({"success": False, "message": "Missing study_id in request"}), 400
137139

138140
# if not subject_ids:
139141
# return jsonify({"success": False, "message": "Missing subject_ids in request"}), 400
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

new_ldot_workflows/b_1_get_new_subjects.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
CLIENT_SECRET = config["client_secret"]
88
LDOT_API_URL = config["LDOT_API_URL"]
99

10-
def get_new_subjects(study_id: str, eaid_qualtrics_survey_link_creation_to_do_date: str) -> list:
10+
def get_new_subjects(study_id: str, eaid_qualtrics_survey_link_creation_to_do_date: str, eaid_qualtrics_survey_link_completed: str) -> list:
1111
"""Get subjects that have not yet been added to Qualtrics by checking their event actions"""
1212

1313
response = requests.post(
@@ -25,6 +25,7 @@ def get_new_subjects(study_id: str, eaid_qualtrics_survey_link_creation_to_do_da
2525
"Authorization": f"Bearer {token}"
2626
}
2727

28+
# Get subjects that have link creation to do date
2829
response = requests.get(
2930
f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{study_id}/Action/{eaid_qualtrics_survey_link_creation_to_do_date}",
3031
headers=headers
@@ -33,12 +34,39 @@ def get_new_subjects(study_id: str, eaid_qualtrics_survey_link_creation_to_do_da
3334
response.raise_for_status()
3435
payload = response.json()
3536
study_event_actions = payload.get("Data", {}).get("StudyEventActions", [])
36-
return [
37+
38+
subjects_link_creation_to_do = set([
3739
action["SubjectGuid"]
3840
for action in study_event_actions
3941
if action.get("SubjectGuid")
40-
]
42+
])
43+
44+
# print(f"Subjects with link creation to do: {subjects_link_creation_to_do}")
45+
46+
# Get subjects that have link completed date
47+
response = requests.get(
48+
f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{study_id}/Action/{eaid_qualtrics_survey_link_completed}",
49+
headers=headers
50+
)
51+
response.raise_for_status()
52+
payload = response.json()
53+
study_event_actions_completed = payload.get("Data", {}).get("StudyEventActions", [])
54+
subjects_link_completed = set([
55+
action["SubjectGuid"]
56+
for action in study_event_actions_completed
57+
if action.get("SubjectGuid")
58+
])
59+
60+
# print(f"Subjects with link completed: {subjects_link_completed}")
61+
62+
# Filter out subjects that have already completed the link
63+
new_subjects = subjects_link_creation_to_do - subjects_link_completed
64+
65+
# print(f"New subjects: {new_subjects}")
66+
return list(new_subjects)
67+
68+
4169

4270
if __name__ == "__main__":
4371
# For testing purposes
44-
print(get_new_subjects("5c9c6a47-c8d7-8142-a8c8-ccdcb8a8044b", "5d31129c-d814-5d4b-a96f-048cadc150ce"))
72+
print(get_new_subjects("5c9c6a47-c8d7-8142-a8c8-ccdcb8a8044b", "5d31129c-d814-5d4b-a96f-048cadc150ce", "31599192-8e9b-4341-b7f4-8b8967dd846a"))

new_ldot_workflows/b_2_send_links_to_ldot.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
LDOT_API_URL = config["LDOT_API_URL"]
99

1010

11-
def send_links_to_ldot(ldot_study_id: str, id_deelnemer_entity: str, id_location: str, custom_var_qualtrics_link: str, subject_id_to_link_dict: dict) -> list:
11+
def send_links_to_ldot(ldot_study_id: str, id_deelnemer_entity: str, id_location: str, custom_var_qualtrics_link: str, link_completed_eaid: str, subject_id_to_link_dict: dict) -> list:
1212
"""Post the Qualtrics links back to Ldot for new subjects"""
1313

1414
response = requests.post(
@@ -25,6 +25,8 @@ def send_links_to_ldot(ldot_study_id: str, id_deelnemer_entity: str, id_location
2525
}
2626

2727
for subject_id, link in subject_id_to_link_dict.items():
28+
29+
# Populate the Qualtrics link in Ldot for the subject
2830
response = requests.post(
2931
f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{ldot_study_id}/Subject/",
3032
headers=headers,
@@ -37,12 +39,26 @@ def send_links_to_ldot(ldot_study_id: str, id_deelnemer_entity: str, id_location
3739
}
3840
)
3941

42+
# Add Qualtrics survey link completed event action for the subject
43+
response = requests.post(
44+
f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{ldot_study_id}/Action/{link_completed_eaid}/",
45+
headers=headers,
46+
params = {
47+
"subjectGuid": subject_id,
48+
}
49+
)
50+
51+
response.raise_for_status() # Raise an exception if the request was unsuccessful
52+
response_data = response.json()
53+
print(f"Successfully sent link for subject {subject_id} to Ldot. Response: {response_data}")
54+
4055

4156
if __name__ == "__main__":
4257
ldot_study_id = "5c9c6a47-c8d7-8142-a8c8-ccdcb8a8044b"
4358
id_deelnemer_entity = "7f61b810-00ed-1d41-8a33-4164f25ebad0"
4459
id_location = "427f304f-9d95-44f5-8f7b-d6a1ce1db293"
4560
subject_id_to_link_dict = {'352fb9d8-962f-4735-9fc7-7b4e18109a51': 'https://survey.uu.nl/jfe/form/SV_efCMOg6wHU0T8ii?Q_CHL=gl&Q_DL=EMD_7AEa416lRwFhrkF_efCMOg6wHU0T8ii_CGC_4WW2MwOaEB01XsM&_g_=g'}
4661
custom_var_qualtrics_link = "customVar01"
62+
link_completed_eaid = "31599192-8e9b-4341-b7f4-8b8967dd846a"
4763

48-
send_links_to_ldot(ldot_study_id, id_deelnemer_entity, id_location, custom_var_qualtrics_link, subject_id_to_link_dict)
64+
send_links_to_ldot(ldot_study_id, id_deelnemer_entity, id_location, custom_var_qualtrics_link, link_completed_eaid, subject_id_to_link_dict)

new_ldot_workflows/b_3_get_incomplete_subjects.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,20 @@ def get_incomplete_subjects(study_id: str, eaid_survey_invitation_completed: str
2525
"Authorization": f"Bearer {token}"
2626
}
2727

28-
# actions = requests.get(
29-
# f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{study_id}/Action",
30-
# headers=headers
31-
# )
3228

33-
# actions.raise_for_status()
34-
# payload = actions.json()
35-
# # print(payload)
36-
# action_guids = []
37-
# for action in payload.get("Data", {}).get("StudyEventActions", []):
38-
# action_guids.append((action["EventActionGuid"], action["Description"]))
39-
40-
# for guid, description in action_guids:
41-
# people_with_this_action = requests.get(
42-
# f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{study_id}/Action/{guid}",
43-
# headers=headers
44-
# )
45-
# people_with_this_action.raise_for_status()
46-
# if "Data" in people_with_this_action.json():
47-
# print (people_with_this_action.json()["Data"]["StudyEventActions"])
48-
49-
50-
# # Get SubjectIDs where survey invitation has been completed but survey progress has not been completed
29+
# Get SubjectIDs where survey invitation has been completed but survey progress has not been completed
5130
result = requests.get(
5231
f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{study_id}/Action/{eaid_survey_invitation_completed}",
5332
headers=headers
5433
)
5534
result.raise_for_status()
5635

5736
subjects_with_survey_invitation_completed = set()
37+
5838
for action in result.json().get("Data", {}).get("StudyEventActions", []):
5939
subjects_with_survey_invitation_completed.add(action["SubjectGuid"])
6040

61-
print(f"Subjects with survey invitation completed: {subjects_with_survey_invitation_completed}")
41+
# print(f"Subjects with survey invitation completed: {subjects_with_survey_invitation_completed}")
6242

6343
result = requests.get(
6444
f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{study_id}/Action/{eaid_survey_progress_completed}",
@@ -70,7 +50,7 @@ def get_incomplete_subjects(study_id: str, eaid_survey_invitation_completed: str
7050
for action in result.json().get("Data", {}).get("StudyEventActions", []):
7151
subjects_with_survey_progress_completed.add(action["SubjectGuid"])
7252

73-
print(f"Subjects with survey progress completed: {subjects_with_survey_progress_completed}")
53+
# print(f"Subjects with survey progress completed: {subjects_with_survey_progress_completed}")
7454

7555
incomplete_subjects = subjects_with_survey_invitation_completed - subjects_with_survey_progress_completed
7656
return list(incomplete_subjects)

study_configs.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ LDOT-001:
1010
eaid_survey_invitation_completed: 337ecba2-3dd3-1141-8c38-6cffdcfbd7eb #Action event for when the survey invitation has been sent
1111

1212
eaid_survey_progress_completed: 120d298d-9aa9-084b-abc6-432148db0e10
13+
1314
qualtrics_variables:
1415
qualtrics_survey_id: SV_efCMOg6wHU0T8ii
1516
directory_id: POOL_10pyxk9leSUisrT

0 commit comments

Comments
 (0)