PatientInterface.class.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. <?php
  2. namespace Models {
  3. require_once 'Models/User.class.php';
  4. require_once 'Tools/Random.class.php';
  5. class PatientInterface {
  6. //
  7. protected $DataInterface;
  8. /**
  9. *
  10. */
  11. public function __construct($DataInterface) {
  12. $this->DataInterface = $DataInterface;
  13. }
  14. /**
  15. * Get patient files existing data.
  16. */
  17. public function patientFilesExistingGet($User) {
  18. $ID_user = $User->ID;
  19. $where = ' P.ID = V.fk_patient ';
  20. if($User->type=='physician') {
  21. $where .= ' AND P.fk_user = '.$User->ID;
  22. }
  23. else if($User->type=='reader') {
  24. $where .= ' AND V.fk_reader = '.$User->ID.' AND V.completed IS NULL ';
  25. }
  26. else {
  27. $where .= ' AND P.fk_user = '.$User->ID;
  28. }
  29. $statement = $this->DataInterface->DatabaseConnection->prepare(
  30. "SELECT JSON_EXTRACT(data, '$.patientListFields') AS data FROM settings"
  31. );
  32. if(!$statement->execute()) {
  33. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  34. }
  35. $settings = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  36. $patientListFields=[];
  37. foreach($settings as $S) {
  38. $patientListFields[str_replace(' ', '_', $S->name)] = $S->display;
  39. }
  40. $statement = $this->DataInterface->DatabaseConnection->prepare(
  41. "SELECT
  42. P.*,
  43. (SELECT COUNT(visit.ID) FROM visit WHERE fk_patient = P.ID) as visitCount,
  44. (SELECT GROUP_CONCAT(CONCAT(visit.ID, ',', visit.visitDate, ',', COALESCE(visit.area, '')) ORDER BY visit.visitDate DESC SEPARATOR ';') FROM visit WHERE fk_patient = P.ID) as visits,
  45. (SELECT GROUP_CONCAT(CONCAT(visit.ID, ',', visit.number) ORDER BY visit.visitDate DESC SEPARATOR ';') FROM visit WHERE fk_patient = P.ID) as reader_visits,
  46. (SELECT COUNT(media.ID) FROM media, visit WHERE JSON_VALUE(media.metrics, '$.fps') IS NULL AND media.fk_visit = visit.ID AND visit.fk_patient = P.ID) as imageCount,
  47. (SELECT COUNT(media.ID) FROM media, visit WHERE media.fk_visit = visit.ID AND visit.fk_patient = P.ID) as mediaCount,
  48. V.visitDate AS lastVisit,
  49. V.number AS visitNumber,
  50. V.ID AS visitID
  51. FROM patient P, visit V
  52. WHERE $where
  53. GROUP BY P.ID
  54. ORDER BY V.visitDate DESC, V.ID DESC"
  55. );
  56. if(!$statement->execute()) {
  57. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  58. }
  59. $visits = $statement->fetchAll(\PDO::FETCH_ASSOC);
  60. return [
  61. 'result' => 'OK',
  62. 'visits' => $visits,
  63. 'user' => $User,
  64. 'patientListFields' => $patientListFields
  65. ];
  66. }
  67. /**
  68. * Get patient files new data.
  69. */
  70. public function patientFilesNewGet($User, $lang) {
  71. $statement = $this->DataInterface->DatabaseConnection->prepare(
  72. "SELECT JSON_EXTRACT(data, '$.newPatientFields') AS data FROM settings"
  73. );
  74. if(!$statement->execute()) {
  75. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  76. }
  77. $settings = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  78. $patientListFields=[];
  79. foreach($settings as $S) {
  80. $patientListFields[str_replace(' ', '_', $S->name)] = $S->display;
  81. $patientListFields[str_replace(' ', '_', $S->name).'_required'] = $S->required;
  82. }
  83. $statement = $this->DataInterface->DatabaseConnection->prepare(
  84. "SELECT *, name_$lang AS name FROM country ORDER BY name_$lang"
  85. );
  86. if(!$statement->execute()) {
  87. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  88. }
  89. $countries = $statement->fetchAll(\PDO::FETCH_ASSOC);
  90. return [
  91. 'result' => 'OK',
  92. 'countries' => $countries,
  93. 'patientID' => \Tools\Random::getString(10),
  94. 'visitNumber' => 1,
  95. 'patientListFields' => $patientListFields
  96. ];
  97. }
  98. /**
  99. * Get patient files pacs data.
  100. */
  101. public function patientFilesPacsGet($User) {
  102. //
  103. return [
  104. 'result' => 'OK'
  105. ];
  106. }
  107. /**
  108. * Post patient data.
  109. */
  110. public function patientCreatePost($User, $data) {
  111. $statement = $this->DataInterface->DatabaseConnection->prepare(
  112. "SELECT JSON_EXTRACT(data, '$.newPatientFields') AS data FROM settings"
  113. );
  114. if(!$statement->execute()) {
  115. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  116. }
  117. $settings = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  118. $patientListFields=[];
  119. foreach($settings as $S) {
  120. $patientListFields[str_replace(' ', '_', $S->name)] = $S->required;
  121. }
  122. if($data['type']=='investigator' && empty($data['patient']['patientID'])) {
  123. return ['result' => 'ERROR', 'reason' => 'patientID'];
  124. }
  125. if($data['type']=='investigator' && empty($data['patient']['visitNumber'])) {
  126. return ['result' => 'ERROR', 'reason' => 'visitNumber'];
  127. }
  128. if($patientListFields['Visit_date'] && empty($data['patient']['visitDate'])) {
  129. return ['result' => 'ERROR', 'reason' => 'visitDate'];
  130. }
  131. if(empty($data['patient']['visitDate'])) {
  132. $data['patient']['visitDate']=date('Y-m-d');
  133. }
  134. if($patientListFields['Firstname'] && empty($data['patient']['firstname'])) {
  135. return ['result' => 'ERROR', 'reason' => 'firstname'];
  136. }
  137. if(empty($data['patient']['firstname'])) {
  138. $data['patient']['firstname']='';
  139. }
  140. if($patientListFields['Lastname'] && empty($data['patient']['lastname'])) {
  141. return ['result' => 'ERROR', 'reason' => 'lastname'];
  142. }
  143. if(empty($data['patient']['lastname'])) {
  144. $data['patient']['lastname']='';
  145. }
  146. if($patientListFields['Sex'] && empty($data['patient']['gender'])) {
  147. return ['result' => 'ERROR', 'reason' => 'gender'];
  148. }
  149. if($patientListFields['Birthdate'] && empty($data['patient']['birthDate'])) {
  150. return ['result' => 'ERROR', 'reason' => 'birthDate'];
  151. }
  152. if(empty($data['patient']['birthDate'])) {
  153. $data['patient']['birthDate']=date('Y-m-d');
  154. }
  155. if($patientListFields['Birth_country'] && empty($data['patient']['birthCountry'])) {
  156. return ['result' => 'ERROR', 'reason' => 'birthCountry'];
  157. }
  158. if($patientListFields['Residence_country'] && empty($data['patient']['residenceCountry'])) {
  159. return ['result' => 'ERROR', 'reason' => 'residenceCountry'];
  160. }
  161. if($patientListFields['Height'] && empty($data['patient']['height'])) {
  162. return ['result' => 'ERROR', 'reason' => 'height'];
  163. }
  164. if($patientListFields['Weight'] && empty($data['patient']['weight'])) {
  165. return ['result' => 'ERROR', 'reason' => 'weight'];
  166. }
  167. if(empty($data['patient']['height'])) {
  168. $data['patient']['height'] = null;
  169. }
  170. if(empty($data['patient']['weight'])) {
  171. $data['patient']['weight'] = null;
  172. }
  173. // Begin transaction
  174. $this->DataInterface->DatabaseConnection->beginTransaction();
  175. // Insert patient
  176. if($data['type']=='investigator') {
  177. $statement = $this->DataInterface->DatabaseConnection->prepare(
  178. "INSERT INTO patient(patientID, ctPatientID, firstname, lastname, gender, birthDate, height, weight, race, fk_birthCountry, fk_residenceCountry, fk_user) VALUES(:patientID, :ctPatientID, :firstname, :lastname, :gender, :birthDate, :height, :weight, :race, :fk_birthCountry, :fk_residenceCountry, :fk_user)"
  179. );
  180. $statement->bindParam(':patientID', $data['patient']['patientID']);
  181. $statement->bindParam(':ctPatientID', $data['patient']['patientID']);
  182. }
  183. else if($data['type']=='physician') {
  184. $statement = $this->DataInterface->DatabaseConnection->prepare(
  185. "INSERT INTO patient(patientID, firstname, lastname, gender, birthDate, height, weight, race, fk_birthCountry, fk_residenceCountry, fk_user) VALUES(:patientID, :firstname, :lastname, :gender, :birthDate, :height, :weight, :race, :fk_birthCountry, :fk_residenceCountry, :fk_user)"
  186. );
  187. $statement->bindParam(':patientID', $data['patient']['patientID']);
  188. }
  189. $statement->bindParam(':firstname', $data['patient']['firstname']);
  190. $statement->bindParam(':lastname', $data['patient']['lastname']);
  191. $statement->bindParam(':gender', $data['patient']['gender']);
  192. $statement->bindParam(':birthDate', $data['patient']['birthDate']);
  193. $statement->bindParam(':height', $data['patient']['height']);
  194. $statement->bindParam(':weight', $data['patient']['weight']);
  195. $statement->bindParam(':race', $data['patient']['patientRace']);
  196. $statement->bindParam(':fk_birthCountry', $data['patient']['birthCountry']);
  197. $statement->bindParam(':fk_residenceCountry', $data['patient']['residenceCountry']);
  198. $statement->bindParam(':fk_user', $User->ID);
  199. // Error check
  200. if(!$statement->execute()) {
  201. $this->DataInterface->DatabaseConnection->rollback();
  202. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo(), 'data2' => $data];
  203. }
  204. $fk_patient = $this->DataInterface->DatabaseConnection->lastInsertId();
  205. // Insert visit
  206. $statement = $this->DataInterface->DatabaseConnection->prepare(
  207. "INSERT INTO visit(number, visitDate, fk_patient) VALUES(:number, :visitDate, :fk_patient)"
  208. );
  209. $statement->bindParam(':number', $data['patient']['visitNumber']);
  210. $statement->bindParam(':visitDate', $data['patient']['visitDate']);
  211. $statement->bindParam(':fk_patient', $fk_patient);
  212. // Error check
  213. if(!$statement->execute()) {
  214. $this->DataInterface->DatabaseConnection->rollback();
  215. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  216. }
  217. $fk_visit = $this->DataInterface->DatabaseConnection->lastInsertId();
  218. // Insert context
  219. $statement = $this->DataInterface->DatabaseConnection->prepare(
  220. "INSERT INTO context(fk_visit) VALUES(:fk_visit)"
  221. );
  222. $statement->bindParam(':fk_visit', $fk_visit);
  223. // Error check
  224. if(!$statement->execute()) {
  225. $this->DataInterface->DatabaseConnection->rollback();
  226. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  227. }
  228. // Commit
  229. $this->DataInterface->DatabaseConnection->commit();
  230. return [
  231. 'result' => 'OK',
  232. 'fk_patient' => $fk_patient,
  233. 'fk_visit' => $fk_visit
  234. ];
  235. }
  236. /**
  237. * Post patient data.
  238. */
  239. public function patientCreateVisitPost($User, $data) {
  240. // Count visit
  241. $statement = $this->DataInterface->DatabaseConnection->prepare(
  242. "SELECT COUNT(ID) AS cnt FROM visit WHERE fk_patient = :fk_patient"
  243. );
  244. $statement->bindParam(':fk_patient', $data['patientID']);
  245. // Error check
  246. if(!$statement->execute()) {
  247. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  248. }
  249. $visitCount = intval($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['cnt']) + 1;
  250. // Insert visit
  251. $statement = $this->DataInterface->DatabaseConnection->prepare(
  252. "INSERT INTO visit(number, visitDate, fk_patient) VALUES(:number, NOW(), :fk_patient)"
  253. );
  254. $statement->bindParam(':number', $visitCount);
  255. $statement->bindParam(':fk_patient', $data['patientID']);
  256. // Error check
  257. if(!$statement->execute()) {
  258. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  259. }
  260. $fk_visit = $this->DataInterface->DatabaseConnection->lastInsertId();
  261. // Insert context
  262. $statement = $this->DataInterface->DatabaseConnection->prepare(
  263. "INSERT INTO context(fk_visit) VALUES(:fk_visit)"
  264. );
  265. $statement->bindParam(':fk_visit', $fk_visit);
  266. // Error check
  267. if(!$statement->execute()) {
  268. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  269. }
  270. return [
  271. 'result' => 'OK',
  272. 'fk_visit' => $fk_visit
  273. ];
  274. }
  275. /**
  276. * Post patient context data.
  277. */
  278. public function patientContextPost($User, $data) {
  279. $field = $data['what'];
  280. $values = json_encode($data['data']);
  281. $fk_visit = $data['patient']['visitID'];
  282. $statement = $this->DataInterface->DatabaseConnection->prepare(
  283. "UPDATE context SET $field = '$values' WHERE fk_visit = $fk_visit"
  284. );
  285. if(!$statement->execute()) {
  286. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  287. }
  288. return [
  289. 'result' => 'OK',
  290. 'field' => $data['what'],
  291. 'values' => $data['data']
  292. ];
  293. }
  294. /**
  295. * Get patient risks data.
  296. */
  297. public function patientRisksGet($User, $fk_visit) {
  298. $statement = $this->DataInterface->DatabaseConnection->prepare(
  299. "SELECT patient.* FROM patient, visit WHERE patient.ID = visit.fk_patient AND visit.ID = $fk_visit"
  300. );
  301. if(!$statement->execute()) {
  302. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  303. }
  304. $patient = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  305. $statement = $this->DataInterface->DatabaseConnection->prepare(
  306. "SELECT ID, risks FROM context WHERE fk_visit = $fk_visit"
  307. );
  308. if(!$statement->execute()) {
  309. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  310. }
  311. $context = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  312. //
  313. return [
  314. 'result' => 'OK',
  315. 'patient' => $patient,
  316. 'context' => $context
  317. ];
  318. }
  319. /**
  320. * Get patient history data.
  321. */
  322. public function patientHistoryGet($User, $fk_visit, $lang) {
  323. $statement = $this->DataInterface->DatabaseConnection->prepare(
  324. "SELECT patient.* FROM patient, visit WHERE patient.ID = visit.fk_patient AND visit.ID = $fk_visit"
  325. );
  326. if(!$statement->execute()) {
  327. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  328. }
  329. $patient = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  330. $statement = $this->DataInterface->DatabaseConnection->prepare(
  331. "SELECT ID, phistory FROM context WHERE fk_visit = $fk_visit"
  332. );
  333. if(!$statement->execute()) {
  334. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  335. }
  336. $context = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  337. $statement = $this->DataInterface->DatabaseConnection->prepare(
  338. "SELECT ID, JSON_UNQUOTE(JSON_EXTRACT(code, '$.$lang')) AS code FROM lst_coronary_type"
  339. );
  340. if(!$statement->execute()) {
  341. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  342. }
  343. $lst_coronary_type = $statement->fetchAll(\PDO::FETCH_ASSOC);
  344. $statement = $this->DataInterface->DatabaseConnection->prepare(
  345. "SELECT ID, JSON_UNQUOTE(JSON_EXTRACT(code, '$.$lang')) AS code FROM lst_stroke_type"
  346. );
  347. if(!$statement->execute()) {
  348. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  349. }
  350. $lst_stroke_type = $statement->fetchAll(\PDO::FETCH_ASSOC);
  351. //
  352. return [
  353. 'result' => 'OK',
  354. 'patient' => $patient,
  355. 'context' => $context,
  356. 'lst_coronary_type' => $lst_coronary_type,
  357. 'lst_stroke_type' => $lst_stroke_type
  358. ];
  359. }
  360. /**
  361. * Get patient family data.
  362. */
  363. public function patientFamilyGet($User, $fk_visit) {
  364. $statement = $this->DataInterface->DatabaseConnection->prepare(
  365. "SELECT patient.* FROM patient, visit WHERE patient.ID = visit.fk_patient AND visit.ID = $fk_visit"
  366. );
  367. if(!$statement->execute()) {
  368. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  369. }
  370. $patient = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  371. $statement = $this->DataInterface->DatabaseConnection->prepare(
  372. "SELECT ID, fhistory FROM context WHERE fk_visit = $fk_visit"
  373. );
  374. if(!$statement->execute()) {
  375. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  376. }
  377. $context = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  378. //
  379. return [
  380. 'result' => 'OK',
  381. 'patient' => $patient,
  382. 'context' => $context
  383. ];
  384. }
  385. /**
  386. * Get patient examination data.
  387. */
  388. public function patientExaminationGet($User, $fk_visit) {
  389. $statement = $this->DataInterface->DatabaseConnection->prepare(
  390. "SELECT patient.* FROM patient, visit WHERE patient.ID = visit.fk_patient AND visit.ID = $fk_visit"
  391. );
  392. if(!$statement->execute()) {
  393. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  394. }
  395. $patient = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  396. $statement = $this->DataInterface->DatabaseConnection->prepare(
  397. "SELECT ID, examination FROM context WHERE fk_visit = $fk_visit"
  398. );
  399. if(!$statement->execute()) {
  400. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  401. }
  402. $context = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  403. //
  404. return [
  405. 'result' => 'OK',
  406. 'patient' => $patient,
  407. 'context' => $context
  408. ];
  409. }
  410. /**
  411. * Get patient treatments data.
  412. */
  413. public function patientTreatmentsGet($User, $fk_visit) {
  414. $statement = $this->DataInterface->DatabaseConnection->prepare(
  415. "SELECT patient.* FROM patient, visit WHERE patient.ID = visit.fk_patient AND visit.ID = $fk_visit"
  416. );
  417. if(!$statement->execute()) {
  418. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  419. }
  420. $patient = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  421. $statement = $this->DataInterface->DatabaseConnection->prepare(
  422. "SELECT ID, treatments FROM context WHERE fk_visit = $fk_visit"
  423. );
  424. if(!$statement->execute()) {
  425. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  426. }
  427. $context = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  428. //
  429. return [
  430. 'result' => 'OK',
  431. 'patient' => $patient,
  432. 'context' => $context
  433. ];
  434. }
  435. /**
  436. *
  437. */
  438. public function patientPacsQueryPost($User, $data) {
  439. $userID = $User->ID;
  440. $statement = $this->DataInterface->DatabaseConnection->prepare(
  441. "SELECT settings_pacs.data AS data FROM settings_pacs, user WHERE settings_pacs.fk_physician = $userID OR (settings_pacs.fk_center = user.fk_center AND user.ID = $userID)"
  442. );
  443. if(!$statement->execute()) {
  444. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  445. }
  446. $conf = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  447. $cmdLine = 'findscu '.$conf->serverAddress.' '.$conf->queryPort.' -aet '.$conf->callingAET.' -aec '.$conf->calledAET.
  448. ' -P -k QueryRetrieveLevel="PATIENT" -k PatientID="*" -k PatientName="'.$data['patient'].'*" -k PatientBirthDate="*" -k PatientSex="*"'.
  449. ' -v 2>&1';
  450. $output=null;
  451. $retval=null;
  452. exec($cmdLine, $output, $retval);
  453. if($retval !== 0 || count($output)<1) {
  454. return [
  455. 'result' => 'ERROR',
  456. 'cmdLine' => $cmdLine,
  457. 'output' => $output,
  458. 'retval' => $retval
  459. ];
  460. }
  461. // clean
  462. for($i=0; $i<count($output); $i++) {
  463. if($output[$i]=="I:") {
  464. array_splice($output, $i, 1);
  465. }
  466. }
  467. // parse
  468. $patients = [];
  469. $patient = [];
  470. for($i=0; $i<count($output); $i++) {
  471. if(strpos($output[$i], "I: Find Response: ")===0) {
  472. if(count($patient)!=0) {
  473. $patients[] = $patient;
  474. $patient = [];
  475. }
  476. }
  477. else if(strpos($output[$i], "I: (0010,0010)")===0) {
  478. $patient['PatientName'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  479. //$patient['PatientName'] = trim(str_replace('^',' ',$patient['PatientName']));
  480. }
  481. else if(strpos($output[$i], "I: (0010,0020)")===0) {
  482. $patient['PatientID'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  483. }
  484. else if(strpos($output[$i], "I: (0010,0030)")===0) {
  485. $patient['PatientBirthDate'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  486. }
  487. else if(strpos($output[$i], "I: (0010,0040)")===0) {
  488. $patient['PatientSex'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  489. }
  490. }
  491. if(count($patient)!=0) {
  492. $patients[] = $patient;
  493. }
  494. array_splice($patients, 0, 1);
  495. // studies
  496. $studies = [];
  497. foreach($patients as $P) {
  498. $cmdLine = 'findscu '.$conf->serverAddress.' '.$conf->queryPort.' -aet '.$conf->callingAET.' -aec '.$conf->calledAET.
  499. ' -P -k QueryRetrieveLevel="STUDY" -k PatientID="'.$P['PatientID'].'" -k StudyInstanceUID="*" -k StudyID="*" -k StudyDate="*" -k StudyTime="*"'.
  500. ' -v 2>&1';
  501. $cmdLines[] = $cmdLine;
  502. $output=null;
  503. $retval=null;
  504. exec($cmdLine, $output, $retval);
  505. if($retval !== 0 || count($output)<1) {
  506. return [
  507. 'result' => 'ERROR',
  508. 'cmdLine' => $cmdLine,
  509. 'output' => $output,
  510. 'retval' => $retval
  511. ];
  512. }
  513. $study = [
  514. 'PatientName' => $P['PatientName'],
  515. 'PatientBirthDate' => $P['PatientBirthDate'],
  516. 'PatientSex' => $P['PatientSex']
  517. ];
  518. $hasFound = false;
  519. for($i=0; $i<count($output); $i++) {
  520. if(strpos($output[$i], "I: Find Response: ")===0) {
  521. if($hasFound) {
  522. $studies[] = $study;
  523. $study = [
  524. 'PatientName' => $P['PatientName'],
  525. 'PatientBirthDate' => $P['PatientBirthDate'],
  526. 'PatientSex' => $P['PatientSex']
  527. ];
  528. }
  529. $hasFound = true;
  530. }
  531. else if(strpos($output[$i], "I: (0008,0020)")===0) {
  532. $study['StudyDate'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  533. }
  534. else if(strpos($output[$i], "I: (0008,0030)")===0) {
  535. $study['StudyTime'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  536. }
  537. else if(strpos($output[$i], "I: (0020,0010)")===0) {
  538. $study['StudyID'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  539. }
  540. else if(strpos($output[$i], "I: (0020,000d)")===0) {
  541. $study['StudyInstanceUID'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  542. }
  543. else if(strpos($output[$i], "I: (0010,0020)")===0) {
  544. $study['PatientID'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  545. }
  546. }
  547. $studies[] = $study;
  548. $studies2[] = $output;
  549. }
  550. /*return [
  551. 'result' => 'ERROR',
  552. 'studies2' => $studies2,
  553. 'studies' => $studies,
  554. 'cmdLines' => $cmdLines
  555. ];*/
  556. //sudo movescu www.dicomserver.co.uk 11112 +P 11112 -P -k QueryRetrieveLevel="PATIENT" -k PatientID="874688" -v
  557. return [
  558. 'studies' => $studies,
  559. 'result' => 'OK'
  560. ];
  561. }
  562. /**
  563. *
  564. */
  565. public function patientPacsRetrievePost($User, $data) {
  566. /*return [
  567. 'result' => 'ERROR',
  568. 'data' => $data
  569. ];*/
  570. $userID = $User->ID;
  571. $statement = $this->DataInterface->DatabaseConnection->prepare(
  572. "SELECT settings_pacs.data AS data FROM settings_pacs, user WHERE settings_pacs.fk_physician = $userID OR (settings_pacs.fk_center = user.fk_center AND user.ID = $userID)"
  573. );
  574. if(!$statement->execute()) {
  575. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  576. }
  577. $conf = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  578. // create or select patient
  579. $patientID = 0;
  580. $statement = $this->DataInterface->DatabaseConnection->prepare(
  581. "SELECT * FROM patient WHERE patientID = :patientID OR ctPatientID = :ctPatientID"
  582. );
  583. $statement->bindParam(':patientID', $data['all']['PatientID']);
  584. $statement->bindParam(':ctPatientID', $data['all']['PatientID']);
  585. if(!$statement->execute()) {
  586. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  587. }
  588. $patients = $statement->fetchAll(\PDO::FETCH_ASSOC);
  589. // insert patient
  590. if(count($patients)==0) {
  591. $statement = $this->DataInterface->DatabaseConnection->prepare(
  592. "INSERT INTO patient(ID, patientID, ctPatientID, firstname, lastname, gender, birthDate, fk_user) VALUES(0, :patientID, :ctPatientID, :firstname, :lastname, :gender, :birthDate, :fk_user)"
  593. );
  594. $lastname = $data['all']['PatientName'];
  595. $firstname = "";
  596. $t = explode('^', $lastname);
  597. if(count($t==2)) {
  598. $lastname = $t[0];
  599. $firstname = $t[1];
  600. }
  601. $birth = substr($data['all']['PatientBirthDate'],0,4).'-'.substr($data['all']['PatientBirthDate'],4,2).'-'.substr($data['all']['PatientBirthDate'],6,2);
  602. $statement->bindParam(':patientID', $data['all']['PatientID']);
  603. $statement->bindParam(':ctPatientID', $data['all']['PatientID']);
  604. $statement->bindParam(':firstname', $firstname);
  605. $statement->bindParam(':lastname', $lastname);
  606. $statement->bindParam(':gender', $data['all']['PatientSex']);
  607. $statement->bindParam(':birthDate', $birth);
  608. $statement->bindParam(':fk_user', $userID);
  609. if(!$statement->execute()) {
  610. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  611. }
  612. $patientID = $this->DataInterface->DatabaseConnection->lastInsertId();
  613. }
  614. // select patient
  615. else {
  616. $patientID = $patients[0]['ID'];
  617. }
  618. // create or select visit
  619. $visitID = 0;
  620. $statement = $this->DataInterface->DatabaseConnection->prepare(
  621. "SELECT visit.* FROM visit, patient WHERE patient.ID = visit.fk_patient AND visit.number = :visitID"
  622. );
  623. $statement->bindParam(':visitID', $data['all']['StudyID']);
  624. if(!$statement->execute()) {
  625. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  626. }
  627. $visits = $statement->fetchAll(\PDO::FETCH_ASSOC);
  628. // insert visit
  629. if(count($visits)==0) {
  630. $statement = $this->DataInterface->DatabaseConnection->prepare(
  631. "INSERT INTO visit(ID, number, visitDate, fk_patient) VALUES(0, :number, :visitDate, :fk_patient)"
  632. );
  633. $sdate = substr($data['all']['StudyDate'],0,4).'-'.substr($data['all']['StudyDate'],4,2).'-'.substr($data['all']['StudyDate'],6,2);
  634. $statement->bindParam(':number', $data['all']['StudyID']);
  635. $statement->bindParam(':visitDate', $sdate);
  636. $statement->bindParam(':fk_patient', $patientID);
  637. if(!$statement->execute()) {
  638. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  639. }
  640. $visitID = $this->DataInterface->DatabaseConnection->lastInsertId();
  641. // Insert context
  642. $statement = $this->DataInterface->DatabaseConnection->prepare(
  643. "INSERT INTO context(fk_visit) VALUES(:fk_visit)"
  644. );
  645. $statement->bindParam(':fk_visit', $visitID);
  646. // Error check
  647. if(!$statement->execute()) {
  648. $this->DataInterface->DatabaseConnection->rollback();
  649. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  650. }
  651. }
  652. // select visit
  653. else {
  654. $visitID = $visits[0]['ID'];
  655. }
  656. $od = '../../storage/media/'.$visitID.'/';
  657. \Tools\FS::mkpath($od);
  658. //sudo www.dicomserver.co.uk 11112 +P 11112 -P -k QueryRetrieveLevel="PATIENT" -k PatientID="874688" -v
  659. $cmdLine = 'movescu '.$conf->serverAddress.' '.$conf->queryPort.' +P 11112 -aet '.$conf->callingAET.' -aec '.$conf->calledAET.' -aem '.$conf->callingAET.
  660. ' -P -k QueryRetrieveLevel="STUDY" -k StudyInstanceUID="'.$data['all']['StudyInstanceUID'].'"'.
  661. ' -od '.$od.
  662. ' -v 2>&1';
  663. $output=null;
  664. $retval=null;
  665. exec($cmdLine, $output, $retval);
  666. if($retval !== 0 || count($output)<1) {
  667. return [
  668. 'result' => 'ERROR',
  669. 'cmdLine' => $cmdLine,
  670. 'output' => $output,
  671. 'retval' => $retval
  672. ];
  673. }
  674. // get files
  675. $files = glob("$od*");
  676. $existingFiles = 0;
  677. $newFiles = 0;
  678. foreach($files as $F) {
  679. // skip previously existing files
  680. if(strpos($F, ".dicom")!==false || strpos($F, ".jpeg")!==false || strpos($F, ".mp4")!==false) {
  681. // do nothing
  682. }
  683. // new dicom file
  684. else {
  685. // existing file
  686. if(file_exists("$F.dicom")) {
  687. // do nothing
  688. $existingFiles++;
  689. unlink($F);
  690. }
  691. // real new file
  692. else {
  693. $newFiles++;
  694. rename($F, "$F.dicom");
  695. // convert JPEG
  696. $cmdLine = "dcmj2pnm $F.dicom $F.jpeg +oj +Jq 100 +F 1 -v 2>&1";
  697. $output=null;
  698. $retval=null;
  699. exec($cmdLine, $output, $retval);
  700. // error
  701. if($retval !== 0 || count($output)<1) {
  702. return [
  703. 'result' => 'ERROR',
  704. 'cmdLine' => $cmdLine,
  705. 'output' => $output,
  706. 'retval' => $retval
  707. ];
  708. }
  709. // image metrics
  710. $metrics = ['width' => 0, 'height' => 0, 'pxwidth' => 0, 'pxheight' => 0];
  711. list($metrics['width'], $metrics['height'], $type, $attr) = getimagesize("$F.jpeg");
  712. // dicom metrics
  713. $cmdLine = 'dcmdump -s +P 0018,602c +P 0018,602e +P 0028,0008 "'.$F.'.dicom"';
  714. $output=null;
  715. $retval=null;
  716. exec($cmdLine, $output, $retval);
  717. // error
  718. if($retval !== 0) {
  719. if(count($output)==0) {
  720. // no such calibration, likely not ULTRASOUND!
  721. }
  722. else {
  723. return [
  724. 'result' => 'ERROR',
  725. 'cmdLine' => $cmdLine,
  726. 'output' => $output,
  727. 'retval' => $retval
  728. ];
  729. }
  730. }
  731. $tab = explode(' ',trim($output[0]));
  732. $metrics['pxwidth'] = floatval($tab[2])*10.0;
  733. $tab = explode(' ',trim($output[1]));
  734. $metrics['pxheight'] = floatval($tab[2])*10.0;
  735. // insert new media
  736. $statement = $this->DataInterface->DatabaseConnection->prepare(
  737. "INSERT INTO media(ID, side, filename, metrics, fk_visit) VALUES(0, 'unknown', :filename, :metrics, :fk_visit)"
  738. );
  739. $statement->bindParam(':filename', basename("$F.dicom"));
  740. $statement->bindParam(':metrics', json_encode($metrics));
  741. $statement->bindParam(':fk_visit', $visitID);
  742. if(!$statement->execute()) {
  743. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  744. }
  745. }
  746. }
  747. }
  748. return [
  749. 'result' => 'OK',
  750. 'cmdLine' => $cmdLine,
  751. 'files' => $files,
  752. 'output' => $output,
  753. 'patientID' => $patientID,
  754. 'visitID' => $visitID,
  755. 'existingFiles' => $existingFiles,
  756. 'newFiles' => $newFiles
  757. ];
  758. }
  759. }
  760. }