Passed
Push — dependabot/npm_and_yarn/highli... ( cb1e44...34f0f6 )
by
unknown
13:27 queued 06:04
created

Version20180319145700   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 65
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B up() 0 57 9
A down() 0 2 1
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 Chamilo\CourseBundle\Entity\CSurvey;
10
use Doctrine\DBAL\Schema\Schema;
11
use Doctrine\DBAL\Types\Types;
12
13
/**
14
 * Survey changes.
15
 */
16
class Version20180319145700 extends AbstractMigrationChamilo
17
{
18
    public function up(Schema $schema): void
19
    {
20
        $survey = $schema->getTable('c_survey');
21
        if (!$survey->hasColumn('is_mandatory')) {
22
            $this->addSql('ALTER TABLE c_survey ADD COLUMN is_mandatory TINYINT(1) DEFAULT "0" NOT NULL');
23
        }
24
        /*if (!$survey->hasIndex('idx_survey_code')) {
25
            $this->addSql('CREATE INDEX idx_survey_code ON c_survey (code)');
26
        }*/
27
28
        $this->addSql('ALTER TABLE c_survey_invitation CHANGE reminder_date reminder_date DATETIME DEFAULT NULL');
29
        $this->addSql(
30
            'UPDATE c_survey_invitation SET reminder_date = NULL WHERE CAST(reminder_date AS CHAR(20)) = "0000-00-00 00:00:00"'
31
        );
32
33
        $table = $schema->getTable('c_survey_invitation');
34
        if (false === $table->hasColumn('answered_at')) {
35
            $this->addSql('ALTER TABLE c_survey_invitation ADD answered_at DATETIME DEFAULT NULL;');
36
        }
37
        if (false === $table->hasIndex('idx_survey_inv_code')) {
38
            $this->addSql('CREATE INDEX idx_survey_inv_code ON c_survey_invitation (survey_code)');
39
        }
40
41
        $table = $schema->getTable('c_survey_question');
42
        if (!$table->hasColumn('is_required')) {
43
            $table
44
                ->addColumn('is_required', Types::BOOLEAN)
45
                ->setDefault(false);
46
        }
47
        if (false === $table->hasIndex('idx_survey_q_qid')) {
48
            $this->addSql('CREATE INDEX idx_survey_q_qid ON c_survey_question (question_id)');
49
        }
50
51
        $table = $schema->getTable('c_survey_question_option');
52
        if (false === $table->hasIndex('idx_survey_qo_qid')) {
53
            $this->addSql('CREATE INDEX idx_survey_qo_qid ON c_survey_question_option (question_id)');
54
        }
55
56
        $em = $this->getEntityManager();
57
58
        $sql = 'SELECT s.* FROM c_survey s
59
                INNER JOIN extra_field_values efv
60
                ON s.iid = efv.item_id
61
                INNER JOIN extra_field ef
62
                ON efv.field_id = ef.id
63
                WHERE
64
                    ef.variable = "is_mandatory" AND
65
                    ef.extra_field_type = '.ExtraField::SURVEY_FIELD_TYPE.' AND
66
                    efv.value = 1
67
        ';
68
69
        $result = $em->getConnection()->executeQuery($sql);
70
        $data = $result->fetchAllAssociative();
71
        if ($data) {
72
            foreach ($data as $item) {
73
                $id = $item['iid'];
74
                $this->addSql("UPDATE c_survey SET is_mandatory = 1 WHERE iid = $id");
75
            }
76
        }
77
    }
78
79
    public function down(Schema $schema): void
80
    {
81
    }
82
}
83