Completed
Pull Request — master (#425)
by René
08:50
created

Version0009Date20181125061900::copyTextOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 0
dl 0
loc 22
ccs 0
cts 18
cp 0
crap 6
rs 9.7666
c 0
b 0
f 0
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 Doctrine\DBAL\Exception\TableNotFoundException;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Exception\TableNotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Platforms\PostgreSqlPlatform was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Doctrine\DBAL\Types\Type;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Types\Type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use OCP\DB\ISchemaWrapper;
0 ignored issues
show
Bug introduced by
The type OCP\DB\ISchemaWrapper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use OCP\DB\QueryBuilder\IQueryBuilder;
31
use OCP\IConfig;
32
use OCP\IDBConnection;
33
use OCP\Migration\SimpleMigrationStep;
0 ignored issues
show
Bug introduced by
The type OCP\Migration\SimpleMigrationStep was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
use OCP\Migration\IOutput;
35
36
/**
37
 * Installation class for the polls app.
38
 * Initial db creation
39
 */
40
class Version0009Date20181125061900 extends SimpleMigrationStep {
41
42
	/** @var IDBConnection */
43
	protected $connection;
44
45
	/** @var IConfig */
46
	protected $config;
47
48
	/**
49
	 * @param IDBConnection $connection
50
	 * @param IConfig $config
51
	 */
52
	public function __construct(IDBConnection $connection, IConfig $config) {
53
		$this->connection = $connection;
54
		$this->config = $config;
55
	}
56
57
	/**
58
	 * @param IOutput $output
59
	 * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
60
	 * @param array $options
61
	 * @return null|ISchemaWrapper
62
	 * @since 13.0.0
63
	 */
64
	public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
65
		/** @var ISchemaWrapper $schema */
66
		$schema = $schemaClosure();
67
68
		if (!$schema->hasTable('polls_events')) {
69
			$table = $schema->createTable('polls_events');
70
			$table->addColumn('id', Type::INTEGER, [
71
				'autoincrement' => true,
72
				'notnull' => true,
73
			]);
74
			$table->addColumn('hash', Type::STRING, [
75
				'notnull' => false,
76
				'length' => 64,
77
			]);
78
			$table->addColumn('type', Type::BIGINT, [
79
				'notnull' => false,
80
				'length' => 16,
81
			]);
82
			$table->addColumn('title', Type::STRING, [
83
				'notnull' => true,
84
				'length' => 128,
85
			]);
86
			$table->addColumn('description', Type::STRING, [
87
				'notnull' => true,
88
				'length' => 1024,
89
			]);
90
			$table->addColumn('owner', Type::STRING, [
91
				'notnull' => true,
92
				'length' => 64,
93
			]);
94
			$table->addColumn('created', Type::DATETIME, [
95
				'notnull' => false,
96
			]);
97
			$table->addColumn('access', Type::STRING, [
98
				'notnull' => false,
99
				'length' => 1024,
100
			]);
101
			$table->addColumn('expire', Type::DATETIME, [
102
				'notnull' => false,
103
			]);
104
			$table->addColumn('is_anonymous', Type::INTEGER, [
105
				'notnull' => false,
106
				'default' => 0,
107
			]);
108
			$table->addColumn('full_anonymous', Type::INTEGER, [
109
				'notnull' => false,
110
				'default' => 0,
111
			]);
112
			$table->addColumn('disallow_maybe', Type::INTEGER, [
113
				'notnull' => false,
114
				'default' => 0
115
			]);
116
			$table->setPrimaryKey(['id']);
117
		}
118
119
		if (!$schema->hasTable('polls_options')) {
120
			$table = $schema->createTable('polls_options');
121
			$table->addColumn('id', Type::INTEGER, [
122
				'autoincrement' => true,
123
				'notnull' => true,
124
			]);
125
			$table->addColumn('poll_id', Type::INTEGER, [
126
				'notnull' => false,
127
			]);
128
			$table->addColumn('poll_option_text', Type::STRING, [
129
				'notnull' => false, // maybe true?
130
				'length' => 256,
131
			]);
132
			$table->addColumn('timestamp', Type::INTEGER, [
133
				'notnull' => false,
134
				'default' => 0
135
			]);
136
			$table->setPrimaryKey(['id']);
137
		}
138
139
		if (!$schema->hasTable('polls_votes')) {
140
			$table = $schema->createTable('polls_votes');
141
			$table->addColumn('id', Type::INTEGER, [
142
				'autoincrement' => true,
143
				'notnull' => true,
144
			]);
145
			$table->addColumn('poll_id', Type::INTEGER, [
146
				'notnull' => false,
147
			]);
148
			$table->addColumn('user_id', Type::STRING, [
149
				'notnull' => true,
150
				'length' => 64,
151
			]);
152
			$table->addColumn('vote_option_id', Type::INTEGER, [
153
				'notnull' => true,
154
				'default' => 0,
155
				'length' => 64,
156
			]);
157
			$table->addColumn('vote_option_text', Type::STRING, [
158
				'notnull' => false, // maybe true?
159
				'length' => 256,
160
			]);
161
			$table->addColumn('vote_answer', Type::STRING, [
162
				'notnull' => false,
163
				'length' => 64,
164
			]);
165
			$table->setPrimaryKey(['id']);
166
		}
167
168
		if (!$schema->hasTable('polls_comments')) {
169
			$table = $schema->createTable('polls_comments');
170
			$table->addColumn('id', Type::INTEGER, [
171
				'autoincrement' => true,
172
				'notnull' => true,
173
			]);
174
			$table->addColumn('poll_id', Type::INTEGER, [
175
				'notnull' => false,
176
			]);
177
			$table->addColumn('user_id', Type::STRING, [
178
				'notnull' => true,
179
				'length' => 64,
180
			]);
181
			$table->addColumn('dt', Type::STRING, [
182
				'notnull' => true,
183
				'length' => 32,
184
			]);
185
			$table->addColumn('comment', Type::STRING, [
186
				'notnull' => false,
187
				'length' => 1024,
188
			]);
189
			$table->setPrimaryKey(['id']);
190
		}
191
192
		if (!$schema->hasTable('polls_notif')) {
193
			$table = $schema->createTable('polls_notif');
194
			$table->addColumn('id', Type::INTEGER, [
195
				'autoincrement' => true,
196
				'notnull' => true,
197
			]);
198
			$table->addColumn('poll_id', Type::INTEGER, [
199
				'notnull' => false,
200
			]);
201
			$table->addColumn('user_id', Type::STRING, [
202
				'notnull' => true,
203
				'length' => 64,
204
			]);
205
			$table->setPrimaryKey(['id']);
206
		}
207
208
		return $schema;
209
	}
210
211
	/**
212
	 * @param IOutput $output
213
	 * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
214
	 * @param array $options
215
	 * @since 13.0.0
216
	 */
217
	public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
218
		/** @var ISchemaWrapper $schema */
219
		$schema = $schemaClosure();
220
221
		if ($schema->hasTable('polls_dts')) {
222
			$this->copyDateOptions();
223
			$this->copyDateVotes();
224
		}
225
		if ($schema->hasTable('polls_txts')) {
226
			$this->copyTextOptions();
227
			$this->copyTextVotes();
228
		}
229
	}
230
231
	/**
232
	 * Copy date options
233
	 */
234
	protected function copyDateOptions() {
235
		$insert = $this->connection->getQueryBuilder();
236
		$insert->insert('polls_options')
237
			->values([
238
				'poll_id' => $insert->createParameter('poll_id'),
239
				// Decide between one of both
240
				// 'poll_date' => $insert->createParameter('poll_date'),
241
				'poll_option_text' => $insert->createParameter('poll_option_text'),
242
			]);
243
		$query = $this->connection->getQueryBuilder();
244
		$query->select('*')
245
			->from('polls_dts');
246
		$result = $query->execute();
247
		while ($row = $result->fetch()) {
248
			$insert
249
				->setParameter('poll_id', $row['poll_id'])
250
				// Decide between one of both
251
				// ->setParameter('poll_date', $row['dt'])
252
				->setParameter('poll_option_text', date($row['dt']));
253
			$insert->execute();
254
		}
255
		$result->closeCursor();
256
	}
257
258
	/**
259
	 * Copy text options
260
	 */
261
	protected function copyTextOptions() {
262
		$insert = $this->connection->getQueryBuilder();
263
		$insert->insert('polls_options')
264
			->values([
265
				'poll_id' => $insert->createParameter('poll_id'),
266
				// Decide between one of both
267
				// 'poll_text' => $insert->createParameter('poll_text'),
268
				'poll_option_text' => $insert->createParameter('poll_option_text'),
269
			]);
270
		$query = $this->connection->getQueryBuilder();
271
		$query->select('*')
272
			->from('polls_txts');
273
		$result = $query->execute();
274
		while ($row = $result->fetch()) {
275
			$insert
276
				->setParameter('poll_id', $row['poll_id'])
277
				// Decide between one of both
278
				// ->setParameter('poll_text', $row['text'])
279
				->setParameter('poll_option_text', preg_replace("/_\d*$/", "$1", $row['text']));
280
			$insert->execute();
281
		}
282
		$result->closeCursor();
283
	}
284
285
	/**
286
	 * Copy date votes
287
	 */
288
	protected function copyDateVotes() {
289
		$insert = $this->connection->getQueryBuilder();
290
		$insert->insert('polls_votes')
291
			->values([
292
				'poll_id' => $insert->createParameter('poll_id'),
293
				'user_id' => $insert->createParameter('user_id'),
294
				// 'vote_date' => $insert->createParameter('vote_date'),
295
				'vote_option_id' => $insert->createParameter('vote_option_id'),
296
				'vote_option_text' => $insert->createParameter('vote_option_text'),
297
				// 'vote_type' => $insert->createParameter('vote_type'),
298
				'vote_answer' => $insert->createParameter('vote_answer'),
299
			]);
300
		$query = $this->connection->getQueryBuilder();
301
		$query->select('*')
302
			->from('polls_particip');
303
		$result = $query->execute();
304
		while ($row = $result->fetch()) {
305
			$insert
306
				->setParameter('poll_id', $row['poll_id'])
307
				->setParameter('user_id', $row['user_id'])
308
				// ->setParameter('vote_date', $row['dt'])
309
				->setParameter('vote_option_id', $this->findOptionId($row['poll_id'], $row['dt']))
310
				->setParameter('vote_option_text', date($row['dt']))
311
				// ->setParameter('vote_type', $row['type'])
312
				->setParameter('vote_answer', $this->translateVoteTypeToAnswer($row['type']));
313
			$insert->execute();
314
		}
315
		$result->closeCursor();
316
	}
317
318
	/**
319
	 * Copy text votes
320
	 */
321
	protected function copyTextVotes() {
322
		$insert = $this->connection->getQueryBuilder();
323
		$insert->insert('polls_votes')
324
			->values([
325
				'poll_id' => $insert->createParameter('poll_id'),
326
				'user_id' => $insert->createParameter('user_id'),
327
				// 'vote_text' => $insert->createParameter('vote_text'),
328
				'vote_option_id' => $insert->createParameter('vote_option_id'),
329
				'vote_option_text' => $insert->createParameter('vote_option_text'),
330
				// 'vote_type' => $insert->createParameter('vote_type'),
331
				'vote_answer' => $insert->createParameter('vote_answer'),
332
			]);
333
		$query = $this->connection->getQueryBuilder();
334
		$query->select('*')
335
			->from('polls_particip_text');
336
		$result = $query->execute();
337
		while ($row = $result->fetch()) {
338
			$insert
339
				->setParameter('poll_id', $row['poll_id'])
340
				->setParameter('user_id', $row['user_id'])
341
				// ->setParameter('vote_text', $row['text'])
342
				->setParameter('vote_option_id', $this->findOptionId($row['poll_id'], preg_replace("/_\d*$/", "$1", $row['text'])))
343
				->setParameter('vote_option_text', preg_replace("/_\d*$/", "$1", $row['text']))
344
				// ->setParameter('vote_type', $row['type'])
345
				->setParameter('vote_answer', $this->translateVoteTypeToAnswer($row['type']));
346
			$insert->execute();
347
		}
348
		$result->closeCursor();
349
	}
350
351
	/**
352
	 * @param int $voteType
353
	 * @return string
354
	 */
355
	protected function findOptionId($pollId, $text) {
356
		$queryFind = $this->connection->getQueryBuilder();
357
		$queryFind->select(['id'])
358
			->from('polls_options')
359
			// ->where($queryFind->expr()->eq('poll_id', $pollId))
360
			// ->andWhere($queryFind->expr()->eq('poll_option', $text));
361
			->where('poll_id = "' . $pollId . '"')
362
			->andWhere('poll_option_text ="' . $text . '"');
363
364
		$resultFind = $queryFind->execute();
365
		$row = $resultFind->fetch();
366
		return $row['id'];
367
368
	}
369
370
	protected function translateVoteTypeToAnswer($voteType) {
371
		switch ($voteType) {
372
			case 0:
373
				$answer = "no";
374
				break;
375
			case 1:
376
				$answer = "yes";
377
				break;
378
			case 2:
379
				$answer = "maybe";
380
				break;
381
			default:
382
				$answer = "no";
383
		}
384
		return $answer;
385
	}
386
}
387