Passed
Push — master ( b5687c...5f4d4b )
by Julito
10:14
created

Version20180319145700::up()   F

Complexity

Conditions 39

Size

Total Lines 271
Code Lines 130

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 39
eloc 130
c 1
b 0
f 0
nop 1
dl 0
loc 271
rs 3.3333

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