aboutsummaryrefslogtreecommitdiffstats
path: root/source/app/Http/Controllers/SkillsToevoegenController.php
blob: 2f367d2b52c1bffc5f1d94a84f5fc545ee3e20ae (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
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;

class SkillsToevoegenController extends Controller
{
    protected string $skillsApiUrl = 'http://10.2.160.208/api/skills';
    protected string $studentsApiUrl = 'http://10.2.160.208/api/students';

    /**
     * Show the form for adding skills to a student.
     */
    public function showSkillsForm(string $id): View
    {
        // Haal de student op om te controleren of die bestaat
        $studentResponse = Http::get("{$this->studentsApiUrl}/{$id}");
        
        if (!$studentResponse->successful()) {
            return view('notfound', ['message' => 'Deze student lijkt niet te bestaan (error code 404). Contacteer de beheerder van de site voor meer informatie']);
        }
        
        $student = $studentResponse->json('data');
        
        // Beschikbare skills ophalen via de API
        $skillsResponse = Http::get($this->skillsApiUrl);
        
        if ($skillsResponse->successful()) {
            $availableSkills = $skillsResponse->json('data');
        } else {
            Log::warning('Kon geen skills ophalen, gebruik fallback waarden');
            // Fallback skills als de API niet beschikbaar is
            $availableSkills = [
                ['id' => 1, 'name' => 'PHP'],
                ['id' => 2, 'name' => 'JavaScript'],
                ['id' => 3, 'name' => 'HTML/CSS'],
                ['id' => 4, 'name' => 'React'],
                ['id' => 5, 'name' => 'Vue.js'],
                ['id' => 6, 'name' => 'Angular'],
                ['id' => 7, 'name' => 'Laravel']
            ];
        }
        
        // Haal de huidige skills van de student op (als die er zijn)
        $currentSkills = [];
        if (isset($student['skills']) && is_array($student['skills'])) {
            $currentSkills = $student['skills'];
        }
        
        return view('student.register.skillstoevoegen', [
            'student' => $student,
            'availableSkills' => $availableSkills,
            'currentSkills' => $currentSkills
        ]);
    }

    /**
     * Save the selected skills for a student.
     */
    
    public function saveSkills(Request $request, string $id): RedirectResponse
    {
        $validated = $request->validate([
            'skill_ids' => 'required|array',
            'skill_ids.*' => 'integer'
        ]);
        
        // Haal de geselecteerde skill IDs op als een gewone array
        $skillIds = $validated['skill_ids'];
        
        Log::info('Skills versturen naar API:', ['skills' => $skillIds]);
        
        // OPLOSSING: Stuur de data in het juiste formaat
        $response = Http::post("{$this->studentsApiUrl}/{$id}/skills", [
            'skill_ids' => $skillIds  // Gebruik de API endpoint voor skills
        ]);
        
        if (!$response->successful()) {
            Log::error('Fout bij opslaan skills: ' . $response->status() . ' - ' . $response->body());
            return redirect()->back()
                ->withErrors(['api' => 'Kon skills niet opslaan: ' . $response->body()])
                ->withInput();
        }
        
        // Redirect naar de login pagina met succes bericht
        return redirect()->route('login.login')
            ->with('status', 'Je profiel en skills zijn succesvol aangemaakt!');
    }
}