middleware('auth'); $this->middleware('permission:observation-list', ['only' => ['index']]); $this->middleware('permission:observation-create', ['only' => ['create', 'store']]); $this->middleware('permission:observation-edit', ['only' => ['edit', 'update']]); $this->middleware('permission:observation-delete', ['only' => ['destroy', 'inactive', 'activate']]); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $observations = Observation::orderBy('name', 'asc')->paginate(50); return view('clinical_data::observations.index', compact('observations')); } /** * Show the form for creating a new resource. * */ public function create() { $age_groups = AgeGroup::orderBy('name', 'asc') ->pluck('name', 'id') ->toArray(); return view('clinical_data::observations.create', compact('age_groups')); } /** * Store a newly created resource in storage. * */ public function store(Request $request) { request()->validate([ 'name' => 'required' ]); $logged_in_user_id = Auth::user()->id; $observation = new Observation; $observation->name = $request->name; $observation->measurement = $request->measurement; if ($request->options_or_range == "2") { $options = implode(',', $request->options); if ($options == "") { flash("Please select options for this observation")->error(); return back()->withInput(); } $observation->options = $options; } $observation->option_for_normal = $request->option_for_normal; $observation->lower_limit = $request->lower_limit; $observation->upper_limit = $request->upper_limit; if(is_array($request->age_group)){ $age_group = implode(',', $request->age_group); } else { $age_group = ""; } $observation->age_group = $age_group; $observation->slug = str_replace(' ', '_', strtolower($request->name)); $observation->compulsory = $request->compulsory; $observation->created_by = $logged_in_user_id; $observation->updated_by = $logged_in_user_id; try { $observation->save(); flash($request->name . " Observation has been saved")->success(); return redirect("/observations/"); } catch (QueryException $e) { flash("An error occurred")->error(); return back()->withInput(); } } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $observation = Observation::where(['id' => $id])->first(); $age_groups = AgeGroup::orderBy('name', 'asc') ->pluck('name', 'id') ->toArray(); if (!$observation) { flash()->error("There is no such observation"); return redirect('/observations/'); } else { return view('clinical_data::observations.edit', compact('observation', 'age_groups')); } } /** * Update the specified resource in storage. * */ public function update(Request $request, $id) { request()->validate([ 'name' => 'required' ]); $logged_in_user_id = Auth::user()->id; $observation = Observation::find($id); $observation->name = $request->name; $observation->measurement = $request->measurement; if ($request->options_or_range == "2") { if (!is_array($request->options)) { flash("Please select options for this observation")->error(); return back()->withInput(); } $options = implode(',', $request->options); $observation->options = $options; } else { $observation->options = NULL; } $observation->option_for_normal = $request->option_for_normal; $observation->lower_limit = $request->lower_limit; $observation->upper_limit = $request->upper_limit; if(is_array($request->age_group)){ $age_group = implode(',', $request->age_group); } else { $age_group = ""; } $observation->age_group = $age_group; $observation->compulsory = $request->compulsory; $observation->created_by = $logged_in_user_id; $observation->updated_by = $logged_in_user_id; try { $observation->save(); flash($request->name . " Observation has been updated")->success(); return redirect("/observations/"); } catch (QueryException $e) { flash("An error occurred")->error(); return back()->withInput(); } } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $observation = Observation::find($id); if ($observation->delete()): flash("Observation has been deleted.")->success(); return redirect('/observations/'); endif; } /** * Display a listing of the inactive resource(s). * * @return \Illuminate\Http\Response */ public function inactive() { $observations = Observation::onlyTrashed() ->orderBy('name', 'asc') ->paginate(50); if (count($observations) < 1) { flash()->error("There is no inactive observation"); return redirect('/observations/'); } else { return view('clinical_data::observations.inactive', compact('observations')); } } /** * Activate the specified resource in storage. * * @param int $id * @return \Illuminate\Http\Response */ public function activate($id) { $observation = Observation::withTrashed()->find($id); if ($observation->restore()){ flash("Observation has been activated.")->success(); return redirect('/observations/inactive'); } } public function get_observation_info($id) { $observation = Observation::find($id); $return_data = []; if ($observation) { $return_data["error"] = 0; $return_data["slug"] = $observation->slug; $return_data["normal_range"] = $observation->lower_limit . ' - ' . $observation->upper_limit; } else { $return_data["error"] = 1; } return json_encode($return_data); } }