protected function setElementDefaultOptions_Element_OphCiExamination_Diagnoses($element, $action)
{
if ($action == 'create') {
// set the diagnoses to match the current patient diagnoses for the episode
// and any other ophthalmic secondary diagnoses the patient has
$diagnoses = array();
if ($principal = $this->episode->diagnosis) {
$d = new models\OphCiExamination_Diagnosis();
$d->disorder_id = $principal->id;
$d->principal = true;
$d->eye_id = $this->episode->eye_id;
$diagnoses[] = $d;
}
foreach ($this->patient->getOphthalmicDiagnoses() as $sd) {
$d = new models\OphCiExamination_Diagnosis();
$d->disorder_id = $sd->disorder_id;
$d->eye_id = $sd->eye_id;
$diagnoses[] = $d;
}
// ensure unique
$_diagnoses = array();
foreach ($diagnoses as $d) {
$already_in = false;
foreach ($_diagnoses as $ad) {
if ($d->disorder_id == $ad->disorder_id) {
$already_in = true;
// set the eye correctly (The principal diagnosis for the episode is the first diagnosis, so
// no need to check that.
if ($d->eye_id != $ad->eye_id) {
$ad->eye_id = \Eye::BOTH;
}
break;
}
}
if (!$already_in) {
$_diagnoses[] = $d;
}
}
$element->diagnoses = $_diagnoses;
}
}