-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverifyData.php
More file actions
160 lines (132 loc) · 4.19 KB
/
verifyData.php
File metadata and controls
160 lines (132 loc) · 4.19 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
<?php
if(SUPER_USER !== '1'){
die("You're not allowed to view this page!");
}
$log = function ($message){
global $module;
$module->log($message);
echo "$message\n";
};
echo '<pre>';
$recordIdFieldName = $module->getRecordIdField();
$addressFieldName = $module->getProjectSetting('address');
$latitudeFieldName = $module->getProjectSetting('latitude');
$longitudeFieldName = $module->getProjectSetting('longitude');
$fields = [
$recordIdFieldName,
$addressFieldName,
$latitudeFieldName,
$longitudeFieldName,
];
$censuses = $module->getSubSettings('censuses');
foreach($censuses as $census){
foreach($census['mappings'] as $mapping){
$fields[] = $mapping['fields'];
}
}
$recordIdFieldName = $module->getRecordIdField();
$recordIds = array_column(json_decode(REDCap::getData([
'return_format' => 'json',
'fields' => $recordIdFieldName,
'filterLogic' => "[geostatus] != 'U'"
]), true), $recordIdFieldName);
$startingRecord = $_GET['starting-record'] ?? false;
if($startingRecord){
$startingRecordIndex = array_search($startingRecord, $recordIds);
if($startingRecordIndex === false){
die('Starting record not found!');
}
$recordIds = array_slice($recordIds, $startingRecordIndex);
}
$id = $_GET['id'] ?? null;
if($id === null){
$batchSize = (int) $_GET['batch-size'];
$batch = (int) $_GET['batch'];
if($batchSize === 0){
die('You must specify the "batch-size" parameter!');
}
}
else{
$batchSize = 1;
$batch = array_search($id, $recordIds);
if($batch === false){
die('Record ID not found');
}
}
$batches = array_chunk($recordIds, $batchSize);
$recordIds = $batches[$batch] ?? null;
if(!is_array($recordIds)){
die('Batch not found');
}
$records = json_decode(REDCap::getData([
'return_format' => 'json',
'fields' => $fields,
'records' => $recordIds
]), true);
$dataToSave = [];
foreach($records as $record){
$recordId = $record[$recordIdFieldName];
$log('checking record ' . $recordId);
$address = $record[$addressFieldName];
$address = str_replace(' ', '+', $address);
$latitude = $record[$latitudeFieldName];
$longitude = $record[$longitudeFieldName];
$usingAddress = empty($latitude) || empty($longitude);
foreach($censuses as $census){
$tries = 0;
while(true){
ob_start();
if($usingAddress){
$_POST['get'] = true;
$_POST['address'] = $address;
$_POST['year'] = $census['year'];
require __DIR__ . '/getAddress.php';
}
else{
$_POST['get'] = true;
$_POST['lat'] = $latitude;
$_POST['long'] = $longitude;
$_POST['year'] = $census['year'];
require __DIR__ . '/getCoordinates.php';
}
$response = json_decode(ob_get_clean(), true);
if(!isset($response['exceptions'])){
// The request succeeded!
break;
}
$tries++;
if($tries === 10){
$log("Census API error after $tries retries:");
var_dump($response);
die();
}
}
$lookupTable = $response['result'];
if($usingAddress){
$lookupTable = $lookupTable['addressMatches'][0];
}
$lookupTable = $lookupTable['geographies']['Census Blocks'][0] ?? [];
foreach($census['mappings'] as $mapping){
$key = $mapping['keys'];
$field = $mapping['fields'];
$expected = $lookupTable[$key] ?? null;
$actual = $record[$field];
if($expected != $actual){
$log("Record $recordId - Field $field - Expected '$expected', but found '$actual'");
if(isset($_GET['save']) && $expected !== null){
$dataToSave[$recordId][$recordIdFieldName] = $recordId;
$dataToSave[$recordId][$field] = $expected;
}
}
}
}
}
$dataToSave = array_values($dataToSave);
if(!empty($dataToSave)){
$result = REDCap::saveData([
'dataFormat' => 'json',
'data' => json_encode($dataToSave)
]);
var_dump($result);
}
echo '</pre>';