(C) Moorfields Eye Hospital NHS Foundation Trust, 2008-2011 (C) OpenEyes Foundation, 2011-2013 This file is part of OpenEyes. OpenEyes is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenEyes is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenEyes in a file titled COPYING. If not, see .
Author: OpenEyes (info@openeyes.org.uk)
Inheritance: extends BaseActiveRecordVersioned
 /**
  * sort the diagnoses (note that the diagnoses submitted may be a subset of all diagnoses, as the display_order
  * is set on the subsets for diagnoses that have parents.
  *
  * @param string $parent_id
  */
 public function actionSortDiagnoses()
 {
     if (!empty($_POST['order'])) {
         foreach ($_POST['order'] as $i => $id) {
             if ($disorder = OphCoTherapyapplication_TherapyDisorder::model()->findByPk($id)) {
                 $disorder->display_order = $i + 1;
                 if (!$disorder->save()) {
                     throw new Exception('Unable to save drug: ' . print_r($disorder->getErrors(), true));
                 }
             }
         }
     }
 }
 public function requiredIfSecondary($attribute, $params)
 {
     if ($params['side'] == 'left' && $this->eye_id != Eye::RIGHT || $params['side'] == 'right' && $this->eye_id != Eye::LEFT) {
         if ($this->{$params}['dependent'] && !$this->{$attribute}) {
             $criteria = new CDbCriteria();
             // FIXME: mysql dependent NULL check
             $criteria->condition = 'disorder_id = :did AND parent_id IS NULL';
             $criteria->params = array(':did' => $this->{$params}['dependent']);
             if ($td = OphCoTherapyapplication_TherapyDisorder::model()->with('disorder')->find($criteria)) {
                 if ($td->getLevel2Disorders()) {
                     $this->addError($attribute, $td->disorder->term . ' must be secondary to another diagnosis');
                 }
             }
         }
     }
 }
 /**
  * return all the disorders that are level 2 for the given $disorder_id.
  *
  * @param int $disorder_id
  *
  * @return Disorder[]
  */
 public function getLevel2Disorders($disorder_id)
 {
     $criteria = new CDbCriteria();
     $criteria->condition = 'parent_id IS NULL AND disorder_id = :did';
     $criteria->params = array(':did' => $disorder_id);
     $disorders = array();
     if ($td = OphCoTherapyapplication_TherapyDisorder::model()->find($criteria)) {
         $disorders = $td->getLevel2Disorders();
     }
     return $disorders;
 }
 /**
  * Set default values for the diagnosis element.
  *
  * This can't be done using setDefaultOptions on the element class because it needs to know about the episode
  *
  * @param Element_OphCoTherapyapplication_Therapydiagnosis $element
  */
 private function setDiagnosisDefaults(Element_OphCoTherapyapplication_Therapydiagnosis $element)
 {
     $episode = $this->episode;
     if ($episode) {
         // get the list of valid diagnosis codes
         $valid_disorders = OphCoTherapyapplication_TherapyDisorder::model()->findAll();
         $vd_ids = array();
         foreach ($valid_disorders as $vd) {
             $vd_ids[] = $vd->disorder_id;
         }
         // foreach eye
         $exam_api = Yii::app()->moduleAPI->get('OphCiExamination');
         foreach (array(Eye::LEFT, Eye::RIGHT) as $eye_id) {
             $prefix = $eye_id == Eye::LEFT ? 'left' : 'right';
             // get specific disorder from injection management
             if ($exam_api && ($exam_imc = $exam_api->getInjectionManagementComplexInEpisodeForSide($this->patient, $episode, $prefix))) {
                 $element->{$prefix . '_diagnosis1_id'} = $exam_imc->{$prefix . '_diagnosis1_id'};
                 $element->{$prefix . '_diagnosis2_id'} = $exam_imc->{$prefix . '_diagnosis2_id'};
             } elseif (($episode->eye_id == $eye_id || $episode->eye_id == Eye::BOTH) && in_array($episode->disorder_id, $vd_ids)) {
                 $element->{$prefix . '_diagnosis1_id'} = $episode->disorder_id;
             } else {
                 if ($exam_api) {
                     $disorders = $exam_api->getOrderedDisorders($this->patient, $episode);
                     foreach ($disorders as $disorder) {
                         if (($disorder['eye_id'] == $eye_id || $disorder['eye_id'] == Eye::BOTH) && in_array($disorder['disorder_id'], $vd_ids)) {
                             $element->{$prefix . '_diagnosis1_id'} = $disorder['disorder_id'];
                             break;
                         }
                     }
                 }
             }
         }
         // set the correct eye_id on the element for rendering
         if (isset($element->left_diagnosis1_id) && isset($element->right_diagnosis1_id)) {
             $element->eye_id = Eye::BOTH;
         } elseif (isset($element->left_diagnosis1_id)) {
             $element->eye_id = Eye::LEFT;
         } elseif (isset($element->right_diagnosis1_id)) {
             $element->eye_id = Eye::RIGHT;
         }
     }
 }