PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/default/LBF/LBFgcac.plugin.php

https://github.com/md-tech/openemr
PHP | 288 lines | 203 code | 30 blank | 55 comment | 80 complexity | d96c51c7cc1828e6ea457368f93aec60 MD5 | raw file
  1. <?php
  2. // Copyright (C) 2009-2011 Rod Roark <rod@sunsetsystems.com>
  3. //
  4. // This program is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU General Public License
  6. // as published by the Free Software Foundation; either version 2
  7. // of the License, or (at your option) any later version.
  8. // This provides enhancement functions for the LBFgcac visit form.
  9. // It is invoked by interface/forms/LBF/new.php.
  10. // Private function. Constructs a query to find a given lbf_data field's
  11. // values from visits within the past 2 weeks.
  12. //
  13. function _LBFgcac_query_recent($more) {
  14. global $pid, $encounter, $formname, $formid;
  15. // Get the date of this visit.
  16. $encrow = sqlQuery("SELECT date FROM form_encounter WHERE " .
  17. "pid = '$pid' AND encounter = '$encounter'");
  18. $encdate = $encrow['date'];
  19. // Query complications from the two weeks prior to this visit.
  20. $query = "SELECT d.field_value " .
  21. "FROM forms AS f, form_encounter AS fe, lbf_data AS d " .
  22. "WHERE f.pid = '$pid' AND " .
  23. "f.formdir = '$formname' AND " .
  24. "f.deleted = 0 AND " .
  25. "fe.pid = f.pid AND fe.encounter = f.encounter AND " .
  26. "fe.date <= '$encdate' AND " .
  27. "DATE_ADD(fe.date, INTERVAL 14 DAY) > '$encdate' AND " .
  28. "d.form_id = f.form_id AND $more";
  29. return $query;
  30. }
  31. // Private function. Given a field name, gets its value from the most
  32. // recent instance of this form type that is not more than 2 weeks old.
  33. //
  34. function _LBFgcac_recent_default($name) {
  35. global $formid;
  36. // This logic only makes sense for a new form.
  37. if ($formid) return '';
  38. $query = _LBFgcac_query_recent(
  39. "d.field_id = '$name' " .
  40. "ORDER BY f.form_id DESC LIMIT 1");
  41. $row = sqlQuery($query);
  42. if (empty($row['field_value'])) return '';
  43. return $row['field_value'];
  44. }
  45. // Private function. Query services within 2 weeks of this encounter.
  46. //
  47. function _LBFgcac_query_recent_services() {
  48. global $pid, $encounter;
  49. // Get the date of this visit.
  50. $encrow = sqlQuery("SELECT date FROM form_encounter WHERE " .
  51. "pid = '$pid' AND encounter = '$encounter'");
  52. $encdate = $encrow['date'];
  53. // Query services from the two weeks prior to this visit.
  54. $query = "SELECT c.related_code " .
  55. "FROM form_encounter AS fe, billing AS b, codes AS c " .
  56. "WHERE fe.pid = '$pid' AND fe.date <= '$encdate' AND " .
  57. "DATE_ADD(fe.date, INTERVAL 14 DAY) > '$encdate' AND " .
  58. "b.pid = fe.pid AND b.encounter = fe.encounter AND b.activity = 1 AND " .
  59. "b.code_type = 'MA' AND c.code_type = '12' AND " .
  60. "c.code = b.code AND c.modifier = b.modifier " .
  61. "ORDER BY fe.date DESC, b.id DESC";
  62. return $query;
  63. }
  64. // Private function. Query services from this encounter.
  65. //
  66. function _LBFgcac_query_current_services() {
  67. global $pid, $encounter;
  68. $query = "SELECT c.related_code " .
  69. "FROM billing AS b, codes AS c WHERE " .
  70. "b.pid = '$pid' AND b.encounter = '$encounter' AND b.activity = 1 AND " .
  71. "b.code_type = 'MA' AND c.code_type = '12' AND " .
  72. "c.code = b.code AND c.modifier = b.modifier " .
  73. "ORDER BY b.id DESC";
  74. return $query;
  75. }
  76. // The purpose of this function is to create JavaScript for the <head>
  77. // section of the page. This in turn defines desired javaScript
  78. // functions.
  79. //
  80. function LBFgcac_javascript() {
  81. global $formid;
  82. // Query complications from the two weeks prior to this visit.
  83. $res = sqlStatement(_LBFgcac_query_recent(
  84. "f.form_id != '$formid' AND " .
  85. "d.field_id = 'complications'"));
  86. // This JavaScript function is to enable items in the "Main complications"
  87. // list that have been selected, and to disable all others.
  88. // Option.disabled seems to work for Firefox 3 and IE8 but not IE6.
  89. echo "// Enable recent complications and disable all others.
  90. function set_main_compl_list() {
  91. var f = document.forms[0];
  92. var sel = f.form_main_compl;
  93. var n = '';
  94. ";
  95. // We use the checkbox object values as a scratch area to note which
  96. // complications were already selected from other forms.
  97. while ($row = sqlFetchArray($res)) {
  98. $a = explode('|', $row['field_value']);
  99. foreach ($a as $complid) {
  100. if (empty($complid)) continue;
  101. echo " n = 'form_complications[$complid]'; if (f[n]) f[n].value = 2;\n";
  102. }
  103. }
  104. echo " // Scan the list items and set their disabled flags.
  105. for (var i = 1; i < sel.options.length; ++i) {
  106. n = 'form_complications[' + sel.options[i].value + ']';
  107. sel.options[i].disabled = (f[n] && (f[n].checked || f[n].value == '2')) ? false : true;
  108. }
  109. }
  110. ";
  111. echo "
  112. // Disable most form fields if refusing abortion.
  113. function client_status_changed() {
  114. var f = document.forms[0];
  115. var dis1 = false; // true to disable complications
  116. var dis2 = false; // true to disable procedures
  117. var cs = f.form_client_status;
  118. var csval = '';
  119. if (cs.type) { // cs = select list
  120. if (cs.selectedIndex >= 0) {
  121. csval = cs.options[cs.selectedIndex].value;
  122. }
  123. }
  124. else { // cs = array of radio buttons
  125. for (var i = 0; i < cs.length; ++i) {
  126. if (cs[i].checked) {
  127. csval = cs[i].value;
  128. break;
  129. }
  130. }
  131. }
  132. if (csval == 'mara' || csval == 'defer' || csval == 'refin') {
  133. dis1 = true;
  134. dis2 = true;
  135. }
  136. else if (csval == 'maaa') {
  137. dis2 = true;
  138. }
  139. for (var i = 0; i < f.elements.length; ++i) {
  140. var e = f.elements[i];
  141. if (e.name.substring(0,18) == 'form_complications' || e.name == 'form_main_compl') {
  142. e.disabled = dis1;
  143. }
  144. else if (e.name == 'form_in_ab_proc') {
  145. e.disabled = dis2;
  146. }
  147. else if (e.name == 'form_ab_location') {
  148. if (csval == 'maaa') {
  149. e.disabled = (e.value == 'part' || e.value == 'oth' || e.value == 'na');
  150. }
  151. else if (csval == 'mara' || csval == 'defer' || csval == 'self') {
  152. e.disabled = true; // (e.value != 'na');
  153. }
  154. // else if (csval == 'refout') {
  155. // e.disabled = (e.value == 'proc' || e.value == 'ma');
  156. // }
  157. else { // inbound referral
  158. e.disabled = (e.value == 'na' || e.value == 'proc' || e.value == 'ma');
  159. }
  160. }
  161. else if (e.name == 'form_gc_rreason') {
  162. e.disabled = (csval != 'mara' && csval != 'refout');
  163. }
  164. }
  165. }
  166. ";
  167. echo "
  168. // Enable some form fields before submitting.
  169. // This is because disabled fields do not submit their data, however
  170. // we do want to save the default values that were set for them.
  171. function mysubmit() {
  172. var f = document.forms[0];
  173. for (var i = 0; i < f.elements.length; ++i) {
  174. var e = f.elements[i];
  175. if (e.name == 'form_in_ab_proc') {
  176. e.disabled = false;
  177. }
  178. }
  179. top.restoreSession();
  180. return true;
  181. }
  182. ";
  183. }
  184. // The purpose of this function is to create JavaScript that is run
  185. // once when the page is loaded.
  186. //
  187. function LBFgcac_javascript_onload() {
  188. echo "
  189. set_main_compl_list();
  190. client_status_changed();
  191. var f = document.forms[0];
  192. for (var i = 0; i < f.elements.length; ++i) {
  193. var e = f.elements[i];
  194. if (e.name.substring(0,18) == 'form_complications')
  195. e.onclick = function () { set_main_compl_list(); };
  196. }
  197. var cs = f.form_client_status;
  198. if (cs.type) { // cs = select list
  199. cs.onchange = function () { client_status_changed(); };
  200. }
  201. else { // cs = array of radio buttons
  202. for (var i = 0; i < cs.length; ++i) {
  203. cs[i].onclick = function () { client_status_changed(); };
  204. }
  205. }
  206. f.onsubmit = function () { return mysubmit(); };
  207. ";
  208. }
  209. // Generate default for client status.
  210. //
  211. function LBFgcac_default_client_status() {
  212. return _LBFgcac_recent_default('client_status');
  213. }
  214. // Generate default for visit type. If there are no recent prior visits,
  215. // then default to new procedure.
  216. //
  217. function LBFgcac_default_ab_location() {
  218. global $formid;
  219. if ($formid) return '';
  220. $vt = _LBFgcac_recent_default('ab_location');
  221. if (empty($vt)) return 'proc';
  222. return $vt;
  223. }
  224. // Generate default for the induced procedure type.
  225. //
  226. function LBFgcac_default_in_ab_proc() {
  227. // Check previous GCAC visit forms for this setting.
  228. $default = _LBFgcac_recent_default('in_ab_proc');
  229. if ($default !== '') return $default;
  230. // If none, query services from recent visits to see if an IPPF code
  231. // matches that of a procedure type in the list.
  232. $res = sqlStatement(_LBFgcac_query_recent_services());
  233. while ($row = sqlFetchArray($res)) {
  234. if (empty($row['related_code'])) continue;
  235. $relcodes = explode(';', $row['related_code']);
  236. foreach ($relcodes as $codestring) {
  237. if ($codestring === '') continue;
  238. list($codetype, $code) = explode(':', $codestring);
  239. if ($codetype !== 'IPPF') continue;
  240. $lres = sqlStatement("SELECT option_id, mapping FROM list_options " .
  241. "WHERE list_id = 'in_ab_proc'");
  242. while ($lrow = sqlFetchArray($lres)) {
  243. $maparr = explode(':', $lrow['mapping']);
  244. if (empty($maparr[1])) continue;
  245. if (preg_match('/^' . $maparr[1] . '/', $code)) {
  246. return $lrow['option_id'];
  247. }
  248. }
  249. } // end foreach
  250. }
  251. return '';
  252. }
  253. // Generate default for the main complication.
  254. //
  255. function LBFgcac_default_main_compl() {
  256. return _LBFgcac_recent_default('main_compl');
  257. }
  258. ?>