/** * @param $id * @param $input * @param $organizations * @return bool * @throws GeneralException */ public function update($id, $input, $project) { $result = $this->findOrThrowException($id, true); //print_r($input); print_r($result);die(); $related_id = $input['related_data']['q']; if (!empty($related_id)) { $this->flushParent($related_id, $result); } $result->section = $input['section']; if ($input['report'] == 'none' || empty($input['report'])) { $result->report = null; } else { $result->report = $input['report']; } $result->answers = $input['answers']; $result->related_data = $input['related_data']; $result->answer_view = $input['answer_view']; $result->sameanswer = isset($input['sameanswer']) ? 1 : 0; $display['qnum'] = isset($input['display']['qnum']) ? 1 : 0; $display['result'] = isset($input['display']['result']) ? 1 : 0; $result->display = $display; //$result->save(); //$toUpdate = $result->whereQnum($input['qnum'])->whereProjectId($project['project_id'])->first(); //dd($toUpdate); if ($result->whereQnum($input['qnum'])->whereProjectId($project['project_id'])->first()->update($input)) { \App\Answers::where('status_id', $result->id)->delete(); foreach ($input['answer'] as $qnum => $answers) { $q = $this->questions->getQuestionByQnum($qnum, $project->id); foreach ($answers as $akey => $aval) { if ($akey == 'radio') { $answerkey = $aval; } else { $answerkey = $akey; } $qanswer = $q->qanswers->where('akey', $answerkey)->first(); if (in_array($qanswer->type, ['radio', 'checkbox'])) { $answerVal = $qanswer->value; } else { $answerVal = $aval; } $answerR = \App\Answers::firstOrNew(['qid' => $q->id, 'akey' => $answerkey, 'status_id' => $result->id]); if (!empty($answerVal)) { $answerR->value = $answerVal; $result->answers()->save($answerR); } } } return true; } throw new GeneralException('There was a problem updating this result. Please try again.'); }
/** * @param type $project * @param type $section * @param type $answers * @param type $incident_id * @param type $resultable * @param type $resultable_type * @return type * @throws GeneralException */ private function saveResults($project, $section, $raw_answers, $incident_id, $resultable, $resultable_type) { // this function defined in app\helpers.php $answers = array_filter_recursive($raw_answers); /** * Result is link between questions,location,paricipants and actual answer * mark the status * Results table is polymophic table between Participants, Locations and Results */ $result = Result::firstOrNew(['section_id' => $section, 'project_id' => $project->id, 'incident_id' => $incident_id, 'resultable_id' => $resultable->id, 'resultable_type' => $resultable_type]); if ($incident_id) { $result->incident_id = $incident_id; } $result->resultable_id = $resultable->id; $result->results = $answers; $result->section_id = $section; $current_user = auth()->user(); $result->user()->associate($current_user); $result->project()->associate($project); if (isset($resultable)) { $result->resultable()->associate($resultable); } if (!empty($answers)) { $result->information = $this->updateStatus($project, $section, $answers, $incident_id, $resultable, $resultable_type); if ($result->save()) { /** * Save actual answers after result status saved * delete all related answers before save. * More like overwriting old answers */ Answers::where('status_id', $result->id)->delete(); foreach ($answers as $qslug => $ans) { $q = $this->questions->getQuestionByQnum($qslug, $section, $project->id); foreach ($ans as $akey => $aval) { if ($akey == 'radio') { $answerkey = $aval; } else { $answerkey = $akey; } $qanswer = $q->qanswers->where('slug', $answerkey)->first(); if (!is_null($qanswer)) { if (in_array($qanswer->type, ['radio', 'checkbox'])) { $answerVal = $qanswer->value; } else { $answerVal = $aval; } $answerR = Answers::firstOrNew(['qid' => $q->id, 'akey' => $answerkey, 'status_id' => $result->id]); if (isset($answerVal) && !empty($answerVal)) { $answerR->value = $answerVal; $result->answers()->save($answerR); } } } } return $result; } } }