OEModule\OphCiExamination\models\Element_OphCiExamination_InjectionManagementComplex::updateQuestionAnswers PHP Method

updateQuestionAnswers() public method

store the answers for the questions asked for the $side diagnosis.
public updateQuestionAnswers ( string $side, array $update_answers )
$side string
$update_answers array - associate array of question id to answer value
    public function updateQuestionAnswers($side, $update_answers)
    {
        $current_answers = array();
        $save_answers = array();
        // note we operate on answers relation here, so that we avoid any custom assignment
        // that might have taken place for the purposes of validation (for $side_answers)
        // TODO: when looking at OE-2927 it might be better if we update the interventions in a different way
        // where the changes are stored when set for validation, and then afterSave is used to do the actual database changes
        foreach ($this->answers as $curr) {
            if ($curr->eye_id == $side) {
                $current_answers[$curr->question_id] = $curr;
            }
        }
        // go through each question answer, if there isn't one for this element,
        // create it and store for saving
        // if there is, check if the value is the same ... if it has changed
        // update and store for saving, otherwise remove from the current answers array
        // anything left in current answers at the end is ripe for deleting
        foreach ($update_answers as $question_id => $answer) {
            if (!array_key_exists($question_id, $current_answers)) {
                $s = new OphCiExamination_InjectionManagementComplex_Answer();
                $s->attributes = array('element_id' => $this->id, 'eye_id' => $side, 'question_id' => $question_id, 'answer' => $answer);
                $save_answers[] = $s;
            } else {
                if ($current_answers[$question_id]->answer != $answer) {
                    $current_answers[$question_id]->answer = $answer;
                    $save_answers[] = $current_answers[$question_id];
                }
                // don't want to delete this, so remove from list which we use later to delete
                unset($current_answers[$question_id]);
            }
        }
        // save what needs saving
        foreach ($save_answers as $save) {
            $save->save();
        }
        // delete any that are no longer relevant
        foreach ($current_answers as $curr) {
            $curr->delete();
        }
    }