public function surveyIndex($project, Request $request) { $sections = count($project->sections); if ($project->validate == 'person') { $resultable_type = 'App\\Participant'; } else { $resultable_type = 'App\\PLocation'; } $total = $project->organization->pcode->count() * 10 * $sections; if ($total !== $project->results->count()) { $current_user = auth()->user(); for ($i = 0; $i < $sections; $i++) { for ($j = 1; $j <= 10; $j++) { foreach ($project->organization->pcode as $pcode) { $result = Result::firstOrNew(['section_id' => $i, 'incident_id' => $j, 'resultable_id' => $pcode->id, 'resultable_type' => $resultable_type]); if (is_null($result->information)) { $result->information = 'missing'; } $result->user()->associate($current_user); $result->project()->associate($project); $result->resultable()->associate($pcode); $result->save(); $final[] = $result; } } } } $alocations = PLocation::where('org_id', $project->organization->id)->get(); return view('frontend.result.survey-index')->withAllLoc($alocations)->withProject($project)->withRequest($request); }
/** * @param $input * @param $organizations * @param $permissions * @return bool * @throws GeneralException * @throws ResultNeedsOrganizationsException */ public function create($input, $project, $section) { $validate = $input['validator_id']; if ($project->validate == 'person') { $resultable = $this->participant->getParticipantByCode($validate, $project->organization->id); $result = Result::firstOrNew(['section_id' => $section, 'project_id' => $project->id, 'resultable_id' => $resultable->id, 'resultable_type' => 'App\\Participant']); //$pcode = $person->pcode; } else { $resultable = $this->pcode->findOrThrowException($validate); //dd($resultable->primaryid); $result = Result::firstOrNew(['section_id' => $section, 'project_id' => $project->id, 'resultable_id' => $resultable->primaryid, 'resultable_type' => 'App\\PLocation']); //$person = $pcode->participants->first(); $result->resultable_id = $resultable->primaryid; } $result->results = $input['answer']; $result->section_id = $section; $result->information = $this->updateStatus($project, $section, $input['answer']); $current_user = auth()->user(); $result->user()->associate($current_user); $result->project()->associate($project); if (isset($resultable)) { $result->resultable()->associate($resultable); } if ($result->save()) { \App\Answers::where('status_id', $result->id)->delete(); foreach ($input['answer'] as $qid => $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 creating 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; } } }