Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.70% covered (warning)
69.70%
23 / 33
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CreateAcademyLevelCriteria
69.70% covered (warning)
69.70%
23 / 33
66.67% covered (warning)
66.67%
2 / 3
8.36
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 up
66.67% covered (warning)
66.67%
20 / 30
0.00% covered (danger)
0.00%
0 / 1
4.59
 down
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Database\Migrations;
4
5use CodeIgniter\Database\Forge;
6use CodeIgniter\Database\Migration;
7
8class CreateAcademyLevelCriteria extends Migration
9{
10    private readonly array $attributes;
11
12    public function __construct(?Forge $forge = null)
13    {
14        parent::__construct($forge);
15
16        $this->attributes = $this->db->getPlatform() === 'MySQLi' ? ['ENGINE' => 'InnoDB'] : [];
17    }
18
19    public function up(): void
20    {
21        $this->forge->addField([
22            'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
23            'academy_level_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
24            'evaluation_type_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
25            'evaluation_criteria_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
26            'weight' => ['type' => 'DECIMAL', 'constraint' => '5,2', 'default' => 1],
27            'is_required' => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 1],
28            'sort_order' => ['type' => 'INT', 'constraint' => 11, 'default' => 0],
29        ]);
30        $this->forge->addPrimaryKey('id');
31        $this->forge->addUniqueKey(['academy_level_id', 'evaluation_type_id', 'evaluation_criteria_id'], 'level_type_criteria');
32        $this->forge->addForeignKey('academy_level_id', 'academy_levels', 'id', '', 'CASCADE');
33        $this->forge->addForeignKey('evaluation_type_id', 'evaluation_types', 'id', '', 'CASCADE');
34        $this->forge->addForeignKey('evaluation_criteria_id', 'evaluation_criteria', 'id', '', 'CASCADE');
35        $this->forge->createTable('academy_level_criteria', true, $this->attributes);
36
37        $levels = $this->db->table('academy_levels')->select('id')->get()->getResultArray();
38        $globalRows = $this->db->table('evaluation_type_criteria')->get()->getResultArray();
39
40        $rows = [];
41        foreach ($levels as $level) {
42            foreach ($globalRows as $row) {
43                $rows[] = [
44                    'academy_level_id' => (int) $level['id'],
45                    'evaluation_type_id' => (int) $row['evaluation_type_id'],
46                    'evaluation_criteria_id' => (int) $row['evaluation_criteria_id'],
47                    'weight' => $row['weight'],
48                    'is_required' => (int) $row['is_required'],
49                    'sort_order' => (int) $row['sort_order'],
50                ];
51            }
52        }
53
54        if ($rows !== []) {
55            $this->db->table('academy_level_criteria')->insertBatch($rows);
56        }
57    }
58
59    public function down(): void
60    {
61        $this->forge->dropTable('academy_level_criteria', true);
62    }
63}