Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
56.03% covered (warning)
56.03%
65 / 116
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConvertAcademyToSwimming
56.03% covered (warning)
56.03%
65 / 116
50.00% covered (danger)
50.00%
1 / 2
11.16
0.00% covered (danger)
0.00%
0 / 1
 up
51.43% covered (warning)
51.43%
54 / 105
0.00% covered (danger)
0.00%
0 / 1
10.13
 down
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Database\Migrations;
4
5use CodeIgniter\Database\Migration;
6
7class ConvertAcademyToSwimming extends Migration
8{
9    public function up(): void
10    {
11        $now = date('Y-m-d H:i:s');
12
13        $this->db->table('settings')
14            ->where('key', 'academy_name')
15            ->update([
16                'value' => 'Splash Academy',
17                'updated_at' => $now,
18            ]);
19
20        $this->db->table('roles')
21            ->where('slug', 'coach')
22            ->update([
23                'name' => 'Trainer',
24                'description' => 'Acces la copii, evaluari si prezenta pentru grupele proprii de inot.',
25                'updated_at' => $now,
26            ]);
27
28        $levelRows = $this->db->table('academy_levels')
29            ->orderBy('sort_order', 'ASC')
30            ->orderBy('id', 'ASC')
31            ->get()
32            ->getResultArray();
33
34        if (count($levelRows) >= 4) {
35            $targetLevels = [
36                [
37                    'name' => 'DelfinaÈ™ (IniÈ›iere)',
38                    'slug' => 'delfinas-initiere',
39                    'sort_order' => 1,
40                    'color_hex' => '#0ea5e9',
41                    'internal_description' => 'Nivel intern pentru cursurile de inot: DelfinaÈ™ (IniÈ›iere).',
42                    'parent_description' => 'Nivelul DelfinaÈ™ (IniÈ›iere) marcheaza acomodarea copilului cu apa si bazele tehnice.',
43                ],
44                [
45                    'name' => 'Rechin (Avansat)',
46                    'slug' => 'rechin-avansat',
47                    'sort_order' => 2,
48                    'color_hex' => '#0284c7',
49                    'internal_description' => 'Nivel intern pentru cursurile de inot: Rechin (Avansat).',
50                    'parent_description' => 'Nivelul Rechin (Avansat) marcheaza progresul copilului in tehnica de inot.',
51                ],
52                [
53                    'name' => 'Pre-Performanță',
54                    'slug' => 'pre-performanta',
55                    'sort_order' => 3,
56                    'color_hex' => '#14b8a6',
57                    'internal_description' => 'Nivel intern pentru cursurile de inot: Pre-Performanță.',
58                    'parent_description' => 'Nivelul Pre-Performanță pregateste copilul pentru volum si obiective sportive.',
59                ],
60                [
61                    'name' => 'Sportiv de Performanță',
62                    'slug' => 'sportiv-de-performanta',
63                    'sort_order' => 4,
64                    'color_hex' => '#f59e0b',
65                    'internal_description' => 'Nivel intern pentru cursurile de inot: Sportiv de Performanță.',
66                    'parent_description' => 'Nivelul Sportiv de Performanță marcheaza integrarea in pregatirea competitiva.',
67                ],
68            ];
69
70            foreach ($targetLevels as $index => $level) {
71                $this->db->table('academy_levels')
72                    ->where('id', (int) $levelRows[$index]['id'])
73                    ->update($level + [
74                        'benchmark_minimum' => 3.0,
75                        'benchmark_target' => 4.0,
76                        'promotion_rules' => 'Necesita evaluare full cu status READY si criterii critice peste prag.',
77                        'updated_at' => $now,
78                    ]);
79            }
80
81            $sportPerformanceId = (int) $levelRows[3]['id'];
82            $oldLevelIds = array_map(static fn (array $row): int => (int) $row['id'], array_slice($levelRows, 4));
83
84            if ($oldLevelIds !== []) {
85                foreach (['children' => 'academy_level_id', 'academy_groups' => 'academy_level_id', 'evaluations' => 'level_at_time_id'] as $table => $column) {
86                    $this->db->table($table)
87                        ->whereIn($column, $oldLevelIds)
88                        ->update([$column => $sportPerformanceId, 'updated_at' => $now]);
89                }
90
91                $this->db->table('academy_levels')->whereIn('id', $oldLevelIds)->delete();
92            }
93        }
94
95        $criteriaMap = [
96            'footwork' => ['Water Confidence', 'water-confidence'],
97            'rally-consistency' => ['Breathing Control', 'breathing-control'],
98            'technique' => ['Stroke Technique', 'stroke-technique'],
99            'serve-basics' => ['Start & Turn Basics', 'start-turn-basics'],
100            'tactical-awareness' => ['Race Awareness', 'race-awareness'],
101        ];
102
103        foreach ($criteriaMap as $oldSlug => [$name, $slug]) {
104            $this->db->table('evaluation_criteria')
105                ->where('slug', $oldSlug)
106                ->update([
107                    'name' => $name,
108                    'slug' => $slug,
109                    'description' => $name . ' in contextul evaluarii academiei de inot.',
110                    'updated_at' => $now,
111                ]);
112        }
113
114        $this->db->table('academy_groups')
115            ->where('code', 'RSP')
116            ->update(['name' => 'Delfinas Initiere', 'code' => 'DFN', 'updated_at' => $now]);
117        $this->db->table('academy_groups')
118            ->where('code', 'OAC')
119            ->update(['name' => 'Rechin Avansat', 'code' => 'RCH', 'updated_at' => $now]);
120        $this->db->table('academy_groups')
121            ->where('code', 'GSM')
122            ->update(['name' => 'Performanta Bazin', 'code' => 'PRF', 'updated_at' => $now]);
123
124        $this->db->table('coaches')
125            ->where('display_role', 'Head Coach')
126            ->update(['display_role' => 'Head Trainer', 'updated_at' => $now]);
127        $this->db->table('coaches')
128            ->where('display_role', 'Coach')
129            ->update(['display_role' => 'Trainer', 'updated_at' => $now]);
130        $this->db->table('coaches')
131            ->where('display_role', 'Assistant Coach')
132            ->update(['display_role' => 'Assistant Trainer', 'updated_at' => $now]);
133    }
134
135    public function down(): void
136    {
137        $now = date('Y-m-d H:i:s');
138
139        $this->db->table('settings')
140            ->where('key', 'academy_name')
141            ->update(['value' => 'Academia Tennis', 'updated_at' => $now]);
142
143        $this->db->table('roles')
144            ->where('slug', 'coach')
145            ->update([
146                'name' => 'Coach',
147                'description' => 'Acces la copii, evaluari si prezenta pentru grupele proprii.',
148                'updated_at' => $now,
149            ]);
150    }
151}