/** * @return Response */ public function api() { $data = []; $groups = User::find($this->userId, ['id'])->trainerGroups()->get(['area_id', 'category_id', 'name']); foreach ($groups as $group) { $key = $group->area_id . ($group->category_id ? '_' . $group->category_id : ''); if (isset($data[$key])) { continue; } $area = Area::findOrFail($group->area_id, ['id', 'name']); $category = null; $query = Response::join('quiz', 'response.quiz_id', '=', 'quiz.id')->join('users', 'response.user_id', '=', 'users.id'); if ($group->category_id) { $category = Category::findOrFail($group->category_id, ['id', 'name']); $query->where('category_id', $category->id); } else { $query->where('area_id', $area->id); } $applicants = $query->get(['response.id', 'response.created_at', 'full_name', 'email', 'school']); foreach ($applicants as $key => $applicant) { $applicants[$key]->id = (int) $applicant->id; $response = Response::findOrFail($applicant->id); $choices = $applicant->trainerchoices()->lists('choice', 'trainer_id'); $choice = isset($choices[$this->userId]) ? (int) $choices[$this->userId] : null; if ($choice !== 1 && !is_null($choice)) { $applicants->forget($key); continue; } $responses = []; foreach ($response->questions as $rquestion) { if (!$rquestion->question) { continue; } $answers = []; $roptions = $rquestion->options->lists('response', 'quiz_question_option_id'); foreach ($rquestion->question->options as $option) { if (array_key_exists($option->id, $roptions)) { if ($rquestion->question->type == 'text' || $rquestion->question->type == 'textbox') { $answer = html_entity_decode($roptions[$option->id]); } else { $answer = true; } } else { $answer = false; } $answers[] = ['option' => $option->option ? html_entity_decode($option->option) : null, 'answer' => $answer]; } $responses[] = ['question' => $rquestion->question->question, 'question_type' => $rquestion->question->type, 'answers' => $answers]; } $applicant->responses = $responses; } $applicants->values(); $data[$key] = ['applicants' => $applicants, 'area' => $area, 'category' => $category]; } return json(array_values($data), 200); }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($_, $id) { $response = Response::findOrFail($id); $response->delete(); return json(1); }