Passed
Push — master ( b61354...af9f4d )
by Julito
19:31
created

Version20180319145700::up()   F

Complexity

Conditions 43

Size

Total Lines 264
Code Lines 138

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 43
eloc 138
nop 1
dl 0
loc 264
rs 3.3333
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
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\Entity\ExtraField;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Doctrine\DBAL\Schema\Schema;
12
use Doctrine\DBAL\Types\Types;
13
14
class Version20180319145700 extends AbstractMigrationChamilo
15
{
16
    public function getDescription(): string
17
    {
18
        return 'Migrate Survey tables';
19
    }
20
21
    public function up(Schema $schema): void
22
    {
23
        $survey = $schema->getTable('c_survey');
24
25
        if ($survey->hasIndex('session_id')) {
26
            $this->addSql(' DROP INDEX session_id ON c_survey;');
27
        }
28
29
        if ($survey->hasIndex('course')) {
30
            $this->addSql(' DROP INDEX course ON c_survey;');
31
        }
32
33
        if (false === $survey->hasColumn('is_mandatory')) {
34
            $this->addSql('ALTER TABLE c_survey ADD COLUMN is_mandatory TINYINT(1) DEFAULT "0" NOT NULL');
35
        }
36
37
        $this->addSql('ALTER TABLE c_survey CHANGE parent_id parent_id INT DEFAULT NULL');
38
        $this->addSql('UPDATE c_survey SET parent_id = NULL WHERE parent_id = 0');
39
40
        if (!$survey->hasColumn('lft')) {
41
            $this->addSql(
42
                'ALTER TABLE c_survey ADD lft INT DEFAULT NULL, ADD rgt INT DEFAULT NULL, ADD lvl INT DEFAULT NULL'
43
            );
44
        }
45
46
        if (!$survey->hasForeignKey('FK_F246DB30727ACA70')) {
47
            $this->addSql(
48
                'ALTER TABLE c_survey ADD CONSTRAINT FK_F246DB30727ACA70 FOREIGN KEY (parent_id) REFERENCES c_survey (iid) ON DELETE CASCADE '
49
            );
50
        }
51
        if (!$survey->hasIndex('IDX_F246DB30727ACA70')) {
52
            $this->addSql('CREATE INDEX IDX_F246DB30727ACA70 ON c_survey (parent_id)');
53
        }
54
55
        if (false === $survey->hasColumn('resource_node_id')) {
56
            $this->addSql('ALTER TABLE c_survey ADD resource_node_id BIGINT DEFAULT NULL');
57
            $this->addSql('ALTER TABLE c_survey ADD CONSTRAINT FK_F246DB301BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE');
58
            $this->addSql('CREATE UNIQUE INDEX UNIQ_F246DB301BAD783F ON c_survey (resource_node_id);');
59
        }
60
61
        $this->addSql('ALTER TABLE c_survey CHANGE avail_from avail_from DATETIME DEFAULT NULL;');
62
        $this->addSql('ALTER TABLE c_survey CHANGE avail_till avail_till DATETIME DEFAULT NULL;');
63
64
        $table = $schema->getTable('c_survey_answer');
65
66
        if ($table->hasIndex('course')) {
67
            $this->addSql('DROP INDEX course ON c_survey_answer;');
68
        }
69
70
        $this->addSql('ALTER TABLE c_survey_answer CHANGE survey_id survey_id INT DEFAULT NULL');
71
        $this->addSql('ALTER TABLE c_survey_answer CHANGE question_id question_id INT DEFAULT NULL');
72
        $this->addSql('DELETE FROM c_survey_answer WHERE question_id NOT IN (select iid from c_survey_question)');
73
74
        //$this->addSql('ALTER TABLE c_survey_answer CHANGE option_id option_id INT DEFAULT NULL');
75
76
        if (!$table->hasForeignKey('FK_8A897DDB3FE509D')) {
77
            $this->addSql('ALTER TABLE c_survey_answer ADD CONSTRAINT FK_8A897DDB3FE509D FOREIGN KEY (survey_id) REFERENCES c_survey (iid);');
78
        }
79
80
        if (!$table->hasForeignKey('FK_8A897DD1E27F6BF')) {
81
            $this->addSql('ALTER TABLE c_survey_answer ADD CONSTRAINT FK_8A897DD1E27F6BF FOREIGN KEY (question_id) REFERENCES c_survey_question (iid);');
82
        }
83
84
        /*if (!$table->hasForeignKey('FK_8A897DDA7C41D6F')) {
85
            $this->addSql('ALTER TABLE c_survey_answer ADD CONSTRAINT FK_8A897DDA7C41D6F FOREIGN KEY (option_id) REFERENCES c_survey_question_option (iid);');
86
        }*/
87
88
        if (!$table->hasIndex('IDX_8A897DDB3FE509D')) {
89
            $this->addSql('CREATE INDEX IDX_8A897DDB3FE509D ON c_survey_answer (survey_id);');
90
        }
91
92
        if (!$table->hasIndex('IDX_8A897DD1E27F6BF')) {
93
            $this->addSql('CREATE INDEX IDX_8A897DD1E27F6BF ON c_survey_answer (question_id);');
94
        }
95
96
        if (!$table->hasIndex('IDX_8A897DDA7C41D6F')) {
97
            $this->addSql('CREATE INDEX IDX_8A897DDA7C41D6F ON c_survey_answer (option_id);');
98
        }
99
100
        /*if (!$survey->hasIndex('idx_survey_code')) {
101
            $this->addSql('CREATE INDEX idx_survey_code ON c_survey (code)');
102
        }*/
103
104
        $this->addSql('ALTER TABLE c_survey_invitation CHANGE reminder_date reminder_date DATETIME DEFAULT NULL');
105
        $this->addSql(
106
            'UPDATE c_survey_invitation SET reminder_date = NULL WHERE CAST(reminder_date AS CHAR(20)) = "0000-00-00 00:00:00"'
107
        );
108
109
        $table = $schema->getTable('c_survey_invitation');
110
111
        if (!$table->hasColumn('user_id')) {
112
            $this->addSql('ALTER TABLE c_survey_invitation ADD user_id INT DEFAULT NULL');
113
            $this->addSql('DELETE FROM c_survey_invitation WHERE user NOT IN (SELECT id FROM user) ');
114
            $this->addSql('UPDATE c_survey_invitation SET user_id = user');
115
        }
116
117
        if (!$table->hasForeignKey('FK_D0BC7C2A76ED395')) {
118
            $this->addSql(
119
                'ALTER TABLE c_survey_invitation ADD CONSTRAINT FK_D0BC7C2A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE'
120
            );
121
        }
122
123
        if (!$table->hasIndex('IDX_D0BC7C2A76ED395')) {
124
            $this->addSql('CREATE INDEX IDX_D0BC7C2A76ED395 ON c_survey_invitation (user_id)');
125
        }
126
127
        if (!$table->hasColumn('answered_at')) {
128
            $this->addSql('ALTER TABLE c_survey_invitation ADD answered_at DATETIME DEFAULT NULL;');
129
        }
130
131
        if (!$table->hasColumn('survey_id')) {
132
            $this->addSql('ALTER TABLE c_survey_invitation ADD survey_id INT DEFAULT NULL');
133
            $this->addSql('ALTER TABLE c_survey_invitation ADD CONSTRAINT FK_D0BC7C2B3FE509D FOREIGN KEY (survey_id) REFERENCES c_survey (iid)');
134
            $this->addSql('CREATE INDEX IDX_D0BC7C2B3FE509D ON c_survey_invitation (survey_id)');
135
        }
136
137
        if ($table->hasIndex('idx_survey_inv_code')) {
138
            $this->addSql('DROP INDEX idx_survey_inv_code ON c_survey_invitation;');
139
        }
140
141
        $this->addSql('ALTER TABLE c_survey_invitation CHANGE c_id c_id INT DEFAULT NULL');
142
        $this->addSql('ALTER TABLE c_survey_invitation CHANGE session_id session_id INT DEFAULT NULL');
143
        $this->addSql('ALTER TABLE c_survey_invitation CHANGE group_id group_id INT DEFAULT NULL');
144
        $this->addSql('DELETE FROM c_survey_invitation WHERE session_id NOT IN (SELECT id FROM session)');
145
146
        if (!$table->hasForeignKey('FK_D0BC7C291D79BD3')) {
147
            $this->addSql(
148
                'ALTER TABLE c_survey_invitation ADD CONSTRAINT FK_D0BC7C291D79BD3 FOREIGN KEY (c_id) REFERENCES course (id) ON DELETE CASCADE'
149
            );
150
        }
151
152
        if (!$table->hasForeignKey('FK_D0BC7C2613FECDF')) {
153
            $this->addSql(
154
                'ALTER TABLE c_survey_invitation ADD CONSTRAINT FK_D0BC7C2613FECDF FOREIGN KEY (session_id) REFERENCES session (id) ON DELETE CASCADE'
155
            );
156
        }
157
158
        if (!$table->hasForeignKey('FK_D0BC7C2FE54D947')) {
159
            $this->addSql(
160
                'ALTER TABLE c_survey_invitation ADD CONSTRAINT FK_D0BC7C2FE54D947 FOREIGN KEY (group_id) REFERENCES c_group_info (iid) ON DELETE CASCADE'
161
            );
162
        }
163
164
        if (!$table->hasIndex('IDX_D0BC7C2613FECDF')) {
165
            $this->addSql('CREATE INDEX IDX_D0BC7C2613FECDF ON c_survey_invitation (session_id)');
166
        }
167
168
        if (!$table->hasIndex('IDX_D0BC7C2FE54D947')) {
169
            $this->addSql('CREATE INDEX IDX_D0BC7C2FE54D947 ON c_survey_invitation (group_id)');
170
        }
171
172
        $table = $schema->getTable('c_survey_question');
173
174
        if ($table->hasIndex('course')) {
175
            $this->addSql('DROP INDEX course ON c_survey_question');
176
        }
177
178
        if (false === $table->hasColumn('is_required')) {
179
            $table
180
                ->addColumn('is_required', Types::BOOLEAN)
181
                ->setDefault(false)
182
            ;
183
        }
184
        /*if (false === $table->hasIndex('idx_survey_q_qid')) {
185
            $this->addSql('CREATE INDEX idx_survey_q_qid ON c_survey_question (question_id)');
186
        }*/
187
188
        $this->addSql('ALTER TABLE c_survey_question CHANGE survey_id survey_id INT DEFAULT NULL;');
189
190
        if ($table->hasForeignKey('FK_92F05EE7B3FE509D')) {
191
            $this->addSql('ALTER TABLE c_survey_question ADD CONSTRAINT FK_92F05EE7B3FE509D FOREIGN KEY (survey_id) REFERENCES c_survey (iid) ON DELETE CASCADE');
192
        }
193
194
        if ($table->hasIndex('IDX_92F05EE7B3FE509D')) {
195
            $this->addSql('CREATE INDEX IDX_92F05EE7B3FE509D ON c_survey_question (survey_id);');
196
        }
197
198
        if ($table->hasIndex('idx_survey_q_qid')) {
199
            $this->addSql('DROP INDEX idx_survey_q_qid ON c_survey_question;');
200
        }
201
202
        if (false === $table->hasColumn('parent_id')) {
203
            $this->addSql('ALTER TABLE c_survey_question ADD parent_id INT DEFAULT NULL');
204
            $this->addSql('ALTER TABLE c_survey_question ADD CONSTRAINT FK_92F05EE7727ACA70 FOREIGN KEY (parent_id) REFERENCES c_survey_question (iid) ON DELETE SET NULL');
205
            $this->addSql('CREATE INDEX IDX_92F05EE7727ACA70 ON c_survey_question (parent_id);');
206
        }
207
208
        if (false === $table->hasColumn('parent_option_id')) {
209
            $this->addSql('ALTER TABLE c_survey_question ADD parent_option_id INT DEFAULT NULL;');
210
            $this->addSql('ALTER TABLE c_survey_question ADD CONSTRAINT FK_92F05EE7568F3281 FOREIGN KEY (parent_option_id) REFERENCES c_survey_question_option (iid)');
211
            $this->addSql('CREATE INDEX IDX_92F05EE7568F3281 ON c_survey_question (parent_option_id);');
212
        }
213
214
        $table = $schema->getTable('c_survey_question_option');
215
        if ($table->hasIndex('course')) {
216
            $this->addSql('DROP INDEX course ON c_survey_question_option');
217
        }
218
219
        /*if (false === $table->hasIndex('idx_survey_qo_qid')) {
220
            $this->addSql('CREATE INDEX idx_survey_qo_qid ON c_survey_question_option (question_id)');
221
        }*/
222
223
        $this->addSql('ALTER TABLE c_survey_question_option CHANGE question_id question_id INT DEFAULT NULL');
224
        $this->addSql('ALTER TABLE c_survey_question_option CHANGE survey_id survey_id INT DEFAULT NULL;');
225
226
        if (false === $table->hasForeignKey('FK_C4B6F5F1E27F6BF')) {
227
            $this->addSql('ALTER TABLE c_survey_question_option ADD CONSTRAINT FK_C4B6F5F1E27F6BF FOREIGN KEY (question_id) REFERENCES c_survey_question (iid) ON DELETE CASCADE');
228
        }
229
230
        if (false === $table->hasForeignKey('FK_C4B6F5FB3FE509D')) {
231
            $this->addSql('ALTER TABLE c_survey_question_option ADD CONSTRAINT FK_C4B6F5FB3FE509D FOREIGN KEY (survey_id) REFERENCES c_survey (iid) ON DELETE CASCADE');
232
        }
233
234
        if (false === $table->hasIndex('IDX_C4B6F5FB3FE509D')) {
235
            $this->addSql('CREATE INDEX IDX_C4B6F5FB3FE509D ON c_survey_question_option (survey_id);');
236
        }
237
238
        $em = $this->getEntityManager();
239
        $sql = 'SELECT * FROM c_survey ';
240
        $result = $em->getConnection()->executeQuery($sql);
241
        $data = $result->fetchAllAssociative();
242
        $surveyList = [];
243
        if ($data) {
244
            foreach ($data as $item) {
245
                $surveyList[$item['c_id']][$item['code']] = $item['iid'];
246
            }
247
        }
248
249
        // Replace survey_code with new survey_id.
250
        $sql = 'SELECT * FROM c_survey_invitation ';
251
        $result = $em->getConnection()->executeQuery($sql);
252
        $data = $result->fetchAllAssociative();
253
        if ($data) {
254
            foreach ($data as $item) {
255
                $id = $item['iid'];
256
                $courseId = $item['c_id'];
257
                $code = $item['survey_code'];
258
259
                $surveyId = $surveyList[$courseId][$code] ?? null;
260
                if (empty($surveyId)) {
261
                    continue;
262
                }
263
264
                $this->addSql("UPDATE c_survey_invitation SET survey_id = $surveyId WHERE iid = $id");
265
            }
266
        }
267
268
        $sql = 'SELECT s.* FROM c_survey s
269
                INNER JOIN extra_field_values efv
270
                ON s.iid = efv.item_id
271
                INNER JOIN extra_field ef
272
                ON efv.field_id = ef.id
273
                WHERE
274
                    ef.variable = "is_mandatory" AND
275
                    ef.extra_field_type = '.ExtraField::SURVEY_FIELD_TYPE.' AND
276
                    efv.value = 1
277
        ';
278
279
        $result = $em->getConnection()->executeQuery($sql);
280
        $data = $result->fetchAllAssociative();
281
        if ($data) {
282
            foreach ($data as $item) {
283
                $id = $item['iid'];
284
                $this->addSql("UPDATE c_survey SET is_mandatory = 1 WHERE iid = {$id}");
285
            }
286
        }
287
    }
288
289
    public function down(Schema $schema): void
290
    {
291
    }
292
}
293