aboutsummaryrefslogtreecommitdiffstats
path: root/source/app/Http/Controllers/AdminController.php
blob: 157e37942eff6bf306cd2772e853e0f6eaaa706c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class AdminController extends Controller
{

    public function show(Request $request)
{
    try {
        $apiLogs = $this->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;
    }

}