OEModule\OphCiExamination\models\Element_OphCiExamination_DRGrading::beforeSave PHP Method

beforeSave() public method

if a secondary diagnosis disorder id has been set, we need to ensure its created on the patient.
See also: parent::beforeSave()
public 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();
    }