Passed
Push — master ( 5f975d...198b2c )
by Julito
10:29
created

Version20180319145700::up()   F

Complexity

Conditions 42

Size

Total Lines 273
Code Lines 140

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 42
eloc 140
nop 1
dl 0
loc 273
rs 3.3333
c 1
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
        // option_id is a long text
97
        /*if (!$table->hasIndex('IDX_8A897DDA7C41D6F')) {
98
            $this->addSql('CREATE INDEX IDX_8A897DDA7C41D6F ON c_survey_answer (option_id);');
99
        }*/
100
101
        /*if (!$survey->hasIndex('idx_survey_code')) {
102
            $this->addSql('CREATE INDEX idx_survey_code ON c_survey (code)');
103
        }*/
104
105
        $table = $schema->getTable('c_survey_invitation');
106
107
        $this->addSql('ALTER TABLE c_survey_invitation CHANGE reminder_date reminder_date DATETIME DEFAULT NULL');
108
        $this->addSql(
109
            'UPDATE c_survey_invitation SET reminder_date = NULL WHERE CAST(reminder_date AS CHAR(20)) = "0000-00-00 00:00:00"'
110
        );
111
112
        // c_survey_invitation.user_id
113
        if (!$table->hasColumn('user_id')) {
114
            $this->addSql('ALTER TABLE c_survey_invitation ADD user_id INT DEFAULT NULL');
115
116
            $this->addSql('ALTER TABLE c_survey_invitation CHANGE user user VARCHAR(255) DEFAULT NULL');
117
            $this->addSql('UPDATE c_survey_invitation SET user = NULL WHERE user NOT IN (SELECT id FROM user)');
118
            $this->addSql('UPDATE c_survey_invitation SET user_id = user');
119
        }
120
121
        if (!$table->hasForeignKey('FK_D0BC7C2A76ED395')) {
122
            $this->addSql(
123
                'ALTER TABLE c_survey_invitation ADD CONSTRAINT FK_D0BC7C2A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE'
124
            );
125
        }
126
127
        if (!$table->hasIndex('IDX_D0BC7C2A76ED395')) {
128
            $this->addSql('CREATE INDEX IDX_D0BC7C2A76ED395 ON c_survey_invitation (user_id)');
129
        }
130
131
        if (!$table->hasColumn('answered_at')) {
132
            $this->addSql('ALTER TABLE c_survey_invitation ADD answered_at DATETIME DEFAULT NULL;');
133
        }
134
135
        if (!$table->hasColumn('survey_id')) {
136
            $this->addSql('ALTER TABLE c_survey_invitation ADD survey_id INT DEFAULT NULL');
137
            $this->addSql('ALTER TABLE c_survey_invitation ADD CONSTRAINT FK_D0BC7C2B3FE509D FOREIGN KEY (survey_id) REFERENCES c_survey (iid)');
138
            $this->addSql('CREATE INDEX IDX_D0BC7C2B3FE509D ON c_survey_invitation (survey_id)');
139
        }
140
141
        if ($table->hasIndex('idx_survey_inv_code')) {
142
            $this->addSql('DROP INDEX idx_survey_inv_code ON c_survey_invitation;');
143
        }
144
145
        $this->addSql('ALTER TABLE c_survey_invitation CHANGE c_id c_id INT DEFAULT NULL');
146
        $this->addSql('ALTER TABLE c_survey_invitation CHANGE session_id session_id INT DEFAULT NULL');
147
        $this->addSql('ALTER TABLE c_survey_invitation CHANGE group_id group_id INT DEFAULT NULL');
148
149
        $this->addSql('DELETE FROM c_survey_invitation WHERE c_id IS NULL OR c_id = 0 ');
150
        $this->addSql('UPDATE c_survey_invitation SET session_id = NULL WHERE session_id = 0 ');
151
        $this->addSql('UPDATE c_survey_invitation SET group_id = NULL WHERE group_id = 0 ');
152
153
        $this->addSql('DELETE FROM c_survey_invitation WHERE session_id IS NOT NULL AND session_id NOT IN (SELECT id FROM session)');
154
155
        if (!$table->hasForeignKey('FK_D0BC7C291D79BD3')) {
156
            $this->addSql(
157
                'ALTER TABLE c_survey_invitation ADD CONSTRAINT FK_D0BC7C291D79BD3 FOREIGN KEY (c_id) REFERENCES course (id) ON DELETE CASCADE'
158
            );
159
        }
160
161
        if (!$table->hasForeignKey('FK_D0BC7C2613FECDF')) {
162
            $this->addSql(
163
                'ALTER TABLE c_survey_invitation ADD CONSTRAINT FK_D0BC7C2613FECDF FOREIGN KEY (session_id) REFERENCES session (id) ON DELETE CASCADE'
164
            );
165
        }
166
167
        if (!$table->hasForeignKey('FK_D0BC7C2FE54D947')) {
168
            $this->addSql(
169
                'ALTER TABLE c_survey_invitation ADD CONSTRAINT FK_D0BC7C2FE54D947 FOREIGN KEY (group_id) REFERENCES c_group_info (iid) ON DELETE CASCADE'
170
            );
171
        }
172
173
        if (!$table->hasIndex('IDX_D0BC7C2613FECDF')) {
174
            $this->addSql('CREATE INDEX IDX_D0BC7C2613FECDF ON c_survey_invitation (session_id)');
175
        }
176
177
        if (!$table->hasIndex('IDX_D0BC7C2FE54D947')) {
178
            $this->addSql('CREATE INDEX IDX_D0BC7C2FE54D947 ON c_survey_invitation (group_id)');
179
        }
180
181
        $table = $schema->getTable('c_survey_question');
182
183
        if ($table->hasIndex('course')) {
184
            $this->addSql('DROP INDEX course ON c_survey_question');
185
        }
186
187
        if (false === $table->hasColumn('is_required')) {
188
            $table
189
                ->addColumn('is_required', Types::BOOLEAN)
190
                ->setDefault(false)
191
            ;
192
        }
193
        /*if (false === $table->hasIndex('idx_survey_q_qid')) {
194
            $this->addSql('CREATE INDEX idx_survey_q_qid ON c_survey_question (question_id)');
195
        }*/
196
197
        $this->addSql('ALTER TABLE c_survey_question CHANGE survey_id survey_id INT DEFAULT NULL;');
198
199
        if ($table->hasForeignKey('FK_92F05EE7B3FE509D')) {
200
            $this->addSql('ALTER TABLE c_survey_question ADD CONSTRAINT FK_92F05EE7B3FE509D FOREIGN KEY (survey_id) REFERENCES c_survey (iid) ON DELETE CASCADE');
201
        }
202
203
        if ($table->hasIndex('IDX_92F05EE7B3FE509D')) {
204
            $this->addSql('CREATE INDEX IDX_92F05EE7B3FE509D ON c_survey_question (survey_id);');
205
        }
206
207
        if ($table->hasIndex('idx_survey_q_qid')) {
208
            $this->addSql('DROP INDEX idx_survey_q_qid ON c_survey_question;');
209
        }
210
211
        if (false === $table->hasColumn('parent_id')) {
212
            $this->addSql('ALTER TABLE c_survey_question ADD parent_id INT DEFAULT NULL');
213
            $this->addSql('ALTER TABLE c_survey_question ADD CONSTRAINT FK_92F05EE7727ACA70 FOREIGN KEY (parent_id) REFERENCES c_survey_question (iid) ON DELETE SET NULL');
214
            $this->addSql('CREATE INDEX IDX_92F05EE7727ACA70 ON c_survey_question (parent_id);');
215
        }
216
217
        if (false === $table->hasColumn('parent_option_id')) {
218
            $this->addSql('ALTER TABLE c_survey_question ADD parent_option_id INT DEFAULT NULL;');
219
            $this->addSql('ALTER TABLE c_survey_question ADD CONSTRAINT FK_92F05EE7568F3281 FOREIGN KEY (parent_option_id) REFERENCES c_survey_question_option (iid)');
220
            $this->addSql('CREATE INDEX IDX_92F05EE7568F3281 ON c_survey_question (parent_option_id);');
221
        }
222
223
        $table = $schema->getTable('c_survey_question_option');
224
        if ($table->hasIndex('course')) {
225
            $this->addSql('DROP INDEX course ON c_survey_question_option');
226
        }
227
228
        /*if (false === $table->hasIndex('idx_survey_qo_qid')) {
229
            $this->addSql('CREATE INDEX idx_survey_qo_qid ON c_survey_question_option (question_id)');
230
        }*/
231
232
        $this->addSql('ALTER TABLE c_survey_question_option CHANGE question_id question_id INT DEFAULT NULL');
233
        $this->addSql('ALTER TABLE c_survey_question_option CHANGE survey_id survey_id INT DEFAULT NULL;');
234
235
        if (false === $table->hasForeignKey('FK_C4B6F5F1E27F6BF')) {
236
            $this->addSql('ALTER TABLE c_survey_question_option ADD CONSTRAINT FK_C4B6F5F1E27F6BF FOREIGN KEY (question_id) REFERENCES c_survey_question (iid) ON DELETE CASCADE');
237
        }
238
239
        if (false === $table->hasForeignKey('FK_C4B6F5FB3FE509D')) {
240
            $this->addSql('ALTER TABLE c_survey_question_option ADD CONSTRAINT FK_C4B6F5FB3FE509D FOREIGN KEY (survey_id) REFERENCES c_survey (iid) ON DELETE CASCADE');
241
        }
242
243
        if (false === $table->hasIndex('IDX_C4B6F5FB3FE509D')) {
244
            $this->addSql('CREATE INDEX IDX_C4B6F5FB3FE509D ON c_survey_question_option (survey_id);');
245
        }
246
247
        $em = $this->getEntityManager();
248
        $sql = 'SELECT * FROM c_survey ';
249
        $result = $em->getConnection()->executeQuery($sql);
250
        $data = $result->fetchAllAssociative();
251
        $surveyList = [];
252
        if ($data) {
253
            foreach ($data as $item) {
254
                $surveyList[$item['c_id']][$item['code']] = $item['iid'];
255
            }
256
        }
257
258
        // Replace survey_code with new survey_id.
259
        $sql = 'SELECT * FROM c_survey_invitation ';
260
        $result = $em->getConnection()->executeQuery($sql);
261
        $data = $result->fetchAllAssociative();
262
        if ($data) {
263
            foreach ($data as $item) {
264
                $id = $item['iid'];
265
                $courseId = $item['c_id'];
266
                $code = $item['survey_code'];
267
268
                $surveyId = $surveyList[$courseId][$code] ?? null;
269
                if (empty($surveyId)) {
270
                    continue;
271
                }
272
273
                $this->addSql("UPDATE c_survey_invitation SET survey_id = $surveyId WHERE iid = $id");
274
            }
275
        }
276
277
        $sql = 'SELECT s.* FROM c_survey s
278
                INNER JOIN extra_field_values efv
279
                ON s.iid = efv.item_id
280
                INNER JOIN extra_field ef
281
                ON efv.field_id = ef.id
282
                WHERE
283
                    ef.variable = "is_mandatory" AND
284
                    ef.extra_field_type = '.ExtraField::SURVEY_FIELD_TYPE.' AND
285
                    efv.value = 1
286
        ';
287
288
        $result = $em->getConnection()->executeQuery($sql);
289
        $data = $result->fetchAllAssociative();
290
        if ($data) {
291
            foreach ($data as $item) {
292
                $id = $item['iid'];
293
                $this->addSql("UPDATE c_survey SET is_mandatory = 1 WHERE iid = {$id}");
294
            }
295
        }
296
    }
297
298
    public function down(Schema $schema): void
299
    {
300
    }
301
}
302