apiUrl . '/admin/logs'; $action = false; $token = ""; //If user just logged in, create session if (session('api_token')) { $token = "Bearer " . session("api_token"); $action = true; } else if ($request['token']) { $token = $request['token']; } if ($token == "") return redirect("/"); //get All data $response = Http::withHeaders( [ "Authorization" => $token ])->get($this->studentsApiUrl); if (!$response->successful()) { redirect("/"); } $students = $response->json('data'); //Second response for companies $response = Http::withHeaders( [ "Authorization" => $token ])->get($this->companiesApiUrl); if (!$response->successful()) { dd(2); } $companies = $response->json('data'); //For appointments $response = Http::withHeaders( [ "Authorization" => $token ])->get($this->appointmentApiUrl); $appointments = $response->json('data'); //For connections $response = Http::withHeaders( [ "Authorization" => $token ])->get($this->connectionsApiUrl); $connections = $response->json('data'); //Degrees $degrees = Http::withHeaders( [ "Authorization" => $token ])->get($this->apiUrl . '/diplomas')->json('data'); //Final response for logs //Check if the apiURL of the requested logs is active. if (isset($request['cursor'])) { $response = Http::withHeaders( [ "Authorization" => $token ])->get($this->apiUrl . '/admin/logs?cursor=' . $request['cursor']); } else $response = Http::withHeaders( [ "Authorization" => $token ])->get($apiLogs); if (!$response->successful()) { redirect('APINotFound'); } $logs = $response->json('data'); foreach ($students as &$student) { foreach ($degrees as $degree) { if ($student['graduation_track'] == $degree['id']) { $student['graduation_track'] = $degree['type']; break; } } } //Translate the ID's to names $appointments = $this->translateCompanies($appointments, $companies); $appointments = $this->translateStudents($appointments, $students); //Same with connections //Translate the ID's to names $connections = $this->translateCompanies($connections, $companies); $connections = $this->translateStudents($connections, $students); foreach ($students as &$student) {$student['logs'] = array(); $student['updated_at'] = substr($student['updated_at'],8,2) . substr($student['updated_at'], 4, 2) . substr($student['updated_at'], 0, 4); } foreach ($companies as &$company) {$company['logs'] = array(); $company['updated_at'] = substr($company['updated_at'],8,2) . substr($company['updated_at'], 4, 2) . substr($company['updated_at'], 0, 4); } foreach ($appointments as &$appointment) { $appointment['time_slot'] = substr($appointment['time_start'], 0, 5) . ' - '. substr($appointment['time_end'], 0,5); } //save next page from lgos, to add it seperatly in the view $nextPage = isset($logs['next_cursor']) ? $logs['next_cursor'] : null; $previousPage = isset($logs['prev_cursor']) ? $logs['prev_cursor'] : null; $logs = $logs['data']; //Replace target id with target name //code generated by github copilot foreach ($logs as &$log) { //Translate log actions to dutch switch ($log['action']) { case 'create': $log['action'] = 'heeft een account aangemaakt'; break; case 'update': $log['action'] = 'heeft hun account geüpdatet'; break; case 'delete': $log['action'] = 'heeft hun account verwijderd'; break; default: $log['action'] = 'heeft hun account bekeken'; } //setup time $log['date'] = substr($log['timestamp'], 8, 2) . "/" . substr($log['timestamp'], 5, 2) . "/" . substr($log['timestamp'], 0, 4); $log['time'] = substr($log['timestamp'], 11, 2) . "u" . substr($log['timestamp'], 14, 2); //Set target_type to dutch if ($log['actor'] === 'Student') { $log['actor'] = 'Student'; } elseif ($log['actor'] === 'Company') { $log['actor'] = 'Bedrijf'; } elseif ($log['actor'] === 'Admin') { $log['actor'] = 'Beheerder'; } else { $log['actor'] = 'Onbekend type'; } $id = $log['actor_id']; if ($log['actor'] === 'Student') { $log['actor_id'] = $this->translateStudent($id, $token); //This code adds a log to the user foreach ($students as &$student) { if ($student['id'] == $id) array_push($student['logs'], $log ); } } elseif ($log['actor'] === 'Bedrijf') { $log['actor_id'] = $this->translateCompany($id, $token); //This code adds a log to the user foreach ($companies as &$company) { if ($company['id'] == $id) array_push($company['logs'], $log ); } } elseif ($log['actor'] === 'Admin') { $log['actor'] = 'Steve'; } } return view('/admin/admin', [ 'students' => $students, 'companies' => $companies, 'appointments' => $appointments, 'connections' => $connections, 'logs' => $logs, 'degrees' => $degrees, 'nextPage' => $nextPage, 'previousPage' => $previousPage, 'action' => $action ]); } catch (\Exception $e) { dd('failure: ' . $e->getMessage()); return view('voorbeeld.index', ['error' => 'Er is een fout opgetreden', 'students' => []]); } } protected function getConnections($token) { $response = Http::withHeaders( [ "Authorization" => $token ])->get($this->connectionsApiUrl); $data = $response->json('data'); foreach ($data as &$connection) { //translate student and company id to names $connection['student_id'] = $this->translateStudent($connection['student_id']); $connection['company_id'] = $this->translateCompany($connection['company_id']); if ($connection['status'] === '1') { $connection['status'] = 'Actief'; } else { $connection['status'] = 'Afgesloten'; } } return $data; } }