public function addDiagnosis($disorder_id, $eye_id = false, $date = false) { if (!$date) { $date = date('Y-m-d'); } if (!($disorder = Disorder::model()->findByPk($disorder_id))) { throw new Exception('Disorder not found: ' . $disorder_id); } if ($disorder->specialty_id) { $type = strtolower(Specialty::model()->findByPk($disorder->specialty_id)->code); } else { $type = 'sys'; } if (!($sd = SecondaryDiagnosis::model()->find('patient_id=? and disorder_id=? and eye_id=? and date=?', array($this->id, $disorder_id, $eye_id, $date)))) { $action = "add-diagnosis-{$type}"; $sd = new SecondaryDiagnosis(); $sd->patient_id = $this->id; $sd->disorder_id = $disorder_id; $sd->eye_id = $eye_id; $sd->date = $date; if (!$sd->save()) { throw new Exception('Unable to save secondary diagnosis: ' . print_r($sd->getErrors(), true)); } $this->audit('patient', $action); } }
/** * if a secondary diagnosis disorder id has been set, we need to ensure its created on the patient. * * @see parent::beforeSave() */ public function beforeSave() { $curr_sd = $this->_getSecondaryDiagnosis(); if ($this->secondarydiagnosis_disorder_id && $curr_sd && $curr_sd->disorder_id != $this->secondarydiagnosis_disorder_id) { // looks like this is an edit and the previous secondary diagnosis should be removed // so we can set the correct value $curr_disorder = $curr_sd->disorder; $curr_sd->delete(); $curr_sd = null; Yii::app()->user->setFlash('warning.alert', "Disorder '" . $curr_disorder->term . "' has been removed because DR Grading diagnosis was updated."); } if (!$curr_sd) { // need to determine if we are setting a specific disorder on the patient, or a generic diabetes // diagnosis (which is implied by recording DR) $patient = $this->event->episode->patient; $sd = null; if ($this->secondarydiagnosis_disorder_id) { // no secondary diagnosis has been set by this element yet but one has been // assigned (i.e. the element is being created with a diabetes type) // final check to ensure nothing has changed whilst processing if (!$patient->hasDisorderTypeByIds(array_merge(\Disorder::$SNOMED_DIABETES_TYPE_I_SET, \Disorder::$SNOMED_DIABETES_TYPE_II_SET))) { $sd = new \SecondaryDiagnosis(); $sd->patient_id = $patient->id; $sd->disorder_id = $this->secondarydiagnosis_disorder_id; } else { // clear out the secondarydiagnosis_disorder_id $this->secondarydiagnosis_disorder_id = null; // reset required flag as patient now has a diabetes type $this->secondarydiagnosis_disorder_required = false; } } elseif (!$patient->hasDisorderTypeByIds(\Disorder::$SNOMED_DIABETES_SET)) { // Set the patient to have diabetes $sd = new \SecondaryDiagnosis(); $sd->patient_id = $patient->id; $sd->disorder_id = \Disorder::SNOMED_DIABETES; } if ($sd !== null) { $sd->save(); \Audit::add('SecondaryDiagnosis', 'add', $sd->id, null, array('patient_id' => $patient->id)); $this->secondarydiagnosis_id = $sd->id; Yii::app()->user->setFlash('info.info', "Disorder '" . $sd->disorder->term . "' has been added to patient by DR Grading."); } } return parent::beforeSave(); }