|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017 René Gieling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author René Gieling <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\Polls\Migration; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\DB\ISchemaWrapper; |
|
27
|
|
|
use OCP\IConfig; |
|
28
|
|
|
use OCP\IDBConnection; |
|
29
|
|
|
use OCP\Migration\SimpleMigrationStep; |
|
30
|
|
|
use OCP\Migration\IOutput; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Installation class for the polls app. |
|
34
|
|
|
* Initial db creation |
|
35
|
|
|
*/ |
|
36
|
|
|
class Version0009Date20181125061900 extends SimpleMigrationStep { |
|
37
|
|
|
|
|
38
|
|
|
/** @var IDBConnection */ |
|
39
|
|
|
protected $connection; |
|
40
|
|
|
|
|
41
|
|
|
/** @var IConfig */ |
|
42
|
|
|
protected $config; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct(IDBConnection $connection, IConfig $config) { |
|
45
|
|
|
$this->connection = $connection; |
|
46
|
|
|
$this->config = $config; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
51
|
|
|
*/ |
|
52
|
|
|
public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
53
|
|
|
/** @var ISchemaWrapper $schema */ |
|
54
|
|
|
$schema = $schemaClosure(); |
|
55
|
|
|
|
|
56
|
|
|
if (!$schema->hasTable('polls_events')) { |
|
57
|
|
|
$table = $schema->createTable('polls_events'); |
|
58
|
|
|
$table->addColumn('id', 'integer', [ |
|
59
|
|
|
'autoincrement' => true, |
|
60
|
|
|
'notnull' => true, |
|
61
|
|
|
]); |
|
62
|
|
|
$table->addColumn('hash', 'string', [ |
|
63
|
|
|
'notnull' => false, |
|
64
|
|
|
'length' => 64, |
|
65
|
|
|
]); |
|
66
|
|
|
$table->addColumn('type', 'bigint', [ |
|
67
|
|
|
'notnull' => false, |
|
68
|
|
|
'length' => 16, |
|
69
|
|
|
]); |
|
70
|
|
|
$table->addColumn('title', 'string', [ |
|
71
|
|
|
'notnull' => true, |
|
72
|
|
|
'length' => 128, |
|
73
|
|
|
]); |
|
74
|
|
|
$table->addColumn('description', 'string', [ |
|
75
|
|
|
'notnull' => true, |
|
76
|
|
|
'length' => 1024, |
|
77
|
|
|
]); |
|
78
|
|
|
$table->addColumn('owner', 'string', [ |
|
79
|
|
|
'notnull' => true, |
|
80
|
|
|
'length' => 64, |
|
81
|
|
|
]); |
|
82
|
|
|
$table->addColumn('created', 'datetime', [ |
|
83
|
|
|
'notnull' => false, |
|
84
|
|
|
]); |
|
85
|
|
|
$table->addColumn('access', 'string', [ |
|
86
|
|
|
'notnull' => false, |
|
87
|
|
|
'length' => 1024, |
|
88
|
|
|
]); |
|
89
|
|
|
$table->addColumn('expire', 'datetime', [ |
|
90
|
|
|
'notnull' => false, |
|
91
|
|
|
]); |
|
92
|
|
|
$table->addColumn('is_anonymous', 'integer', [ |
|
93
|
|
|
'notnull' => false, |
|
94
|
|
|
'default' => 0, |
|
95
|
|
|
]); |
|
96
|
|
|
$table->addColumn('full_anonymous', 'integer', [ |
|
97
|
|
|
'notnull' => false, |
|
98
|
|
|
'default' => 0, |
|
99
|
|
|
]); |
|
100
|
|
|
$table->addColumn('allow_maybe', 'integer', [ |
|
101
|
|
|
'notnull' => false, |
|
102
|
|
|
'default' => 1, |
|
103
|
|
|
]); |
|
104
|
|
|
$table->setPrimaryKey(['id']); |
|
105
|
|
|
} else { |
|
106
|
|
|
$table = $schema->getTable('polls_events'); |
|
107
|
|
|
if (!$table->hasColumn('allow_maybe')) { |
|
108
|
|
|
$table->addColumn('allow_maybe', 'integer', [ |
|
109
|
|
|
'notnull' => false, |
|
110
|
|
|
'default' => 1, |
|
111
|
|
|
]); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (!$schema->hasTable('polls_options')) { |
|
116
|
|
|
$table = $schema->createTable('polls_options'); |
|
117
|
|
|
$table->addColumn('id', 'integer', [ |
|
118
|
|
|
'autoincrement' => true, |
|
119
|
|
|
'notnull' => true, |
|
120
|
|
|
]); |
|
121
|
|
|
$table->addColumn('poll_id', 'integer', [ |
|
122
|
|
|
'notnull' => false, |
|
123
|
|
|
]); |
|
124
|
|
|
$table->addColumn('poll_option_text', 'string', [ |
|
125
|
|
|
'notnull' => false, |
|
126
|
|
|
'length' => 256, |
|
127
|
|
|
]); |
|
128
|
|
|
$table->addColumn('timestamp', 'integer', [ |
|
129
|
|
|
'notnull' => false, |
|
130
|
|
|
'default' => 0 |
|
131
|
|
|
]); |
|
132
|
|
|
$table->setPrimaryKey(['id']); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (!$schema->hasTable('polls_votes')) { |
|
136
|
|
|
$table = $schema->createTable('polls_votes'); |
|
137
|
|
|
$table->addColumn('id', 'integer', [ |
|
138
|
|
|
'autoincrement' => true, |
|
139
|
|
|
'notnull' => true, |
|
140
|
|
|
]); |
|
141
|
|
|
$table->addColumn('poll_id', 'integer', [ |
|
142
|
|
|
'notnull' => false, |
|
143
|
|
|
]); |
|
144
|
|
|
$table->addColumn('user_id', 'string', [ |
|
145
|
|
|
'notnull' => true, |
|
146
|
|
|
'length' => 64, |
|
147
|
|
|
]); |
|
148
|
|
|
$table->addColumn('vote_option_id', 'integer', [ |
|
149
|
|
|
'notnull' => true, |
|
150
|
|
|
'default' => 0, |
|
151
|
|
|
'length' => 64, |
|
152
|
|
|
]); |
|
153
|
|
|
$table->addColumn('vote_option_text', 'string', [ |
|
154
|
|
|
'notnull' => false, |
|
155
|
|
|
'length' => 256, |
|
156
|
|
|
]); |
|
157
|
|
|
$table->addColumn('vote_answer', 'string', [ |
|
158
|
|
|
'notnull' => false, |
|
159
|
|
|
'length' => 64, |
|
160
|
|
|
]); |
|
161
|
|
|
$table->setPrimaryKey(['id']); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
if (!$schema->hasTable('polls_comments')) { |
|
165
|
|
|
$table = $schema->createTable('polls_comments'); |
|
166
|
|
|
$table->addColumn('id', 'integer', [ |
|
167
|
|
|
'autoincrement' => true, |
|
168
|
|
|
'notnull' => true, |
|
169
|
|
|
]); |
|
170
|
|
|
$table->addColumn('poll_id', 'integer', [ |
|
171
|
|
|
'notnull' => false, |
|
172
|
|
|
]); |
|
173
|
|
|
$table->addColumn('user_id', 'string', [ |
|
174
|
|
|
'notnull' => true, |
|
175
|
|
|
'length' => 64, |
|
176
|
|
|
]); |
|
177
|
|
|
$table->addColumn('dt', 'string', [ |
|
178
|
|
|
'notnull' => true, |
|
179
|
|
|
'length' => 32, |
|
180
|
|
|
]); |
|
181
|
|
|
$table->addColumn('comment', 'string', [ |
|
182
|
|
|
'notnull' => false, |
|
183
|
|
|
'length' => 1024, |
|
184
|
|
|
]); |
|
185
|
|
|
$table->setPrimaryKey(['id']); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
if (!$schema->hasTable('polls_notif')) { |
|
189
|
|
|
$table = $schema->createTable('polls_notif'); |
|
190
|
|
|
$table->addColumn('id', 'integer', [ |
|
191
|
|
|
'autoincrement' => true, |
|
192
|
|
|
'notnull' => true, |
|
193
|
|
|
]); |
|
194
|
|
|
$table->addColumn('poll_id', 'integer', [ |
|
195
|
|
|
'notnull' => false, |
|
196
|
|
|
]); |
|
197
|
|
|
$table->addColumn('user_id', 'string', [ |
|
198
|
|
|
'notnull' => true, |
|
199
|
|
|
'length' => 64, |
|
200
|
|
|
]); |
|
201
|
|
|
$table->setPrimaryKey(['id']); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return $schema; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
209
|
|
|
* |
|
210
|
|
|
* @return void |
|
211
|
|
|
*/ |
|
212
|
|
|
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
213
|
|
|
/** @var ISchemaWrapper $schema */ |
|
214
|
|
|
$schema = $schemaClosure(); |
|
215
|
|
|
|
|
216
|
|
|
if ($schema->hasTable('polls_dts')) { |
|
217
|
|
|
$this->copyDateOptions(); |
|
218
|
|
|
$this->copyDateVotes(); |
|
219
|
|
|
} |
|
220
|
|
|
if ($schema->hasTable('polls_txts')) { |
|
221
|
|
|
$this->copyTextOptions(); |
|
222
|
|
|
$this->copyTextVotes(); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Copy date options |
|
228
|
|
|
*/ |
|
229
|
|
|
protected function copyDateOptions(): void { |
|
230
|
|
|
$insert = $this->connection->getQueryBuilder(); |
|
231
|
|
|
$insert->insert('polls_options') |
|
232
|
|
|
->values([ |
|
233
|
|
|
'poll_id' => $insert->createParameter('poll_id'), |
|
234
|
|
|
'poll_option_text' => $insert->createParameter('poll_option_text'), |
|
235
|
|
|
]); |
|
236
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
237
|
|
|
$query->select('*') |
|
238
|
|
|
->from('polls_dts'); |
|
239
|
|
|
$result = $query->execute(); |
|
240
|
|
|
while ($row = $result->fetch()) { |
|
241
|
|
|
$insert |
|
242
|
|
|
->setParameter('poll_id', $row['poll_id']) |
|
243
|
|
|
->setParameter('poll_option_text', date($row['dt'])); |
|
244
|
|
|
$insert->execute(); |
|
245
|
|
|
} |
|
246
|
|
|
$result->closeCursor(); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Copy text options |
|
251
|
|
|
*/ |
|
252
|
|
|
protected function copyTextOptions(): void { |
|
253
|
|
|
$insert = $this->connection->getQueryBuilder(); |
|
254
|
|
|
$insert->insert('polls_options') |
|
255
|
|
|
->values([ |
|
256
|
|
|
'poll_id' => $insert->createParameter('poll_id'), |
|
257
|
|
|
'poll_option_text' => $insert->createParameter('poll_option_text'), |
|
258
|
|
|
]); |
|
259
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
260
|
|
|
$query->select('*') |
|
261
|
|
|
->from('polls_txts'); |
|
262
|
|
|
$result = $query->execute(); |
|
263
|
|
|
while ($row = $result->fetch()) { |
|
264
|
|
|
$insert |
|
265
|
|
|
->setParameter('poll_id', $row['poll_id']) |
|
266
|
|
|
->setParameter('poll_option_text', preg_replace("/_\d*$/", "$1", $row['text'])); |
|
267
|
|
|
$insert->execute(); |
|
268
|
|
|
} |
|
269
|
|
|
$result->closeCursor(); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Copy date votes |
|
274
|
|
|
*/ |
|
275
|
|
|
protected function copyDateVotes(): void { |
|
276
|
|
|
$insert = $this->connection->getQueryBuilder(); |
|
277
|
|
|
$insert->insert('polls_votes') |
|
278
|
|
|
->values([ |
|
279
|
|
|
'poll_id' => $insert->createParameter('poll_id'), |
|
280
|
|
|
'user_id' => $insert->createParameter('user_id'), |
|
281
|
|
|
'vote_option_id' => $insert->createParameter('vote_option_id'), |
|
282
|
|
|
'vote_option_text' => $insert->createParameter('vote_option_text'), |
|
283
|
|
|
'vote_answer' => $insert->createParameter('vote_answer'), |
|
284
|
|
|
]); |
|
285
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
286
|
|
|
$query->select('*') |
|
287
|
|
|
->from('polls_particip'); |
|
288
|
|
|
$result = $query->execute(); |
|
289
|
|
|
while ($row = $result->fetch()) { |
|
290
|
|
|
$insert |
|
291
|
|
|
->setParameter('poll_id', $row['poll_id']) |
|
292
|
|
|
->setParameter('user_id', $row['user_id']) |
|
293
|
|
|
->setParameter('vote_option_id', $this->findOptionId($row['poll_id'], $row['dt'])) |
|
294
|
|
|
->setParameter('vote_option_text', date($row['dt'])) |
|
295
|
|
|
->setParameter('vote_answer', $this->translateVoteTypeToAnswer($row['type'])); |
|
296
|
|
|
$insert->execute(); |
|
297
|
|
|
} |
|
298
|
|
|
$result->closeCursor(); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Copy text votes |
|
303
|
|
|
*/ |
|
304
|
|
|
protected function copyTextVotes(): void { |
|
305
|
|
|
$insert = $this->connection->getQueryBuilder(); |
|
306
|
|
|
$insert->insert('polls_votes') |
|
307
|
|
|
->values([ |
|
308
|
|
|
'poll_id' => $insert->createParameter('poll_id'), |
|
309
|
|
|
'user_id' => $insert->createParameter('user_id'), |
|
310
|
|
|
'vote_option_id' => $insert->createParameter('vote_option_id'), |
|
311
|
|
|
'vote_option_text' => $insert->createParameter('vote_option_text'), |
|
312
|
|
|
'vote_answer' => $insert->createParameter('vote_answer'), |
|
313
|
|
|
]); |
|
314
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
315
|
|
|
$query->select('*') |
|
316
|
|
|
->from('polls_particip_text'); |
|
317
|
|
|
$result = $query->execute(); |
|
318
|
|
|
while ($row = $result->fetch()) { |
|
319
|
|
|
$insert |
|
320
|
|
|
->setParameter('poll_id', $row['poll_id']) |
|
321
|
|
|
->setParameter('user_id', $row['user_id']) |
|
322
|
|
|
->setParameter('vote_option_id', $this->findOptionId($row['poll_id'], preg_replace("/_\d*$/", "$1", $row['text']))) |
|
323
|
|
|
->setParameter('vote_option_text', preg_replace("/_\d*$/", "$1", $row['text'])) |
|
324
|
|
|
->setParameter('vote_answer', $this->translateVoteTypeToAnswer($row['type'])); |
|
325
|
|
|
$insert->execute(); |
|
326
|
|
|
} |
|
327
|
|
|
$result->closeCursor(); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
protected function findOptionId(int $pollId, ?string $text): int { |
|
331
|
|
|
$queryFind = $this->connection->getQueryBuilder(); |
|
332
|
|
|
$queryFind->select(['id']) |
|
333
|
|
|
->from('polls_options') |
|
334
|
|
|
->where('poll_id = :pollId') |
|
335
|
|
|
->andWhere('poll_option_text = :text') |
|
336
|
|
|
->setParameter('pollId', $pollId) |
|
337
|
|
|
->setParameter('text', $text); |
|
338
|
|
|
|
|
339
|
|
|
$resultFind = $queryFind->execute(); |
|
340
|
|
|
$row = $resultFind->fetch(); |
|
341
|
|
|
|
|
342
|
|
|
if ($row['id'] === null) { |
|
343
|
|
|
// seems, we have an orphaned vote, return 0 as vote_option_id |
|
344
|
|
|
return 0; |
|
345
|
|
|
} else { |
|
346
|
|
|
return $row['id']; |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
protected function translateVoteTypeToAnswer(string $voteType): string { |
|
351
|
|
|
switch ($voteType) { |
|
352
|
|
|
case 0: |
|
353
|
|
|
$answer = "no"; |
|
354
|
|
|
break; |
|
355
|
|
|
case 1: |
|
356
|
|
|
$answer = "yes"; |
|
357
|
|
|
break; |
|
358
|
|
|
case 2: |
|
359
|
|
|
$answer = "maybe"; |
|
360
|
|
|
break; |
|
361
|
|
|
default: |
|
362
|
|
|
$answer = "no"; |
|
363
|
|
|
} |
|
364
|
|
|
return $answer; |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
|