psr12 fixes for new PHP_CodeSniffer (#4795)
[openemr.git] / interface / modules / zend_modules / module / PatientFlowBoard / src / PatientFlowBoard / Listener / PatientFlowBoardEventsSubscriber.php
blob4c94d59b6a9e318a181d8587d773f157177a2d25
1 <?php
3 /**
4 * PatientFlowBoardEventsSubscriber Listens to system data save events for the patient flow board and updates flow board
5 * data. It can be used to listen to any patient flow board events and trigger system functionality for the flow board
7 * @package openemr
8 * @link http://www.open-emr.org
9 * @author Stephen Nielson <stephen@nielson.org>
10 * @author Terry Hill <terry@lillysystems.com>
11 * @copyright Copyright (C) 2015 Terry Hill <terry@lillysystems.com>
12 * @copyright Copyright (c) 2021 Stephen Nielson <stephen@nielson.org>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 namespace OpenEMR\ZendModules\PatientFlowBoard\Listener;
18 use OpenEMR\Events\Services\ServiceSaveEvent;
19 use OpenEMR\Services\AppointmentService;
20 use OpenEMR\Services\PatientTrackerService;
21 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23 class PatientFlowBoardEventsSubscriber implements EventSubscriberInterface
25 /**
26 * @return array<string, mixed> The event names to listen to
28 public static function getSubscribedEvents()
30 $events = [];
31 // we only subscribe to this event if the drug_screen is enabled as a feature
32 if ($GLOBALS['drug_screen']) {
33 $events[ServiceSaveEvent::EVENT_POST_SAVE] = 'onServicePostSaveEvent';
35 return $events;
38 /**
39 * Receives all of the save events from the OpenEMR services (that support the event) and populates any mapping uuids
40 * that are needed.
41 * @param ServiceSaveEvent $event
43 public function onServicePostSaveEvent(ServiceSaveEvent $event, $eventName)
45 if (!$event->getService() instanceof PatientTrackerService) {
46 return;
49 $trackerData = $event->getSaveData();
50 $element = $trackerData['element'] ?? [];
51 $status = $element['status'] ?? null;
53 if (!empty($status)) {
54 $apptService = new AppointmentService();
55 if ($apptService->isCheckInStatus($status)) {
56 $yearly_limit = $GLOBALS['maximum_drug_test_yearly'];
57 $percentage = $GLOBALS['drug_testing_percentage'];
58 $this->random_drug_test($trackerData['id'], $percentage, $yearly_limit);
63 /**
64 * @param $tracker_id
65 * @param $percentage
66 * @param $yearly_limit
68 private function random_drug_test($tracker_id, $percentage, $yearly_limit)
71 # Check if randomization has not yet been done (is random_drug_test NULL). If already done, then exit.
72 $drug_test_done = sqlQuery("SELECT `random_drug_test`, pid from patient_tracker " .
73 "WHERE id =? ", array($tracker_id));
74 $Patient_id = $drug_test_done['pid'];
76 if (is_null($drug_test_done['random_drug_test'])) {
77 # get a count of the number of times the patient has been screened.
78 if ($yearly_limit > 0) {
79 # check to see if screens are within the current year.
80 $lastyear = date("Y-m-d", strtotime("-1 year", strtotime(date("Y-m-d H:i:s"))));
81 $drug_test_count = sqlQuery("SELECT COUNT(*) from patient_tracker " .
82 "WHERE drug_screen_completed = '1' AND apptdate >= ? AND pid =? ", array($lastyear,$Patient_id));
85 # check that the patient is not at the yearly limit.
86 if ($drug_test_count['COUNT(*)'] >= $yearly_limit && ($yearly_limit > 0)) {
87 $drugtest = 0;
88 } else {
89 # Now do the randomization and set random_drug_test to the outcome.
91 $drugtest = 0;
92 $testdrug = mt_rand(0, 100);
93 if ($testdrug <= $percentage) {
94 $drugtest = 1;
98 #Update the tracker file.
99 sqlStatement("UPDATE patient_tracker SET " .
100 "random_drug_test = ? " .
101 "WHERE id =? ", array($drugtest,$tracker_id));