/**
  * Fetches all the options for this attribute_element, standard _and_ subspecialty specific
  * @param integer $subspecialty_id
  * @return OphCiExamination_AttributeOption[]
  */
 public function findAllOptionsForSubspecialty($subspecialty_id = null)
 {
     $condition = 'attribute_element_id = :attribute_element_id AND ';
     $params = array(':attribute_element_id' => $this->id);
     if ($subspecialty_id) {
         $condition .= '(subspecialty_id = :subspecialty_id OR subspecialty_id IS NULL)';
         $params[':subspecialty_id'] = $subspecialty_id;
     } else {
         $condition .= 'subspecialty_id IS NULL';
     }
     return OphCiExamination_AttributeOption::model()->findAll($condition, $params);
 }
    /**
     * Fetches all the attributes for an element_type filtered by subspecialty
     * Options are stashed in attribute_options property for easy iteration.
     *
     * @param int  $element_type_id
     * @param int  $subspecialty_id
     * @param bool $include_descendents
     *
     * @return OphCiExamination_Attribute[]
     */
    public function findAllByElementAndSubspecialty($element_type_id, $subspecialty_id = null, $include_descendents = true)
    {
        $criteria = new \CDbCriteria();
        $criteria->select = 't.*';
        $criteria->distinct = true;
        $element_type_ids = array($element_type_id);
        if ($include_descendents) {
            $element_type = \ElementType::model()->findByPk($element_type_id);
            foreach ($element_type->getDescendents() as $descendent) {
                $element_type_ids[] = $descendent->id;
            }
        }
        $criteria->addInCondition('attribute_element.element_type_id', $element_type_ids);
        if ($subspecialty_id) {
            $criteria->addCondition('subspecialty_id = :subspecialty_id OR subspecialty_id IS NULL');
            $criteria->params[':subspecialty_id'] = $subspecialty_id;
        } else {
            $criteria->addCondition('subspecialty_id IS NULL');
        }
        $criteria->join = 'JOIN ophciexamination_attribute_element attribute_element ON attribute_element.id = t.attribute_element_id
							JOIN ophciexamination_attribute attribute ON attribute_element.attribute_id = attribute.id';
        $criteria->order = 'attribute.display_order,attribute_element.attribute_id,t.display_order,t.id';
        $all_attribute_options = OphCiExamination_AttributeOption::model()->findAll($criteria);
        $attributes = array();
        $attribute = null;
        $attribute_options = array();
        foreach ($all_attribute_options as $attribute_option) {
            if (!$attribute || $attribute->id != $attribute_option->attribute_element->attribute_id) {
                if ($attribute) {
                    $attribute->attribute_options = array_values($attribute_options);
                    $attribute_options = array();
                    $attributes[] = $attribute;
                }
                $attribute = $attribute_option->attribute_element->attribute;
            }
            $attribute_options[] = $attribute_option;
        }
        if ($attribute) {
            $attribute->attribute_options = array_values($attribute_options);
            $attributes[] = $attribute;
        }
        return $attributes;
    }