PatientInterface.class.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. $cmdLines = [];
  448. $cmdLine = 'findscu '.$conf->serverAddress.' '.$conf->queryPort.' -aet '.$conf->callingAET.' -aec '.$conf->calledAET.
  449. ' -P -k QueryRetrieveLevel="PATIENT" -k PatientID="*" -k PatientName="*'.$data['patient'].'*" -k PatientBirthDate="*" -k PatientSex="*"'.
  450. ' -v 2>&1';
  451. $cmdLines[] = $cmdLine;
  452. $output=null;
  453. $retval=null;
  454. exec($cmdLine, $output, $retval);
  455. if($retval !== 0 || count($output)<1) {
  456. return [
  457. 'result' => 'ERROR',
  458. 'cmdLine' => $cmdLine,
  459. 'output' => $output,
  460. 'retval' => $retval
  461. ];
  462. }
  463. // clean
  464. for($i=0; $i<count($output); $i++) {
  465. if($output[$i]=="I:") {
  466. array_splice($output, $i, 1);
  467. }
  468. }
  469. // parse
  470. $patients = [];
  471. $patient = [];
  472. for($i=0; $i<count($output); $i++) {
  473. if(strpos($output[$i], "I: Find Response: ")===0) {
  474. if(count($patient)!=0) {
  475. $patients[] = $patient;
  476. $patient = [];
  477. }
  478. }
  479. else if(strpos($output[$i], "I: (0010,0010)")===0) {
  480. $patient['PatientName'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  481. //$patient['PatientName'] = trim(str_replace('^',' ',$patient['PatientName']));
  482. }
  483. else if(strpos($output[$i], "I: (0010,0020)")===0) {
  484. $patient['PatientID'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  485. }
  486. else if(strpos($output[$i], "I: (0010,0030)")===0) {
  487. $patient['PatientBirthDate'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  488. }
  489. else if(strpos($output[$i], "I: (0010,0040)")===0) {
  490. $patient['PatientSex'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  491. }
  492. }
  493. if(count($patient)!=0) {
  494. $patients[] = $patient;
  495. }
  496. array_splice($patients, 0, 1);
  497. // studies
  498. $studies = [];
  499. foreach($patients as $P) {
  500. $cmdLine = 'findscu '.$conf->serverAddress.' '.$conf->queryPort.' -aet '.$conf->callingAET.' -aec '.$conf->calledAET.
  501. ' -P -k QueryRetrieveLevel="STUDY" -k PatientID="'.$P['PatientID'].'" -k StudyInstanceUID="" -k StudyID="" -k StudyDate="" -k StudyTime=""'.
  502. ' -v 2>&1';
  503. $cmdLines[] = $cmdLine;
  504. $output=null;
  505. $retval=null;
  506. exec($cmdLine, $output, $retval);
  507. if($retval !== 0 || count($output)<1) {
  508. return [
  509. 'result' => 'ERROR',
  510. 'cmdLine' => $cmdLine,
  511. 'output' => $output,
  512. 'retval' => $retval
  513. ];
  514. }
  515. $study = [
  516. 'PatientName' => $P['PatientName'],
  517. 'PatientBirthDate' => $P['PatientBirthDate'],
  518. 'PatientSex' => $P['PatientSex']
  519. ];
  520. $hasFound = false;
  521. for($i=0; $i<count($output); $i++) {
  522. if(strpos($output[$i], "I: Find Response: ")===0) {
  523. if($hasFound) {
  524. $studies[] = $study;
  525. $study = [
  526. 'PatientName' => $P['PatientName'],
  527. 'PatientBirthDate' => $P['PatientBirthDate'],
  528. 'PatientSex' => $P['PatientSex']
  529. ];
  530. }
  531. $hasFound = true;
  532. }
  533. else if(strpos($output[$i], "I: (0008,0020)")===0) {
  534. $study['StudyDate'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  535. }
  536. else if(strpos($output[$i], "I: (0008,0030)")===0) {
  537. $study['StudyTime'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  538. }
  539. else if(strpos($output[$i], "I: (0020,0010)")===0) {
  540. $study['StudyID'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  541. }
  542. else if(strpos($output[$i], "I: (0020,000d)")===0) {
  543. $study['StudyInstanceUID'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  544. }
  545. else if(strpos($output[$i], "I: (0010,0020)")===0) {
  546. $study['PatientID'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  547. }
  548. }
  549. $studies[] = $study;
  550. $studies2[] = $output;
  551. }
  552. /*return [
  553. 'result' => 'ERROR',
  554. 'studies2' => $studies2,
  555. 'studies' => $studies,
  556. 'cmdLines' => $cmdLines
  557. ];*/
  558. //sudo movescu www.dicomserver.co.uk 11112 +P 11112 -P -k QueryRetrieveLevel="PATIENT" -k PatientID="874688" -v
  559. return [
  560. 'studies' => $studies,
  561. 'studies2' => $studies2,
  562. 'result' => 'OK',
  563. 'cmdLines' => $cmdLines
  564. ];
  565. }
  566. /**
  567. *
  568. */
  569. public function patientPacsRetrievePost($User, $data) {
  570. /*return [
  571. 'result' => 'ERROR',
  572. 'data' => $data
  573. ];*/
  574. $userID = $User->ID;
  575. $statement = $this->DataInterface->DatabaseConnection->prepare(
  576. "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)"
  577. );
  578. if(!$statement->execute()) {
  579. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  580. }
  581. $conf = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  582. // create or select patient
  583. $patientID = 0;
  584. $statement = $this->DataInterface->DatabaseConnection->prepare(
  585. "SELECT * FROM patient WHERE patientID = :patientID OR ctPatientID = :ctPatientID"
  586. );
  587. $statement->bindParam(':patientID', $data['all']['PatientID']);
  588. $statement->bindParam(':ctPatientID', $data['all']['PatientID']);
  589. if(!$statement->execute()) {
  590. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  591. }
  592. $patients = $statement->fetchAll(\PDO::FETCH_ASSOC);
  593. // insert patient
  594. if(count($patients)==0) {
  595. $statement = $this->DataInterface->DatabaseConnection->prepare(
  596. "INSERT INTO patient(ID, patientID, ctPatientID, firstname, lastname, gender, birthDate, fk_user) VALUES(0, :patientID, :ctPatientID, :firstname, :lastname, :gender, :birthDate, :fk_user)"
  597. );
  598. $lastname = $data['all']['PatientName'];
  599. $firstname = "";
  600. $t = explode('^', $lastname);
  601. if(count($t==2)) {
  602. $lastname = $t[0];
  603. $firstname = $t[1];
  604. }
  605. $birth = substr($data['all']['PatientBirthDate'],0,4).'-'.substr($data['all']['PatientBirthDate'],4,2).'-'.substr($data['all']['PatientBirthDate'],6,2);
  606. $statement->bindParam(':patientID', $data['all']['PatientID']);
  607. $statement->bindParam(':ctPatientID', $data['all']['PatientID']);
  608. $statement->bindParam(':firstname', $firstname);
  609. $statement->bindParam(':lastname', $lastname);
  610. $statement->bindParam(':gender', $data['all']['PatientSex']);
  611. $statement->bindParam(':birthDate', $birth);
  612. $statement->bindParam(':fk_user', $userID);
  613. if(!$statement->execute()) {
  614. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  615. }
  616. $patientID = $this->DataInterface->DatabaseConnection->lastInsertId();
  617. }
  618. // select patient
  619. else {
  620. $patientID = $patients[0]['ID'];
  621. }
  622. // create or select visit
  623. $visitID = 0;
  624. $statement = $this->DataInterface->DatabaseConnection->prepare(
  625. "SELECT visit.* FROM visit, patient WHERE patient.ID = visit.fk_patient AND visit.number = :visitID"
  626. );
  627. $statement->bindParam(':visitID', $data['all']['StudyID']);
  628. if(!$statement->execute()) {
  629. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  630. }
  631. $visits = $statement->fetchAll(\PDO::FETCH_ASSOC);
  632. // insert visit
  633. if(count($visits)==0) {
  634. $statement = $this->DataInterface->DatabaseConnection->prepare(
  635. "INSERT INTO visit(ID, number, visitDate, fk_patient) VALUES(0, :number, :visitDate, :fk_patient)"
  636. );
  637. $sdate = substr($data['all']['StudyDate'],0,4).'-'.substr($data['all']['StudyDate'],4,2).'-'.substr($data['all']['StudyDate'],6,2);
  638. $statement->bindParam(':number', $data['all']['StudyID']);
  639. $statement->bindParam(':visitDate', $sdate);
  640. $statement->bindParam(':fk_patient', $patientID);
  641. if(!$statement->execute()) {
  642. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  643. }
  644. $visitID = $this->DataInterface->DatabaseConnection->lastInsertId();
  645. // Insert context
  646. $statement = $this->DataInterface->DatabaseConnection->prepare(
  647. "INSERT INTO context(fk_visit) VALUES(:fk_visit)"
  648. );
  649. $statement->bindParam(':fk_visit', $visitID);
  650. // Error check
  651. if(!$statement->execute()) {
  652. $this->DataInterface->DatabaseConnection->rollback();
  653. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  654. }
  655. }
  656. // select visit
  657. else {
  658. $visitID = $visits[0]['ID'];
  659. }
  660. $od = '../../storage/media/'.$visitID.'/';
  661. \Tools\FS::mkpath($od);
  662. //sudo www.dicomserver.co.uk 11112 +P 11112 -P -k QueryRetrieveLevel="PATIENT" -k PatientID="874688" -v
  663. $cmdLine = 'movescu '.$conf->serverAddress.' '.$conf->queryPort.' +P 11112 -aet '.$conf->callingAET.' -aec '.$conf->calledAET.' -aem '.$conf->callingAET.
  664. ' -P -k QueryRetrieveLevel="STUDY" -k StudyInstanceUID="'.$data['all']['StudyInstanceUID'].'"'.
  665. ' -od '.$od.
  666. ' -v 2>&1';
  667. $output=null;
  668. $retval=null;
  669. exec($cmdLine, $output, $retval);
  670. if($retval !== 0 || count($output)<1) {
  671. return [
  672. 'result' => 'ERROR',
  673. 'cmdLine' => $cmdLine,
  674. 'output' => $output,
  675. 'retval' => $retval
  676. ];
  677. }
  678. // get files
  679. $files = glob("$od*");
  680. $existingFiles = 0;
  681. $newFiles = 0;
  682. foreach($files as $F) {
  683. // skip previously existing files
  684. if(strpos($F, ".dicom")!==false || strpos($F, ".jpeg")!==false || strpos($F, ".mp4")!==false) {
  685. // do nothing
  686. }
  687. // new dicom file
  688. else {
  689. // existing file
  690. if(file_exists("$F.dicom")) {
  691. // do nothing
  692. $existingFiles++;
  693. unlink($F);
  694. }
  695. // real new file
  696. else {
  697. $newFiles++;
  698. rename($F, "$F.dicom");
  699. // convert JPEG
  700. $cmdLine = "dcmj2pnm $F.dicom $F.jpeg +oj +Jq 100 +F 1 -v 2>&1";
  701. $output=null;
  702. $retval=null;
  703. exec($cmdLine, $output, $retval);
  704. // error
  705. if($retval !== 0 || count($output)<1) {
  706. return [
  707. 'result' => 'ERROR',
  708. 'cmdLine' => $cmdLine,
  709. 'output' => $output,
  710. 'retval' => $retval
  711. ];
  712. }
  713. // image metrics
  714. $metrics = ['width' => 0, 'height' => 0, 'pxwidth' => 0, 'pxheight' => 0];
  715. list($metrics['width'], $metrics['height'], $type, $attr) = getimagesize("$F.jpeg");
  716. // dicom metrics
  717. $cmdLine = 'dcmdump -s +P 0018,602c +P 0018,602e +P 0028,0008 "'.$F.'.dicom"';
  718. $output=null;
  719. $retval=null;
  720. exec($cmdLine, $output, $retval);
  721. // error
  722. if($retval !== 0) {
  723. if(count($output)==0) {
  724. // no such calibration, likely not ULTRASOUND!
  725. }
  726. else {
  727. return [
  728. 'result' => 'ERROR',
  729. 'cmdLine' => $cmdLine,
  730. 'output' => $output,
  731. 'retval' => $retval
  732. ];
  733. }
  734. }
  735. if(count($output)) {
  736. $tab = explode(' ',trim($output[0]));
  737. $metrics['pxwidth'] = floatval($tab[2])*10.0;
  738. $tab = explode(' ',trim($output[1]));
  739. $metrics['pxheight'] = floatval($tab[2])*10.0;
  740. }
  741. // insert new media
  742. $statement = $this->DataInterface->DatabaseConnection->prepare(
  743. "INSERT INTO media(ID, side, filename, metrics, fk_visit) VALUES(0, 'unknown', :filename, :metrics, :fk_visit)"
  744. );
  745. $statement->bindParam(':filename', basename("$F.dicom"));
  746. $statement->bindParam(':metrics', json_encode($metrics));
  747. $statement->bindParam(':fk_visit', $visitID);
  748. if(!$statement->execute()) {
  749. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  750. }
  751. }
  752. }
  753. }
  754. return [
  755. 'result' => 'OK',
  756. 'cmdLine' => $cmdLine,
  757. 'files' => $files,
  758. 'output' => $output,
  759. 'patientID' => $patientID,
  760. 'visitID' => $visitID,
  761. 'existingFiles' => $existingFiles,
  762. 'newFiles' => $newFiles
  763. ];
  764. }
  765. }
  766. }