aboutsummaryrefslogtreecommitdiffstats
path: root/source/app/Http/Controllers/StudentController.php
blob: 48fb3a278dbc18929fe7afbca77942185672c638 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;
use Illuminate\Support\Facades\Http;
use App\Http\Controllers\MessageController;

class StudentController extends Controller
{


    //Function that shows only one specific student, and all of the companies. To be used by the students page
    public function index(string $id)
    {
        // Check if user is authenticated
        if (!session('api_token')) {
            return redirect()->route('student.login.form');
        }

        $token = "Bearer " . session("api_token");


        $url = "{$this->studentsApiUrl}/{$id}";

        $response = Http::withHeaders([
            "Authorization" => $token
        ])->get("{$url}");

        //If the student is not found, user can go back to welcome page
        if (!$response->successful()) {
            return view('notfound', ['message' => 'Deze student lijkt niet te bestaan (error code 404). Contacteer de beheerder van de site voor meer informatie']);
        }
        //One student exists, so Array just consists of all the keys and their values
        $student = $response->json('data');

        $skills = Http::withHeaders([
            "Authorization" => $token
        ])->get("{$this->apiUrl}/skills")->json('data');


        $response = Http::withHeaders([
            "Authorization" => $token
        ])->get($this->companiesApiUrl);
        if (!$response->successful()) {
            return view('notfound', ['message' => 'Technisch probleem bij ophalen server (error code 404). Contacteer de beheerder van de site voor meer informatie']);
        }

        $companies = $response->json('data');

        // Get liked companies for the message list
        $likedCompanies = $this->getLikedCompanies($id, $token);
        foreach ($companies as $company) {
            foreach ($likedCompanies as $lCompany) {
                if ($lCompany['id'] == $company['id']) {
                    $key = array_search($company, $companies);
                    array_splice( $companies, $key,1);
                }
            }
        }

        $response = Http::withHeaders([
            "Authorization" => $token
        ])->get("{$this->appointmentApiUrl}");
        //get all appointments where the student is involved
        $appointments = $response->json('data');
        //Checks which appointments belong to the student
        $appointments = collect($appointments)->where('student_id', $id)->all();

        //Gives names to the ID's
        foreach ($appointments as &$appointment) {
            //translate student and company id to names
            $appointment['student_name'] = $this->translateStudent($appointment['student_id'], $token);


            $appointment['company_name'] = $this->translateCompany($appointment['company_id'], $token);
        }

        $connectionController = new \App\Http\Controllers\ConnectionController();
        $connections = $connectionController->getConnections($id, 'student');

        // get all messages for this student
        $messageController = new MessageController();
        $allMessages = $messageController->getAllMessagesForStudent($id);

        // fetch diplomas for graduation track dropdown
        $diplomasResponse = Http::withHeaders([
            "Authorization" => $token
        ])->get($this->diplomasApiUrl);
        if ($diplomasResponse->successful()) {
            $diplomasData = $diplomasResponse->json('data');

            // check if the data is nested like companies API
            $diplomas = isset($diplomasData['data']) ? $diplomasData['data'] : $diplomasData;
        } else {
            $diplomas = [];
        }

        return view('student.html.student', [
            'id' => $id,
            'student' => $student,
            'companies' => $companies, // All companies for swiping
            'likedCompanies' => $likedCompanies, // Only liked companies for message list
            'appointments' => $appointments,
            'connections' => $connections,
            'allMessages' => $allMessages,
            'token' => $token,
            'diplomas' => $diplomas,
            'skills' => $skills
        ]);

    }

     public function indexTest()
    {
        $id = 13;
        $response = Http::get("{$this->studentsApiUrl}/{$id}");
        //If the student is not found, user can go back to welcome page
        if (!$response->successful()) {
            return view('notfound', ['message' => 'Deze student lijkt niet te bestaan (error code 404). Contacteer de beheerder van de site voor meer informatie']);
        }
        //One student exists, so Array just consists of all the keys and their values
        $student = $response->json('data');

        $response = Http::get($this->companiesApiUrl);
        if (!$response->successful()) {
            return view('notfound', ['message' => 'Technisch probleem bij ophalen server (error code 404). Contacteer de beheerder van de site voor meer informatie']);
        }
        $companies = $response->json('data');
        //get all appointments where the student is involved
        $response = Http::get("{$this->appointmentApiUrl}");

        $appointments = $response->json('data');
        //Checks which appointments belong to the student
        $appointments = collect($appointments)->where('student_id', $id)->all();

        //Gives names to the ID's
        foreach ($appointments as &$appointment) {
            //translate student and company id to names
            $appointment['student_name'] = $this->translateStudent($appointment['student_id']);


            $appointment['company_name'] = $this->translateCompany($appointment['company_id']);
        }

        $connectionController = new \App\Http\Controllers\ConnectionController();
        $connections = $connectionController->getConnections($id, 'student');

        // Get all messages for this student
        $messageController = new MessageController();
        $allMessages = $messageController->getAllMessagesForStudent($id);

        // fetch diplomas for graduation track dropdown
        $diplomasResponse = Http::get($this->diplomasApiUrl);
        if ($diplomasResponse->successful()) {
            $diplomasData = $diplomasResponse->json('data');
            // Check if the data is nested like companies API
            $diplomas = isset($diplomasData['data']) ? $diplomasData['data'] : $diplomasData;
        } else {
            $diplomas = [];
        }

        return view('student.html.student', [
            'student' => $student,
            'companies' => $companies,
            'appointments' => $appointments,
            'connections' => $connections,
            'allMessages' => $allMessages,
            'diplomas' => $diplomas
        ]);

    }

    //Function that shows all students. To be used by the admin page
    public function showAll()
{
    try {
        // data studenten ophalen
        $response = Http::get($this->studentsApiUrl);
        if (!$response->successful()) {
            return view('student.html.student', [
                'error' => 'Studenten API niet beschikbaar',
                'students' => [],
                'companies' => []
            ]);
        }
        $students = $response->json('data');

        // data bedrijven ophalen
        $response = Http::get($this->companiesApiUrl);
        if (!$response->successful()) {
            return view('student.html.student', [
                'error' => 'Bedrijven API niet beschikbaar',
                'students' => [],
                'companies' => []
            ]);
        }
        $companies = $response->json('data');

        return view('student.html.student', [
            'students' => $students,
            'companies' => $companies
        ]);
    } catch (\Exception $e) {
        return view('voorbeeld.index', ['error' => 'Er is een fout opgetreden', 'students' => []]);
    }
}

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        try {
        $validated = $request->validate([
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'email' => 'required|email',
            //password 1 and 2 for validation
            'password1' => 'required|string|min:8',
            'password2' => 'required|same:password1',
            'study_direction' => 'required|string|max:255', // Nu verplicht
            'graduation_track' => 'required|string|max:255', // Nu verplicht
            'interests' => 'nullable|string',
            'job_preferences' => 'nullable|string',
            'cv' => 'nullable|string',
            'profile_complete' => 'nullable|boolean',
        ]);
        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'Validatie mislukt: ' . $e->getMessage());
        }

        // Standaardwaarden alleen voor optionele velden
        $defaults = [
            'interests' => 'Nog niet ingevuld',
            'job_preferences' => 'Nog niet ingevuld',
            'profile_complete' => false
        ];

        foreach ($defaults as $field => $value) {
            if (!isset($validated[$field])) {
                $validated[$field] = $value;
            }
        }
        //Code block generated by coPilot
        // User password2 as password
        $validated['password'] = $validated['password2'];
        // remove password1 and password2 from the validated data
        unset($validated['password1'], $validated['password2']);
        try {
            $response = Http::post($this->studentsApiUrl, $validated);

            if ($response->successful()) {
                return redirect()->back()->with('success', 'Account succesvol aangemaakt!');
    }
    else {
        return redirect()->back()->with('error', 'Er is een fout opgetreden bij het aanmaken van het account.');
    }
} catch (\Exception $e) {
    return redirect()->back()->with('error', 'Er is een fout opgetreden: ' . $e->getMessage());
}
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        try {
            $validated = $request->validate([
            'first_name' => 'sometimes|string|max:255',
            'last_name' => 'sometimes|string|max:255',
            'password' => 'sometimes|required|string|min:8',
            'study_direction' => 'sometimes|required|string|max:255',
            'graduation_track' => 'sometimes|required|integer',
            'interests' => 'sometimes|required|string',
            'job_preferences' => 'sometimes|required|string',
            'skills' => 'sometimes',
            'Authorization' => 'required'
        ]);
            $token = $validated['Authorization'];
            unset($validated['Authorization']);
        } catch (\Exception $e) {
            if ($request->expectsJson()) {
                return response()->json(['error' => 'Validation failed: ' . $e->getMessage()], 422);
            }
            return redirect()->back()->with('error', 'Validatie mislukt: ' . $e->getMessage());
        }
        //Get skills for a student
        try {
            $skills = Http::withHeaders([
                "Authorization" => $token
            ])->get("{$this->apiUrl}skills/{$validated['skills']}")->json('data');

            $validated['skills'] = $skills;
        }
        catch (\Exception $e) {
            dd($e->getMessage());
        }


        try {

            //Current data is taken and merged with the validated data
            $current = Http::withHeaders([
                "Authorization" => $token
            ])->get("{$this->studentsApiUrl}/{$id}");
            if (!$current->successful()) {
                throw new \Exception('Could not fetch current student data');
            }

            $currentData = $current->json('data');
            $data = array_merge($currentData, $validated);

            // Remove password field if it is not set in the request
            if (!isset($validated['password'])) {
                unset($data['password']);
            }

            $response = Http::withHeaders([
                "Authorization" => $token
            ])->patch("{$this->studentsApiUrl}/{$id}", $data);

            if (!$response->successful()) {
                throw new \Exception('API update failed');
            }

            if ($request->expectsJson()) {
                return response()->json(['success' => true, 'message' => 'Profile updated successfully']);
            }

            return redirect()->back()->with('success', 'Account succesvol aangepast');

        } catch (\Exception $e) {
            if ($request->expectsJson()) {
                return response()->json(['error' => 'Er is een fout opgetreden bij het bijwerken van het account.'], 500);
            }
            return redirect()->back()->with('error', 'Er is een fout opgetreden bij het bijwerken van het account.');
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id, Request $request)
    {
        $token = "Bearer " . $request['token'];
        $response = Http::withHeaders( [
            "Authorization" => $token
        ])->delete("{$this->studentsApiUrl}/{$id}");

        return redirect()->back()->with('success', 'Account succesvol verwijderd!');
    }

    /**
     * Get companies that the student has liked (status = true in connections)
     *
     * @param string $student_id
     * @param string $token
     * @return array
     */
    protected function getLikedCompanies(string $student_id, string $token): array
    {
        try {
            // Get all connections for this student using ConnectionController
            $connectionController = new \App\Http\Controllers\ConnectionController();
            $connections = $connectionController->getConnections($student_id, 'student');

            // If no connections from API, try session backup
            $likedCompanyIds = [];

            if (empty($connections)) {
                // Fallback to session storage
                $sessionKey = "liked_companies_student_{$student_id}";
                $likedCompanyIds = session($sessionKey, []);
            } else {
                // Filter to only liked connections (status = true)
                $likedConnections = collect($connections)->where('status', true)->all();

                if (empty($likedConnections)) {
                    return [];
                }

                // Get company IDs that were liked
                $likedCompanyIds = collect($likedConnections)->pluck('company_id')->toArray();
            }

            if (empty($likedCompanyIds)) {
                return [];
            }

            // Fetch all companies
            $response = Http::withHeaders([
                "Authorization" => $token
            ])->get($this->companiesApiUrl);

            if (!$response->successful()) {
                return [];
            }

            $allCompanies = $response->json('data');

            // Filter companies to only include liked ones
            $likedCompanies = collect($allCompanies)
                ->whereIn('id', $likedCompanyIds)
                ->values()
                ->toArray();

            return $likedCompanies;

        } catch (\Exception $e) {
            return [];
        }
    }

}