Completed
Push — master ( ac9b94...2050b8 )
by Julito
11:32
created

Version20180319145700::up()   F

Complexity

Conditions 12
Paths 1536

Size

Total Lines 79
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 44
nc 1536
nop 1
dl 0
loc 79
rs 2.8
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
6
7
use Chamilo\CoreBundle\Entity\ExtraField;
8
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
9
use Doctrine\DBAL\Schema\Schema;
10
use Doctrine\DBAL\Types\Types;
11
12
/**
13
 * Survey changes.
14
 */
15
class Version20180319145700 extends AbstractMigrationChamilo
16
{
17
    public function up(Schema $schema): void
18
    {
19
        $survey = $schema->getTable('c_survey');
20
        if (false === $survey->hasColumn('is_mandatory')) {
21
            $this->addSql('ALTER TABLE c_survey ADD COLUMN is_mandatory TINYINT(1) DEFAULT "0" NOT NULL');
22
        }
23
24
        if (false === $survey->hasColumn('resource_node_id')) {
25
            $this->addSql('ALTER TABLE c_survey ADD resource_node_id INT DEFAULT NULL');
26
            $this->addSql('ALTER TABLE c_survey ADD CONSTRAINT FK_F246DB301BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE');
27
            $this->addSql('CREATE UNIQUE INDEX UNIQ_F246DB301BAD783F ON c_survey (resource_node_id);');
28
        }
29
30
        $this->addSql('ALTER TABLE c_survey CHANGE avail_from avail_from DATETIME DEFAULT NULL;');
31
        $this->addSql('ALTER TABLE c_survey CHANGE avail_till avail_till DATETIME DEFAULT NULL;');
32
33
        /*if (!$survey->hasIndex('idx_survey_code')) {
34
            $this->addSql('CREATE INDEX idx_survey_code ON c_survey (code)');
35
        }*/
36
37
        $this->addSql('ALTER TABLE c_survey_invitation CHANGE reminder_date reminder_date DATETIME DEFAULT NULL');
38
        $this->addSql(
39
            'UPDATE c_survey_invitation SET reminder_date = NULL WHERE CAST(reminder_date AS CHAR(20)) = "0000-00-00 00:00:00"'
40
        );
41
42
        $table = $schema->getTable('c_survey_invitation');
43
        if (false === $table->hasColumn('answered_at')) {
44
            $this->addSql('ALTER TABLE c_survey_invitation ADD answered_at DATETIME DEFAULT NULL;');
45
        }
46
        if (false === $table->hasIndex('idx_survey_inv_code')) {
47
            $this->addSql('CREATE INDEX idx_survey_inv_code ON c_survey_invitation (survey_code)');
48
        }
49
50
        $table = $schema->getTable('c_survey_question');
51
        if (false === $table->hasColumn('is_required')) {
52
            $table
53
                ->addColumn('is_required', Types::BOOLEAN)
54
                ->setDefault(false);
55
        }
56
        if (false === $table->hasIndex('idx_survey_q_qid')) {
57
            $this->addSql('CREATE INDEX idx_survey_q_qid ON c_survey_question (question_id)');
58
        }
59
60
        if (false === $table->hasColumn('parent_id')) {
61
            $this->addSql('ALTER TABLE c_survey_question ADD parent_id INT DEFAULT NULL');
62
            $this->addSql('ALTER TABLE c_survey_question ADD CONSTRAINT FK_92F05EE7727ACA70 FOREIGN KEY (parent_id) REFERENCES c_survey_question (iid);');
63
            $this->addSql('CREATE INDEX IDX_92F05EE7727ACA70 ON c_survey_question (parent_id);');
64
        }
65
66
        if (false === $table->hasColumn('parent_option_id')) {
67
            $this->addSql('ALTER TABLE c_survey_question ADD parent_option_id INT DEFAULT NULL;');
68
            $this->addSql('ALTER TABLE c_survey_question ADD CONSTRAINT FK_92F05EE7568F3281 FOREIGN KEY (parent_option_id) REFERENCES c_survey_question_option (iid)');
69
            $this->addSql('CREATE INDEX IDX_92F05EE7568F3281 ON c_survey_question (parent_option_id);');
70
        }
71
72
        $table = $schema->getTable('c_survey_question_option');
73
        if (false === $table->hasIndex('idx_survey_qo_qid')) {
74
            $this->addSql('CREATE INDEX idx_survey_qo_qid ON c_survey_question_option (question_id)');
75
        }
76
77
        $em = $this->getEntityManager();
78
79
        $sql = 'SELECT s.* FROM c_survey s
80
                INNER JOIN extra_field_values efv
81
                ON s.iid = efv.item_id
82
                INNER JOIN extra_field ef
83
                ON efv.field_id = ef.id
84
                WHERE
85
                    ef.variable = "is_mandatory" AND
86
                    ef.extra_field_type = '.ExtraField::SURVEY_FIELD_TYPE.' AND
87
                    efv.value = 1
88
        ';
89
90
        $result = $em->getConnection()->executeQuery($sql);
91
        $data = $result->fetchAllAssociative();
92
        if ($data) {
93
            foreach ($data as $item) {
94
                $id = $item['iid'];
95
                $this->addSql("UPDATE c_survey SET is_mandatory = 1 WHERE iid = $id");
96
            }
97
        }
98
    }
99
100
    public function down(Schema $schema): void
101
    {
102
    }
103
}
104