A
download class.EMRModule.php
Language: PHP
LOC: 716
Project Info
FreeMED Project(freemed)
Server: SourceForge
Type: cvs
...reemed\freemed\freemed\lib\
   acl.php
   API.php
   bcadd.php
   calendar-functions.php
   class.AdminModule.php
   class.Agata.php
   class.Authorizations.php
   class.BaseModule.php
   class.BillingModule.php
   class.CalendarModule.php
   class.ClaimLog.php
   class.Coverage.php
   class.Debug.php
   class.diagnosis_set.php
   class.Djvu.php
   class.EMRModule.php
   class.Fax.php
   class.FixedFormEntry.php
   ...s.FixedFormRenderer.php
   class.FormRenderer.php
   class.FreeMEDSelfTest.php
   class.GeneralConfig.php
   class.GraphModule.php
   class.Guarantor.php
   class.Handler_HL7v2.php
   ...s.Handler_HL7v2_A04.php
   ...s.Handler_HL7v2_A08.php
   ...s.Handler_HL7v2_S12.php
   ...s.Handler_HL7v2_S15.php
   class.InsuranceCompany.php
   class.LanguageRegistry.php
   class.Ledger.php
   ...s.MaintenanceModule.php
   class.Messages.php
   class.OMBS_Patient.php
   class.OMBS_Wrapper.php
   class.Parser_HL7v2.php
   class.Patient.php
   class.Payer.php
   class.PHPlot.php
   class.Physician.php
   class.Procedure.php
   class.RecordLock.php
   class.Remitt.php
   class.ReportsModule.php
   class.rxlist.php
   class.Scheduler.php
   class.TeX.php
   class.User.php
   class.UtilityModule.php
   class.vCalendar.php
   class.vCalendarEvent.php
   error_handler.php
   freemed.php
   i18n.php
   iso-set.php
   macros.php
   mail-functions.php
   settings.php
   settings.php.tmpl
   xml.php
   xmlrpc_services.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
<?php
 // $Id: class.EMRModule.php,v 1.48 2005/07/14 18:19:29 rufustfirefly Exp $
 // desc: module prototype
 // lic : GPL, v2

LoadObjectDependency('_FreeMED.BaseModule');

// Class: FreeMED.EMRModule
//
//	Electronic Medical Record module superclass. It is descended from
//	<BaseModule>.
//
class EMRModule extends BaseModule {

	// override variables
	var $CATEGORY_NAME = "Electronic Medical Record";
	var $CATEGORY_VERSION = "0.4";

	// Variable: $this->widget_hash
	//
	//	Specifies the format of the <widget> method. This is
	//	formatted by having SQL field names surrounded by '##'s.
	//
	// Example:
	//
	//	$this->widget_hash = '##phylname##, ##phyfname##';
	//	
	var $widget_hash;

	// vars to be passed from child modules
	var $order_fields;
	var $form_vars;
	var $table_name;

	// Variable: disable_patient_box
	//
	//	Whether or not to disable the patient box display at
	//	the top of the screen. Defaults to false.
	//
	var $disable_patient_box = false;

	// Variable: date_field
	//
	//	Field name that contains the date pertaining to the
	//	EMR record fragment in question. This is used by
	//	certain routines in FreeMED to guess as to the most
	//	accurate piece of information for a particular
	//	query.
	//
	// Example:
	//
	//	$this->date_field = 'rxdtfrom';
	//
	var $date_field;

	// Variable: patient_field
	//
	//	Field name that describes the patient. This is used by
	//	FreeMED's module handler to determine whether a record
	//	should be displayed in the EMR summary screen.
	//
	// Example:
	//
	//	$this->patient_field = 'eocpatient';
	//
	var $patient_field; // the field that links to the patient ID

	// Variable: display_format
	//
	//	Hash describing the format which is used to display the
	//	current record by default. It needs to be overridden by
	//	child classes. It uses '##' seperated values to signify
	//	variables.
	//
	// Example:
	//
	//	$this->display_format = '##phylname##, ##phyfname##';
	//
	var $display_format;

	// Variable: summary_conditional
	//
	//	An SQL logical phrase which is used to pare down the
	//	results from a summary view query.
	//
	// Example:
	//
	//	$this->summary_conditional = "ptsex = 'm'";
	var $summary_conditional = '';

	// Variable: summary_query_link
	//
	//	Allows another table (or more) to be "linked" via a
	//	WHERE clause.
	//
	// Example:
	//
	//	$this->summary_query_link = array ('payrecproc', 'procrec');
	//
	// See Also:
	//	<summary>
	//
	var $summary_query_link = false;

	// Variable: summary_order_by
	//
	//	The order in which the EMR summary items are displayed.
	//	This is passed verbatim to an SQL "ORDER BY" clause, so
	//	DESC can be used. Defaults to 'id' if nothing else is
	//	specified.
	//
	// Example:
	//
	//	$this->summary_order_by = 'eocdtlastsimilar, eocstate DESC';
	//
	// See Also:
	//	<summary>
	//
	var $summary_order_by = 'id';

	// Variable: $this->rpc_field_map
	//
	//	Specifies the format of the XML-RPC structures returned by
	//	the FreeMED.DynamicModule.picklist method. These are
	//	passed as key => value, where key is the target name of the
	//	structure item and value is the name of the SQL field. "id"
	//	is passed as "id" by default, as <$this->patient_field> is
	//	passed as "patient". If this is not defined,
	//	FreeMED.DynamicModule.picklist will fail for the target
	//	module.
	//
	// Example:
	//
	//	$this->rpc_field_map = array ( 'last_name' => 'ptlname' );
	//
	var $rpc_field_map;

	// contructor method
	function EMRModule () {
		// Check for patient, if so, then set _ref appropriately
		if ($GLOBALS['patient'] > 0) {
			$GLOBALS['_ref'] = "manage.php?id=".urlencode($GLOBALS['patient']);
		}

		// Add meta information for patient_field, if it exists
		if (isset($this->date_field)) {
			$this->_SetMetaInformation('date_field', $this->date_field);
		}
		if (isset($this->patient_field)) {
			$this->_SetMetaInformation('patient_field', $this->patient_field);
		}
		if (isset($this->table_name)) {
			$this->_SetMetaInformation('table_name', $this->table_name);
		}
		if (!empty($this->widget_hash)) {
			$this->_SetMetaInformation('widget_hash', $this->widget_hash);
		}
		if (!empty($this->rpc_field_map)) {
			$this->_SetMetaInformation('rpc_field_map', $this->rpc_field_map);
		}

		// Call parent constructor
		$this->BaseModule();
	} // end function EMRModule

	// override check_vars method
	function check_vars ($nullvar = "") {
		global $module, $patient;
		if (!isset($module)) {
			trigger_error("No Module Defined", E_ERROR);
		}
		if ($patient < 1) {
			trigger_error( "No Patient Defined", E_ERROR);
		}
		// check access to patient
		if (!freemed::check_access_for_patient($patient)) {
			trigger_error("User not Authorized for this function", E_USER_ERROR);
		}
		return true;
	} // end function check_vars

	// Method: locked
	//
	// 	Determines if record id is locked or not.
	//
	// Parameters:
	//
	//	$id - Record id to be checked
	//
	//	$quiet - (optional) Boolean. If set to true, this value
	//	will cause a denial screen to be displayed. Defaults to
	//	false.
	//
	// Returns:
	//
	//	Boolean, whether the record is locked or not.
	//
	// Example:
	//
	//	if ($this->locked($id)) return false;
	//
	function locked ($id, $quiet = false) {
		global $sql, $display_buffer;
		static $locked;

		// If there is no table_name, we can skip this altogether
		if (empty($this->table_name)) { return false; }

		if (!isset($locked)) {
			$query = "SELECT * FROM ".$this->table_name." WHERE ".
				"id='".addslashes($id)."' AND (locked > 0)";
			$result = $sql->query($query);
			$locked = $sql->results($result);
		}

		if ($locked) {
			if (!$quiet) 
			$display_buffer .= "
			<div ALIGN=\"CENTER\">

			</div>

			<p/>

			<div ALIGN=\"CENTER\">
			".(
				($GLOBALS['return'] == "manage") ?
				"<a href=\"manage.php?id=".urlencode($GLOBALS['patient']).
					"\">".__("Manage Patient")."</a>" :
				"<a href=\"module_loader.php?module=".
					get_class($this)."\">".__("back")."</a>"
			)."
			</div>
			";
			return true;
		} else {
			return false;
		}
	} // end function locked

	// function main
	// - generic main function
	function main ($nullvar = "") {
		global $display_buffer;
		global $action, $patient, $__submit, $return;

		if ($action=='print') {
			$this->disable_patient_box = true;
		}

		// Pull current patient from session if needed
		if (!isset($patient)) {
			$patient = $_SESSION['current_patient'];
		}

		if (!isset($this->this_patient))
			$this->this_patient = CreateObject('FreeMED.Patient', $patient);
		if (!isset($this->this_user))
			$this->this_user    = CreateObject('FreeMED.User');

		// display universal patient box
		if (!$this->disable_patient_box) {
			$display_buffer .= freemed::patient_box($this->this_patient)."<p/>\n";
		}

		// Kludge for older "submit" actions
		if (!isset($__submit)) { $__submit = $GLOBALS['submit']; }

		// Handle cancel action from __submit
		if ($__submit==__("Cancel")) {
			// Unlock record, if it is locked
			$__lock = CreateObject('_FreeMED.RecordLock', $this->table_name);
			$__lock->UnlockRow ( $_REQUEST['id'] );

			if ($return=="manage") {
			Header("Location: manage.php?".
				"id=".urlencode($patient));
			} else {
			Header("Location: ".$this->page_name.
				"?module=".urlencode($this->MODULE_CLASS).
				"&patient=".urlencode($patient));
			}
			die("");
		}

		switch ($action) {
			case "add":
				if (!$this->acl_access('add', $patient)) {
					trigger_error(__("You do not have access to do that."), E_USER_ERROR);
				}
				$this->add();
				break;

			case "addform":
				if (!$this->acl_access('add', $patient)) {
					trigger_error(__("You do not have access to do that."), E_USER_ERROR);
				}
				$this->addform();
				break;

			case "del":
			case "delete":
				if (!$this->acl_access('delete', $patient)) {
					trigger_error(__("You do not have access to do that."), E_USER_ERROR);
				}
				$this->in_use($_REQUEST['id']);
				$this->del();
				break;

			case "lock":
				if (!$this->acl_access('lock', $patient)) {
					trigger_error(__("You do not have access to do that."), E_USER_ERROR);
				}
				$this->lock();
				break;

			case "mod":
			case "modify":
				if (!$this->acl_access('modify', $patient)) {
					trigger_error(__("You do not have access to do that."), E_USER_ERROR);
				}
				$this->in_use($_REQUEST['id']);
				$this->mod();
				break;

			case "modform":
				if (!$this->acl_access('modify', $patient)) {
					trigger_error(__("You do not have access to do that."), E_USER_ERROR);
				}
				$this->in_use($_REQUEST['id']);
				$this->modform();
				break;

			case "print":
				$this->printaction();
				break;

			case "display";
				if (!$this->acl_access('view', $patient)) {
					trigger_error(__("You do not have access to do that."), E_USER_ERROR);
				}
				$this->display();
				break;

			case "view":
			default:
				if (!$this->acl_access('view', $patient)) {
					trigger_error(__("You do not have access to do that."), E_USER_ERROR);
				}
				$this->view();
				break;
		} // end switch action
	} // end function main

	// Method: additional_move
	//
	//	Stub function. Define additional EMR movement functionality
	//	per module. Note that this function does *not* perform the
	//	actual move, but instead moves support files, et cetera.
	//
	// Parameters:
	//
	//	$id - Id of the record in question
	//
	//	$from - Original patient
	//
	//	$to - Destination patient
	//
	function additional_move ($id, $from, $to) { }

	// Method: acl_access
	//
	//	Should be overridden by any module which needs different
	//	access checks.
	//
	function acl_access ( $type, $patient ) { 
		return freemed::acl_patient('emr', $type, $patient);
	} // end method acl_access
	
	function display_message () {
		global $display_buffer;
		if (isset($this->message)) {
			$display_buffer .= "
			<p/>
			<div ALIGN=\"CENTER\">
			<b>".prepare($this->message)."</b>
			</div>
			";
		}
	} // end function display_message

	// Method: form_table
	//
	//	Builds the table used by the add/mod form methods, and
	//	returns it as an associative array which is passed to
	//	<html_form::form_table>. By default this returns NULL
	//	and needs to be overridden by child classes. It is only
	//	used if the default <form> method is used.
	//
	// Returns:
	//
	//	Associative array describing form.
	//
	// See Also:
	//	<form>
	//
	function form_table () {
		return NULL;
	} // end function form_table

	function in_use ( $id ) {
		// Check for record locking
		$lock = CreateObject('_FreeMED.RecordLock', $this->table_name);
		if ($lock->IsLocked($id)) {
			trigger_error(__("This record is currently in use."), E_USER_ERROR);
		} else {
			// Add record lock
			$lock->LockRow( $id );
		}
	} // end method in_use

	// ********************** MODULE SPECIFIC ACTIONS *********************

	// function add
	// - addition routine
	function add () { $this->_add(); }
	function _add ($_param = NULL) {
		global $display_buffer;
		foreach ($GLOBALS as $k => $v) global ${$k};

		if (is_array($_param)) {
			foreach ($_param AS $k => $v) {
				global ${$k}; ${$k} = $v;
				$_REQUEST[$k] = $v;
			}
		}

		$this->_preadd($_param);
		$this->{get_class($this)}();

		$result = $GLOBALS['sql']->query (
			$GLOBALS['sql']->insert_query (
				$this->table_name,
				$this->variables
			)
		);
		$new_id = $GLOBALS['sql']->last_record($result, $this->table_name);

		if ($result) {
			$this->message = __("Record added successfully.");
			if (is_array($_param)) { return $new_id; }
		} else {
			$this->message = __("Record addition failed.");
			if (is_array($_param)) { return false; }
		}
		$this->view(); $this->display_message();

		// Check for return to management screen
		if ($GLOBALS['return'] == 'manage') {
			global $refresh, $patient;
			$refresh = "manage.php?id=".urlencode($patient);
			Header('Location: '.$refresh); die();
		}

		// If called without 'return', send back new id
		return $new_id;
	} // end function _add
	function _preadd ( $param = NULL ) { }

	// function del
	// - delete function
	function del () { $this->_del(); }
	function _del ($_id = -1) {
		global $display_buffer;
		global $id, $sql;

		// Pull from parameter, if given
		if ($_id > 0) { $id = $_id; }

		// Check for record locking

		// If there is an override ...
		if (!freemed::lock_override()) {
			if ($this->locked($id)) return false;
		}

		$query = "DELETE FROM ".$this->table_name." ".
			"WHERE id = '".prepare($id)."'";
		$result = $sql->query ($query);
		if ($result) {
			$this->message = __("Record deleted successfully.");
			if ($_id > 0) { return true; }
		} else {
			$this->message = __("Record deletion failed.");
			if ($_id > 0) { return false; }
		}
		$this->view(); $this->display_message();

		// Check for return to management screen
		if ($GLOBALS['return'] == 'manage') {
			global $refresh, $patient;
			$refresh = "manage.php?id=".urlencode($patient);
			Header('Location: '.$refresh); die();
		}
	} // end function _del

	// function mod
	// - modification function
	function mod () { $this->_mod(); }
	function _mod ($_param = NULL) {
		global $display_buffer;
		foreach ($GLOBALS as $k => $v) global $$k;

		if (is_array($_param)) {
			foreach ($_param AS $k => $v) {
				global ${$k};
				${$k} = $v;
			}
		}

		// Check for record locking
		if (!freemed::lock_override()) {
			if ($this->locked($id)) return false;
		}
		$__lock = CreateObject('_FreeMED.RecordLock', $this->table_name);
		if ($__lock->IsLocked($id)) {
			$this->message = __("Record modification failed due to record lock.");
			if (is_array($_param)) { return false; }
			$this->view(); $this->display_message();
		}

		$result = $sql->query (
			$sql->update_query (
				$this->table_name,
				$this->variables,
				array (
					"id" => $id
				)
			)
		);

		// Unlock row, since update is done
		$__lock->UnlockRow( $id );

		if ($result) {
			$this->message = __("Record modified successfully.");
			if (is_array($_param)) { return true; }
		} else {
			$this->message = __("Record modification failed.");
			if (is_array($_param)) { return false; }
		}
		$this->view(); $this->display_message();

		// Check for return to management screen
		if ($GLOBALS['return'] == 'manage') {
			global $refresh, $patient;
			$refresh = "manage.php?id=".urlencode($patient);
			Header('Location: '.$refresh);
			die();
		}
	} // end function _mod

	// function add/modform
	// - wrappers for form
	function addform () { $this->form(); }
	function modform () { $this->form(); }

	// function display
	// by default, a wrapper for view
	function display () { $this->view(); }

	// function form
	// - add/mod form stub
	function form () {
		global $display_buffer, $module, $action, $id, $sql, $patient;

		if (is_array($this->form_vars)) {
			reset ($this->form_vars);
			while (list ($k, $v) = each ($this->form_vars)) global ${$v};
		} // end if is array

		// Check for record locking
		if ($this->locked($id)) return false;

		// Handle additional hidden variables
		$this->form_hidden[] = $this->patient_field;
		if ($this->patient_field != 'id') {
			$_REQUEST[$this->patient_field] = $_REQUEST['patient'];
		}
		foreach ($this->form_hidden AS $k => $v) {
			if ( (($k+0)>0) or empty($k)) {
				$k = $v;
				$v = $_REQUEST[$v];
			}
			// TODO: should handle arrays, etc
			$form_hidden .= "<input type=\"hidden\" ".
				"name=\"".prepare($k)."\" ".
				"value=\"".prepare($v)."\" />\n";
		}

		switch ($action) {
			case "addform":
				break;

			case "modform":
				if ($this->table_name) {
					$r = freemed::get_link_rec($id, $this->table_name);
					foreach ($r as $k => $v) {
						global ${$k};
						${$k} = $v;
					}
				} // end checking for table name
				break;
		} // end of switch action
		
		$display_buffer .= "
		<div align=\"center\">
		<form action=\"".$this->page_name."\" method=\"post\">
		<input type=\"hidden\" name=\"module\" value=\"".
			prepare($module)."\"/>
		<input type=\"hidden\" name=\"return\" value=\"".
			prepare($GLOBALS['return'])."\"/>
		<input type=\"hidden\" name=\"action\" value=\"".
			( $action=="addform" ? "add" : "mod" )."\"/>
		<input type=\"hidden\" name=\"patient\" value=\"".
			prepare($patient)."\"/>
		<input type=\"hidden\" name=\"id\" value=\"".
			prepare($id)."\"/>
		".$form_hidden."
		".html_form::form_table($this->form_table())."
		<p/>
		<input type=\"submit\" name=\"__submit\" value=\"".
			 ( ($action=="addform") ? __("Add") : __("Modify") )."\" ".
			 "class=\"button\" />
		<input type=\"submit\" name=\"__submit\" value=\"".
			__("Cancel")."\" class=\"button\" />
		</form>
		</div>
		";
	} // end function form

	// function lock
	// - locking function
	function lock () { $this->_lock(); }
	function _lock () {
		global $display_buffer;
		foreach ($GLOBALS as $k => $v) global ${$k};

		// Check for record locking
		if ($this->locked($id)) return false;

		$result = $sql->query (
			$sql->update_query (
				$this->table_name,
				array (
					"locked" => $_SESSION['authdata']['user']
				),
				array (
					"id" => $id
				)
			)
		);
		if ($result) $this->message = __("Record locked successfully.");
			else $this->message = __("Record locking failed.");
		$this->view(); $this->display_message();

		// Check for return to management screen
		if ($GLOBALS['return'] == 'manage') {
			global $refresh, $patient;
			$refresh = "manage.php?id=".urlencode($patient);
		}
	} // end function _mod

	// function summary
	// - show summary view of last few items
	function summary ($patient, $items) {
		global $sql, $display_buffer, $patient;

		if (is_array($this->summary_query_link)) {
			foreach ($this->summary_query_link AS $my_k => $my_v) {
				// Format: field => table_name
				$_from[] = $my_v;
				$_where[] = $my_v.'.id = '.$this->table_name.'.'.$my_k;
			}
			$this->summary_query[] = $this->table_name.'.id AS __actual_id';
		}

		// get last $items results
		$query = "SELECT *".
			( (count($this->summary_query)>0) ? 
			",".join(",", $this->summary_query)." " : " " ).
			"FROM ".$this->table_name." ".
			( is_array($this->summary_query_link) ? ",".join(',',$_from).' ' : ' ' ).
			"WHERE ".$this->patient_field."='".addslashes($patient)."' ".
			($this->summary_conditional ? 'AND '.$this->summary_conditional.' ' : '' ).
			( is_array($this->summary_query_link) ? ' AND '.join(' AND ',$_where).' ' : ' ' ).
			"ORDER BY ".( (is_array($this->summary_query_link) and $this->summary_order_by == 'id') ? $this->table_name.'.' : '' ).$this->summary_order_by." DESC LIMIT ".addslashes($items);
		//if ($this->summary_query_link) { die ($query); }
		$result = $sql->query($query);

		// Check to see if there *are* any...
		if ($sql->num_rows($result) < 1) {
			// If not, let the world know
			$buffer .= "<b>".__("No data entered.")."</b>\n";
		} else { // checking for results
			// Or loop and display
			$buffer .= "
			<table WIDTH=\"100%\" CELLSPACING=\"0\"
			 CELLPADDING=\"2\" BORDER=\"0\">
			<TR>
			";

			foreach ($this->summary_vars AS $k => $v) {
				$buffer .= "
				<td VALIGN=\"MIDDLE\" CLASS=\"menubar_info\">
				<b>".prepare($k)."</b>
				</td>
				";
			} // end foreach summary_vars
			$buffer .= "
				<td VALIGN=\"MIDDLE\" CLASS=\"menubar_info\">
				<b>".__("Action")."</b>
				</td>
			</tr>
			";
			while ($r = $sql->fetch_array($result)) {
				// Deal with record.id for query_link
				if (is_array($this->summary_query_link)) {
					$r['id'] = $r['__actual_id'];
				}

				// Pull out all variables
				extract ($r);

				// Check for annotations
				if ($_anno = module_function('Annotations', 'getAnnotations', array (get_class($this), $id))) {
					$use_anno = true;
					$_anno = module_function('Annotations', 'outputAnnotations', array ($_anno));
				} else {
					$use_anno = false;
				}

				// Use $this->summary_vars
				$buffer .= "
				<tr VALIGN=\"MIDDLE\">
				";
				$first = true;
				foreach ($this->summary_vars AS $k => $v) {
					if (!(strpos($v, ":")===false)) {
						// Split it up
						list ($p1, $p2, $p3) = explode(":", $v);
					
						switch ($p2) {
						case "phy":
						case "physician":
						$p = CreateObject('FreeMED.Physician', ${$p1});
						${$v} = $p->fullName();
						break;

						case "user":
						$__u = CreateObject('FreeMED.User', ${$p1});
						${$v} = $__u->getDescription();
						break;

						default:
						// use fields ...
						${$v} = freemed::get_link_field(${$p1}, $p2, $p3);
						break;
						}
					}
					$buffer .= "
					<td VALIGN=\"MIDDLE\">
					<small>".
					( ($use_anno and $first) ?
						"<span style=\"text-decoration: underline;\" ".
						"onMouseOver=\"tooltip('".module_function('Annotations', 'prepareAnnotation', array($_anno))."');\" ".
						"onMouseOut=\"hidetooltip();\">" : "" ).
					prepare(${$v}).
					( ($use_anno and $first) ? "</span>" : "" ).
					"</small>
					</td>
					";
					$first = false;
				} // end looping through summary vars
				$buffer .= "
				<td VALIGN=\"MIDDLE\">".
				( ((!$r['locked'] > 0) or freemed::lock_override()) ?
				"\n".template::summary_modify_link($this,
				"module_loader.php?module=".
				get_class($this)."&patient=$patient&".
				"action=modform&id=".$r['id']."&return=manage") : "" ).
				// Delete option
				( (((!$r['locked'] > 0) or freemed::lock_override()) and ($this->summary_options & SUMMARY_DELETE)) ?
				"\n".template::summary_delete_link($this,
				"module_loader.php?module=".
				get_class($this)."&patient=$patient&".
				"action=del&id=".$r['id']."&return=manage") : "" ).
				( ($this->summary_options & SUMMARY_VIEW) ?
				"\n".template::summary_view_link($this,
				"module_loader.php?module=".
				get_class($this)."&patient=$patient&".
				"action=display&id=".$r['id']."&return=manage",
				($this->summary_options & SUMMARY_VIEW_NEWWINDOW)) : "" ).

				// "Lock" link for quick locking from the menu
				
				( (($this->summary_options & SUMMARY_LOCK) and
				!($r['locked'] > 0)) ?
				"\n".template::summary_lock_link($this,
				"module_loader.php?module=".
				get_class($this)."&patient=$patient&".
				"action=lock&id=".$r['id']."&return=manage") : "" ).

				// Process a "locked" link, which does nothing other
				// than display that the record is locked
				
				( (($this->summary_options & SUMMARY_LOCK) and
				($r['locked'] > 0)) ?
				"\n".template::summary_locked_link($this) : "" ).

				// Printing stuff
				( ($this->summary_options & SUMMARY_PRINT) ?
				"\n".template::summary_print_link($this,
				"module_loader.php?module=".
				get_class($this)."&patient=$patient&".
				"action=print&id=".$r['id']) : "" ).

				// Annotations
				( !($this->summary_options & SUMMARY_NOANNOTATE) ?
				"\n".template::summary_annotate_link($this,
				"module_loader.php?module=annotations&".
				"atable=".$this->table_name."&".
				"amodule=".get_class($this)."&".
				"patient=$patient&action=addform&".
				"aid=".$r['id']."&return=manage") : "" ).
				// Additional summary icon callback
				$this->additional_summary_icons ( $patient, $r['id'] ).
				"</td>
				</tr>
				";
			} // end of loop and display
			$buffer .= "</table>\n";
		} // checking if there are any results

		// Send back the buffer
		return $buffer;
	} // end function summary

	// Method: additional_summary_icons
	//
	//	Callback to allow additional summary icons to be added.
	//	Each icon should be prefixed with a linefeed (\n)
	//	character to be equally spaced in the output. By default
	//	this is just a stub.
	//
	// Parameters:
	//
	//	$patient - Patient record id
	//
	//	$id - Record id
	//
	// Returns:
	//
	//	HTML code for additional icons
	//
	function additional_summary_icons ( $patient, $id ) { return ''; }

	// Method: summary_bar
	//
	//	Produces the text for the EMR summary bar menu. By
	//	default it produces View/Manage and Add links. Override
	//	this function to change the basic EMR summary bar menu.
	//
	// Parameters:
	//
	//	$patient - Id of current patient
	//
	// Returns:
	//
	//	XHTML formatted text links.
	//
	function summary_bar ($patient) {
		return "
		<a HREF=\"module_loader.php?module=".
		get_class($this)."&patient=".urlencode($patient).
		"&return=manage\">".__("View/Manage")."</a> |
		<a HREF=\"module_loader.php?module=".
		get_class($this)."&patient=".urlencode($patient).
		"&action=addform&return=manage\">".__("Add")."</a>
		";
	} // end function summary_bar

	// function view
	// - view stub
	function view () {
		global $display_buffer;
		global $sql;
		$result = $sql->query ("SELECT ".$this->order_fields." FROM ".
			$this->table_name." ORDER BY ".$this->order_fields);
		$display_buffer .= freemed_display_itemlist (
			$result,
			"module_loader.php",
			$this->form_vars,
			array ("", __("NO DESCRIPTION")),
			"",
			"t_page"
		);
	} // end function view

	// override _setup with create_table
	function _setup () {
		if (!$this->create_table()) return false;
		return freemed_import_stock_data ($this->table_name);
	} // end function _setup

	// function create_table
	// - used to initally create SQL table
	function create_table () {
		global $sql;

		if (!isset($this->table_definition)) return false;
		$query = $sql->create_table_query(
			$this->table_name,
			$this->table_definition,
			( is_array($this->table_keys) ?
				array_merge(array("id"), $this->table_keys) :
				array("id")
			)
		);
		$result = $sql->query($query);
		return !empty($query);
	} // end function create_table

	// this function exports XML for the entire patient record
	function xml_export () {
		global $display_buffer;
		global $patient;

		if (!isset($this->this_patient))
			$this->this_patient = CreateObject('FreeMED.Patient', $patient);

		return $this->xml_generate($this->this_patient);
	} // end function EMRModule->xml_export

	function xml_generate ($patient) { return ""; } // stub 

	// ----- XML-RPC Functions -----------------------------------------

	// Method: picklist
	//
	// Parameters:
	//
	//	$criteria - Hash of criteria information. If this is to
	//	be patient specific, 'patient' must be passed with the
	//	patient id number.
	//
	// Returns:
	//
	//	Array of hashes
	//
	// See Also:
	//	<$this->rpc_field_map>
	//
	function picklist ($criteria) {
		global $sql;

		if (is_array($criteria) and $criteria['patient']>0) {
			$patient = $criteria['patient'];
		}

		if (!is_array($criteria)) {
			$c[] = $this->patient_field." = '".addslashes($criteria)."'";
			// Check for access violation from user
			$user_id = $GLOBALS['__freemed']['basic_auth_id'];
	
			if (!freemed::check_for_access($patient, $user_id)) {
				// TODO: Set to return XML-RPC error
				return false;
			}
		} else {
			foreach ($criteria AS $k => $v) {
				if ($k == 'patient') {
					$c[] = $this->patient_field." = '".addslashes($v)."'";
				} else {
					$c[] = "LOWER(".addslashes($k).") LIKE '%'".addslashes($v)."%'";
				}
			}
		}

		$result = $sql->query(
			"SELECT * FROM ".$this->table_name." ".
			"WHERE ".join(' AND ', $c)." ".
			( $this->order_fields ? "ORDER BY ".$this->order_fields : "" )
		);
		
		if (!$GLOBALS['sql']->results($result)) {
			return CreateObject('PHP.xmlrpcresp',
				CreateObject('PHP.xmlrpcval', 'none', 'string')
			);
		}

		return rpc_generate_sql_hash (
			$this->table_name,
			array_merge (
				$this->rpc_field_map,
				array (
					'id' => 'id',
					'patient' => $this->patient_field
				)
			),
			" WHERE ".join(' AND ', $c)." ".
			( $this->order_fields ? "ORDER BY ".$this->order_fields : "" )
		);
	} // end method picklist

	// Method: widget
	//
	//	Generic widget code to allow a picklist-based widget for
	//	simple modules. Should be overridden for more complex tasks.
	//
	//	This function uses $this->widget_hash, which contains field
	//	names surrounded by '##'s.
	//
	// Parameters:
	//
	//	$varname - Name of the variable that the widget's data is
	//	passed in.
	//
	//	$patient - Id of patient this deals with.
	//
	//	$conditions - (optional) Additional clauses for SQL WHERE.
	//	defaults to none.
	//
	// Returns:
	//
	//	XHTML-compliant picklist widget.
	//
	function widget ( $varname, $patient, $conditions = false ) {
		$query = "SELECT * FROM ".$this->table_name." WHERE ".
			"( ".$this->patient_field.
				" = '".addslashes($patient)."') ".
			( $conditions ? "AND ( ".$conditions." ) " : "" ).
			( $this->order_fields ? "ORDER BY ".$this->order_fields : "" );
		$result = $GLOBALS['sql']->query($query);
		$return[__("NONE SELECTED")] = "";
		while ($r = $GLOBALS['sql']->fetch_array($result)) {
			if (!(strpos($this->widget_hash, "##") === false)) {
				$key = '';
				$hash_split = explode('##', $this->widget_hash);
				foreach ($hash_split AS $_k => $_v) {
					if (!($_k & 1)) {
						$key .= prepare($_v);
					} else {
						$key .= prepare($r[$_v]);
					}
				}
			} else {
				$key = $this->widget_hash;
			}
			$return[$key] = $r['id'];
		}
		return html_form::select_widget($varname, $return);
	} // end method widget

	// Method: _recent_record
	//
	//	Return most recent record, possibly qualified by a
	//	particular date.
	//
	// Parameters:
	//
	//	$patient - Id of patient record
	//
	//	$recent_date - (optional) Date to qualify this by
	//
	// Returns:
	//
	//	Associative array (hash) of record
	//
	function _recent_record ( $patient, $recent_date = NULL ) {
		$query = "SELECT * FROM ".$this->table_name." ".
			( $recent_date ? "WHERE ".$this->date_field." <= '".addslashes($recent_date)."' " : "" ).
			"ORDER BY ".$this->date_field." DESC, id DESC";
		$res = $GLOBALS['sql']->query($query);
		return $GLOBALS['sql']->fetch_array($res);
	} // end method _recent_record

	//------ Internal Printing ----------------------------------------

	// Method: _RenderTeX
	//
	//	Internal TeX renderer for the record. By default this
	//	uses the <print_format> class variable to determine the
	//	proper format. If another format is to be used, override
	//	this class.
	//
	// Parameters:
	//
	//	$TeX - <FreeMED.TeX> object reference. Must be prefixed by
	//	an amphersand, otherwise changes will be lost!
	//
	//	$id - Record id to be printed
	//
	// Example:
	//
	//	$this->_RenderTeX ( &$TeX, $id );
	//
	// See Also:
	//	<_RenderField>
	//
	function _RenderTeX ( $TeX, $id ) {
		if (is_array($id)) {
			foreach ($id AS $k => $v) {
				$buffer .= _RenderTeX ( &$TeX, $v );
			}
			return $buffer;
		} else {
			if (!$id) return false;
			die('here');

			// Determine template
			if ($_REQUEST['print_template']) {
				$my_template = $_REQUEST['print_template'];
			} else {
				$my_template = $this->print_template;
			}

			// Handle templating elsewhere
			return $TeX->RenderFromTemplate(
				$my_template,
				$this->_print_mapping($TeX, $id)
			);
		}
	} // end method _RenderTeX

	// Method: _RenderField
	//
	//	Render out ##a:b@c## or ##a## type fields. "a" stands for
	//	the record, "b" stands for the target field, and "c" stands
	//	for the target table.
	//
	// Parameters:
	//
	//	$arg - Formatted field
	//
	//	$r - Associative array containing record
	//
	// Returns:
	//
	//	Rendered field
	//
	// Example:
	//
	//	$return = $this->_RenderField ( '##eocpatient:ptlname@patient##' );
	//
	function _RenderField ( $arg, $r = NULL ) {
		if (!(strpos($arg, '##') === false)) {
			// We need to deal with the content
			$displayed = '';
			$f_split = explode ('##', $arg);
			foreach ($f_split AS $f_k => $f_v) {
				if (!($f_k & 1)) {
					// Outside of '##'s
					$displayed .= $f_v;
				} else {
					// Inside ... process
					if (!(strpos($f_v, ':') === false)) {
						// Process as ##a:b@c##
						list ($a, $_b) = explode (':', $f_v);
						list ($b, $c) = explode ('@', $_b);
						$f_q = $GLOBALS['sql']->query(
							'SELECT '.$c.'.'.$b.' AS result '.
							'FROM '.$c.', '.$this->table_name.' '.
							'WHERE '.$this->table_name.'.'.$a.' = '.$c.'.id AND '.
							$this->table_name.'.id = '.$_REQUEST['id']
						);
						$f_r = $GLOBALS['sql']->fetch_array($f_q);
						$displayed .= $f_r['result'];
					} else {
						// Simple field replacement
						$displayed .= $r[$f_v];
					}
				}
			}
			return $displayed;
		} else {
			// No processing required. Return as is.
			return $arg;
		}
	} // end method _RenderField

	// Method: _TeX_Information
	//
	//	Callback to provide information to the TeX renderer about
	//	formatting.
	//
	// Returns:
	//
	//	Array ( title, heading, physician )
	//
	function _TeX_Information ( ) {
		// abstract
		$rec = freemed::get_link_rec($_REQUEST['id'], $this->table_name);
		$patient = CreateObject('FreeMED.Patient', $_REQUEST['patient']);
		$user = CreateObject('FreeMED.User');
		if ($user->isPhysician()) {
			$phy = $user->getPhysician();
		} else {
			$phy = $patient->local_record['patphy'];
		}
		$physician_object = CreateObject('FreeMED.Physician', $phy);
		$title = __($this->record_name);
		$heading = $patient->fullName().' ('.$patient->local_record['ptid'].')';
		$physician = $physician_object->fullName();
		return array ($title, $heading, $physician);
		return array ($title, $heading, $physician);
	} // end method _TeX_Information

} // end class EMRModule

?>

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us