PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/include/core/class_person.php

https://github.com/timschofield/2.8
PHP | 1126 lines | 557 code | 41 blank | 528 comment | 104 complexity | 180dc5eeb75dcd6ba1183c0bdbe9c772 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-2.0
  1. <?php
  2. error_reporting(E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR);
  3. /**
  4. * @package care_api
  5. */
  6. /**
  7. */
  8. require_once(CARE_BASE .'include/core/class_core.php');
  9. /**
  10. * Person methods.
  11. *
  12. * Note this class should be instantiated only after a "$db" adodb connector object has been established by an adodb instance
  13. * @author Elpidio Latorilla, Gjergj Sheldija, Robert Meggle
  14. * @version beta 3.0.0
  15. * @copyright 2002 - 2010: Elpidio Latorilla; 2011 - Today: Mauri Niemi
  16. * @package care_api
  17. */
  18. class Person extends Core {
  19. /**#@+
  20. * @access private
  21. */
  22. /**
  23. * Table name for person registration data.
  24. * @public string
  25. */
  26. public $tb_person='care_person';
  27. /**
  28. * Table name for city town name.
  29. * @public string
  30. */
  31. public $tb_citytown='care_address_citytown';
  32. /**
  33. * Table name for ethnic origin.
  34. * Add by Jean-Philippe LIOT 13/05/2004
  35. * @public string
  36. */
  37. public $tb_ethnic_orig='care_type_ethnic_orig';
  38. /**
  39. * Table name for encounter data.
  40. * @public string
  41. */
  42. public $tb_enc='care_encounter';
  43. /**
  44. * Table name for employee data.
  45. * @public string
  46. */
  47. public $tb_employ='care_staff';
  48. /**
  49. * SQL query
  50. */
  51. public $sql;
  52. /**#@-*/
  53. /**
  54. * PID number
  55. * @public int
  56. */
  57. public $pid;
  58. /**
  59. * Sql query result buffer
  60. * @public adodb record object
  61. */
  62. public $result;
  63. /**
  64. * Universal flag
  65. * @public boolean
  66. */
  67. public $ok;
  68. /**
  69. * Internal data buffer
  70. * @public array
  71. */
  72. public $data_array;
  73. /**
  74. * Universal buffer
  75. * @public mixed
  76. */
  77. public $buffer;
  78. /**
  79. * Returned row buffer
  80. * @public array
  81. */
  82. public $row;
  83. /**
  84. * Returned person data buffer
  85. * @public array
  86. */
  87. public $person=array();
  88. /**
  89. * Preloaded data flag
  90. * @public boolean
  91. */
  92. public $is_preloaded=false;
  93. /**
  94. * Valid number flag
  95. * @public boolean
  96. */
  97. public $is_nr=false;
  98. /**
  99. * Field names of basic registration data to be returned.
  100. * @public array
  101. */
  102. public $basic_list='pid,title,name_first,name_last,name_2,name_3,name_middle,name_maiden,name_others,date_birth,
  103. sex,addr_str,addr_str_nr,addr_zip,addr_citytown_nr,photo_filename';
  104. /**
  105. * Field names of table care_person
  106. * @public array
  107. */
  108. public $elems_array=array(
  109. 'pid',
  110. 'title',
  111. 'date_reg',
  112. 'name_last',
  113. 'name_first',
  114. 'date_birth',
  115. 'sex',
  116. 'name_2',
  117. 'name_3',
  118. 'name_middle',
  119. 'name_maiden',
  120. 'name_others',
  121. 'date_birth',
  122. 'blood_group',
  123. 'addr_str',
  124. 'addr_str_nr',
  125. 'addr_zip',
  126. 'addr_citytown_nr',
  127. 'phone_1_code',
  128. 'phone_1_nr',
  129. 'phone_2_code',
  130. 'phone_2_nr',
  131. 'cellphone_1_nr',
  132. 'cellphone_2_nr',
  133. 'fax',
  134. 'email',
  135. 'civil_status',
  136. 'photo_filename',
  137. 'ethnic_orig',
  138. 'org_id',
  139. 'sss_nr',
  140. 'nat_id_nr',
  141. 'religion',
  142. 'mother_pid',
  143. 'father_pid',
  144. 'contact_person',
  145. 'contact_pid',
  146. 'contact_relation',
  147. 'death_date',
  148. 'death_encounter_nr',
  149. 'death_cause',
  150. 'death_cause_code',
  151. 'status',
  152. 'history',
  153. 'modify_id',
  154. 'modify_time',
  155. 'create_id',
  156. 'create_time',
  157. 'relative_name_first',
  158. 'relative_name_last',
  159. 'relative_phone');
  160. /**
  161. * Constructor
  162. * @param int PID number
  163. */
  164. function Person ($pid='') {
  165. $this->pid=$pid;
  166. $this->ref_array=$this->elems_array;
  167. $this->coretable=$this->tb_person;
  168. }
  169. /**
  170. * Sets the PID number.
  171. * @access public
  172. * @param int PID number
  173. */
  174. function setPID($pid) {
  175. $this->pid=$pid;
  176. }
  177. /**
  178. * Resolves the PID number to used in the methods.
  179. * @access public
  180. * @param int PID number
  181. * @return boolean
  182. */
  183. function internResolvePID($pid) {
  184. if (empty($pid)) {
  185. if(empty($this->pid)) {
  186. return false;
  187. } else { return true; }
  188. } else {
  189. $this->pid=$pid;
  190. return true;
  191. }
  192. }
  193. /**
  194. * Checks if PID number exists in the database.
  195. * @access public
  196. * @param int PID number
  197. * @return boolean
  198. */
  199. function InitPIDExists($init_nr){
  200. global $db;
  201. // Patch for db where the pid does not start with the predefined init
  202. //$this->sql="SELECT pid FROM $this->tb_person WHERE pid=$init_nr";
  203. $this->sql="SELECT pid FROM $this->tb_person";
  204. if($this->result=$db->Execute($this->sql)){
  205. if($this->result->RecordCount()){
  206. return true;
  207. } else { return false; }
  208. } else { return false; }
  209. }
  210. /**
  211. * Prepares the internal buffer array for insertion routine.
  212. * @access private
  213. */
  214. function prepInsertArray(){
  215. global $_POST;
  216. $x='';
  217. $v='';
  218. $this->data_array=NULL;
  219. if(!isset($_POST['create_time'])||empty($_POST['create_time'])) $_POST['create_time']=date('YmdHis');
  220. while(list($x,$v)=each($this->elems_array)) {
  221. if(isset($_POST[$v])&&!empty($_POST[$v])) $this->data_array[$v]=$_POST[$v];
  222. }
  223. }
  224. /**
  225. * Database transaction. Uses the adodb transaction method.
  226. * @access private
  227. */
  228. function Transact($sql='') {
  229. global $db;
  230. //$db->debug=true;
  231. if(!empty($sql)) $this->sql=$sql;
  232. $db->BeginTrans();
  233. $this->ok=$db->Execute($this->sql);
  234. if($this->ok) {
  235. $db->CommitTrans();
  236. return true;
  237. } else {
  238. $db->RollbackTrans();
  239. return false;
  240. }
  241. }
  242. /**
  243. * Inserts the data into the care_person table.
  244. * @access private
  245. * @param int PID number
  246. * @return boolean
  247. */
  248. function insertDataFromArray(&$array) {
  249. $x='';
  250. $v='';
  251. $index='';
  252. $values='';
  253. if(!is_array($array)) return false;
  254. while(list($x,$v)=each($array)) {
  255. $index.="$x,";
  256. $values.="'$v',";
  257. }
  258. $index=substr_replace($index,'',(strlen($index))-1);
  259. $values=substr_replace($values,'',(strlen($values))-1);
  260. $this->sql="INSERT INTO $this->tb_person ($index) VALUES ($values)";
  261. return $this->Transact();
  262. }
  263. /**
  264. * Inserts the data from the internal buffer array into the care_person table.
  265. *
  266. * The data must be packed in the buffer array with index keys as outlined in the <var>$elems_array</var> array.
  267. * @access public
  268. * @return boolean
  269. */
  270. function insertDataFromInternalArray() {
  271. //$this->data_array=NULL;
  272. $this->prepInsertArray();
  273. // Check if "create_time" key has a value, if no, create a new value
  274. if(!isset($this->buffer_array['create_time'])||empty($this->buffer_array['create_time'])) $this->buffer_array['create_time']=date('YmdHis');
  275. return $this->insertDataFromArray($this->data_array);
  276. }
  277. /* function updateDataFromArray(&$array,$item_nr='') {
  278. $x='';
  279. $v='';
  280. $sql='';
  281. if(!is_array($array)) return false;
  282. if(empty($item_nr)||!is_numeric($item_nr)) return false;
  283. while(list($x,$v)=each($array)) {
  284. if(stristr($v,'concat')||stristr($v,'null')) $sql.="$x= $v,";
  285. else $sql.="$x='$v',";
  286. }
  287. $sql=substr_replace($sql,'',(strlen($sql))-1);
  288. $this->sql="UPDATE $this->tb_person SET $sql WHERE pid=$item_nr";
  289. return $this->Transact();
  290. }
  291. */
  292. /**
  293. * Gets all person registration information based on its PID number key.
  294. *
  295. * The returned adodb record object contains a row or array.
  296. * This array contains data with the following index keys:
  297. * - all index keys as outlined in the <var>$elems_array</var> array
  298. * - addr_citytown_name = name of the city or town
  299. *
  300. * @access public
  301. * @param int PID number
  302. * @return mixed adodb object or boolean
  303. */
  304. function getAllInfoObject($pid='') {
  305. global $db;
  306. if(!$this->internResolvePID($pid)) return false;
  307. $this->sql="SELECT p.*, addr.name AS addr_citytown_name,ethnic.name AS ethnic_orig_txt
  308. FROM $this->tb_person AS p
  309. LEFT JOIN $this->tb_citytown AS addr ON p.addr_citytown_nr=addr.nr
  310. LEFT JOIN $this->tb_ethnic_orig AS ethnic ON p.ethnic_orig=ethnic.nr
  311. WHERE p.pid='$this->pid' ";
  312. //echo $this->sql;
  313. if($this->result=$db->Execute($this->sql)) {
  314. if($this->result->RecordCount()) {
  315. return $this->result;
  316. } else { return false; }
  317. } else { return false; }
  318. }
  319. /**
  320. * Same as getAllInfoObject() but returns a 2 dimensional array.
  321. *
  322. * The returned data in the array have the following index keys:
  323. * - all index keys as outlined in the <var>$elems_array</var> array
  324. * - citytown = name of the city or town
  325. *
  326. * @access public
  327. * @param int PID number
  328. * @return mixed array or boolean
  329. */
  330. function getAllInfoArray($pid='') {
  331. global $db;
  332. $x='';
  333. $v='';
  334. if(!$this->internResolvePID($pid)) return false;
  335. $this->sql="SELECT p.* , addr.name AS citytown
  336. FROM $this->tb_person AS p LEFT JOIN $this->tb_citytown AS addr ON p.addr_citytown_nr=addr.nr
  337. WHERE p.pid=$this->pid";
  338. if($this->result=$db->Execute($this->sql)) {
  339. if($this->result->RecordCount()) {
  340. return $this->row=$this->result->FetchRow();
  341. } else { return false; }
  342. } else { return false; }
  343. }
  344. /**
  345. * Gets a particular registration item based on its PID number.
  346. *
  347. * Use this preferably after the person registration data was successfully preloaded in the internal buffer with the <var>preloadPersonInfo()</var> method.
  348. * For details on field names of items that can be fetched, see the <var>$elems_array</var> array.
  349. * @access private
  350. * @param string Field name of the item to be fetched
  351. * @param int PID number
  352. * @return mixed string, integer, or boolean
  353. */
  354. function getValue($item,$pid='') {
  355. global $db;
  356. if($this->is_preloaded) {
  357. if(isset($this->person[$item])) return $this->person[$item];
  358. else return false;
  359. } else {
  360. if(!$this->internResolvePID($pid)) return false;
  361. $this->sql="SELECT $item FROM $this->tb_person WHERE pid=$this->pid";
  362. //return $this->sql;
  363. if($this->result=$db->Execute($this->sql)) {
  364. if($this->result->RecordCount()) {
  365. $this->person=$this->result->FetchRow();
  366. return $this->person[$item];
  367. } else { return false; }
  368. } else { return false; }
  369. }
  370. }
  371. /**
  372. * Gets registration items based on an item list and PID number.
  373. *
  374. * For details on field names of items that can be fetched, see the <var>$elems_array</var> array.
  375. * Several items can be fetched at once but their field names must be separated by comma.
  376. * @access public
  377. * @param string Field names of items to be fetched separated by comma.
  378. * @param int PID number
  379. * @return mixed
  380. */
  381. function getValueByList($list,$pid='') {
  382. global $db;
  383. if(empty($list)) return false;
  384. if(!$this->internResolvePID($pid)) return false;
  385. $this->sql="SELECT $list FROM $this->tb_person WHERE pid=$this->pid";
  386. if($this->result=$db->Execute($this->sql)) {
  387. if($this->result->RecordCount()) {
  388. $this->person=$this->result->FetchRow();
  389. return $this->person;
  390. } else { return false; }
  391. } else { return false; }
  392. }
  393. /**
  394. * Preloads the person registration data in the internal buffer <var>$person</var>.
  395. *
  396. * The preload success status is stored in the <var>$is_preloaded</var> variable.
  397. * The buffered adodb record object contains a row or array.
  398. * This array contains data with index keys as outlined in the <var>$elems_array</var> array
  399. *
  400. * @access public
  401. * @param int PID number
  402. * @return boolean
  403. */
  404. function preloadPersonInfo($pid) {
  405. global $db;
  406. if(!$this->internResolvePID($pid)) return false;
  407. $this->sql="SELECT * FROM $this->tb_person WHERE pid=$this->pid";
  408. if($this->result=$db->Execute($this->sql)) {
  409. if($this->result->RecordCount()) {
  410. $this->person=$this->result->FetchRow();
  411. $this->is_preloaded=true;
  412. return true;
  413. } else { return false; }
  414. } else { return false; }
  415. }
  416. /**#@+
  417. *
  418. * Use this preferably after the person registration data was successfully preloaded in the internal buffer with the <var>preloadPersonInfo()</var> method.
  419. * @access public
  420. * @return string
  421. */
  422. /**
  423. * Returns person's first name.
  424. */
  425. function FirstName() {
  426. return $this->getValue('name_first');
  427. }
  428. /**
  429. * Returns person's last or family name.
  430. */
  431. function LastName() {
  432. return $this->getValue('name_last');
  433. }
  434. /**
  435. * Returns person's second name.
  436. */
  437. function SecondName() {
  438. return $this->getValue('name_2');
  439. }
  440. /**
  441. * Returns person's third name.
  442. */
  443. function ThirdName() {
  444. return $this->getValue('name_3');
  445. }
  446. /**
  447. * Returns person's middle name.
  448. */
  449. function MiddleName() {
  450. return $this->getValue('name_middle');
  451. }
  452. /**
  453. * Returns person's maiden (unmarried) name.
  454. */
  455. function MaidenName() {
  456. return $this->getValue('name_maiden');
  457. }
  458. /**
  459. * Returns person's other name(s).
  460. */
  461. function OtherName() {
  462. return $this->getValue('name_others');
  463. }
  464. /**
  465. * Returns person's date of birth.
  466. */
  467. function BirthDate() {
  468. return $this->getValue('date_birth');
  469. }
  470. /**
  471. * Returns street number. Not stricly numeric. Could be alphanumeric.
  472. */
  473. function StreetNr() {
  474. return $this->getValue('addr_str_nr');
  475. }
  476. /**
  477. * Returns street name.
  478. */
  479. function StreetName() {
  480. return $this->getValue('addr_str');
  481. }
  482. /**
  483. * Returns ZIP code.
  484. */
  485. function ZIPCode() {
  486. return $this->getValue('addr_zip');
  487. }
  488. /**
  489. * Returns the valid address status. Returns 1 or 0.
  490. */
  491. function isValidAddress() {
  492. return $this->getValue('addr_is_valid');
  493. }
  494. /**
  495. * Returns the city or town code number. Reserved.
  496. */
  497. function CityTownCode() {
  498. return $this->getValue('addr_citytown_nr');
  499. }
  500. /**
  501. * Returns citizenship.
  502. */
  503. function Citizenship() {
  504. return $this->getValue('citizenship');
  505. }
  506. /**
  507. * Returns first phone area code.
  508. */
  509. function FirstPhoneAreaCode() {
  510. return $this->getValue('phone_1_code');
  511. }
  512. /**
  513. * Returns first phone number. Can be used as private phone number.
  514. */
  515. function FirstPhoneNumber() {
  516. return $this->getValue('phone_1_nr');
  517. }
  518. /**
  519. * Returns second phone area code.
  520. */
  521. function SecondPhoneAreaCode() {
  522. return $this->getValue('phone_2_code');
  523. }
  524. /**
  525. * Returns second phone number. Can be used as business phone number.
  526. */
  527. function SecondPhoneNumber() {
  528. return $this->getValue('phone_2_nr');
  529. }
  530. /**
  531. * Returns first cellphone number. Can be used as private cellphone number.
  532. */
  533. function FirstCellphoneNumber() {
  534. return $this->getValue('cellphone_1_nr');
  535. }
  536. /**
  537. * Returns second cellphone number.Can be used as business cellphone number
  538. */
  539. function SecondCellphoneNumber() {
  540. return $this->getValue('cellphone_2_nr');
  541. }
  542. /**
  543. * Returns fax number.
  544. */
  545. function FaxNumber() {
  546. return $this->getValue('fax');
  547. }
  548. /**
  549. * Returns email address.
  550. */
  551. function EmailAddress() {
  552. return $this->getValue('email');
  553. }
  554. /**
  555. * Returns sex.
  556. */
  557. function Sex() {
  558. return $this->getValue('sex');
  559. }
  560. /**
  561. * Returns title.
  562. */
  563. function Title() {
  564. return $this->getValue('title');
  565. }
  566. /**
  567. * Returns filename of stored id photo.
  568. */
  569. function PhotoFilename() {
  570. return $this->getValue('photo_filename');
  571. }
  572. /**
  573. * Returns ethnic origin.
  574. */
  575. function EthnicOrigin() {
  576. return $this->getValue('ethnic_origin');
  577. }
  578. /**
  579. * Returns organization id.
  580. */
  581. function OrgID() {
  582. return $this->getValue('org_id');
  583. }
  584. /**
  585. * Returns social security (system) number.
  586. */
  587. function SSSNumber() {
  588. return $this->getValue('sss_nr');
  589. }
  590. /**
  591. * Returns national id number.
  592. */
  593. function NationalIDNumber() {
  594. return $this->getValue('nat_id_nr');
  595. }
  596. /**
  597. * Returns religion.
  598. */
  599. function Religion() {
  600. return $this->getValue('religion');
  601. }
  602. /**
  603. * Returns pid number of mother.
  604. */
  605. function MotherPID() {
  606. return $this->getValue('mother_pid');
  607. }
  608. /**
  609. * Returns pid number of father.
  610. */
  611. function FatherPID() {
  612. return $this->getValue('father_pid');
  613. }
  614. /**
  615. * Returns date of death. In case person is deceased.
  616. */
  617. function DeathDate() {
  618. return $this->getValue('death_date');
  619. }
  620. /**
  621. * Returns case of death. In case person is deceased.
  622. */
  623. function DeathCause() {
  624. return $this->getValue('death_cause');
  625. }
  626. /**
  627. * returns a list of other hospital numbers
  628. *
  629. * Added by Kurt Brauchli
  630. * @access public
  631. * @return Associative array
  632. */
  633. function OtherHospNrList(){
  634. global $db;
  635. if($this->pid){
  636. $sql = "SELECT * FROM care_person_other_number WHERE pid=".$this->pid." AND status NOT IN ($this->dead_stat)";
  637. $result = $db->Execute($sql);
  638. if( !$result )
  639. return false;
  640. unset($other_hosp_no);
  641. while( $row = $result->FetchRow() ){
  642. $other_hosp_no[$row['org']] = $row['other_nr'];
  643. }
  644. return $other_hosp_no;
  645. }else{
  646. return FALSE;
  647. }
  648. }
  649. /**
  650. * Sets the number for other hospitals (orgs)
  651. *
  652. * Added by Kurt Brauchli. Enhanced by Elpidio Latorilla 2004-05-23
  653. * @access public
  654. * @param string The other hospital, org , or institution
  655. * @param int The other number
  656. * @param string User id
  657. * @return Boolean
  658. */
  659. function OtherHospNrSet($org='',$other_nr='',$user='system'){
  660. global $db;
  661. if(empty($org)) return FALSE;
  662. if(empty($other_nr)){
  663. // if number field is empty, delete other number
  664. //$this->sql = "DELETE FROM care_person_other_number WHERE org='$org' AND pid=".$this->pid;
  665. // We do not delete the record but instead set its status to "deleted"
  666. $this->sql = "UPDATE care_person_other_number
  667. SET status='deleted',
  668. history=".$this->ConcatHistory("Deleted ".date('Y-m-d H:i:s')." ".$user."\n").",
  669. modify_id='$user',
  670. modify_time='".date('YmdHis')."'
  671. WHERE org='$org' AND pid=".$this->pid;
  672. }else{
  673. $this->sql = "SELECT other_nr FROM care_person_other_number WHERE org='$org' AND pid='$this->pid'";
  674. if($result = $db->Execute( $this->sql )){
  675. if( $row = $result->FetchRow() ){
  676. $this->sql = "UPDATE care_person_other_number ";
  677. // If old number equals new number, we just set the status to "normal"
  678. // else change the number but document the old number in history
  679. if($row['other_nr']==$other_nr){
  680. $this->sql.="SET status='normal',
  681. history=".$this->ConcatHistory("Reactivated ".date('Y-m-d H:i:s')." ".$user."\n").", ";
  682. }else{
  683. $this->sql.="SET other_nr='$other_nr',
  684. status='normal',
  685. history=".$this->ConcatHistory("Changed (".$row['other_nr'].") -> ($other_nr) ".date('Y-m-d H:i:s')." ".$user."\n").", ";
  686. }
  687. $this->sql.=" modify_id='$user', modify_time='".date('YmdHis')."' WHERE org='$org' AND pid=".$this->pid;
  688. }else{
  689. $this->sql = "INSERT INTO care_person_other_number (pid,other_nr,org,status,history,create_id,create_time) ".
  690. " VALUES( ".$this->pid.",
  691. '$other_nr',
  692. '$org',
  693. 'normal',
  694. 'Created ".date('Y-m-d H:i:s')." ".$user."\n',
  695. '$user',
  696. '".date('YmdHis')."'
  697. )";
  698. }
  699. }
  700. }
  701. //$db->Execute($sql);
  702. return $this->Transact($this->sql);
  703. }
  704. /**
  705. * Returns table record's technical status.
  706. */
  707. function RecordStatus() {
  708. return $this->getValue('status');
  709. }
  710. /**
  711. * Returns table record's history.
  712. */
  713. function RecordHistory() {
  714. return $this->getValue('history');
  715. }
  716. /**#@-*/
  717. /**
  718. * Returns encounter number in case person died during that encounter.
  719. * @access public
  720. * @return int
  721. */
  722. function DeathEncounterNumber() {
  723. return $this->getValue('death_encounter_nr');
  724. }
  725. /**
  726. * Returns city or town name based on its "nr" key.
  727. * @access public
  728. * @return mixed string or boolean
  729. */
  730. function CityTownName($code_nr=''){
  731. global $db;
  732. if(!$this->is_preloaded) $this->sql="SELECT name FROM $this->tb_citytown WHERE nr=$code_nr";
  733. else $this->sql="SELECT name FROM $this->tb_citytown WHERE nr=".$this->CityTownCode();
  734. //echo $this->sql;exit;
  735. if($this->result=$db->Execute($this->sql)) {
  736. if($this->result->RecordCount()) {
  737. $this->row=$this->result->FetchRow();
  738. return $this->row['name'];
  739. } else { return false; }
  740. } else { return false; }
  741. }
  742. /**
  743. * Returns person registration items as listed in the <var>$basic_list</var> array based on pid key.
  744. *
  745. * The data is returned as associative array.
  746. * @access public
  747. * @param int PID number
  748. * @return mixed string or boolean
  749. */
  750. function BasicDataArray($pid){
  751. if(!$this->internResolvePID($pid)) return false;
  752. return $this->getValueByList($this->basic_list,$this->pid);
  753. }
  754. /**
  755. * Adds a "View" note in the history field of the person's registration record.
  756. *
  757. * @access public
  758. * @param string Name of viewing person
  759. * @param int PID number
  760. * @return mixed string or boolean
  761. */
  762. function setHistorySeen($encoder='',$pid=''){
  763. global $db, $dbtype;
  764. //$db->debug=true;
  765. if(empty($encoder)) return false;
  766. if(!$this->internResolvePID($pid)) return false;
  767. /*
  768. if($dbtype=='mysql')
  769. $this->sql="UPDATE $this->tb_person SET history= CONCAT(history,'\nView ".date('Y-m-d H:i:s')." = $encoder') WHERE pid=$this->pid";
  770. else
  771. $this->sql="UPDATE $this->tb_person SET history= history || '\nView ".date('Y-m-d H:i:s')." = $encoder' WHERE pid=$this->pid";
  772. */
  773. $this->sql="UPDATE $this->tb_person SET history=".$this->ConcatHistory("\nView ".date('Y-m-d H:i:s')." = $encoder")." WHERE pid=$this->pid";
  774. if($this->Transact($this->sql)) {return true;}
  775. else {echo $this->sql;return false;}
  776. }
  777. /**
  778. * Checks if a person is currently admitted (either inpatient & outpatient).
  779. *
  780. * If person is currently admitted, his current encounter number is returned, else FALSE.
  781. * @access public
  782. * @param int PID number
  783. * @return mixed integer or boolean
  784. */
  785. function CurrentEncounter($pid){
  786. global $db;
  787. if(!$pid) return false;
  788. $this->sql="SELECT encounter_nr FROM $this->tb_enc WHERE pid='$pid' AND is_discharged=0 AND encounter_status <> 'cancelled' AND status NOT IN ($this->dead_stat)";
  789. if($buf=$db->Execute($this->sql)){
  790. if($buf->RecordCount()) {
  791. $buf2=$buf->FetchRow();
  792. //echo $this->sql;
  793. return $buf2['encounter_nr'];
  794. }else{return false;}
  795. }else{return false;}
  796. }
  797. /**
  798. * Gets all encounters of a person based on its pid key.
  799. *
  800. * The returned adodb record object contains rows of arrays.
  801. * Each array contains the encounter data with the following index keys:
  802. * - encounter_nr = the encounter number
  803. * - encounter_class_nr = encountr class number, contains 1 (inpatient) or 2 (outpatient), etc.
  804. * - is_discharged = discharge flag, contains 0 (not discharged) or 1 (discharged)
  805. * - discharge_date = date of discharge (end of encounter)
  806. *
  807. * @access public
  808. * @param int PID number
  809. * @return mixed integer or boolean
  810. */
  811. function EncounterList($pid){
  812. global $db;
  813. if(!$pid) return false;
  814. $this->sql="SELECT encounter_nr,encounter_date,encounter_class_nr,is_discharged,discharge_date FROM $this->tb_enc WHERE pid='$pid' AND status NOT IN ($this->dead_stat)";
  815. if($this->res['_enl']=$db->Execute($this->sql)){
  816. if($this->rec_count=$this->res['_enl']->RecordCount()) {
  817. return $this->res['_enl'];
  818. }else{return false;}
  819. }else{return false;}
  820. }
  821. /**
  822. * Searches and returns a list of persons based on search key.
  823. *
  824. * The returned adodb record object contains rows of arrays.
  825. * Each array contains the encounter data with the following index keys:
  826. * - pid = the PID number
  827. * - name_last = person's last or family name
  828. * - name_first = person's first or given name
  829. * - date_birth = date of birth
  830. * - sex = sex
  831. *
  832. * @access public
  833. * @param string Search keyword
  834. * @param string Sort by the item name, default = name_last (last/family name)
  835. *'@param string Sort direction, default = ASC (ascending)
  836. * @return mixed integer or boolean
  837. */
  838. function Persons($searchkey='',$order_item='name_last',$order_dir='ASC'){
  839. global $db, $sql_LIKE;
  840. $searchkey=trim($searchkey);
  841. $searchkey=strtr($searchkey,'*?','%_');
  842. if(is_numeric($searchkey)) {
  843. $searchkey=(int) $searchkey;
  844. $this->is_nr=true;
  845. $order_item='pid';
  846. if(empty($order_dir)) $order_dir='DESC';
  847. } else {
  848. if(empty($order_item)) $order_item='name_last';
  849. if(empty($order_dir)) $order_dir='ASC';
  850. $this->is_nr=false;
  851. }
  852. return $this->SearchSelect($searchkey,'','',$order_item,$order_dir);
  853. /*
  854. $this->sql="SELECT pid, name_last, name_first, date_birth, sex FROM $this->tb_person WHERE status NOT IN ($this->dead_stat) ";
  855. if(!empty($searchkey)){
  856. $this->sql.=" AND (name_last $sql_LIKE '$searchkey%'
  857. OR name_first $sql_LIKE '$searchkey%'
  858. OR pid $sql_LIKE '$searchkey' )";
  859. }
  860. $this->sql.=" ORDER BY $order_item $oder_dir";
  861. if($this->res['pers']=$db->Execute($this->sql)){
  862. if($this->rec_count=$this->res['pers']->RecordCount()) {
  863. return $this->res['pers'];
  864. }else{return false;}
  865. }else{return false;}
  866. */
  867. }
  868. /**
  869. * Searches and returns a block list of persons based on search key.
  870. *
  871. * The following can be set:
  872. * - maximum number of rows in the returned list
  873. * - beginning row offset
  874. * - Field name for sorting
  875. * - Sort direction
  876. * - A boolean flag to include the first name in searching
  877. *
  878. * The returned adodb record object contains rows of arrays.
  879. * Each array contains the encounter data with the following index keys:
  880. * - pid = the PID number
  881. * - name_last = person's last or family name
  882. * - name_first = person's first or given name
  883. * - date_birth = date of birth in YYYY-mm-dd format
  884. * - sex = sex
  885. * - death_date = The date the person died (if applicable)
  886. * - addr_zip = Address zip code
  887. * - status = Record status
  888. *
  889. * @access public
  890. * @param string Search keyword
  891. * @param string Sort by the item name, default = name_last (last/family name)
  892. *'@param string Sort direction, default = ASC (ascending)
  893. * @return mixed integer or boolean
  894. */
  895. function SearchSelect($searchkey='',$maxcount=100,$offset=0,$oitem='name_last',$odir='ASC',$fname=FALSE){
  896. global $db, $sql_LIKE;
  897. //$db->debug=true;
  898. if(empty($maxcount)) $maxcount=100;
  899. if(empty($offset)) $offset=0;
  900. include_once(CARE_BASE .'include/helpers/inc_date_format_functions.php');
  901. // convert * and ? to % and &
  902. $searchkey=strtr($searchkey,'*?','%_');
  903. $searchkey=trim($searchkey);
  904. $suchwort=$searchkey;
  905. if(is_numeric($suchwort)) {
  906. $suchwort=(int) $suchwort;
  907. //$numeric=1;
  908. $this->is_nr=TRUE;
  909. //if($suchwort<$GLOBAL_CONFIG['person_id_nr_adder']){
  910. // $suchbuffer=(int) ($suchwort + $GLOBAL_CONFIG['person_id_nr_adder']) ;
  911. //}
  912. if(empty($oitem)) $oitem='pid';
  913. if(empty($odir)) $odir='DESC'; // default, latest pid at top
  914. $sql2=" WHERE pid=$suchwort ";
  915. } else {
  916. // Try to detect if searchkey is composite of first name + last name
  917. if(stristr($searchkey,',')){
  918. $lastnamefirst=TRUE;
  919. }else{
  920. $lastnamefirst=FALSE;
  921. }
  922. $searchkey=strtr($searchkey,',',' ');
  923. $cbuffer=explode(' ',$searchkey);
  924. // Remove empty variables
  925. for($x=0;$x<sizeof($cbuffer);$x++){
  926. $cbuffer[$x]=trim($cbuffer[$x]);
  927. if($cbuffer[$x]!='') $comp[]=$cbuffer[$x];
  928. }
  929. // Arrange the values, ln= lastname, fn=first name, bd = birthday
  930. if($lastnamefirst){
  931. $fn=$comp[1];
  932. $ln=$comp[0];
  933. $bd=$comp[2];
  934. }else{
  935. $fn=$comp[0];
  936. $ln=$comp[1];
  937. $bd=$comp[2];
  938. }
  939. // Check the size of the comp
  940. if(sizeof($comp)>1){
  941. $sql2=" WHERE (name_last $sql_LIKE '".strtr($ln,'+',' ')."%' AND name_first $sql_LIKE '".strtr($fn,'+',' ')."%')";
  942. if(!empty($bd)){
  943. $DOB=@formatDate2STD($bd,$date_format);
  944. if($DOB=='') {
  945. $sql2.=" AND date_birth $sql_LIKE '$bd%' ";
  946. }else{
  947. $sql2.=" AND date_birth = '$DOB' ";
  948. }
  949. }
  950. }else{
  951. // Check if * or %
  952. if($suchwort=='%'||$suchwort=='%%'){
  953. $sql2=" WHERE status NOT IN ($this->dead_stat)";
  954. }else{
  955. // Check if it is a complete DOB
  956. $DOB=@formatDate2STD($suchwort,$date_format);
  957. if($DOB=='') {
  958. if(defined('SHOW_FIRSTNAME_CONTROLLER')&&SHOW_FIRSTNAME_CONTROLLER){
  959. if($fname){
  960. $sql2=" WHERE name_last $sql_LIKE '".strtr($suchwort,'+',' ')."%' OR name_first $sql_LIKE '".strtr($suchwort,'+',' ')."%'";
  961. }else{
  962. $sql2=" WHERE name_last $sql_LIKE '".strtr($suchwort,'+',' ')."%' ";
  963. }
  964. }else{
  965. $sql2=" WHERE name_last $sql_LIKE '".strtr($suchwort,'+',' ')."%' ";
  966. }
  967. }else{
  968. $sql2=" WHERE date_birth = '$DOB'";
  969. }
  970. $sql2.=" AND status NOT IN ($this->dead_stat) ";
  971. }
  972. }
  973. }
  974. $this->buffer=$this->tb_person.$sql2;
  975. // Save the query in buffer for pagination
  976. //$this->buffer=$fromwhere;
  977. //$sql2.=' AND status NOT IN ("void","hidden","deleted","inactive") ORDER BY '.$oitem.' '.$odir;
  978. // Set the sorting directive
  979. if(isset($oitem)&&!empty($oitem)) $sql3 =" ORDER BY $oitem $odir";
  980. $this->sql='SELECT pid, name_last, name_first, date_birth, addr_zip, sex, death_date, status FROM '.$this->buffer.$sql3;
  981. if($this->res['ssl']=$db->SelectLimit($this->sql,$maxcount,$offset)){
  982. if($this->rec_count=$this->res['ssl']->RecordCount()) {
  983. return $this->res['ssl'];
  984. }else{return false;}
  985. }else{return false;}
  986. }
  987. /**
  988. * Checks if the person is currently employed in this hospital.
  989. *
  990. * If currently employed the employee number is returned, else FALSE.
  991. * @access public
  992. * @param int PID number
  993. * @return mixed integer or boolean
  994. */
  995. function CurrentEmployment($pid){
  996. global $db;
  997. if(!$pid) return false;
  998. $this->sql="SELECT nr FROM $this->tb_employ
  999. WHERE pid='$pid' AND is_discharged IN ('',0) AND status NOT IN ($this->dead_stat)";
  1000. if($buf=$db->Execute($this->sql)){
  1001. if($buf->RecordCount()){
  1002. $buf2=$buf->FetchRow();
  1003. return $buf2['nr'];
  1004. }else{return false;}
  1005. }else{return false;}
  1006. }
  1007. /**
  1008. * Sets death information.
  1009. *
  1010. * The data must be passed by reference with associative array.
  1011. * Data array must have the following index keys.
  1012. * - 'death_date' = date of death
  1013. * - 'death_encounter_nr' = encounter number in case person died during that encounter
  1014. * - 'death_cause' = text of death cause
  1015. * - 'death_cause_code' = code of death cause (if available)
  1016. * - 'history' = text to be appended to "history" item
  1017. * - 'modify_id' = name of user
  1018. * - 'modify_time' = time of this modification in yyyymmddhhMMss format
  1019. *
  1020. * @access public
  1021. * @param int PID number
  1022. * @param array Death information.
  1023. * @return mixed integer or boolean
  1024. */
  1025. function setDeathInfo($pid,&$data){
  1026. $this->setDataArray($data);
  1027. $this->setWhereCondition("pid=$pid");
  1028. return $this->updateDataFromInternalArray($pid);
  1029. }
  1030. /**
  1031. * Returns the PID ('nr' of a column) based on OID key
  1032. *
  1033. * Special for postgresql or dbms that returns an OID key after an insert
  1034. *
  1035. * @access public
  1036. * @param int OID return insert key of a column
  1037. * @return mixed integer or boolean
  1038. */
  1039. function postgre_PIDbyOID($oid=0){
  1040. if(!$oid) return false;
  1041. else return $this->postgre_Insert_ID($this->tb_person,'pid',$oid);
  1042. }
  1043. /**
  1044. * returns basic data of living person(s) based on family name, first name & b-day
  1045. *
  1046. * @access public
  1047. * @param array The data keys
  1048. * @param boolean Flags if non-living persons are also returned. Default = FALSE
  1049. * @return mixed array or boolean
  1050. */
  1051. function PIDbyData(&$data,$deadtoo=FALSE){
  1052. global $db, $sql_LIKE, $dbf_nodate;
  1053. $this->sql="SELECT pid,name_last,name_first,date_birth,sex FROM $this->tb_person WHERE name_last $sql_LIKE '".$data['name_last']."'
  1054. AND name_first $sql_LIKE '".$data['name_first']."'
  1055. AND date_birth='".$data['date_birth']."'
  1056. AND sex $sql_LIKE '".$data['sex']."'";
  1057. if(!$deadtoo) $this->sql.=" AND death_date='$dbf_nodate'";
  1058. if($res['pbd']=$db->Execute($this->sql)){
  1059. if($res['pbd']->RecordCount()) {
  1060. return $res['pbd'];//
  1061. }else{return false;}
  1062. }else{return false;}
  1063. }
  1064. /**
  1065. * Sets the filename if the person in the databank
  1066. *
  1067. * @access public
  1068. * @param int PID number
  1069. * @param string Filename
  1070. * @return mixed string or boolean
  1071. */
  1072. function setPhotoFilename($pid='',$fn=''){
  1073. global $db, $_SESSION;
  1074. if(empty($pid)||empty($fn)) return false;
  1075. if(!$this->internResolvePID($pid)) return false;
  1076. $this->sql="UPDATE $this->tb_person SET photo_filename='$fn',
  1077. history=".$this->ConcatHistory("\nPhoto set ".date('Y-m-d H:i:s')." = ".$_SESSION['sess_user_name'])." WHERE pid=$this->pid";
  1078. return $this->Transact($this->sql);
  1079. }
  1080. }
  1081. ?>