Passed
Push — master ( b9a35c...ab20b0 )
by Julito
15:41
created

Version20180319145700::up()   D

Complexity

Conditions 10
Paths 384

Size

Total Lines 64
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 34
c 0
b 0
f 0
nc 384
nop 1
dl 0
loc 64
rs 4.5333

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
        /*if (!$survey->hasIndex('idx_survey_code')) {
31
            $this->addSql('CREATE INDEX idx_survey_code ON c_survey (code)');
32
        }*/
33
34
        $this->addSql('ALTER TABLE c_survey_invitation CHANGE reminder_date reminder_date DATETIME DEFAULT NULL');
35
        $this->addSql(
36
            'UPDATE c_survey_invitation SET reminder_date = NULL WHERE CAST(reminder_date AS CHAR(20)) = "0000-00-00 00:00:00"'
37
        );
38
39
        $table = $schema->getTable('c_survey_invitation');
40
        if (false === $table->hasColumn('answered_at')) {
41
            $this->addSql('ALTER TABLE c_survey_invitation ADD answered_at DATETIME DEFAULT NULL;');
42
        }
43
        if (false === $table->hasIndex('idx_survey_inv_code')) {
44
            $this->addSql('CREATE INDEX idx_survey_inv_code ON c_survey_invitation (survey_code)');
45
        }
46
47
        $table = $schema->getTable('c_survey_question');
48
        if (!$table->hasColumn('is_required')) {
49
            $table
50
                ->addColumn('is_required', Types::BOOLEAN)
51
                ->setDefault(false);
52
        }
53
        if (false === $table->hasIndex('idx_survey_q_qid')) {
54
            $this->addSql('CREATE INDEX idx_survey_q_qid ON c_survey_question (question_id)');
55
        }
56
57
        $table = $schema->getTable('c_survey_question_option');
58
        if (false === $table->hasIndex('idx_survey_qo_qid')) {
59
            $this->addSql('CREATE INDEX idx_survey_qo_qid ON c_survey_question_option (question_id)');
60
        }
61
62
        $em = $this->getEntityManager();
63
64
        $sql = 'SELECT s.* FROM c_survey s
65
                INNER JOIN extra_field_values efv
66
                ON s.iid = efv.item_id
67
                INNER JOIN extra_field ef
68
                ON efv.field_id = ef.id
69
                WHERE
70
                    ef.variable = "is_mandatory" AND
71
                    ef.extra_field_type = '.ExtraField::SURVEY_FIELD_TYPE.' AND
72
                    efv.value = 1
73
        ';
74
75
        $result = $em->getConnection()->executeQuery($sql);
76
        $data = $result->fetchAllAssociative();
77
        if ($data) {
78
            foreach ($data as $item) {
79
                $id = $item['iid'];
80
                $this->addSql("UPDATE c_survey SET is_mandatory = 1 WHERE iid = $id");
81
            }
82
        }
83
    }
84
85
    public function down(Schema $schema): void
86
    {
87
    }
88
}
89