generated from susom/redcap-em-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedcapOneDirectoryLookup.php
224 lines (178 loc) · 5.77 KB
/
RedcapOneDirectoryLookup.php
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace Stanford\RedcapOneDirectoryLookup;
require_once "emLoggerTrait.php";
use GuzzleHttp\Client;
# trigger build
/**
* Class RedcapOneDirectoryLookup
* @package Stanford\RedcapOneDirectoryLookup
* @property array $fieldsMap
* @property \GuzzleHttp\Client $client
*/
class RedcapOneDirectoryLookup extends \ExternalModules\AbstractExternalModule
{
use emLoggerTrait;
private $fieldsMap;
private $client;
private $serverURL = '';
public function __construct()
{
parent::__construct();
// Other code to run when object is instantiated
$this->setClient(new Client);
}
public function redcap_data_entry_form_top($project_id, $record, $instrument, $event_id, $group_id, $repeat_instance)
{
$this->processFields($instrument);
}
public function redcap_survey_page_top($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance)
{
$this->processFields($instrument);
}
/**
* Perform a OneDirectory Search
* @param $term
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function searchUsers($term)
{
// Build search object
$headers = [
'Content-Type' => 'application/json'
];
$body = '{
"query": {
"multi_match": {
"query": "'.$term.'",
"analyzer": "standard"
}
}
}';
// Tried other searches but wouldn't return sunet lookup or email lookups. I think
// those fields don't have indexing configured so it can't do much
// $search->query->multi_match->term = "most_fields";
// $search->query->multi_match->fields = [ "first_name", "last_name", "fullname", "email", "affiliate", "title", "suid" ];
// Do a search
$q = $this->getClient()->post($this->getServerURL(), [
'body' => $body,
'headers' => $headers,
]);
$result = $q->getBody()->getContents();
return $this->processOneDirectoryResponse($result);
}
/**
*
*/
private function processOneDirectoryResponse($response)
{
$response = json_decode($response);
$result = array();
if ($response->hits->total > 0) {
foreach ($response->hits->hits as $item) {
if ($item->_source->affiliate == "Stanford University") {
$image = $this->getUrl('assets/images/stanford_university.png', true, true);
} else {
$image = $this->getUrl('assets/images/stanford_medicine.png', true, true);;
}
$result[] = array(
'id' => $item->_id,
'label' => $item->_source->fullname,
'title' => $item->_source->title,
'suid' => $item->_source->suid,
'value' => $item->_source->fullname,
'array' => $item->_source,
'image' => $image
);
}
}
return $result;
}
/**
* Loop through config and set the $fieldMap which will be passed through to the javascript code
*/
private function processInstances()
{
$instances = $this->getSubSettings('instance');
// Array of all lookup instances then the values from the lookup result being used
$lookup_result_attributes = $this->getProjectSetting("one-directory-attribute");
// Matching array of fields in the project where the lookup results will be placed
$lookup_result_fields = $this->getProjectSetting("mapped-field");
$fieldMap = $this->getFieldsMap();
foreach ($instances as $index => $instance) {
$ins = array();
$ins['search-field'] = $instance['search-field'];
$ins['alert-if-exist'] = $instance['alert-if-exist'];
foreach ($instance['attribute_instance'] as $a_index => $attribute) {
$k = $lookup_result_attributes[$index][$a_index];
$v = $lookup_result_fields[$index][$a_index];
$ins['map'][$k] = $v;
}
$fieldMap[] = $ins;
}
$this->setFieldsMap($fieldMap);
}
private function processFields($instrument)
{
$this->processInstances();
// Determine if search-fields are in instrument
$fields = [];
foreach ($this->fieldsMap as $instance) array_push($fields, $instance['search-field']);
$instrument_fields = \REDCap::getFieldNames($instrument);
if (count(array_intersect($fields, $instrument_fields)) > 0) {
$this->includeFile('view/fields.php');
}
}
/**
* @return array
*/
public function getFieldsMap()
{
return $this->fieldsMap;
}
/**
* @param array $fieldsMap
*/
public function setFieldsMap($fieldsMap)
{
$this->fieldsMap = $fieldsMap;
}
/**
* @param string $path
*/
public function includeFile($path)
{
include_once $path;
}
/**
* @return \GuzzleHttp\Client
*/
public function getClient()
{
return $this->client;
}
/**
* @param \GuzzleHttp\Client $client
*/
public function setClient(\GuzzleHttp\Client $client)
{
$this->client = $client;
}
/**
* @return string
*/
public function getServerURL(): string
{
if(!$this->serverURL){
$this->setServerURL($this->getSystemSetting('onedirectory-url'));
}
return $this->serverURL;
}
/**
* @param string $serverURL
*/
public function setServerURL(string $serverURL): void
{
$this->serverURL = $serverURL;
}
}