/** * get all bus stop satisfaction for command center * * @return \Illuminate\Http\JsonResponse */ public function allBusStopSatisfaction() { $userFeedbackModel = new UserFeedback(); try { $listUserFeedback = $userFeedbackModel->select('satisfaction', 'directed_to_bus_stop')->whereNotNull('directed_to_bus_stop')->where('directed_to_bus_stop', '!=', 0)->get()->toArray(); $groupUserFeedback = array(); $counter = 0; foreach ($listUserFeedback as $userFeedback) { if ($counter == 0) { //initialization $groupUserFeedback[$counter]['halte_id'] = $userFeedback['directed_to_bus_stop']; $groupUserFeedback[$counter]['rating'] = $userFeedback['satisfaction']; $groupUserFeedback[$counter]['input'] = 1; $counter++; } else { //this is the story begin... for ($counterGroup = 0; $counterGroup < sizeof($groupUserFeedback); $counterGroup++) { if ($userFeedback['directed_to_bus_stop'] == $groupUserFeedback[$counterGroup]['halte_id']) { $groupUserFeedback[$counterGroup]['input']++; $groupUserFeedback[$counterGroup]['rating'] = ($groupUserFeedback[$counterGroup]['rating'] * ($groupUserFeedback[$counterGroup]['input'] - 1) + $userFeedback['satisfaction']) / $groupUserFeedback[$counterGroup]['input']; } else { $groupUserFeedback[$counter]['halte_id'] = $userFeedback['directed_to_bus_stop']; $groupUserFeedback[$counter]['rating'] = $userFeedback['satisfaction']; $groupUserFeedback[$counter]['input'] = 1; $counter++; } } } } if (isset($groupUserFeedback[0])) { $response = array(); $response['code'] = 200; $response['data'] = $groupUserFeedback; } else { $response['code'] = 400; $response['data']['msg'] = 'could not find rating evaluation'; } } catch (\Exception $e) { $response['code'] = 500; $response['data']['msg'] = 'internal error, please try again later or contact administrator'; } header("Access-Control-Allow-Origin: *"); return response()->json($response); }