forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSocialHistoryService.php
208 lines (182 loc) · 7.46 KB
/
SocialHistoryService.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
<?php
/**
* SocialHistoryService.php
* @package openemr
* @link http://www.open-emr.org
* @author Stephen Nielson <stephen@nielson.org>
* @copyright Copyright (c) 2021 Stephen Nielson <stephen@nielson.org>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use OpenEMR\Common\Database\QueryUtils;
use OpenEMR\Common\Database\SqlQueryException;
use OpenEMR\Common\Uuid\UuidRegistry;
use OpenEMR\Events\Services\ServiceSaveEvent;
use OpenEMR\Services\FHIR\UtilsService;
use OpenEMR\Services\Search\FhirSearchWhereClauseBuilder;
use OpenEMR\Services\Search\TokenSearchField;
use OpenEMR\Validators\ProcessingResult;
class SocialHistoryService extends BaseService
{
public const TABLE_NAME = "history_data";
public function __construct()
{
parent::__construct(self::TABLE_NAME);
UuidRegistry::createMissingUuidsForTables([self::TABLE_NAME]);
}
// To prevent sql injection on this function, if a variable is used for $given parameter, then
// it needs to be escaped via whitelisting prior to using this function; see lines 2020-2121 of
// library/clinical_rules.php script for example of this.
function getHistoryData($pid, $given = "*", $dateStart = '', $dateEnd = '')
{
$where = '';
if ($given == 'tobacco') {
$where = 'tobacco is not null and';
}
if ($dateStart && $dateEnd) {
$res = sqlQuery("select $given from history_data where $where pid = ? and date >= ? and date <= ? order by date DESC limit 0,1", array($pid,$dateStart,$dateEnd));
} elseif ($dateStart && !$dateEnd) {
$res = sqlQuery("select $given from history_data where $where pid = ? and date >= ? order by date DESC limit 0,1", array($pid,$dateStart));
} elseif (!$dateStart && $dateEnd) {
$res = sqlQuery("select $given from history_data where $where pid = ? and date <= ? order by date DESC limit 0,1", array($pid,$dateEnd));
} else {
$res = sqlQuery("select $given from history_data where $where pid=? order by date DESC limit 0,1", array($pid));
}
return $res;
}
public function getHistoryDataForPatientPid($pid, $limit = null)
{
$sql = "SELECT id, tobacco, alcohol, exercise_patterns, recreational_drugs FROM history_data WHERE pid=? ORDER BY id DESC LIMIT 1";
$search = [
new TokenSearchField('pid', $pid)
];
$result = $this->search($search, true, $limit);
if (!empty($result->getData())) {
return $result->getData();
}
return [];
}
public function search($search, $isAndCondition = true, $limit = null)
{
// history_data contains a table record for every single insert into the database
$sql = "
SELECT
history.id
,history.uuid
,history.date
,history.tobacco
,history.alcohol
,history.exercise_patterns
,history.recreational_drugs
,patients.pid
,patients.puuid
FROM
history_data history
JOIN
(
SELECT
-- we could have this be max date, but this should be fine
max(id) AS id
FROM history_data
GROUP BY pid
) latest_history_records ON history.id = latest_history_records.id
LEFT JOIN
(
SELECT
uuid AS puuid
,pid
FROM patient_data
) patients ON history.pid = patients.pid";
$whereClause = FhirSearchWhereClauseBuilder::build($search, $isAndCondition);
$sql .= $whereClause->getFragment();
if (is_numeric($limit)) {
$sql .= " LIMIT " . intval($limit);
}
$sqlBindArray = $whereClause->getBoundValues();
$statementResults = QueryUtils::sqlStatementThrowException($sql, $sqlBindArray);
$processingResult = new ProcessingResult();
while ($row = sqlFetchArray($statementResults)) {
$resultRecord = $this->createResultRecordFromDatabaseResult($row);
$processingResult->addData($resultRecord);
}
return $processingResult;
}
protected function createResultRecordFromDatabaseResult($row)
{
$record = parent::createResultRecordFromDatabaseResult($row);
$listService = new ListService();
$tobaccoColumn = $record['tobacco'] ?? "";
$tobacco = explode('|', $tobaccoColumn);
if (!empty($tobacco[3])) {
$listOption = $listService->getListOption('smoking_status', $tobacco[3]) ?? "";
$record['smoking_status_codes'] = $this->addCoding($listOption['codes']);
}
return $record;
}
public function create($record)
{
if (!is_array($record)) {
throw new \InvalidArgumentException("argument must be a valid array");
}
return $this->insertRecord($record);
}
private function insertRecord($record)
{
$record = $this->dispatchSaveEvent(ServiceSaveEvent::EVENT_PRE_SAVE, $record);
$pid = $record['pid'] ?? null;
if (!is_numeric($pid)) {
throw new \InvalidArgumentException("pid must be a valid number");
}
$uuid = UuidRegistry::getRegistryForTable(self::TABLE_NAME)->createUuid();
$sql = "insert into history_data set pid = ?, date = NOW(), uuid = ? ";
$arraySqlBind = [$pid, $uuid];
unset($record['pid']);
unset($record['uuid']);
if (!empty($record)) {
$arraySqlBind = array_merge($arraySqlBind, array_values($record));
$sql .= ", " . implode(", ", array_map(function ($key) {
return "`$key` = ?";
}, array_keys($record)));
}
$insertId = QueryUtils::sqlInsert($sql, $arraySqlBind);
// now put everything back.
$record['id'] = $insertId;
$record['uuid'] = $uuid;
$record['pid'] = $pid;
$record = $this->dispatchSaveEvent(ServiceSaveEvent::EVENT_POST_SAVE, $record);
return $record;
}
function getUuidFields(): array
{
// note the uuid here is the uuid_mapping table's uuid since each column in the table has its own distinct uuid
// in the system.
return ['puuid', 'euuid', 'uuid', 'user_uuid'];
}
/**
*
* @param string $type The type of save event to dispatch
* @param $saveData The history data to send in the event
* @return array
*/
private function dispatchSaveEvent(string $type, $saveData)
{
$saveEvent = new ServiceSaveEvent($this, $saveData);
$filteredData = $GLOBALS["kernel"]->getEventDispatcher()->dispatch($saveEvent, $type);
if ($filteredData instanceof ServiceSaveEvent) { // make sure whoever responds back gives us the right data.
$saveData = $filteredData->getSaveData();
}
return $saveData;
}
public function updateHistoryDataForPatientPid($pid, $new)
{
// grab our history data and replace any new values in the row
$real = $this->getHistoryData($pid);
foreach ($new as $key => $value) {
$real[$key] = $value;
}
$real['id'] = "";
// need to unset date, so can reset it below
unset($real['date']);
return $this->insertRecord($real);
}
}