Passed
Pull Request — master (#5831)
by
unknown
07:40
created

Version20240928003000::down()   B

Complexity

Conditions 8
Paths 51

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 17
c 2
b 0
f 0
nc 51
nop 1
dl 0
loc 34
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Schema\Schema;
11
12
final class Version20240928003000 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Add new fields to session and c_lp tables for handling reinscription and session repetition logic, and insert new settings if not exist.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $schemaManager = $this->connection->createSchemaManager();
22
23
        // Add fields to the 'session' table
24
        if ($schemaManager->tablesExist('session')) {
25
            $sessionTable = $schemaManager->listTableColumns('session');
26
27
            if (!isset($sessionTable['parent_id'])) {
28
                $this->addSql("ALTER TABLE session ADD parent_id INT DEFAULT NULL");
29
            }
30
            if (!isset($sessionTable['days_to_reinscription'])) {
31
                $this->addSql("ALTER TABLE session ADD days_to_reinscription INT DEFAULT NULL");
32
            }
33
            if (!isset($sessionTable['last_repetition'])) {
34
                $this->addSql("ALTER TABLE session ADD last_repetition TINYINT(1) DEFAULT 0 NOT NULL");
35
            }
36
            if (!isset($sessionTable['days_to_new_repetition'])) {
37
                $this->addSql("ALTER TABLE session ADD days_to_new_repetition INT DEFAULT NULL");
38
            }
39
        }
40
41
        // Add fields to the 'c_lp' (Learnpath) table
42
        if ($schemaManager->tablesExist('c_lp')) {
43
            $clpTable = $schemaManager->listTableColumns('c_lp');
44
45
            if (!isset($clpTable['validity_in_days'])) {
46
                $this->addSql("ALTER TABLE c_lp ADD validity_in_days INT DEFAULT NULL");
47
            }
48
        }
49
50
        // Insert new settings if not exist
51
        $this->addSql("
52
            INSERT INTO settings (variable, subkey, type, category, selected_value, title, comment, scope, subkeytext, access_url, access_url_changeable, access_url_locked)
53
            SELECT 'enable_auto_reinscription', NULL, NULL, 'session', '0', 'Enable Auto Reinscription', 'Allow users to be automatically reinscribed in new sessions.', '', NULL, 1, 1, 1
54
            WHERE NOT EXISTS (
55
                SELECT 1 FROM settings WHERE variable = 'enable_auto_reinscription'
56
            )
57
        ");
58
59
        $this->addSql("
60
            INSERT INTO settings (variable, subkey, type, category, selected_value, title, comment, scope, subkeytext, access_url, access_url_changeable, access_url_locked)
61
            SELECT 'enable_session_replication', NULL, NULL, 'session', '0', 'Enable Session Replication', 'Allow replication of session data across instances.', '', NULL, 1, 1, 1
62
            WHERE NOT EXISTS (
63
                SELECT 1 FROM settings WHERE variable = 'enable_session_replication'
64
            )
65
        ");
66
    }
67
68
    public function down(Schema $schema): void
69
    {
70
        $schemaManager = $this->connection->createSchemaManager();
71
72
        // Revert changes in the 'session' table
73
        if ($schemaManager->tablesExist('session')) {
74
            $sessionTable = $schemaManager->listTableColumns('session');
75
76
            if (isset($sessionTable['parent_id'])) {
77
                $this->addSql("ALTER TABLE session DROP COLUMN parent_id");
78
            }
79
            if (isset($sessionTable['days_to_reinscription'])) {
80
                $this->addSql("ALTER TABLE session DROP COLUMN days_to_reinscription");
81
            }
82
            if (isset($sessionTable['last_repetition'])) {
83
                $this->addSql("ALTER TABLE session DROP COLUMN last_repetition");
84
            }
85
            if (isset($sessionTable['days_to_new_repetition'])) {
86
                $this->addSql("ALTER TABLE session DROP COLUMN days_to_new_repetition");
87
            }
88
        }
89
90
        // Revert changes in the 'c_lp' table
91
        if ($schemaManager->tablesExist('c_lp')) {
92
            $clpTable = $schemaManager->listTableColumns('c_lp');
93
94
            if (isset($clpTable['validity_in_days'])) {
95
                $this->addSql("ALTER TABLE c_lp DROP COLUMN validity_in_days");
96
            }
97
        }
98
99
        // Remove settings
100
        $this->addSql("DELETE FROM settings WHERE variable = 'enable_auto_reinscription'");
101
        $this->addSql("DELETE FROM settings WHERE variable = 'enable_session_replication'");
102
    }
103
}
104