/
Agenda.php
1436 lines (1253 loc) · 44.8 KB
/
Agenda.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
<?php
/**
*
* @package Gems
* @subpackage Agenda
* @author Matijs de Jong <mjong@magnafacta.nl>
* @copyright Copyright (c) 2013 Erasmus MC
* @license New BSD License
* @version $Id: Afenda.php$
*/
use Gems\Agenda\AppointmentFilterInterface;
use Gems\Agenda\AppointmentSelect;
use Gems\Agenda\EpisodeOfCare;
/**
*
*
* @package Gems
* @subpackage Agenda
* @copyright Copyright (c) 2013 Erasmus MC
* @license New BSD License
* @since Class available since version 1.6.2
*/
class Gems_Agenda extends \Gems_Loader_TargetLoaderAbstract
{
/**
*
* @var \Gems_Agenda_Appointment[]
*/
private $_appointments = array();
/**
*
* @var AppointmentFilterInterface[]
*/
private $_filters = array();
/**
*
* @var string
*/
public $appointmentDisplayFormat = 'dd-MM-yyyy HH:mm';
/**
*
* @var string
*/
public $episodeDisplayFormat = 'dd-MM-yyyy';
/**
*
* @var \Zend_Cache_Core
*/
protected $cache;
/**
* Allows sub classes of \Gems_Loader_LoaderAbstract to specify the subdirectory where to look for.
*
* @var string $cascade An optional subdirectory where this subclass always loads from.
*/
protected $cascade = 'Agenda';
/**
*
* @var \Zend_Db_Adapter_Abstract
*/
protected $db;
/**
*
* @var \Gems_Loader
*/
protected $loader;
/**
*
* @var \Zend_Translate
*/
protected $translate;
/**
*
* @var \Zend_Translate_Adapter
*/
protected $translateAdapter;
/**
*
* @param type $container A container acting as source for \MUtil_Registry_Source
* @param array $dirs The directories where to look for requested classes
*/
public function __construct($container, array $dirs)
{
parent::__construct($container, $dirs);
// Make sure the tracker is known
$this->addRegistryContainer(array('agenda' => $this));
}
/**
* Copy from \Zend_Translate_Adapter
*
* Translates the given string
* returns the translation
*
* @param string $text Translation string
* @param string|\Zend_Locale $locale (optional) Locale/Language to use, identical with locale
* identifier, @see \Zend_Locale for more information
* @return string
*/
public function _($text, $locale = null)
{
return $this->translateAdapter->_($text, $locale);
}
/**
* Get the select statement for appointments in getAppointments()
*
* Allows for overruling on project level
*
* @return \Zend_Db_Select
*/
protected function _getAppointmentSelect()
{
$select = $this->db->select();
$select->from('gems__appointments')
->joinLeft( 'gems__agenda_activities', 'gap_id_activity = gaa_id_activity')
->joinLeft('gems__agenda_procedures', 'gap_id_procedure = gapr_id_procedure')
->joinLeft('gems__locations', 'gap_id_location = glo_id_location')
->order('gap_admission_time DESC');
return $select;
}
/**
* Called after the check that all required registry values
* have been set correctly has run.
*
* This function is no needed if the classes are setup correctly
*
* @return void
*/
public function afterRegistry()
{
parent::afterRegistry();
$this->initTranslateable();
}
/**
* Add Activities to the table
*
* Override this method for other defaults
*
* @param string $name
* @param int $organizationId
* @return array
*/
public function addActivity($name, $organizationId)
{
$model = new \MUtil_Model_TableModel('gems__agenda_activities');
\Gems_Model::setChangeFieldsByPrefix($model, 'gaa');
$values = array(
'gaa_name' => $name,
'gaa_id_organization' => $organizationId,
'gaa_match_to' => $name,
'gaa_active' => 1,
'gaa_filter' => 0,
);
$result = $model->save($values);
$this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array('activity', 'activities'));
return $result;
}
/**
* Add HealthcareStaff to the table
*
* Override this method for other defaults
*
* @param string $name
* @param int $organizationId
* @return array
*/
public function addHealthcareStaff($name, $organizationId)
{
$model = new \MUtil_Model_TableModel('gems__agenda_staff');
\Gems_Model::setChangeFieldsByPrefix($model, 'gas');
$values = array(
'gas_name' => $name,
'gas_id_organization' => $organizationId,
'gas_match_to' => $name,
'gas_active' => 1,
'gas_filter' => 0,
);
$result = $model->save($values);
$this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array('staff'));
return $result;
}
/**
*
* @param type $name
* @param type $organizationId
* @param array $matches
* @return array
*/
public function addLocation($name, $organizationId, $matches)
{
if (empty($matches)) {
// A new match
$values = array(
'glo_name' => $name,
'glo_organizations' => ':' . $organizationId . ':',
'glo_match_to' => $name,
'glo_active' => 1,
'glo_filter' => 0,
);
} else {
// Update
$first = reset($matches);
// Change this match, add this organization
$values = array(
'glo_id_location' => $first['glo_id_location'],
'glo_organizations' => ':' . implode(':', array_keys($matches)) . ':' .
$organizationId . ':'
);
}
$model = new \MUtil_Model_TableModel('gems__locations');
\Gems_Model::setChangeFieldsByPrefix($model, 'glo');
$result = $model->save($values);
$this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array('location', 'locations'));
return $result;
}
/**
* Add Procedure to the table
*
* Override this method for other defaults
*
* @param string $name
* @param int $organizationId
* @return array
*/
public function addProcedure($name, $organizationId)
{
$model = new \MUtil_Model_TableModel('gems__agenda_procedures');
\Gems_Model::setChangeFieldsByPrefix($model, 'gapr');
$values = array(
'gapr_name' => $name,
'gapr_id_organization' => $organizationId,
'gapr_match_to' => $name,
'gapr_active' => 1,
'gapr_filter' => 0,
);
$result = $model->save($values);
$this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array('procedure', 'procedures'));
return $result;
}
/**
* Dynamically load and create a [Gems|Project]_Agenda_ class
*
* @param string $className
* @param mixed $param1
* @param mixed $param2
* @return object
*/
public function createAgendaClass($className, $param1 = null, $param2 = null)
{
$params = func_get_args();
array_shift($params);
return $this->_loadClass($className, true, $params);
}
/**
* Create agenda select
*
* @param string|array $fields The appointment fields to select
* @return \Gems\Agenda\AppointmentSelect
*/
public function createAppointmentSelect($fields = '*')
{
return $this->_loadClass('AppointmentSelect', true, array($fields));
}
/**
* Find the first appointment matching this query
*
* @param int $filterId
* @param \Gems_Tracker_RespondentTrack $respTrack
* @param mixed $from Optional date or appointment after which the appointment must occur
* @param string $oper Comaprison operator for the from date
* @param int $uniqueness 0 is not unique, 1 within respTreck, 2 within track, 3 overall
* @return int The first found appointment id or false
* /
public function findFirstAppointmentId($filterId, \Gems_Tracker_RespondentTrack $respTrack,
$from = null, $oper = '>=', $uniqueness = 0)
{
$filter = $this->getFilter($filterId);
if (! $filter) {
return false;
}
$respondentId = $respTrack->getRespondentId();
$orgId = $respTrack->getOrganizationId();
$select = $this->db->select();
$select->from('gems__appointments', 'gap_id_appointment')
->where('gap_id_user = ?', $respondentId)
->where('gap_id_organization = ?', $orgId)
->where('gap_status IN (?)', $this->getStatusKeysActiveDbQuoted())
->where($filter->getSqlWhere())
->limit(1);
if ($from) {
if ($from instanceof \Gems_Agenda_Appointment) {
$from = $from->getAdmissionTime();
}
if ($from instanceof \Zend_Date) {
$from = $from->toString('yyyy-MM-dd HH:mm:ss');
}
$select->where("gap_admission_time $oper ?", $from);
}
if ('<' === $oper[0]) {
$select->order('gap_admission_time DESC');
} else {
$select->order('gap_admission_time ASC');
}
if ($uniqueness) {
$fieldId = intval($filter->getAppointmentFieldId());
$respTrackId = intval($respTrack->getRespondentTrackId());
switch ($uniqueness) {
case 1:
$select->where(
"gap_id_appointment NOT IN
(SELECT gr2t2a_id_appointment FROM gems__respondent2track2appointment
WHERE gr2t2a_id_appointment IS NOT NULL AND
gr2t2a_id_respondent_track = $respTrackId AND
gr2t2a_id_app_field != $fieldId)"
);
break;
case 2:
$trackId = $respTrack->getTrackId();
$select->where(
"gap_id_appointment NOT IN
(SELECT gr2t2a_id_appointment FROM gems__respondent2track2appointment
INNER JOIN gems__respondent2track
ON gr2t2a_id_respondent_track = gr2t_id_respondent_track
WHERE gr2t2a_id_appointment IS NOT NULL AND
gr2t_id_track = $trackId AND
NOT (gr2t2a_id_respondent_track = $respTrackId AND
gr2t2a_id_app_field = $fieldId))"
);
break;
// case 3:
// $select->where(
// "gap_id_appointment NOT IN
// (SELECT gr2t2a_id_appointment FROM gems__respondent2track2appointment
// WHERE gr2t2a_id_appointment IS NOT NULL AND
// NOT (gr2t2a_id_respondent_track = $respTrackId AND
// gr2t2a_id_app_field = $fieldId))"
// );
// break;
// default:
}
}
// \MUtil_Echo::track($select->__toString(), $uniqueness, $filter->getSqlWhere());
return $this->db->fetchOne($select);
}
/**
* Get all active respondents for this user
*
* @param int $respondentId When null $patientNr is required
* @param int $organizationId
* @param string $patientNr Optional for when $respondentId is null
* @param string $where Optional extra where statement
* @return array appointmentId => appointment description
*/
public function getActiveAppointments($respondentId, $organizationId, $patientNr = null, $where = null)
{
if ($where) {
$where = "($where) AND ";
} else {
$where = "";
}
$where .= sprintf('gap_status IN (%s)', $this->getStatusKeysActiveDbQuoted());
return $this->getAppointments($respondentId, $organizationId, $patientNr, $where);
}
/**
*
* @param int $organizationId Optional
* @return array activity_id => name
*/
public function getActivities($organizationId = null)
{
$cacheId = __CLASS__ . '_' . __FUNCTION__ . '_' . $organizationId;
if ($results = $this->cache->load($cacheId)) {
return $results;
}
$select = $this->db->select();
$select->from('gems__agenda_activities', array('gaa_id_activity', 'gaa_name'))
->order('gaa_name');
if ($organizationId) {
// Check only for active when with $orgId: those are usually used
// with editing, while the whole list is used for display.
$select->where('gaa_active = 1')
->where('(
gaa_id_organization IS NULL
AND
gaa_name NOT IN (SELECT gaa_name FROM gems__agenda_activities WHERE gaa_id_organization = ?)
) OR
gaa_id_organization = ?', $organizationId);
}
// \MUtil_Echo::track($select->__toString());
$results = $this->db->fetchPairs($select);
$this->cache->save($results, $cacheId, array('activities'));
return $results;
}
/**
* Overrule this function to adapt the display of the agenda items for each project
*
* @see \Gems_Agenda_Appointment->getDisplayString()
*
* @param array $row Row containing result select
* @return string
*/
public function getAppointmentDisplay(array $row)
{
$date = new \MUtil_Date($row['gap_admission_time'], 'yyyy-MM-dd HH:mm:ss');
$results[] = $date->toString($this->appointmentDisplayFormat);
if ($row['gaa_name']) {
$results[] = $row['gaa_name'];
}
if ($row['gapr_name']) {
$results[] = $row['gapr_name'];
}
if ($row['glo_name']) {
$results[] = $row['glo_name'];
}
return implode($this->_('; '), $results);
}
/**
* Get an appointment object
*
* @param mixed $appointmentData Appointment id or array containing appointment data
* @return \Gems_Agenda_Appointment
*/
public function getAppointment($appointmentData)
{
if (! $appointmentData) {
throw new \Gems_Exception_Coding('Provide at least the apppointment id when requesting an appointment.');
}
if (is_array($appointmentData)) {
if (!isset($appointmentData['gap_id_appointment'])) {
throw new \Gems_Exception_Coding(
'$appointmentData array should atleast have a key "gap_id_appointment" containing the requested appointment id'
);
}
$appointmentId = $appointmentData['gap_id_appointment'];
} else {
$appointmentId = $appointmentData;
}
// \MUtil_Echo::track($appointmentId, $appointmentData);
if (! isset($this->_appointments[$appointmentId])) {
$this->_appointments[$appointmentId] = $this->_loadClass('appointment', true, array($appointmentData));
} elseif (is_array($appointmentData)) {
// Make sure the new values are set in the object
$this->_appointments[$appointmentId]->refresh($appointmentData);
}
return $this->_appointments[$appointmentId];
}
/**
* Get all appointments for a respondent
*
* @param int $respondentId When null $patientNr is required
* @param int $organizationId
* @param string $patientNr Optional for when $respondentId is null
* @param string $where Optional extra where statement
* @return array appointmentId => appointment description
*/
public function getAppointments($respondentId, $organizationId, $patientNr = null, $where = null)
{
$select = $this->_getAppointmentSelect();
if ($where) {
$select->where($where);
}
if ($respondentId) {
$select->where('gap_id_user = ?', $respondentId)
->where('gap_id_organization = ?', $organizationId);
} else {
// Join might have been created in _getAppointmentSelect
$from = $select->getPart(\Zend_Db_Select::FROM);
if (! isset($from['gems__respondent2org'])) {
$select->joinInner(
'gems__respondent2org',
'gap_id_user = gr2o_id_user AND gap_id_organization = gr2o_id_organization',
array()
);
}
$select->where('gr2o_patient_nr = ?', $patientNr)
->where('gr2o_id_organization = ?', $organizationId);
}
// \MUtil_Echo::track($select->__toString());
$rows = $this->db->fetchAll($select);
if (! $rows) {
return array();
}
$results = array();
foreach ($rows as $row) {
$results[$row['gap_id_appointment']] = $this->getAppointmentDisplay($row);
}
return $results;
}
/**
* Get an appointment object
*
* @param mixed $episodeData Episode id or array containing episode data
* @return \Gems\Agenda\EpisodeOfCare
*/
public function getEpisodeOfCare($episodeData)
{
if (! $episodeData) {
throw new \Gems_Exception_Coding('Provide at least the episode id when requesting an episode of care.');
}
if (is_array($episodeData)) {
if (!isset($episodeData['gec_episode_of_care_id'])) {
throw new \Gems_Exception_Coding(
'$episodeData array should atleast have a key "gec_episode_of_care_id" containing the requested episode id'
);
}
$episodeId = $episodeData['gec_episode_of_care_id'];
} else {
$episodeId = $episodeData;
}
// \MUtil_Echo::track($appointmentId, $appointmentData);
return $this->_loadClass('episodeOfCare', true, array($episodeData));
}
/**
* Get the status codes for all episode of care items
*
* @return array code => label
*/
public function getEpisodeStatusCodes()
{
$codes = $this->getEpisodeStatusCodesActive() +
$this->getEpisodeStatusCodesInactive();
asort($codes);
return $codes;
}
/**
* Get the status codes for active episode of care items
*
* see https://www.hl7.org/fhir/episodeofcare.html
*
* @return array code => label
*/
public function getEpisodeStatusCodesActive()
{
// A => active, C => Cancelled, E => Error, F => Finished, O => Onhold, P => Planned, W => Waitlist
$codes = array(
'A' => $this->_('Active'),
'F' => $this->_('Finished'),
'O' => $this->_('On hold'),
'P' => $this->_('Planned'),
'W' => $this->_('Waitlist'),
);
asort($codes);
return $codes;
}
/**
* Get the status codes for inactive episode of care items
*
* @return array code => label
*/
public function getEpisodeStatusCodesInactive()
{
$codes = array(
'C' => $this->_('Cancelled'),
'E' => $this->_('Erroneous input'),
);
asort($codes);
return $codes;
}
/**
* Get the options list episodes for episodes
*
* @param array $episodes
* @return array of $epsiodeId => Description
*/
public function getEpisodesAsOptions(array $episodes)
{
$options = [];
foreach ($episodes as $id => $episode) {
if ($episode instanceof EpisodeOfCare) {
$options[$id] = $episode->getDisplayString();
}
}
return $options;
}
/**
* Get the episodes for a respondent
*
* @param \Gems_Tracker_Respondent $respondent
* @param $where mixed Optional extra string or array filter
* @return array of $epsiodeId => \Gems\Agenda\EpisodeOfCare
*/
public function getEpisodesFor(\Gems_Tracker_Respondent $respondent, $where = null)
{
return $this->getEpisodesForRespId($respondent->getId(), $respondent->getOrganizationId(), $where);
}
/**
* Get the episodes for a respondent
*
* @param int $respondentId
* @param int $orgId
* @param $where mixed Optional extra string or array filter
* @return array of $epsiodeId => \Gems\Agenda\EpisodeOfCare
*/
public function getEpisodesForRespId($respondentId, $orgId, $where = null)
{
$select = $this->db->select();
$select->from('gems__episodes_of_care')
->where('gec_id_user = ?', $respondentId)
->where('gec_id_organization = ?', $orgId)
->order('gec_startdate DESC');
if ($where) {
if (is_array($where)) {
foreach ($where as $expr => $param) {
if (is_int($expr)) {
$select->where($param);
} else {
$select->where($expr, $param);
}
}
} else {
$select->where($where);
}
}
// \MUtil_Echo::track($select->__toString());
$episodes = $this->db->fetchAll($select);
$output = [];
foreach ($episodes as $episodeData) {
$episode = $this->getEpisodeOfCare($episodeData);
$output[$episode->getId()] = $episode;
}
return $output;
}
/**
* Load the list of assignable filters
*
* @return array filter_id => label
*/
public function getFilterList()
{
$cacheId = __CLASS__ . '_' . __FUNCTION__;
$output = $this->cache->load($cacheId);
if ($output) {
return $output;
}
$output = $this->db->fetchPairs("SELECT gaf_id, COALESCE(gaf_manual_name, gaf_calc_name) "
. "FROM gems__appointment_filters WHERE gaf_active = 1 ORDER BY gaf_id_order");
$this->cache->save($output, $cacheId, array('appointment_filters'));
return $output;
}
/**
* Get the field names in appontments with their labels as the value
*
* @return array fieldname => label
*/
public final function getFieldLabels()
{
$output = \Mutil_Ra::column('label', $this->getFieldData());
asort($output);
return $output;
}
/**
* Get a structured nested array contain information on all the appointment
*
* @return array fieldname => array(label[, tableName, tableId, tableLikeFilter))
*/
protected function getFieldData()
{
return array(
'gap_id_organization' => array(
'label' => $this->_('Organization'),
'tableName' => 'gems__organizations',
'tableId' => 'gor_id_organization',
'tableLikeFilter' => "gor_active = 1 AND gor_name LIKE '%s'",
),
'gap_source' => array(
'label' => $this->_('Source of appointment'),
),
'gap_id_attended_by' => array(
'label' => $this->_('With'),
'tableName' => 'gems__agenda_staff',
'tableId' => 'gas_id_staff',
'tableLikeFilter' => "gas_active = 1 AND gas_name LIKE '%s'",
),
'gap_id_referred_by' => array(
'label' => $this->_('Referrer'),
'tableName' => 'gems__agenda_staff',
'tableId' => 'gas_id_staff',
'tableLikeFilter' => "gas_active = 1 AND gas_name LIKE '%s'",
),
'gap_id_activity' => array(
'label' => $this->_('Activity'),
'tableName' => 'gems__agenda_activities',
'tableId' => 'gaa_id_activity',
'tableLikeFilter' => "gaa_active = 1 AND gaa_name LIKE '%s'",
),
'gap_id_procedure' => array(
'label' => $this->_('Procedure'),
'tableName' => 'gems__agenda_procedures',
'tableId' => 'gapr_id_procedure',
'tableLikeFilter' => "gapr_active = 1 AND gapr_name LIKE '%s'",
),
'gap_id_location' => array(
'label' => $this->_('Location'),
'tableName' => 'gems__locations',
'tableId' => 'glo_id_location',
'tableLikeFilter' => "glo_active = 1 AND glo_name LIKE '%s'",
),
'gap_subject' => array(
'label' => $this->_('Subject'),
),
);
}
/**
* Get a filter from the database
*
* @param $filterId Id of a single filter
* @return AppointmentFilterInterface or null
*/
public function getFilter($filterId)
{
static $filters = array();
if (isset($filters[$filterId])) {
return $filters[$filterId];
}
$found = $this->getFilters("SELECT *
FROM gems__appointment_filters LEFT JOIN gems__track_appointments ON gaf_id = gtap_filter_id
WHERE gaf_active = 1 AND gaf_id = $filterId LIMIT 1");
if ($found) {
$filters[$filterId] = reset($found);
return $filters[$filterId];
}
}
/**
* Get the filters from the database
*
* @param $sql SQL statement
* @return AppointmentFilterInterface[]
*/
public function getFilters($sql)
{
$classes = array();
$filterRows = $this->db->fetchAll($sql);
$output = array();
// \MUtil_Echo::track($filterRows);
foreach ($filterRows as $key => $filter) {
$className = $filter['gaf_class'];
if (! isset($classes[$className])) {
$classes[$className] = $this->newFilterObject($className);
}
$filterObject = clone $classes[$className];
if ($filterObject instanceof AppointmentFilterInterface) {
$filterObject->exchangeArray($filter);
$output[$key] = $filterObject;
}
}
// \MUtil_Echo::track(count($filterRows), count($output));
return $output;
}
/**
*
* @param int $organizationId Optional
* @return array activity_id => name
*/
public function getHealthcareStaff($organizationId = null)
{
$cacheId = __CLASS__ . '_' . __FUNCTION__ . '_' . $organizationId;
if ($results = $this->cache->load($cacheId)) {
return $results;
}
$select = $this->db->select();
$select->from('gems__agenda_staff', array('gas_id_staff', 'gas_name'))
->order('gas_name');
if ($organizationId) {
// Check only for active when with $orgId: those are usually used
// with editing, while the whole list is used for display.
$select->where('gas_active = 1')
->where('gas_id_organization = ?', $organizationId);
}
// \MUtil_Echo::track($select->__toString());
$results = $this->db->fetchPairs($select);
$this->cache->save($results, $cacheId, array('staff'));
return $results;
}
/**
* Returns an array with identical key => value pairs containing care provision locations.
*
* @param int $orgId Optional to slect for single organization
* @return array
*/
public function getLocations($orgId = null)
{
// Make sure no invalid data gets through
$orgId = intval($orgId);
$cacheId = __CLASS__ . '_' . __FUNCTION__ . '_' . $orgId;
if ($results = $this->cache->load($cacheId)) {
return $results;
}
$select = $this->db->select();
$select->from('gems__locations', array('glo_id_location', 'glo_name'))
->order('glo_name');
if ($orgId) {
// Check only for active when with $orgId: those are usually used
// with editing, while the whole list is used for display.
$select->where('glo_active = 1');
$select->where("glo_organizations LIKE '%:$orgId:%'");
}
$results = $this->db->fetchPairs($select);
$this->cache->save($results, $cacheId, array('locations'));
return $results;
}
/**
*
* @param int $organizationId Optional
* @return array activity_id => name
*/
public function getProcedures($organizationId = null)
{
$cacheId = __CLASS__ . '_' . __FUNCTION__ . '_' . $organizationId;
if ($results = $this->cache->load($cacheId)) {
return $results;
}
$select = $this->db->select();
$select->from('gems__agenda_procedures', array('gapr_id_procedure', 'gapr_name'))
->order('gapr_name');
if ($organizationId) {
// Check only for active when with $orgId: those are usually used
// with editing, while the whole list is used for display.
$select->where('gapr_active = 1')
->where('(
gapr_id_organization IS NULL
AND
gapr_name NOT IN (SELECT gapr_name FROM gems__agenda_procedures WHERE gapr_id_procedure = ?)
) OR
gapr_id_organization = ?', $organizationId);
}
// \MUtil_Echo::track($select->__toString());
$results = $this->db->fetchPairs($select);
$this->cache->save($results, $cacheId, array('procedures'));
return $results;
}
/**
* Get the status codes for all active agenda items
*
* @return array code => label
*/
public function getStatusCodes()
{
$codes = $this->getStatusCodesActive() +
$this->getStatusCodesInactive();
asort($codes);
return $codes;
}
/**
* Get the status codes for active agenda items
*
* @return array code => label
*/
public function getStatusCodesActive()
{
$codes = array(
'AC' => $this->_('Active appointment'),
'CO' => $this->_('Completed appointment'),
);
asort($codes);
return $codes;
}
/**
* Get the status codes for inactive agenda items
*
* @return array code => label
*/
public function getStatusCodesInactive()
{
$codes = array(
'AB' => $this->_('Aborted appointment'),
'CA' => $this->_('Cancelled appointment'),
);
asort($codes);
return $codes;
}