Skip to content

Instantly share code, notes, and snippets.

@iaditya
Created May 25, 2017 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iaditya/a2f4279b19fa3bfbfa3e9e690a0597bb to your computer and use it in GitHub Desktop.
Save iaditya/a2f4279b19fa3bfbfa3e9e690a0597bb to your computer and use it in GitHub Desktop.
<?php
namespace App\Controller;
use Aura\Intl\Exception;
use App\Controller\AppController;
use Cake\I18n\Date;
use Cake\I18n\Time;
use Cake\Log\Log;
use Shim\Model\Table\Table;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
use Notifier\Utility\NotificationManager;
/**
* Events Controller
*
* @property \App\Model\Table\EventsTable $Events
*/
class EventsController extends AppController
{
public function initialize()
{
parent::initialize(); // TODO: Change the autogenerated stub
$this->Auth->allow(['addAppointmentApi', 'getPatientAppointmentsApi', 'cancelAppointmentApi']);
$this->loadComponent('Notifier.Notifier');
$this->loadComponent('RequestHandler');
$this->loadComponent('Shortner');
// $this->loadComponent('Ajax.Ajax');
$this->loadComponent('Search.Prg', [
'actions' => ['index']
]);
}
/**
* Index method
*
* @return \Cake\Network\Response|null
*/
public function index()
{
$facility_id = isset($this->request->query['facility_id']) ? $this->request->query['facility_id'] : null;
$patient_id = isset($this->request->query['patient_id']) ? $this->request->query['patient_id'] : null;
$provider_id = isset($this->request->query['provider_id']) ? $this->request->query['provider_id'] : null;
$this->paginate = [
'contain' => ['EventTypes', 'Providers' => ['Users'], 'Patients' => ['Users'], 'Facilities']
];
if ($this->request->session()->read('Auth.User.is_superuser') == "1") {
$events = $this->paginate(
$this->Events->find('all')
->order(['Events.start' => 'DESC'])
);
}
if ($this->request->session()->read('Auth.User.role') == "Provider") {
$provider_user_id = $this->request->session()->read('Auth.User.id');
$providers = $this->Events->Providers->findByUserId($provider_user_id);
$provider = $providers->first();
$events = $this->paginate(
$this->Events->find('search', $this->Events->filterParams($this->request->query))
->where(['provider_id' => $provider->id])
->order(['Events.start' => 'DESC']));
}
if ($this->request->session()->read('Auth.User.role') == "Patient") {
$patient_user_id = $this->request->session()->read('Auth.User.id');
$patients = $this->Events->Patients->findByUserId($patient_user_id);
$patient = $patients->first();
$events = $this->paginate(
$this->Events->find('search', $this->Events->filterParams($this->request->query))
->where(['patient_id' => $patient->id])
->order(['Events.start' => 'DESC'])
);
}
if ($this->request->session()->read('Auth.User.role') == "FacilityAdmin") {
$facility_user_id = $this->request->session()->read('Auth.User.id');
$facilities = $this->Events->Facilities->find('all')->matching('FacilityAdmins')->where(['FacilityAdmins.user_id' => $facility_user_id]);
$facility = $facilities->first();
$events = $this->paginate(
$this->Events->find('search', $this->Events->filterParams($this->request->query))
->where(['facility_id' => $facility->id])
->order(['Events.start' => 'DESC'])
);
}
$this->set(compact('events'));
$this->set('_serialize', ['events']);
}
public function indexByPatient($patient_user_id = null)
{
if (empty($patient_user_id))
return;
$this->paginate = [
'contain' => ['EventTypes', 'Providers' => ['Users'], 'Patients' => ['Users'], 'Facilities']
];
$patients = $this->Events->Patients->findByUserId($patient_user_id);
$patient = $patients->first();
$events = $this->paginate($this->Events->findByPatientId($patient->id));
$this->set(compact('events'));
$this->set('_serialize', ['events']);
}
public function getPatientAppointmentsApi($patient_id = null)
{
if (empty($patient_id))
$patient_id = isset($this->request->query['patient_id']) ? $this->request->query['patient_id'] : null;
if (empty($patient_id))
$patient_id = isset($this->request->data['patient_id']) ? $this->request->data['patient_id'] : null;
Log::debug("getPatientAppointmentsApi" . $patient_id);
$start = Time::now()->toDateTimeString();
$end = Time::now()->addMonths(1)->toDateTimeString();
Log::debug("getPatientAppointmentsApi start: " . $start);
Log::debug("getPatientAppointmentsApi start: " . $end);
$events = $this->Events->findByPatientId($patient_id)
->contain(['EventTypes', 'Providers' => ['Users'], 'Patients' => ['Users'], 'Facilities'])
->where(['OR' => [['Events.status' => 'Confirmed'],
['Events.status' => 'PaymentPending']]])
->andWhere(['Events.start BETWEEN :now AND :then'])
->bind(':now', $start, 'time')
->bind(':then', $end, 'time');
Log::debug("getPatientAppointmentsApi" . $events);
// Log::debug("getPatientAppointmentsApi".$events->first());
$this->set(compact('events'));
$this->set('_serialize', ['events']);
}
/**
* View method
*
* @param string|null $id Event id.
* @return \Cake\Network\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$event = $this->Events->get($id, [
'contain' => ['EventTypes', 'Providers' => ['Users'], 'Patients' => ['Users'], 'Facilities']
]);
$this->set('event', $event);
$this->set('_serialize', ['event']);
}
public function addAppointmentApi()
{
if ($this->request->is('post')) {
$facility_id = isset($this->request->data['facility_id']) ? $this->request->data['facility_id'] : null;
$patient_id = isset($this->request->data['patient_id']) ? $this->request->data['patient_id'] : null;
$provider_id = isset($this->request->data['provider_id']) ? $this->request->data['provider_id'] : null;
$slot_type = isset($this->request->data['slot_type']) ? $this->request->data['slot_type'] : null;
unset($this->request->data['slot_type']);
$start = isset($this->request->data['start']) ? $this->request->data['start'] : null;
$end = isset($this->request->data['end']) ? $this->request->data['end'] : null;
Log::debug("Event addAppointmentApi slottype: " . $slot_type);
Log::debug("Event addAppointmentApi facility id:" . $facility_id . " patient_id:" . $patient_id . " provider_id:" . $provider_id);
Log::debug("Event addAppointmentApi start: " . $start);
if (!empty($provider_id)) {
$provider = $this->Events->Providers->get($provider_id, ['contain' => ['Users']]);
}
$event = $this->Events->newEntity();
$this->request->data['all_day'] = 0;
$this->request->data['active'] = 1;
$this->request->data['title'] = "Appointment";
$this->request->data['details'] = "Appointment";
$this->request->data['issues'][0]['title'] = $this->request->data['title'];
$this->request->data['issues'][0]['patient_id'] = $this->request->data['patient_id'];
$this->request->data['issues'][0]['issue_type_id'] = 1;
$this->request->data['issues'][0]['referredby'] = "";
$this->request->data['issues'][0]['reaction'] = "";
$this->request->data['issues'][0]['allergies'] = "";
$event = $this->Events->patchEntity($event, $this->request->data,
['associated' => ['Issues']]);
$stime = new Time($start);
$event->start = $stime;
$event->event_type_id = 1;
Log::debug("Event addAppointmentApi start:" . $this->request->data['start']);
$slot_time = 10;
if (!empty($provider->inhouse_slot_time))
$slot_time = $provider->inhouse_slot_time;
$event->end = $stime->addMinute($slot_time);
Log::debug("Event addAppointmentApi end:" . $end);
if ($slot_type === 'telecon') {
$event->event_type_id = 2;
$stime = new Time($start);
$event->start = $stime->toDateTimeString();
$event->end = $stime->addMinute($provider->telecon_slot_time)->toDateTimeString();
}
$event->status = 'Confirmed';
if ($event->event_type_id == 2) {
$event->status = 'PaymentPending';
}
Log::debug("Event addAppointmentApi event before save:" . $event);
//echo "<pre>",print_r($event),"</pre>"; exit;
if ($this->Events->save($event, array('validate' => 'only'))) {
Log::debug("Event saved");
if ($event->event_type_id == 1) {
$event_type = "Inhouse";
} else {
$event_type = "Teleconsult";
}
if ($event->event_type_id == 2) {
Log::Debug("Event type is Teleconsult. Creating zoom meeting for " . $event->provider_id);
$zoomuser = $this->Events->Providers->Zoomusers->findByProviderId($event->provider_id)->first();
//$test = $this->Events->Zoommeetings->testecho("testecho");
// $this->set(compact('test'));
$result = $this->Events->Zoommeetings->createMeeting($zoomuser, $event->id, $event->title, $event->start);
if (isset($result)) {
Log::Debug("Zoom meeting created");
$event->zoommeeting_id = $result->id;
$this->Events->save($event);
}
Log::Debug("Zoom user" . $zoomuser);
// Create the Payment id and set the info
$PaymentSettings = TableRegistry::get('facilities_providers');
$paymentsetting = $PaymentSettings->find("all")
->where(['facility_id' => $facility_id, 'provider_id' => $provider_id])
->first();
$Payments = TableRegistry::get('payments');
$payment = $Payments->newEntity();
$payment->transactionid = Time::now()->toUnixString();
$payment->final_amount = $paymentsetting->price_per_slot_india;
$payment->total_amount = $paymentsetting->price_per_slot_india;
$payment->payment_date = Date::today();
$payment->patient_id = $event->patient_id;
$payment->provider_id = $event->provider_id;
$payment->event_id = $event->id;
$Payments->save($payment);
}
$data['event'] = $event;
if (!empty($payment))
$data['payment'] = $payment;
$data['status'] = "Success";
} else {
Log::Debug("Save Failed" . $event);
$data['status'] = "Failed";
}
$this->set(array(
'data' => $data,
'_serialize' => 'data'
));
}
}
/**
* Add method
*
* @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
*/
public function cadd()
{
$facility_id = isset($this->request->query['facility_id']) ? $this->request->query['facility_id'] : null;
$patient_id = isset($this->request->query['patient_id']) ? $this->request->query['patient_id'] : null;
$provider_id = isset($this->request->query['provider_id']) ? $this->request->query['provider_id'] : null;
$slot_type = isset($this->request->query['slot_type']) ? $this->request->query['slot_type'] : 'both';
$start = isset($this->request->query['start']) ? $this->request->query['start'] : null;
$end = isset($this->request->query['end']) ? $this->request->query['end'] : null;
Log::debug("Event cadd slottype: " . $slot_type);
Log::debug("Event cadd facility id:" . $facility_id . " patient_id:" . $patient_id . " provider_id:" . $provider_id);
$start = $_GET['start'];
$end = $_GET['end'];
if ((empty($provider)) && !empty($provider_id)) {
$provider = $this->Events->Providers->get($provider_id, ['contain' => ['Users']]);
$this->set(compact('provider'));
}
$stime = new Time($start);
$end = $stime->addMinute($provider->inhouse_slot_time)->toDateTimeString();
if ($slot_type === 'telecon') {
$stime = new Time($start);
$end = $stime->addMinute($provider->telecon_slot_time)->toDateTimeString();
}
$start = (new Time($start))->toDateTimeString();
if ($this->request->is('ajax')) {
$this->response->disableCache();
}
if (($this->request->session()->read('Auth.User.role') == "Provider") || !empty($provider_user_id)) {
if (empty($provider_user_id))
$provider_user_id = $this->request->session()->read('Auth.User.id');
$providers = $this->Events->Providers->findByUserId($provider_user_id)
->contain(['Users']);
$provider = $providers->first();
$this->set(compact('provider'));
$patients = $this->Events->Patients->find('list', [
'keyField' => 'id',
'valueField' => 'user.full_name'])
->contain(['Users'])
->matching('Providers')
->where(['PatientsProviders.provider_id' => $provider->id]);
}
if (($this->request->session()->read('Auth.User.role') == "Patient") || !empty($patient_user_id)) {
if (empty($patient_user_id))
$patient_user_id = $this->request->session()->read('Auth.User.id');
$patients = $this->Events->Patients->findByUserId($patient_user_id)
->contain(['Users']);
$patient = $patients->first();
$this->set(compact('patient'));
}
if ((empty($patient)) && !empty($patient_id)) {
$patient = $this->Events->Patients->get($patient_id, ['contain' => ['Users']]);
$this->set(compact('patient'));
}
if (!empty($facility_id)) {
$facility = $this->Events->Facilities->get($facility_id);
$this->set(compact('facility'));
}
if ($this->request->session()->read('Auth.User.role') == "FacilityAdmin") {
if (empty($facility)) {
if (empty($facility_id))
$facility_user_id = $this->request->session()->read('Auth.User.id');
$facilities = $this->Events->Facilities->find('all')
->matching('FacilityAdmins')->where(['FacilityAdmins.user_id' => $facility_user_id]);
$facility = $facilities->first();
$this->set(compact('facility'));
}
$providers = $this->Events->Providers->find('list',
[
'keyField' => 'id',
'valueField' => 'user.first_name'
])
->contain(['Users'])
->matching('Facilities')
->where(['FacilitiesProviders.facility_id' => $facility->id]);
$patients = $this->Events->Patients->find('list', [
'keyField' => 'id',
'valueField' => 'user.full_name'])
->contain(['Users'])
->matching('Facilities')
->where(['FacilitiesPatients.facility_id' => $facility->id]);
}
if (!empty($start)) {
$this->set(compact('start'));
Log::Debug("Start" . $start);
}
if (!empty($end)) {
$this->set(compact('end'));
Log::Debug("end" . $end);
}
// $this->viewClass = 'Ajax.Ajax';
$event = $this->Events->newEntity();
if ($this->request->is('post')) {
$this->request->data['issues'][0]['title'] = $this->request->data['title'];
$this->request->data['issues'][0]['patient_id'] = $this->request->data['patient_id'];
$event = $this->Events->patchEntity($event, $this->request->data, [
'associated' => ['Issues']]);
$event->start = Time::parse($this->request->data['start']);
$event->end = Time::parse($this->request->data['end']);
if (($this->request->session()->read('Auth.User.role') == "Patient") && ($event->event_type_id == 2)) {
$event->status = 'PaymentPending';
}
//echo "<pre>",print_r($event),"</pre>"; exit;
Log::Debug("saving" . $event);
if ($this->Events->save($event, array('validate' => 'only'))) {
Log::debug("Event saved");
// Link patient is causing trouble
//commenting out for now
//
// $this->linkPatient($event);
// $this->sendNotification($event);
$url = Router::url(['controller' => 'Events', 'action' => 'edit', $event->id, '_full' => true]);
$link = $this->Shortner->shortUrl($url);
$notificationManager = new NotificationManager();
$facility_admin_ids = [];
$i = 0;
$Users = TableRegistry::get('users');
$facilities = TableRegistry::get('facilities');
$facility_admins = TableRegistry::get('facility_admins');
$facility = $facilities->findById($this->request->data['facility_id'])->first();
$user = $Users->get($this->request->session()->read('Auth.User.id'));
if ($event->event_type_id == 1) {
$event_type = "Inhouse";
} else {
$event_type = "Teleconsult";
}
$role = $this->request->session()->read('Auth.User.role');
if ($role == "Provider") {
$patient = $this->Events->Patients->findById($this->request->data['patient_id'])->first();
$patient_user = $Users->get($patient->user_id);
$f_admins = $facility_admins->findByFacilityId($this->request->data['facility_id']);
foreach ($f_admins as $f_admin):
$facility_admin_ids[$i] = $f_admin->user_id;
$i++;
endforeach;
$notificationManager->addRecipientList('providerAddAppointmentToPatient', [$patient->user_id]);
$notificationManager->addRecipientList('providerAddAppointmentToFacilityAdmins', $facility_admin_ids);
$notificationManager->addRecipientList('providerAddAppointmentToSelf', [$user->id]);
$this->Notifier->notify([
'recipientLists' => 'providerAddAppointmentToPatient',
'template' => 'providerAddAppointmentToPatient',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'providerAddAppointmentToFacilityAdmins',
'template' => 'providerAddAppointmentToFacilityAdmins',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'providerAddAppointmentToSelf',
'template' => 'providerAddAppointmentToSelf',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
}
// if status is 'PaymentPending' then we are sending notification after payment confirmed. [adds in payments controller]
if ($role == "Patient" && ($event->status != 'PaymentPending')) {
$provider = $this->Events->Providers->findById($this->request->data['provider_id'])->first();
$provider_user = $Users->find()->where(['id' => $provider->user_id])->first();
$f_admins = $facility_admins->findByFacilityId($this->request->data['facility_id']);
foreach ($f_admins as $f_admin):
$facility_admin_ids[$i] = $f_admin->user_id;
$i++;
endforeach;
$notificationManager->addRecipientList('patientAddAppointmentToProvider', [$provider->user_id]);
$notificationManager->addRecipientList('patientAddAppointmentToFacilityAdmins', $facility_admin_ids);
$notificationManager->addRecipientList('patientAddAppointmentToSelf', [$user->id]);
$this->Notifier->notify([
'recipientLists' => 'patientAddAppointmentToProvider',
'template' => 'patientAddAppointmentToProvider',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'patientAddAppointmentToFacilityAdmins',
'template' => 'patientAddAppointmentToFacilityAdmins',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'patientAddAppointmentToSelf',
'template' => 'patientAddAppointmentToSelf',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
}
if ($role == "FacilityAdmin") {
$provider = $this->Events->Providers->findById($this->request->data['provider_id'])->first();
$provider_user = $Users->find()->where(['id' => $provider->user_id])->first();
$patient = $this->Events->Patients->findById($this->request->data['patient_id'])->first();
$patient_user = $Users->get($patient->user_id);
$notificationManager->addRecipientList('facilityAdminAddAppointmentToPatient', [$patient->user_id]);
$notificationManager->addRecipientList('facilityAdminAddAppointmentToProvider', [$provider->user_id]);
$notificationManager->addRecipientList('facilityAdminAddAppointmentToSelf', [$user->id]);
$this->Notifier->notify([
'recipientLists' => 'facilityAdminAddAppointmentToPatient',
'template' => 'facilityAdminAddAppointmentToPatient',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'facilityAdminAddAppointmentToProvider',
'template' => 'facilityAdminAddAppointmentToProvider',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'facilityAdminAddAppointmentToSelf',
'template' => 'facilityAdminAddAppointmentToSelf',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
}
if ($event->event_type_id == 2) {
Log::Debug("Event type is Teleconsult. Creating zoom meeting for " . $event->provider_id);
$zoomuser = $this->Events->Providers->Zoomusers->findByProviderId($event->provider_id)->first();
//$test = $this->Events->Zoommeetings->testecho("testecho");
// $this->set(compact('test'));
$result = $this->Events->Zoommeetings->createMeeting($zoomuser, $event->id, $event->title, $event->start);
if (isset($result)) {
Log::Debug("Zoom meeting created");
$event->zoommeeting_id = $result->id;
$this->Events->save($event);
}
Log::Debug("Zoom user" . $zoomuser);
$this->set(compact('zoomuser'));
if ($this->request->session()->read('Auth.User.role') == "Patient") {
$slots = 0;
$start_time = $event->start;
$end_time = $event->end;
$since_start = $start_time->diff($end_time);
if ($since_start->h != 0) {
$slots = ($since_start->h * 60) / $provider->telecon_slot_time;
}
$slots = $slots + ($since_start->i) / $provider->telecon_slot_time;
return $this->redirect(['controller' => 'Payments', 'action' => 'add', '?' => ['patient_id' => $event->patient_id, 'event_id' => $event->id], 'provider_id' => $event->provider_id, 'facility_id' => $event->facility_id, 'slots' => $slots]);
}
}
$this->Flash->success(__('The appointment has been saved.'));
return $this->redirect(['controller' => 'EventCalendar', 'action' => 'index']);
} else {
Log::Debug("Save Failed" . var_dump($event->errors));
$this->Flash->error(__('The appointment could not be saved. Please, try again.'));
}
}
$eventTypes = $this->Events->EventTypes->find('list', ['limit' => 200]);
if (empty($providers)) {
$providers = $this->Events->Providers->find('list', [
'keyField' => 'id',
'valueField' => 'user.first_name'
])->contain(['Users']);
}
$facilities = $this->Events->Facilities->find('list', ['limit' => 200]);
$issueTypes = $this->Events->Issues->IssueTypes->find('list', ['limit' => 200]);
$this->set(compact('event', 'eventTypes', 'slot_type', 'providers', 'patients', 'facilities', 'issueTypes'));
$this->set('_serialize', ['event']);
}
/**
* Add method
*
* @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
*/
public function add($provider__user_id = null, $patient_user_id = null, $facility_id = null)
{
if ($this->request->session()->read('Auth.User.role') == "Provider" || !empty($provider__user_id)) {
if (empty($provider_user_id))
$provider__user_id = $this->request->session()->read('Auth.User.id');
$providers = $this->Events->Providers->findByUserId($provider__user_id);
$provider = $providers->first();
$this->set(compact('provider'));
}
if ($this->request->session()->read('Auth.User.role') == "Patient" || !empty($patient_user_id)) {
if (empty($patient_user_id))
$patient_user_id = $this->request->session()->read('Auth.User.id');
$patients = $this->Events->Patients->findByUserId($patient_user_id);
$patient = $patients->first();
$this->set(compact('patient'));
}
if (!empty($facility_id)) {
$facilities = $this->Events->Facilities->findById($facility_id);
$facility = $facilities->first();
$this->set(compact('facility'));
}
$event = $this->Events->newEntity();
if ($this->request->is('post')) {
$event = $this->Events->patchEntity($event, $this->request->data);
if ($this->Events->save($event)) {
$url = Router::url(['controller' => 'Events', 'action' => 'edit', $event->id, '_full' => true]);
$link = $this->Shortner->shortUrl($url);
//$this->sendNotification($event);
//Notifications
$notificationManager = new NotificationManager();
$facility_admin_ids = [];
$i = 0;
$users = TableRegistry::get('users');
$facilities = TableRegistry::get('facilities');
$facility_admins = TableRegistry::get('facility_admins');
$facility = $facilities->findById($this->request->data['facility_id'])->first();
$user = $users->find()->where(['id' => $this->request->session()->read('Auth.User.id')])->first();
if ($event->event_type_id == 1) {
$event_type = "Inhouse";
} else {
$event_type = "Teleconsult";
}
$role = $this->request->session()->read('Auth.User.role');
if ($role == "Provider") {
$patient = $this->Events->Patients->findById($this->request->data['patient_id'])->first();
$patient_user = $users->get($patient->user_id);
$f_admins = $facility_admins->findByFacilityId($this->request->data['facility_id']);
foreach ($f_admins as $f_admin):
$facility_admin_ids[$i] = $f_admin->user_id;
$i++;
endforeach;
$notificationManager->addRecipientList('providerAddAppointmentToPatient', [$patient->user_id]);
$notificationManager->addRecipientList('providerAddAppointmentToFacilityAdmins', $facility_admin_ids);
$notificationManager->addRecipientList('providerAddAppointmentToSelf', [$user->id]);
$this->Notifier->notify([
'recipientLists' => 'providerAddAppointmentToPatient',
'template' => 'providerAddAppointmentToPatient',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => $event->end,
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'providerAddAppointmentToFacilityAdmins',
'template' => 'providerAddAppointmentToFacilityAdmins',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => $event->end,
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'providerAddAppointmentToSelf',
'template' => 'providerAddAppointmentToSelf',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => $event->end,
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
}
if ($role == "Patient") {
$provider = $this->Events->Providers->findById($this->request->data['provider_id'])->first();
$provider_user = $users->find()->where(['id' => $provider->user_id])->first();
$f_admins = $facility_admins->findByFacilityId($this->request->data['facility_id']);
foreach ($f_admins as $f_admin):
$facility_admin_ids[$i] = $f_admin->user_id;
$i++;
endforeach;
$notificationManager->addRecipientList('patientAddAppointmentToProvider', [$provider->user_id]);
$notificationManager->addRecipientList('patientAddAppointmentToFacilityAdmins', $facility_admin_ids);
$notificationManager->addRecipientList('patientAddAppointmentToSelf', [$user->id]);
$this->Notifier->notify([
'recipientLists' => 'patientAddAppointmentToProvider',
'template' => 'patientAddAppointmentToProvider',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => $event->end,
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'patientAddAppointmentToFacilityAdmins',
'template' => 'patientAddAppointmentToFacilityAdmins',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => $event->end,
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'patientAddAppointmentToSelf',
'template' => 'patientAddAppointmentToSelf',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
}
if ($role == "FacilityAdmin") {
$provider = $this->Events->Providers->findById($this->request->data['provider_id'])->first();
$provider_user = $users->find()->where(['id' => $provider->user_id])->first();
$patient = $this->Events->Patients->findById($this->request->data['patient_id'])->first();
$patient_user = $users->get($patient->user_id);
$notificationManager->addRecipientList('facilityAdminAddAppointmentToPatient', [$patient->user_id]);
$notificationManager->addRecipientList('facilityAdminAddAppointmentToProvider', [$provider->user_id]);
$notificationManager->addRecipientList('facilityAdminAddAppointmentToSelf', [$user->id]);
$this->Notifier->notify([
'recipientLists' => 'facilityAdminAddAppointmentToPatient',
'template' => 'facilityAdminAddAppointmentToPatient',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'facilityAdminAddAppointmentToProvider',
'template' => 'facilityAdminAddAppointmentToProvider',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'facilityAdminAddAppointmentToSelf',
'template' => 'facilityAdminAddAppointmentToSelf',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
}
if ($event->event_type_id == 2) {
Log::Debug("Event type is Teleconsult. Creating zoom meeting for " . $event->provider_id);
$zoomuser = $this->Events->Providers->Zoomusers->findByProviderId($event->provider_id)->first();
$result = $this->Events->Zoommeetings->createMeeting($zoomuser, $event->id, $event->title, $event->start);
if (isset($result)) {
Log::Debug("Zoom meeting created");
$event->zoommeeting_id = $result->id;
$this->Events->save($event);
Log::Debug("Event updated");
}
Log::Debug("Zoom user" . $zoomuser);
//$this->set(compact('zoomuser'));
}
$this->Flash->success(__('The appointment has been saved.'));
return $this->redirect(['controller' => 'EventCalendar', 'action' => 'index']);
} else {
$this->Flash->error(__('The appointment could not be saved. Please, try again.'));
}
}
$eventTypes = $this->Events->EventTypes->find('list', ['limit' => 200]);
$providers = $this->Events->Providers->find('list', [
'keyField' => 'id',
'valueField' => 'user.first_name'
])->contain(['Users']);
$patients = $this->Events->Patients->find('list', [
'keyField' => 'id',
'valueField' => 'user.first_name'
])->contain(['Users']);
$facilities = $this->Events->Facilities->find('list', ['limit' => 200]);
$this->set(compact('event', 'eventTypes', 'slot_type', 'providers', 'patients', 'facilities'));
$this->set('_serialize', ['event']);
}
/**
* Edit method
*
* @param string|null $id Event id.
* @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$event = $this->Events->get($id);
//dump($event);
if ($this->request->is(['patch', 'post', 'put'])) {
$event = $this->Events->patchEntity($event, $this->request->data);
$event->start = Time::parse($this->request->data['start']);
$event->end = Time::parse($this->request->data['end']);
//debug($this->request->data['start']);
//debug($this->request->data['end']);
//$s = explode("-", $this->request->data['new-date']);
//debug(new Time($s[0]));
//debug(new Time($s[1]));
//debug($this->request->data['new-date']);
//exit;
if ($this->Events->save($event)) {
$url = Router::url(['controller' => 'Events', 'action' => 'edit', $id, '_full' => true]);
$link = $this->Shortner->shortUrl($url);
//Notifications
$notificationManager = new NotificationManager();
$facility_admin_ids = [];
$i = 0;
$Users = TableRegistry::get('users');
$facilities = TableRegistry::get('facilities');
$facility_admins = TableRegistry::get('facility_admins');
$facility = $facilities->get($event->facility_id);
if ($event->event_type_id == 1) {
$event_type = "Inhouse";
} else {
$event_type = "Teleconsult";
}
$user = $Users->find()->where(['id' => $this->request->session()->read('Auth.User.id')])->first();
$role = $this->request->session()->read('Auth.User.role');
if ($role == "Provider") {
$patient = $this->Events->Patients->findById($event->patient_id)->first();
$patient_user = $Users->get($patient->user_id);
$f_admins = $facility_admins->findByFacilityId($event->facility_id);
foreach ($f_admins as $f_admin):
$facility_admin_ids[$i] = $f_admin->user_id;
$i++;
endforeach;
$notificationManager->addRecipientList('providerUpdateAppointmentToPatient', [$patient->user_id]);
$notificationManager->addRecipientList('providerUpdateAppointmentToFacilityAdmins', $facility_admin_ids);
$notificationManager->addRecipientList('providerUpdateAppointmentToSelf', [$user->id]);
$this->Notifier->notify([
'recipientLists' => 'providerUpdateAppointmentToPatient',
'template' => 'providerUpdateAppointmentToPatient',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'providerUpdateAppointmentToFacilityAdmins',
'template' => 'providerUpdateAppointmentToFacilityAdmins',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'providerUpdateAppointmentToSelf',
'template' => 'providerUpdateAppointmentToSelf',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
}
/*
if ($role == "Patient") {
$provider = $this->Events->Providers->findById($event->provider_id)->first();
$provider_user = $Users->find()->where(['id' => $provider->user_id])->first();
$f_admins = $facility_admins->findByFacilityId($event->facility_id);
foreach ($f_admins as $f_admin):
$facility_admin_ids[$i] = $f_admin->user_id;
$i++;
endforeach;
$notificationManager->addRecipientList('patientUpdateAppointmentToProvider', [$provider->user_id]);
$notificationManager->addRecipientList('patientUpdateAppointmentToFacilityAdmins', $facility_admin_ids);
$notificationManager->addRecipientList('patientUpdateAppointmentToSelf', [$user->id]);
$this->Notifier->notify([
'recipientLists' => 'patientUpdateAppointmentToProvider',
'template' => 'patientUpdateAppointmentToProvider',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'patientUpdateAppointmentToFacilityAdmins',
'template' => 'patientUpdateAppointmentToFacilityAdmins',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'patientUpdateAppointmentToSelf',
'template' => 'patientUpdateAppointmentToSelf',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
}
*/
if ($role == "FacilityAdmin") {
$provider = $this->Events->Providers->findById($event->provider_id)->first();
$provider_user = $Users->find()->where(['id' => $provider->user_id])->first();
//$patient = $this->Events->Patients->findById($this->request->data['patient_id'])->first();
$patient = $this->Events->Patients->findById($event->patient_id)->first();
$patient_user = $Users->get($patient->user_id);
$notificationManager->addRecipientList('facilityAdminUpdateAppointmentToPatient', [$patient->user_id]);
$notificationManager->addRecipientList('facilityAdminUpdateAppointmentToProvider', [$provider->user_id]);
$notificationManager->addRecipientList('facilityAdminUpdateAppointmentToSelf', [$user->id]);
$this->Notifier->notify([
'recipientLists' => 'facilityAdminUpdateAppointmentToPatient',
'template' => 'facilityAdminUpdateAppointmentToPatient',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'facilityAdminUpdateAppointmentToProvider',
'template' => 'facilityAdminUpdateAppointmentToProvider',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
$this->Notifier->notify([
'recipientLists' => 'facilityAdminUpdateAppointmentToSelf',
'template' => 'facilityAdminUpdateAppointmentToSelf',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'role' => $role,
'provider_name' => $provider_user->first_name,
'facility_name' => $facility->name,
'patient_name' => $patient_user->first_name,
'event_type' => $event_type,
'start_date' => date_format($event->start, 'd/m/y'),
'start_time' => date_format($event->start, 'g:i A'),
'end_date' => date_format($event->end, 'Y-m-d H:i:s'),
'hyperlink' => $link,
],
], $this->request->session()->read('Auth.User.id'));
}
$this->Flash->success(__('The appointment has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The appointment could not be saved. Please, try again.'));
}
}
$eventTypes = $this->Events->EventTypes->find('list', ['limit' => 200]);
$this->set(compact('event', 'eventTypes'));
$this->set('_serialize', ['event']);
}
/**
* Delete method
*
* @param string|null $id Event id.
* @return \Cake\Network\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$event = $this->Events->get($id);
if ($this->Events->delete($event)) {
$this->Flash->success(__('The appointment has been deleted.'));
} else {
$this->Flash->error(__('The appointment could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
public function cancelAppointmentApi($event_id = null, $status = 'Cancelled')
{
if (empty($event_id))
$event_id = isset($this->request->query['event_id']) ? $this->request->query['event_id'] : null;
if (empty($event_id))
$event_id = isset($this->request->data['event_id']) ? $this->request->data['event_id'] : null;
$event = $this->Events->get($event_id);
$event->status = $status;
if ($this->Events->save($event)) {
$data['status'] = 'Success';
Log::debug("cancelAppointmentApi" . $data['status']);
} else {
$data['status'] = 'Failure';
Log::debug("cancelAppointmentApi" . $data['status']);
}
$this->set(array(
'data' => $data,
'_serialize' => 'data'
));
}
public function changeStatus($id = null, $status)
{
$this->request->allowMethod(['post', 'cancel']);
$event = $this->Events->get($id);
$event->status = $status;
if ($this->Events->save($event)) {
$this->Flash->success(__('The appointment has been:' . $status));
} else {
$this->Flash->error(__('The appointment could not be modified. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
public function arrived($id = null)
{
$this->request->allowMethod(['post', 'cancel']);
$event = $this->Events->get($id);
$event->arrived = Time::now();
if ($this->Events->save($event)) {
$this->Flash->success(__('Patient Arrived:' . $event->arrived));
} else {
$this->Flash->error(__('Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
public function checkin($id = null)
{
$this->request->allowMethod(['post', 'cancel']);
$event = $this->Events->get($id);
$event->checkin = Time::now();
if ($this->Events->save($event)) {
$this->Flash->success(__('Patient Checked In:' . $event->checkin));
} else {
$this->Flash->error(__('Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
public function checkout($id = null)
{
$this->request->allowMethod(['post', 'cancel']);
$event = $this->Events->get($id);
$event->checkout = Time::now();
if ($this->Events->save($event)) {
$this->Flash->success(__('Patient Checked Out:' . $event->checkout));
} else {
$this->Flash->error(__('Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
// The feed action is called from "webroot/js/ready.js" to get the list of events (JSON)
public function feed($id = null)
{
$role = $this->request->session()->read('Auth.User.role');
// $conditions = array('conditions' => array('UNIX_TIMESTAMP(start) >=' => $vars['start'], 'UNIX_TIMESTAMP(start) <=' => $vars['end']));
if ($this->request->session()->read('Auth.User.is_superuser') == "1") {
$events = $this->Events->find('all')
->contain(['Zoommeetings', 'Facilities', 'Providers', 'Patients' => ['Users']]);
}
if ($role == "Provider") {
$provider_user_id = $this->request->session()->read('Auth.User.id');
$providers = $this->Events->Providers->findByUserId($provider_user_id);
$provider = $providers->first();
$events = $this->Events->findByProviderId($provider->id)
->contain(['Zoommeetings', 'Facilities', 'Providers', 'Patients' => ['Users']]);
}
if ($role == "Patient") {
$patient_user_id = $this->request->session()->read('Auth.User.id');
$patients = $this->Events->Patients->findByUserId($patient_user_id);
$patient = $patients->first();
$events = $this->Events->findByPatientId($patient->id)
->contain(['Zoommeetings', 'Facilities', 'Providers', 'Patients' => ['Users']]);
}
if ($role == "FacilityAdmin") {
$facility_user_id = $this->request->session()->read('Auth.User.id');
$facilities = $this->Events->Facilities->find('all')->matching('FacilityAdmins')->where(['FacilityAdmins.user_id' => $facility_user_id]);
$facility = $facilities->first();
$events = $this->Events->findByFacilityId($facility->id)
->contain(['Zoommeetings', 'Facilities', 'Providers', 'Patients' => ['Users']]);
}
$vars = $this->params['url'];
Log::debug("feed vars:" . $vars);
Log::debug("feed events:" . $events);
foreach ($events as $event) {
Log::debug("feed event:" . $event);
if ($event->all_day == 1) {
$allday = true;
$end = $event->start;
} else {
$allday = false;
$end = $event->end;
}
$teleurl = null;
$color = "red";
if (isset($event->zoommeeting)) {
if ($event->status == "Confirmed")
$color = "blue";
else
$color = "brown";
if ($role == "Provider") {
$teleurl = $event->zoommeeting->start_url;
} else
$teleurl = $event->zoommeeting->join_url;
} else {
if ($event->status == "Confirmed")
$color = "purple";
}
$encounter_url = null;
$facility_id = null;
$facility = null;
if (isset($event->facility)) {
$facility = $event->facility->name;
$facility_id = $event->facility->id;
}
if (($role == "Provider") || ($role == "FacilityAdmin")) {
$encounter_url = "/encounters/add?event_id=" . $event->id;
}
$patient_uuid = null;
$patient = null;
if (isset($event->patient)) {
$patient_uuid = $event->patient->patient_uuid;
$patient = $event->patient->user->first_name;
}
$data[] = array(
'id' => $event->id,
'title' => $event->title,
'start' => $event->start,
'end' => $end,
'allDay' => $allday,
// 'url' => '/events/view/'.$event->id,
'patient_uuid' => $patient_uuid,
'patient' => $patient,
'facility' => $facility,
'url' => '/events/edit/' . $event->id,
'teleurl' => $teleurl,
'encounterUrl' => $encounter_url,
'description' => $event->details,
'color' => $color,
'status' => $event->status,
'resource' => $facility->id
);
}
// $data = current($data);
$this->set(array(
'data' => $data,
'_serialize' => 'data'
));
// $this->set("data", $data);
}
// The feed action is called from "webroot/js/ready.js" to get the list of events (JSON)
// using for patient_cal.ctp
public function patientfeed($id = null)
{
if (!empty($start)) {
$this->set(compact('start'));
Log::Debug("Start" . $start);
}
if (!empty($end)) {
$this->set(compact('end'));
Log::Debug("end" . $end);
}
$role = $this->request->session()->read('Auth.User.role');
if ($role == "Patient") {
$patient_user_id = $this->request->session()->read('Auth.User.id');
$patients = $this->Events->Patients->findByUserId($patient_user_id);
$current_patient = $patients->first();
$events = $this->Events->findByPatientId($current_patient->id)
->contain(['Zoommeetings', 'Facilities', 'Providers' => ['Users'], 'Patients' => ['Users']]);
$vars = $this->params['url'];
Log::debug("feed vars:" . $vars);
//Log::debug("feed events:" . $events);
foreach ($events as $event) {
Log::debug("feed event:" . $event);
$allday = false;
$end = $event->end;
$teleurl = null;
$color = "red";
if (isset($event->zoommeeting)) {
$color = "blue";
$teleurl = $event->zoommeeting->join_url;
} else {
if ($event->status == "Confirmed")
$color = "purple";
}
$encounter_url = null;
$facility_id = null;
$facility = null;
if (isset($event->facility)) {
$facility = $event->facility->name;
$facility_id = $event->facility->id;
}
$patient_uuid = null;
$patient = null;
if (isset($event->patient)) {
$patient_uuid = $event->patient->patient_uuid;
$patient = $event->patient->user->first_name;
}
if (isset($event->provider)) {
$provider = $event->provider->user->first_name;
}
if (!empty($current_patient)) {
if ($current_patient->patient_uuid != $patient_uuid) {
$event->title = "busy";
$teleurl = null;
$url = null;
$patient_uuid = "Hidden";
$patient = "Hidden";
}
}
$patient_uuid = !$event->patient->external_patient_id ? $event->patient->patient_uuid : $event->patient->external_patient_id;
$data[] = array(
'id' => $event->id,
'user_role' =>$this->request->session()->read('Auth.User.role'),
'title' => $event->title,
'start' => $event->start,
'end' => $end,
'allDay' => $allday,
// 'url' => '/events/view/'.$event->id,
'patient_uuid' => $patient_uuid,
'provider' => $provider,
'patient' => $patient,
'facility' => $facility,
'url' => '/events/edit/' . $event->id,
'teleurl' => $teleurl,
'description' => $event->details,
'status' => $event->status,
'color' => $color
);
}
}
// $data = current($data);
$this->set(array(
'data' => $data,
'_serialize' => 'data'
));
// $this->set("data", $data);
}
// The feed action is called from "webroot/js/ready.js" to get the list of events (JSON)
public function feedbyprovider($id = null)
{
$role = $this->request->session()->read('Auth.User.role');
if ($role == "Provider") {
$provider_user_id = $this->request->session()->read('Auth.User.id');
$providers = $this->Events->Providers->findByUserId($provider_user_id);
$provider = $providers->first();
$events = $this->Events->findByProviderId($provider->id)
->contain(['Zoommeetings', 'Providers', 'Facilities', 'Patients' => ['Users']]);
$vars = $this->params['url'];
Log::debug("feed vars:" . $vars);
Log::debug("feed events:" . $events);
foreach ($events as $event) {
Log::debug("feed event:" . $event);
$allday = false;
$end = $event->end;
$teleurl = null;
$color = "red";
if (isset($event->zoommeeting)) {
$color = "blue";
$teleurl = $event->zoommeeting->start_url;
} else {
if ($event->status == "Confirmed")
$color = "purple";
}
$encounter_url = "/encounters/add?event_id=" . $event->id;
$facility_id = null;
$facility = null;
if (isset($event->facility)) {
$facility = $event->facility->name;
$facility_id = $event->facility->id;
}
$patient_uuid = null;
$patient = null;
if (isset($event->patient)) {
$patient_uuid = $event->patient->patient_uuid;
$patient = $event->patient->user->first_name;
}
$data[] = array(
'id' => $event->id,
'title' => $event->title,
'start' => $event->start,
'end' => $end,
'allDay' => $allday,
// 'url' => '/events/view/'.$event->id,
'patient_uuid' => $patient_uuid,
'patient' => $patient,
'facility' => $facility,
'url' => '/events/edit/' . $event->id,
'teleurl' => $teleurl,
'description' => $event->details,
'encounterUrl' => $encounter_url,
'color' => $color,
'status' => $event->status,
'resourceId' => $event->facility_id
);
}
}
// $data = current($data);
$this->set(array(
'data' => $data,
'_serialize' => 'data'
));
// $this->set("data", $data);
}
// The feed action is called from "webroot/js/ready.js" to get the list of events (JSON)
public function feedbyfacility($id = null)
{
$role = $this->request->session()->read('Auth.User.role');
if ($role == "FacilityAdmin") {
$facility_user_id = $this->request->session()->read('Auth.User.id');
$facilities = $this->Events->Facilities->find('all')
->matching('FacilityAdmins')->where(['FacilityAdmins.user_id' => $facility_user_id]);
$facility = $facilities->first();
$events = $this->Events->findByFacilityId($facility->id)
->contain(['Zoommeetings', 'Providers', 'Facilities', 'Patients' => ['Users']]);
$vars = $this->params['url'];
Log::debug("feed vars:" . $vars);
Log::debug("feed events:" . $events);
foreach ($events as $event) {
Log::debug("feed event:" . $event);
$allday = false;
$end = $event->end;
$teleurl = null;
$color = "red";
if (isset($event->zoommeeting)) {
$color = "blue";
$teleurl = $event->zoommeeting->start_url;
} else {
if ($event->status == "Confirmed")
$color = "purple";
}
$encounter_url = "/encounters/add?event_id=" . $event->id;
$facility_id = null;
$facility = null;
if (isset($event->facility)) {
$facility = $event->facility->name;
$facility_id = $event->facility->id;
}
$patient_uuid = null;
$patient = null;
if (isset($event->patient)) {
$patient_uuid = $event->patient->patient_uuid;
$patient = $event->patient->user->full_name;
}
$data[] = array(
'id' => $event->id,
'title' => $event->title,
'start' => $event->start,
'end' => $end,
'allDay' => $allday,
// 'url' => '/events/view/'.$event->id,
'patient_uuid' => $patient_uuid,
'patient' => $patient,
'facility' => $facility,
'url' => '/events/edit/' . $event->id,
'teleurl' => $teleurl,
'description' => $event->details,
'encounterUrl' => $encounter_url,
'color' => $color,
'status' => $event->status,
'resourceId' => $event->provider_id
);
}
}
// $data = current($data);
$this->set(array(
'data' => $data,
'_serialize' => 'data'
));
// $this->set("data", $data);
}
// The update action is called from "webroot/js/ready.js" to update date/time when an event is dragged or resized
function update()
{
$vars = $this->params['url'];
$this->Event->id = $vars['id'];
$this->Event->saveField('start', $vars['start']);
$this->Event->saveField('end', $vars['end']);
$this->Event->saveField('all_day', $vars['allday']);
}
function linkPatient($event)
{
$patient = $this->Events->Patients->get($event->patient_id, ['contain' => ['Users']]);
$provider = $this->Events->Providers->get($event->provider_id);
$facility = $this->Events->Facilities->get($event->facility_id);
Log::debug("Linking Provider: $provider->id with patient: $patient->id");
// Log::debug("Provider: $provider");
$this->Events->Providers->Patients->unlink($provider, [$patient]);
$this->Events->Providers->Patients->link($provider, [$patient]);
// $provider = $this->Events->Providers->patchEntity($provider, [$provider], [
// 'associated' => ['Patients']]);
$this->Events->Providers->save($provider, ['associated' => ['Patients']]);
$patient = $this->Events->Patients->get($event->patient_id);
Log::debug("Linking Facility: $facility->id with patient: $patient->id");
$this->Events->Facilities->Patients->unlink($facility, [$patient]);
$this->Events->Facilities->Patients->link($facility, [$patient]);
$this->Events->Facilities->save($facility, ['associated' => ['Patients']]);
}
//using on patientDashboard - from today
// only see confirmed event
public function nextAppointment($patient_id)
{
$start = Time::now()->toDateString(); // today
$event = $this->Events->findByPatientId($patient_id)
->contain(['Facilities', 'Providers' => ['Users'],'Zoommeetings'])
->where(['Events.start >= :now'])
->andWhere(['status' => "Confirmed"])
->bind(':now', $start, 'time')->first();
$this->set(compact('event'));
}
//using on patientDashboard - last recent appointment
public function lastAppointment($patient_id)
{
$start = Time::now()->toDateString(); // today
$event = $this->Events->findByPatientId($patient_id)
->contain(['Facilities', 'Providers' => ['Users']])
->where(['Events.start < :now'])
->order(['Events.id' => 'DESC'])
->bind(':now', $start, 'time')
->first();
$this->set(compact('event'));
}
public function updateCsv()
{
/** File must be related to exactly on provider.
* ##### CSV file contains no heading and arranged in following order
* ####
*
* dump($data[0]) => date
* dump($data[1]) => patient_ P _ id
* dump($data[3]) => schedule at ( time )
*/
ini_set('memory_limit', '-1');
set_time_limit(0);
if ($this->Auth->user('role') == "user") {
$patients = TableRegistry::get('Patients');
$facilityTable = TableRegistry::get('Facilities');
$usersTable = TableRegistry::get('Users');
$Providers = TableRegistry::get('Providers');
$facilities = $facilityTable->find('list');
$providersList = $usersTable->find('list', [
'keyField' => 'id',
'valueField' => 'first_name'])
->where(['role' => 'Provider']);
if ($this->request->is('post') && $_FILES['csv']['size'] > 0) {
$facility = $this->request->data['facilities'];
$provider = $this->request->data['providersList'];
$event_type_id = 1; // set to default to 1 [ inhouse ]
$status = "confirmed";
$file = $this->request->data['csv']['tmp_name'];
$handle = fopen($file, "r");
$path = pathinfo($this->request->data['csv']['name'], PATHINFO_FILENAME);
$facility_label = explode('_', $path)[0];
$facility_id = $facility['_ids'][0];
$provider_id = $Providers->findByUserId($provider['_ids'][0])->first()->id;
while (($data = fgetcsv($handle, ",")) !== false) {
//Log::debug("P-ID:" . $data[1]);
try {
$patient = $patients->find()
->where(['external_patient_id' => $facility_label . "_" . $data[1]])
->first();
} catch (Exception $exception) {
Log::debug($data[1] . " " . "not saved");
Log::error("P-ID:" . $data[1] . "error" . $exception->getMessage());
}
if ($patient) {
Log::debug($data[1]);
$patient_id = $patient->id;
$event = $this->Events->newEntity();
$time = " {$data[2]}" . ":00";
$date = date_format(date_create_from_format('j M Y', implode(" ", explode(", ", $data[0]))), 'Y-m-d');
$data['start'] = $date . $time;
$data['patient_id'] = $patient_id;
$data['provider_id'] = $provider_id;
$data['facility_id'] = $facility_id;
$data['event_type_id'] = $event_type_id;
$data['title'] = ' ';
$data['details'] = ' ';
$data['status'] = $status;
unset($data[0], $data[1], $data[2], $data[3]);
$event = $this->Events->patchEntity($event, $data, [
'associated' => ['EventTypes']
]);
$event->start = $data['start'];
$this->Events->save($event);
//debug($event); exit;
}
}
}
$this->Flash->success(__("Updated successfully."));
}
$this->set(compact('facilities'));
$this->set(compact('providersList'));
}
public function cancelAppointment($event_id) {
$this->autoRender = false;
$notificationManager = new NotificationManager();
$event = $this->Events->get($event_id);
$event->status = "Cancelled";
$this->Events->save($event);
$patient = $this->Events->Patients->findById($event->patient_id)->first();
$patient_user = $this->Events->Patients->Users->findById($patient->user_id)->first();
$notificationManager->addRecipientList('cancelTeleAppointmentByPatient', [$patient->user_id]);
$this->Notifier->notify([
'recipientLists' => 'cancelTeleAppointmentByPatient',
'template' => 'cancelTeleAppointmentByPatient',
'vars' => [
'patient_name' => $patient_user->first_name,
],
], $this->request->session()->read('Auth.User.id'));
$this->Flash->success("Your last tele-appointment has been cancelled.");
return $this->redirect(['controller' => 'Providers', 'action' => 'myDoctors']);
$this->set(compact('event'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment