-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatientSearchAjax.php
More file actions
189 lines (153 loc) · 6.12 KB
/
patientSearchAjax.php
File metadata and controls
189 lines (153 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
require_once('base.php');
/* @var $module RedcapAfrica\OrganRegistryModule\OrganRegistryModule */
$searchFields = $module->getProjectSetting("search-fields");
$lookupFields = $module->getProjectSetting("matching-fields");
$logicTypes = $module->getProjectSetting("matching-logic");
$displayFields = $module->getProjectSetting("display-fields");
if(count($lookupFields) == 0 || count($searchFields) == 0) die();
## Get submitted form data
$searchData = [];
foreach($searchFields as $fieldKey => $thisField) {
if($thisField == "") continue;
if((is_array($_POST[$thisField]) && strlen($thisField) > 0) || (!is_array($_POST[$thisField] && $_POST[$thisField] != ""))) {
$searchData[$fieldKey] = $_POST[$thisField];
}
}
$user = $_SESSION['username'];
$savedQueryParams = array_combine($searchFields, $searchData);
$logParams = [
'user' => $user,
'searchParams' => json_encode(array_combine($searchFields, $searchData)),
];
if($_SESSION['debug_logging'] == "on") {
echo "Search Data:<br />";
echo "<pre>";var_dump($searchData);echo "</pre>";
}
$sql = "SELECT d.record,d.field_name,d.value,d.instance
FROM redcap_data d
WHERE d.field_name IN (";
foreach($lookupFields as $fieldKey => $fieldName) {
if($fieldName == "") continue;
$sql .= ($fieldKey == 0 ? "" : ", ")."'".db_escape($fieldName)."'";
}
$sql .= ") AND d.project_id = '".db_escape($project)."'";
$q = db_query($sql);
if($e = db_error()) {
var_dump($e);
die();
}
$moduleProject = $module->framework->getProject();
$checkboxFields = [];
$recordData = [];
while($row = db_fetch_assoc($q)) {
$isCheckbox = $moduleProject->getField($row["field_name"])->getType() == "checkbox";
if($isCheckbox) {
$checkboxFields[$row["field_name"]] = 1;
}
if(is_array($recordData[$row["record"]]) && array_key_exists($row["field_name"],$recordData[$row["record"]])) {
if(!is_array($recordData[$row["record"]][$row["field_name"]])) {
$recordData[$row["record"]][$row["field_name"]] = [$recordData[$row["record"]][$row["field_name"]]];
}
$recordData[$row["record"]][$row["field_name"]][] = $row["value"];
}
else {
if($isCheckbox) {
$recordData[$row["record"]][$row["field_name"]] = [$row["value"]];
}
else {
$recordData[$row["record"]][$row["field_name"]] = $row["value"];
}
}
}
if($_SESSION['debug_logging'] == "on") {
echo "<br /><pre>";
var_dump($checkboxFields);
echo "</pre><br />";
}
## Now need to do the matching here
$recordIds = [];
$skippedFields = [];
$skippedRecordMessages = [];
foreach($recordData as $recordId => $recordDetails) {
$recordMatches = true;
## Sometimes lots of options show up for a given antibody, so repeating instead of single
## Loop through this record's searchable fields and compare to the form data submitted
foreach($searchData as $fieldKey => $searchValue) {
## Support non-array search fields
if(!is_array($searchValue)) {
$searchValue = [$searchValue];
}
foreach($searchValue as $actualValue) {
if($actualValue == "") {
$skippedFields[$lookupFields[$fieldKey]] = 1;
continue;
}
$lookupField = $lookupFields[$fieldKey];
$logicType = $logicTypes[$fieldKey];
if($lookupField == "") continue;
is_array($recordDetails[$lookupField]) && sort($recordDetails[$lookupField]);
## All search fields need to have a value for the record or else skip
if(!array_key_exists($lookupField,$recordDetails)) {
$recordMatches = false;
## Log the records that were skipped and why
$skippedRecordMessages['skipped_record_'.$recordId] = "Skipping record $recordId as has blank values for $lookupField";
break;
}
if(is_array($recordDetails[$lookupField])) {
$fieldMatches = in_array($actualValue,$recordDetails[$lookupField]);
$fieldMessage = '[' .implode("', '", $recordDetails[$lookupField]) . ']';
}
else {
$fieldMatches = ($actualValue === $recordDetails[$lookupField]);
$labels = $module->getChoiceLabels($lookupField);
if (!empty($labels)) {
$actualValue = $labels[$actualValue];
$fieldMessage = '[' . $labels[$recordDetails[$lookupField]] . ']';
} else {
$fieldMessage = '['.$recordDetails[$lookupField].']';
}
}
if(($logicType == "not" && $fieldMatches) || ($logicType == "equals" && !$fieldMatches)) {
// $skippedRecordMessages[] = "Excluding record $recordId because $lookupField has $logicType $actualValue - ".var_export($recordDetails[$lookupField],true)."<br />";
## Log the records that were skipped and why
// $fieldMessage = "";
// foreach ($recordDetails[$lookupField] as $value) {
// $fieldMessage .= $value . "\n";
// }
if ($logicType == 'not') {
$skippedRecordMessages['skipped_record_'.$recordId] = "Excluding record $recordId because '$actualValue' was found in $lookupField - $fieldMessage";
} else if ($logicType == 'equals') {
$skippedRecordMessages['skipped_record_'.$recordId] = "Excluding record $recordId because '$actualValue' was not found in $lookupField - $fieldMessage";
}
$recordMatches = false;
break;
}
}
}
if($recordMatches) {
$recordIds[] = $recordId;
}
}
$module->log("Ran Query", $skippedRecordMessages);
$repeatingFields = [];
$recordCount = 0;
$recordOutputs = [];
$headerFields = [];
foreach($recordIds as $recordId) {
$recordOutputs[$recordId] = $module->getDisplayDataforRecord($project, $recordId);
}
$headerFields = $module->getDisplayHeaders($project);
//remove empty entries
$recordOutputs = array_filter($recordOutputs);
//$logParams['searchResults'] = json_encode(array_combine($recordIds, array_column($recordOutputs, 'fields')));
$logParams['searchResults'] = json_encode(array_keys($recordOutputs));
$logParams['resultCount'] = count($recordOutputs);
$module->log('searchHistory',$logParams);
if(count($recordIds) == 0 ) {
echo "No matching records found";
die();
}
$vars['records'] = $recordOutputs;
$vars['headers'] = $headerFields;
echo $twig->render('patientSearch.twig', $vars);