/**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $this->validate($request, $this->rules);
     $context = ContextScenarioUserAppInteraction::find($id);
     $updateData = $request->all();
     if (!isset($updateData["accompanying"])) {
         $updateData["accompanying"] = '0';
     }
     if (!isset($updateData["intermittent"])) {
         $updateData["intermittent"] = '0';
     }
     if (!isset($updateData["interrupting"])) {
         $updateData["interrupting"] = '0';
     }
     $context->update($updateData);
     // insert into context_scene_relation
     $compareData = array('context_id' => $id, 'user_id' => $this->user['id']);
     $contextSceneRelationData = array('context_id' => $id, 'user_id' => $this->user['id'], 'scene' => $updateData['scene']);
     ContextSceneRelation::updateOrCreate($compareData, $contextSceneRelationData);
     Session::flash('flash_message', 'Congratulations, Data updated successfully!');
     return redirect("requirements/{$request->requirement_id}");
 }
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function show($id)
 {
     $requirement = Requirements::find($id);
     $scenario = Scenarios::find($requirement->scenario_id);
     $project = Projects::find($scenario->project_id);
     $context = ContextScenarioUserAppInteraction::leftJoin('users', 'users.id', '=', 'context_scenario_user_app_interaction.user_id')->leftJoin('context_scenario_ideal_way AS context', 'context.id', '=', 'context_scenario_user_app_interaction.context_id')->leftJoin('context_ratings as CR', 'CR.context_id', '=', 'context_scenario_user_app_interaction.id')->leftJoin('context_ratings as CR1', function ($join) {
         $join->on('CR1.context_id', '=', 'context_scenario_user_app_interaction.id');
         $join->where('CR1.user_id', '=', $this->user['id']);
     })->leftJoin('ways_of_interaction_voting as WOIV', function ($join) {
         $join->on('WOIV.context_id', '=', 'context_scenario_user_app_interaction.id');
         $join->where('WOIV.user_id', '=', $this->user['id']);
     })->select('context_scenario_user_app_interaction.*', 'users.name AS user_name', 'context.context_name', 'context.full_name', 'CR1.rating', DB::raw('avg(CR.rating) AS avg_rating, count(CR.id) AS rating_count'), 'WOIV.accompanying', 'WOIV.intermittent', 'WOIV.interrupting')->where('requirement_id', $id)->groupBy('context_scenario_user_app_interaction.id')->orderBy('context_id', 'asc')->get();
     $context_arr = $context->toArray();
     if ($context_arr) {
         foreach ($context_arr as &$c) {
             $cid = $c['id'];
             $context_scene = ContextSceneRelation::select('context_scene_relation.user_id AS context_scene_user', 'context_scene_relation.scene', 'users.name AS user_name')->leftJoin('users', 'users.id', '=', 'context_scene_relation.user_id')->where('context_id', $cid)->get()->toArray();
             $c['scenes'] = $context_scene;
             $c = (object) $c;
         }
     }
     $context_arr = (object) $context_arr;
     $breadcrumbs = array('Projects' => '/projects', 'All Requirements' => "/scenarios/{$requirement->scenario_id}");
     return view('requirements.details', compact('requirement', 'context_arr', 'project', 'breadcrumbs'));
 }