AdminInterface.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. <?php
  2. namespace Models {
  3. require_once 'Models/User.class.php';
  4. require_once 'Tools/Crypto.class.php';
  5. class AdminInterface {
  6. //
  7. protected $DataInterface;
  8. /**
  9. *
  10. */
  11. public function __construct($DataInterface) {
  12. $this->DataInterface = $DataInterface;
  13. }
  14. /**
  15. * Check login/password and create JWT token.
  16. */
  17. public function adminLogin(&$User, $email, $clearPassword) {
  18. $statement = $this->DataInterface->DatabaseConnection->prepare(
  19. "SELECT
  20. ID, password, firstname, lastname
  21. FROM
  22. user
  23. WHERE
  24. active = 1 AND
  25. email = '$email' AND
  26. type = 'imt-master'"
  27. );
  28. if(!$statement->execute()) {
  29. $results = Array('result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo());
  30. }
  31. else {
  32. $results = $statement->fetchAll(\PDO::FETCH_ASSOC);
  33. if(count($results)){
  34. if(\Tools\Crypto::verify($clearPassword, $results[0]['password'])) {
  35. // Generate JWT token
  36. $issuer_claim = \Config\Settings::getTokenIssuer();
  37. $audience_claim = \Config\Settings::getAdminTokenAudience();
  38. $issuedat_claim = time(); // issued at
  39. $notbefore_claim = $issuedat_claim + \Config\Settings::getTokenNotBefore();
  40. $expire_claim = $issuedat_claim + \Config\Settings::getTokenExpiration();
  41. $token = array(
  42. "iss" => $issuer_claim,
  43. "aud" => $audience_claim,
  44. "iat" => $issuedat_claim,
  45. "nbf" => $notbefore_claim,
  46. "exp" => $expire_claim,
  47. "data" => array(
  48. "ID" => $results[0]['ID'],
  49. "firstname" => $results[0]['firstname'],
  50. "lastname" => $results[0]['lastname'],
  51. "email" => $email
  52. )
  53. );
  54. $jwt = \Firebase\JWT\JWT::encode($token, \Config\Settings::getTokenPrivateKey());
  55. // OK
  56. $results = Array(
  57. "result" => "OK",
  58. "token" => $jwt,
  59. "email" => $email,
  60. "expireAt" => $expire_claim
  61. );
  62. }
  63. else {
  64. $results = Array('result' => 'ERROR', 'reason' => 'bad_password', 'message' => 'Invalid password');
  65. }
  66. }
  67. else {
  68. $results = Array('result' => 'ERROR', 'reason' => 'unknown', 'message' => 'No such user');
  69. }
  70. }
  71. return $results;
  72. }
  73. /**
  74. * Logout.
  75. */
  76. public function adminLogout(&$User) {
  77. $User->logout();
  78. return Array('result' => 'OK');
  79. }
  80. /**
  81. * Get profile data.
  82. */
  83. public function adminProfileGet($User) {
  84. $userID = $User->ID;
  85. // OK
  86. return array(
  87. 'result' => 'OK',
  88. 'ID' => $User->ID,
  89. 'firstname' => $User->firstname,
  90. 'lastname' => $User->lastname,
  91. 'email' => $User->email
  92. );
  93. }
  94. /**
  95. * Get common data.
  96. */
  97. public function adminCommonGet($User) {
  98. $userID = $User->ID;
  99. $statement = $this->DataInterface->DatabaseConnection->prepare(
  100. "SELECT data FROM settings"
  101. );
  102. if(!$statement->execute()) {
  103. $results = Array('result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo());
  104. }
  105. $settings = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data'], JSON_NUMERIC_CHECK);
  106. // OK
  107. return array(
  108. 'result' => 'OK',
  109. 'ID' => $User->ID,
  110. 'firstname' => $User->firstname,
  111. 'lastname' => $User->lastname,
  112. 'email' => $User->email,
  113. 'settings' => $settings
  114. );
  115. }
  116. /**
  117. * Get export data.
  118. */
  119. public function adminExportGet($User) {
  120. $userID = $User->ID;
  121. $statement = $this->DataInterface->DatabaseConnection->prepare(
  122. "SELECT * FROM user WHERE type = 'physician'"
  123. );
  124. if(!$statement->execute()) {
  125. $results = Array('result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo());
  126. }
  127. $users = $statement->fetchAll(\PDO::FETCH_ASSOC);
  128. // OK
  129. return array(
  130. 'result' => 'OK',
  131. 'users' => $users,
  132. );
  133. }
  134. /**
  135. * Post export data.
  136. */
  137. public function adminExportPost($User, $data) {
  138. $userID = $User->ID;
  139. return $this->exportByID($data['ID']);
  140. }
  141. public function exportByID($ID) {
  142. // rm old files
  143. $now = time();
  144. $files = glob('../../storage/tmp/*.zip');
  145. foreach($files as $F) {
  146. if ($now - filemtime($F) >= 60 * 60 * 24 * 1) { // 1 day
  147. unlink($F);
  148. }
  149. }
  150. $files = glob('../../storage/tmp/*.csv');
  151. foreach($files as $F) {
  152. if ($now - filemtime($F) >= 60 * 60 * 24 * 1) { // 1 day
  153. unlink($F);
  154. }
  155. }
  156. $statement = $this->DataInterface->DatabaseConnection->prepare(
  157. "SELECT * FROM user WHERE ID = $ID"
  158. );
  159. if(!$statement->execute()) {
  160. $results = Array('result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo());
  161. }
  162. $user = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  163. $prefix = date('Y-m-d').'_'.str_replace([' ','\''], '_', $user['firstname'].'_'.$user['lastname']);
  164. $od = '../../storage/tmp/'.$prefix.'_'.$ID.'/';
  165. \Tools\FS::mkpath($od);
  166. $data = [];
  167. // header
  168. $data[] =
  169. 'Visit_PatientID,Visit_PatientLastname,Visit_PatientFistname,Visit_PatientBirth,Visit_PatientSex,Visit_ID,Visit_Date,Visit_Created,Visit_Area,'.
  170. 'Media_Side,Media_Location,Media_Incidence,Media_Filename,Media_Width,Media_Height,Media_PixelWidth,Media_PixelHeight,Media_FrameCount,Media_FramePerSecond,'.
  171. 'Measure_Created,Measure_Frame,Measure_Distance,Measure_ImtMean,Measure_ImtMax,Measure_ImtStddev,Measure_IntimaMean,Measure_MediaMean,Measure_NearWall,Measure_QualityIndex,Measure_NumberOfPoints';
  172. // visit
  173. $statement = $this->DataInterface->DatabaseConnection->prepare("
  174. SELECT patient.*, visit.*
  175. FROM patient, visit
  176. WHERE patient.ID = visit.fk_patient
  177. AND visit.area = 'carotid'
  178. AND patient.fk_user = $ID
  179. ");
  180. // Error check
  181. if(!$statement->execute()) {
  182. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  183. }
  184. $visits = $statement->fetchAll(\PDO::FETCH_ASSOC);
  185. foreach($visits as $visit) {
  186. $fk_visit = $visit['ID'];
  187. // media
  188. $statement = $this->DataInterface->DatabaseConnection->prepare("
  189. SELECT media.*
  190. FROM media
  191. WHERE fk_visit = $fk_visit
  192. ");
  193. // Error check
  194. if(!$statement->execute()) {
  195. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  196. }
  197. $medias = $statement->fetchAll(\PDO::FETCH_ASSOC);
  198. foreach($medias as $media) {
  199. $fk_media = $media['ID'];
  200. // measure
  201. $statement = $this->DataInterface->DatabaseConnection->prepare("
  202. SELECT measure.*
  203. FROM measure
  204. WHERE fk_media = $fk_media
  205. AND type = 'imt'
  206. ");
  207. // Error check
  208. if(!$statement->execute()) {
  209. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  210. }
  211. $measures = $statement->fetchAll(\PDO::FETCH_ASSOC);
  212. // save file
  213. if(count($measures)) {
  214. copy('../../storage/media/'.$visit['ID'].'/'.$media['filename'], $od.'/'.$media['filename']);
  215. }
  216. foreach($measures as $measure) {
  217. $V['Visit_PatientID'] = $visit['patientID'];
  218. $V['Visit_PatientLastname'] = $visit['lastname'];
  219. $V['Visit_PatientFistname'] = $visit['firstname'];
  220. $V['Visit_PatientBirth'] = $visit['birthDate'];
  221. $V['Visit_PatientSex'] = $visit['gender'];
  222. $V['Visit_ID'] = $visit['number'];
  223. $V['Visit_Date'] = $visit['visitDate'];
  224. $V['Visit_Created'] = $visit['created'];
  225. $V['Visit_Area'] = $visit['area'];
  226. $V['Media_Side'] = $media['side'];
  227. $V['Media_Location'] = $media['location'];
  228. $V['Media_Incidence'] = $media['incidence'];
  229. $V['Media_Filename'] = $media['filename'];
  230. $metrics = json_decode($media['metrics']);
  231. $V['Media_Width'] = $metrics->width;
  232. $V['Media_Height'] = $metrics->height;
  233. $V['Media_PixelWidth'] = $metrics->pxwidth;
  234. $V['Media_PixelHeight'] = $metrics->pxheight;
  235. $V['Media_FrameCount'] = $metrics->frameCount;
  236. $V['Media_FramePerSecond'] = $metrics->fps;
  237. $V['Measure_Created'] = $measure['created'];
  238. $V['Measure_Frame'] = $measure['frame'];
  239. $computation = json_decode($measure['computation']);
  240. $V['Measure_Distance'] = $computation->distance;
  241. $V['Measure_ImtMean'] = $computation->imt_mean;
  242. $V['Measure_ImtMax'] = $computation->imt_max;
  243. $V['Measure_ImtStddev'] = $computation->imt_stddev;
  244. $V['Measure_IntimaMean'] = $computation->intima_mean;
  245. $V['Measure_MediaMean'] = $computation->media_mean;
  246. $V['Measure_Location'] = $computation->nearWall?'Proximal':'Distal';
  247. $V['Measure_QualityIndex'] = $computation->qualityIndex;
  248. $V['Measure_NumberOfPoints'] = $computation->numberOfPoints;
  249. //$data[] = $V;
  250. $data[] = implode(",", $V);
  251. }
  252. }
  253. }
  254. unlink('../../storage/tmp/'.$prefix.'_'.$ID);
  255. // make archive
  256. $dst = '../../storage/tmp/'.$prefix.'_'.$ID.'.zip';
  257. unlink($dst);
  258. $cmdLine = 'cd ../../storage/tmp && zip -r '.$prefix.'_'.$ID.'.zip '.$prefix.'_'.$ID.'/ 2>&1';
  259. $output=null;
  260. $retval=null;
  261. exec($cmdLine, $output, $retval);
  262. // error
  263. if($retval !== 0 || count($output)<1) {
  264. return [
  265. 'result' => 'ERROR',
  266. 'cmdLine' => $cmdLine,
  267. 'output' => $output,
  268. 'retval' => $retval
  269. ];
  270. }
  271. // make csv
  272. unlink('../../storage/tmp/'.$prefix.'_'.$ID.'.csv');
  273. file_put_contents('../../storage/tmp/'.$prefix.'_'.$ID.'.csv', implode("\n", $data));
  274. // OK
  275. return array(
  276. 'result' => 'OK',
  277. 'ID' => $ID,
  278. 'data' => $data,
  279. 'csv' => 'tmp/'.$prefix.'_'.$ID.'.csv',
  280. 'zip' => 'tmp/'.$prefix.'_'.$ID.'.zip'
  281. );
  282. }
  283. /**
  284. * Post common data.
  285. */
  286. public function adminCommonPost($User, $data) {
  287. $userID = $User->ID;
  288. // update settings
  289. $statement = $this->DataInterface->DatabaseConnection->prepare(
  290. "UPDATE settings SET data = :data"
  291. );
  292. $data = json_encode($data['data'], JSON_NUMERIC_CHECK);
  293. $statement->bindParam(':data', $data);
  294. // Error check
  295. if(!$statement->execute()) {
  296. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  297. }
  298. // OK
  299. return array(
  300. 'result' => 'OK',
  301. 'data' => $data
  302. );
  303. }
  304. /**
  305. *
  306. */
  307. public function adminCtParamsGet($User) {
  308. $userID = $User->ID;
  309. $statement = $this->DataInterface->DatabaseConnection->prepare(
  310. "SELECT * FROM clinical_trial"
  311. );
  312. // Error check
  313. if(!$statement->execute()) {
  314. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  315. }
  316. $settings = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  317. $settings['comment'] = stripslashes($settings['comment']);
  318. // OK
  319. return array(
  320. 'result' => 'OK',
  321. 'settings' => $settings
  322. );
  323. }
  324. /**
  325. *
  326. */
  327. public function adminCtStatsGet($User) {
  328. $userID = $User->ID;
  329. // patients
  330. $statement = $this->DataInterface->DatabaseConnection->prepare("
  331. SELECT DATE_FORMAT(visit.created, '%Y-%m') AS m, COUNT(visit.ID) AS patients
  332. FROM visit, patient, user
  333. WHERE visit.fk_patient = patient.ID AND patient.fk_user = user.ID AND user.type = 'investigator'
  334. GROUP BY DATE_FORMAT(visit.created, '%Y-%m')
  335. ");
  336. // Error check
  337. if(!$statement->execute()) {
  338. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  339. }
  340. $resPatients = $statement->fetchAll(\PDO::FETCH_ASSOC);
  341. // measures
  342. $statement = $this->DataInterface->DatabaseConnection->prepare("
  343. SELECT DATE_FORMAT(measure.created, '%Y-%m') AS m, COUNT(measure.ID) AS measures
  344. FROM measure, user
  345. WHERE measure.fk_user = user.ID AND user.type = 'reader'
  346. GROUP BY DATE_FORMAT(measure.created, '%Y-%m')
  347. ");
  348. // Error check
  349. if(!$statement->execute()) {
  350. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  351. }
  352. $resMeasures = $statement->fetchAll(\PDO::FETCH_ASSOC);
  353. $patients = [];
  354. $start = (new \DateTime($resPatients[0]['m'].'-01'))->modify('first day of this month');
  355. $end = (new \DateTime(date('Y-m-d')))->modify('first day of next month');
  356. $interval = \DateInterval::createFromDateString('1 month');
  357. $period = new \DatePeriod($start, $interval, $end);
  358. foreach($period as $dt) {
  359. $patients[] = array('m' => $dt->format("Y-m"), 'patients' => 0, 'measures' => 0);
  360. }
  361. foreach($resPatients as $R) {
  362. foreach($patients as &$P) {
  363. if($P['m'] == $R['m']) {
  364. $P['patients'] = intval($R['patients']);
  365. }
  366. }
  367. }
  368. foreach($resMeasures as $R) {
  369. foreach($patients as &$P) {
  370. if($P['m'] == $R['m']) {
  371. $P['measures'] = intval($R['measures']);
  372. }
  373. }
  374. }
  375. // OK
  376. return array(
  377. 'result' => 'OK',
  378. 'patients' => $patients
  379. );
  380. }
  381. /**
  382. *
  383. */
  384. public function adminPhyStatsGet($User) {
  385. $userID = $User->ID;
  386. // patients
  387. $statement = $this->DataInterface->DatabaseConnection->prepare("
  388. SELECT DATE_FORMAT(visit.created, '%Y-%m') AS m, COUNT(visit.ID) AS patients
  389. FROM visit, patient, user
  390. WHERE visit.fk_patient = patient.ID AND patient.fk_user = user.ID AND user.type = 'physician'
  391. GROUP BY DATE_FORMAT(visit.created, '%Y-%m')
  392. ");
  393. // Error check
  394. if(!$statement->execute()) {
  395. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  396. }
  397. $resPatients = $statement->fetchAll(\PDO::FETCH_ASSOC);
  398. // measures
  399. $statement = $this->DataInterface->DatabaseConnection->prepare("
  400. SELECT DATE_FORMAT(measure.created, '%Y-%m') AS m, COUNT(measure.ID) AS measures
  401. FROM measure, user
  402. WHERE measure.fk_user = user.ID AND user.type = 'physician'
  403. GROUP BY DATE_FORMAT(measure.created, '%Y-%m')
  404. ");
  405. // Error check
  406. if(!$statement->execute()) {
  407. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  408. }
  409. $resMeasures = $statement->fetchAll(\PDO::FETCH_ASSOC);
  410. $patients = [];
  411. $start = (new \DateTime($resPatients[0]['m'].'-01'))->modify('first day of this month');
  412. $end = (new \DateTime(date('Y-m-d')))->modify('first day of next month');
  413. $interval = \DateInterval::createFromDateString('1 month');
  414. $period = new \DatePeriod($start, $interval, $end);
  415. foreach($period as $dt) {
  416. $patients[] = array('m' => $dt->format("Y-m"), 'patients' => 0, 'measures' => 0);
  417. }
  418. foreach($resPatients as $R) {
  419. foreach($patients as &$P) {
  420. if($P['m'] == $R['m']) {
  421. $P['patients'] = intval($R['patients']);
  422. }
  423. }
  424. }
  425. foreach($resMeasures as $R) {
  426. foreach($patients as &$P) {
  427. if($P['m'] == $R['m']) {
  428. $P['measures'] = intval($R['measures']);
  429. }
  430. }
  431. }
  432. // OK
  433. return array(
  434. 'result' => 'OK',
  435. 'patients' => $patients
  436. );
  437. }
  438. /**
  439. *
  440. */
  441. public function adminCtUsersGet($User) {
  442. $userID = $User->ID;
  443. $statement = $this->DataInterface->DatabaseConnection->prepare(
  444. "SELECT COUNT(ID) AS cnt FROM user WHERE type = 'cro'"
  445. );
  446. // Error check
  447. if(!$statement->execute()) {
  448. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  449. }
  450. $admin = $statement->fetchAll(\PDO::FETCH_ASSOC)[0]['cnt'];
  451. $statement = $this->DataInterface->DatabaseConnection->prepare(
  452. "SELECT COUNT(ID) AS cnt FROM user WHERE type = 'investigator'"
  453. );
  454. // Error check
  455. if(!$statement->execute()) {
  456. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  457. }
  458. $investigator = $statement->fetchAll(\PDO::FETCH_ASSOC)[0]['cnt'];
  459. $statement = $this->DataInterface->DatabaseConnection->prepare(
  460. "SELECT COUNT(ID) AS cnt FROM user WHERE type = 'reader'"
  461. );
  462. // Error check
  463. if(!$statement->execute()) {
  464. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  465. }
  466. $reader = $statement->fetchAll(\PDO::FETCH_ASSOC)[0]['cnt'];
  467. // OK
  468. return array(
  469. 'result' => 'OK',
  470. 'data' => [
  471. 'admin' => $admin,
  472. 'investigator' => $investigator,
  473. 'reader' => $reader
  474. ]
  475. );
  476. }
  477. /**
  478. *
  479. */
  480. public function adminCreditGet($User, $ID) {
  481. $userID = $User->ID;
  482. // total purchased credits
  483. $statement = $this->DataInterface->DatabaseConnection->prepare(
  484. "SELECT SUM(count) AS purchased FROM credit WHERE ID_user = :fk_user"
  485. );
  486. $statement->bindParam(':fk_user', $ID);
  487. // Error check
  488. if(!$statement->execute()) {
  489. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  490. }
  491. $purchased = intval($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['purchased']);
  492. // total used credits
  493. $statement = $this->DataInterface->DatabaseConnection->prepare(
  494. "SELECT COUNT(ID) AS used FROM credit_usage WHERE fk_user = :fk_user"
  495. );
  496. $statement->bindParam(':fk_user', $ID);
  497. // Error check
  498. if(!$statement->execute()) {
  499. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  500. }
  501. $used = intval($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['used']);
  502. // credit
  503. $statement = $this->DataInterface->DatabaseConnection->prepare(
  504. "SELECT * FROM credit WHERE ID_user = $ID ORDER BY stamp DESC"
  505. );
  506. // Error check
  507. if(!$statement->execute()) {
  508. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  509. }
  510. $credit = $statement->fetchAll(\PDO::FETCH_ASSOC);
  511. // OK
  512. return array(
  513. 'result' => 'OK',
  514. 'credit' => $credit,
  515. 'purchased' => $purchased,
  516. 'used' => $used
  517. );
  518. }
  519. /**
  520. *
  521. */
  522. public function adminCreditPost($User, $data) {
  523. $userID = $User->ID;
  524. $customerID = $data['ID'];
  525. $count = $data['count'];
  526. // credit
  527. $statement = $this->DataInterface->DatabaseConnection->prepare(
  528. "INSERT INTO credit(ID_user, count) VALUES($customerID, $count)"
  529. );
  530. // Error check
  531. if(!$statement->execute()) {
  532. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  533. }
  534. // OK
  535. return array(
  536. 'result' => 'OK',
  537. 'credit' => $credit
  538. );
  539. }
  540. /**
  541. *
  542. */
  543. public function adminCustomerGet($User, $who) {
  544. $userID = $User->ID;
  545. if($who=='physician') {
  546. // customers
  547. $statement = $this->DataInterface->DatabaseConnection->prepare(
  548. "SELECT ID, firstname, lastname, email FROM user WHERE type = 'physician' ORDER BY lastname, firstname"
  549. );
  550. // Error check
  551. if(!$statement->execute()) {
  552. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  553. }
  554. $customer = $statement->fetchAll(\PDO::FETCH_ASSOC);
  555. }
  556. else {
  557. // customers
  558. $statement = $this->DataInterface->DatabaseConnection->prepare(
  559. "SELECT * FROM user WHERE type = 'cro' ORDER BY ID LIMIT 0,1"
  560. );
  561. // Error check
  562. if(!$statement->execute()) {
  563. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  564. }
  565. $customer = $statement->fetchAll(\PDO::FETCH_ASSOC);
  566. // clinical trial
  567. $statement = $this->DataInterface->DatabaseConnection->prepare(
  568. "SELECT max_readers, max_investigators FROM clinical_trial"
  569. );
  570. // Error check
  571. if(!$statement->execute()) {
  572. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  573. }
  574. $res = $statement->fetchAll(\PDO::FETCH_ASSOC);
  575. if(count($res)) {
  576. $customer[0]['max_readers'] = $res[0]['max_readers'];
  577. $customer[0]['max_investigators'] = $res[0]['max_investigators'];
  578. }
  579. else {
  580. $customer[0]['max_readers'] = 1;
  581. $customer[0]['max_investigators'] = 1;
  582. }
  583. $credits = $this->adminCreditGet($User, $customer[0]['ID']);
  584. if($credits['result'] == 'ERROR') {
  585. return $credits;
  586. }
  587. $customer[0]['credits'] = $credits;
  588. }
  589. // OK
  590. return array(
  591. 'result' => 'OK',
  592. 'who' => $who,
  593. 'customer' => $customer
  594. );
  595. }
  596. /**
  597. * Get pacs data.
  598. */
  599. public function adminPacsGet($User) {
  600. $userID = $User->ID;
  601. // customers
  602. $statement = $this->DataInterface->DatabaseConnection->prepare(
  603. "SELECT ID AS physicianID, firstname, lastname, email FROM user WHERE type = 'physician' ORDER BY lastname, firstname"
  604. );
  605. // Error check
  606. if(!$statement->execute()) {
  607. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  608. }
  609. $customer = $statement->fetchAll(\PDO::FETCH_ASSOC);
  610. // pacs
  611. $statement = $this->DataInterface->DatabaseConnection->prepare(
  612. "SELECT * FROM settings_pacs WHERE fk_physician IS NOT NULL AND fk_center IS NULL ORDER BY fk_physician"
  613. );
  614. // Error check
  615. if(!$statement->execute()) {
  616. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  617. }
  618. $pacs = $statement->fetchAll(\PDO::FETCH_ASSOC);
  619. foreach($pacs as &$p) {
  620. $p['data'] = json_decode($p['data']);
  621. }
  622. // OK
  623. return array(
  624. 'result' => 'OK',
  625. 'ourAET' => 'IIMT',
  626. 'customer' => $customer,
  627. 'pacs' => $pacs
  628. );
  629. }
  630. /**
  631. * Post pacs data.
  632. */
  633. public function adminPacsPost($User, $data) {
  634. $userID = $User->ID;
  635. $fk_physician = $data['data']['physicianID'];
  636. // insert
  637. if(intval($data['data']['PACSID'])==0) {
  638. $statement = $this->DataInterface->DatabaseConnection->prepare(
  639. "INSERT INTO settings_pacs VALUES(0, :data, :fk_physician, NULL)"
  640. );
  641. unset($data['data']['PACSID']);
  642. $data = json_encode($data['data'], JSON_NUMERIC_CHECK);
  643. $statement->bindParam(':data', $data);
  644. $statement->bindParam(':fk_physician', $fk_physician);
  645. // Error check
  646. if(!$statement->execute()) {
  647. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  648. }
  649. }
  650. // update
  651. else {
  652. $statement = $this->DataInterface->DatabaseConnection->prepare(
  653. "UPDATE settings_pacs SET data=:data, fk_physician=:fk_physician WHERE ID = ".$data['data']['PACSID']
  654. );
  655. unset($data['data']['PACSID']);
  656. $data = json_encode($data['data'], JSON_NUMERIC_CHECK);
  657. $statement->bindParam(':data', $data);
  658. $statement->bindParam(':fk_physician', $fk_physician);
  659. // Error check
  660. if(!$statement->execute()) {
  661. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  662. }
  663. }
  664. // OK
  665. return array(
  666. 'result' => 'OK',
  667. 'data' => $data
  668. );
  669. }
  670. /**
  671. *
  672. */
  673. public function adminEchoPost($User, $data) {
  674. $cmdLine = 'echoscu '.$data['serverAddress'].' '.$data['queryPort'].' -v 2>&1';
  675. $output=null;
  676. $retval=null;
  677. exec($cmdLine, $output, $retval);
  678. if($retval !== 0 || count($output)<1) {
  679. return [
  680. 'result' => 'ERROR',
  681. 'cmdLine' => $cmdLine,
  682. 'output' => $output,
  683. 'retval' => $retval
  684. ];
  685. }
  686. return [
  687. 'result' => 'OK',
  688. 'output' => $output
  689. ];
  690. }
  691. /**
  692. * Post customer data : ct only
  693. */
  694. public function adminCustomerPost($User, $data) {
  695. $userID = $User->ID;
  696. // Select
  697. $statement = $this->DataInterface->DatabaseConnection->prepare(
  698. "SELECT ID, email, password FROM user WHERE type = 'cro' ORDER BY ID LIMIT 0, 1"
  699. );
  700. // Error check
  701. if(!$statement->execute()) {
  702. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  703. }
  704. $knownData = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  705. $fk_user = $knownData['ID'];
  706. $password = $knownData['password'];
  707. $newUser = 0;
  708. if($knownData['email']!=$data['user']['email']) {
  709. $password = \Tools\UUID::v4();
  710. $newUser = 1;
  711. }
  712. // Update
  713. $statement = $this->DataInterface->DatabaseConnection->prepare(
  714. "UPDATE user SET firstname = :firstname, lastname = :lastname, password = :password, email = :email, phone = :phone WHERE ID = :ID"
  715. );
  716. $statement->bindParam(':firstname', $data['user']['firstname']);
  717. $statement->bindParam(':lastname', $data['user']['lastname']);
  718. $statement->bindParam(':password', $password);
  719. $statement->bindParam(':email', $data['user']['email']);
  720. $statement->bindParam(':phone', $data['user']['phone']);
  721. $statement->bindParam(':ID', $fk_user);
  722. // Error check
  723. if(!$statement->execute()) {
  724. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  725. }
  726. /*
  727. // Insert user
  728. $statement = $this->DataInterface->DatabaseConnection->prepare(
  729. "INSERT INTO user(activation_token, activation_expire, password, firstname, lastname, email, phone, type) VALUES(:activation_token, :activation_expire, :password, :firstname, :lastname, :email, :phone, 'cro')"
  730. );
  731. $activation_token = \Tools\UUID::v4();
  732. $activation_expire = date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s'). ' + 1 days'));
  733. $statement->bindParam(':activation_token', $activation_token);
  734. $statement->bindParam(':activation_expire', $activation_expire);
  735. $password = \Tools\Crypto::getHashPassword('SUPER_SECURE_DEFAULT_PASSWORD');
  736. $statement->bindParam(':password', $password);
  737. $statement->bindParam(':firstname', $data['user']['firstname']);
  738. $statement->bindParam(':lastname', $data['user']['lastname']);
  739. $statement->bindParam(':email', $data['user']['email']);
  740. $statement->bindParam(':phone', $data['user']['phone']);
  741. // Error check
  742. if(!$statement->execute()) {
  743. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  744. }
  745. $fk_user = $this->DataInterface->DatabaseConnection->lastInsertId();
  746. */
  747. // OK
  748. return [
  749. 'result' => 'OK',
  750. 'data' => $data,
  751. 'emailTo' => $data['user']['email'],
  752. 'emailFrom' => $User->email,
  753. 'password_token' => $password,
  754. 'newUser' => $newUser
  755. ];
  756. }
  757. }
  758. }