Passed
Push — master ( b80fb7...0c270b )
by René
11:50
created

Version009000Date20171202105141::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
rs 9.7666
c 0
b 0
f 0
ccs 0
cts 18
cp 0
crap 6
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 Version009000Date20171202105141 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->setPrimaryKey(['id']);
113
		}
114
115
		if (!$schema->hasTable('polls_options')) {
116
			$table = $schema->createTable('polls_options');
117
			$table->addColumn('id', Type::INTEGER, [
118
				'autoincrement' => true,
119
				'notnull' => true,
120
			]);
121
			$table->addColumn('poll_id', Type::INTEGER, [
122
				'notnull' => false,
123
			]);
124
			$table->addColumn('poll_option_text', Type::STRING, [
125
				'notnull' => false, // maybe true?
126
				'length' => 256,
127
			]);
128
			$table->setPrimaryKey(['id']);
129
		}
130
		
131
		if (!$schema->hasTable('polls_votes')) {
132
			$table = $schema->createTable('polls_votes');
133
			$table->addColumn('id', Type::INTEGER, [
134
				'autoincrement' => true,
135
				'notnull' => true,
136
			]);
137
			$table->addColumn('poll_id', Type::INTEGER, [
138
				'notnull' => false,
139
			]);
140
			$table->addColumn('user_id', Type::STRING, [
141
				'notnull' => true,
142
				'length' => 64,
143
			]);
144
			$table->addColumn('vote_option_id', Type::INTEGER, [
145
				'notnull' => true,
146
				'length' => 64,
147
			]);
148
			$table->addColumn('vote_option_text', Type::STRING, [
149
				'notnull' => false, // maybe true?
150
				'length' => 256,
151
			]);
152
			$table->addColumn('vote_answer', Type::STRING, [
153
				'notnull' => false,
154
				'length' => 64,
155
			]);
156
			$table->setPrimaryKey(['id']);
157
		}
158
159
		if (!$schema->hasTable('polls_comments')) {
160
			$table = $schema->createTable('polls_comments');
161
			$table->addColumn('id', Type::INTEGER, [
162
				'autoincrement' => true,
163
				'notnull' => true,
164
			]);
165
			$table->addColumn('poll_id', Type::INTEGER, [
166
				'notnull' => false,
167
			]);
168
			$table->addColumn('user_id', Type::STRING, [
169
				'notnull' => true,
170
				'length' => 64,
171
			]);
172
			$table->addColumn('dt', Type::STRING, [
173
				'notnull' => true,
174
				'length' => 32,
175
			]);
176
			$table->addColumn('comment', Type::STRING, [
177
				'notnull' => false,
178
				'length' => 1024,
179
			]);
180
			$table->setPrimaryKey(['id']);
181
		}
182
183
		if (!$schema->hasTable('polls_notif')) {
184
			$table = $schema->createTable('polls_notif');
185
			$table->addColumn('id', Type::INTEGER, [
186
				'autoincrement' => true,
187
				'notnull' => true,
188
			]);
189
			$table->addColumn('poll_id', Type::INTEGER, [
190
				'notnull' => false,
191
			]);
192
			$table->addColumn('user_id', Type::STRING, [
193
				'notnull' => true,
194
				'length' => 64,
195
			]);
196
			$table->setPrimaryKey(['id']);
197
		}
198
199
		return $schema;
200
	}
201
202
	/**
203
	 * @param IOutput $output
204
	 * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
205
	 * @param array $options
206
	 * @since 13.0.0
207
	 */
208
	public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
209
		/** @var ISchemaWrapper $schema */
210
		$schema = $schemaClosure();
211
212
		if ($schema->hasTable('polls_dts')) {
213
			$this->copyDateOptions();
214
			$this->copyDateVotes();
215
		}
216
		if ($schema->hasTable('polls_txts')) {
217
			$this->copyTextOptions();
218
			$this->copyTextVotes();
219
		}
220
	}
221
222
	/**
223
	 * Copy date options 
224
	 */
225
	protected function copyDateOptions() {
226
		$insert = $this->connection->getQueryBuilder();
227
		$insert->insert('polls_options')
228
			->values([
229
				'poll_id' => $insert->createParameter('poll_id'),
230
				// Decide between one of both
231
				// 'poll_date' => $insert->createParameter('poll_date'),
232
				'poll_option_text' => $insert->createParameter('poll_option_text'),
233
			]);
234
		$query = $this->connection->getQueryBuilder();
235
		$query->select('*')
236
			->from('polls_dts');
237
		$result = $query->execute();
238
		while ($row = $result->fetch()) {
239
			$insert
240
				->setParameter('poll_id', $row['poll_id'])
241
				// Decide between one of both
242
				// ->setParameter('poll_date', $row['dt'])
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() {
253
		$insert = $this->connection->getQueryBuilder();
254
		$insert->insert('polls_options')
255
			->values([
256
				'poll_id' => $insert->createParameter('poll_id'),
257
				// Decide between one of both
258
				// 'poll_text' => $insert->createParameter('poll_text'),
259
				'poll_option_text' => $insert->createParameter('poll_option_text'),
260
			]);
261
		$query = $this->connection->getQueryBuilder();
262
		$query->select('*')
263
			->from('polls_txts');
264
		$result = $query->execute();
265
		while ($row = $result->fetch()) {
266
			$insert
267
				->setParameter('poll_id', $row['poll_id'])
268
				// Decide between one of both
269
				// ->setParameter('poll_text', $row['text'])
270
				->setParameter('poll_option_text', preg_replace("/_\d*$/", "$1", $row['text']));
271
			$insert->execute();
272
		}
273
		$result->closeCursor();
274
	}
275
276
	/**
277
	 * Copy date votes 
278
	 */
279
	protected function copyDateVotes() {
280
		$insert = $this->connection->getQueryBuilder();
281
		$insert->insert('polls_votes')
282
			->values([
283
				'poll_id' => $insert->createParameter('poll_id'),
284
				'user_id' => $insert->createParameter('user_id'),
285
				// 'vote_date' => $insert->createParameter('vote_date'),
286
				'vote_option_id' => $insert->createParameter('vote_option_id'),
287
				'vote_option_text' => $insert->createParameter('vote_option_text'),
288
				// 'vote_type' => $insert->createParameter('vote_type'),
289
				'vote_answer' => $insert->createParameter('vote_answer'),
290
			]);
291
		$query = $this->connection->getQueryBuilder();
292
		$query->select('*')
293
			->from('polls_particip');
294
		$result = $query->execute();
295
		while ($row = $result->fetch()) {
296
			$insert
297
				->setParameter('poll_id', $row['poll_id'])
298
				->setParameter('user_id', $row['user_id'])
299
				// ->setParameter('vote_date', $row['dt'])
300
				->setParameter('vote_option_id', $this->findOptionId($row['poll_id'], $row['dt']))
301
				->setParameter('vote_option_text', date($row['dt']))
302
				// ->setParameter('vote_type', $row['type'])
303
				->setParameter('vote_answer', $this->translateVoteTypeToAnswer($row['type']));
304
			$insert->execute();
305
		}
306
		$result->closeCursor();
307
	}
308
	
309
	/**
310
	 * Copy text votes 
311
	 */
312
	protected function copyTextVotes() {
313
		$insert = $this->connection->getQueryBuilder();
314
		$insert->insert('polls_votes')
315
			->values([
316
				'poll_id' => $insert->createParameter('poll_id'),
317
				'user_id' => $insert->createParameter('user_id'),
318
				// 'vote_text' => $insert->createParameter('vote_text'),
319
				'vote_option_id' => $insert->createParameter('vote_option_id'),
320
				'vote_option_text' => $insert->createParameter('vote_option_text'),
321
				// 'vote_type' => $insert->createParameter('vote_type'),
322
				'vote_answer' => $insert->createParameter('vote_answer'),
323
			]);
324
		$query = $this->connection->getQueryBuilder();
325
		$query->select('*')
326
			->from('polls_particip_text');
327
		$result = $query->execute();
328
		while ($row = $result->fetch()) {
329
			$insert
330
				->setParameter('poll_id', $row['poll_id'])
331
				->setParameter('user_id', $row['user_id'])
332
				// ->setParameter('vote_text', $row['text'])
333
				->setParameter('vote_option_id', $this->findOptionId($row['poll_id'], preg_replace("/_\d*$/", "$1", $row['text'])))
334
				->setParameter('vote_option_text', preg_replace("/_\d*$/", "$1", $row['text']))
335
				// ->setParameter('vote_type', $row['type'])
336
				->setParameter('vote_answer', $this->translateVoteTypeToAnswer($row['type']));
337
			$insert->execute();
338
		}
339
		$result->closeCursor();
340
	}
341
	/**
342
	 * @param int $voteType
343
	 * @return string
344
	 */
345
346
	 
347
	 
348
349
	protected function findOptionId($pollId, $text) {
350
		$queryFind = $this->connection->getQueryBuilder();
351
		$queryFind->select(['id'])
352
			->from('polls_options')
353
			// ->where($queryFind->expr()->eq('poll_id', $pollId))
354
			// ->andWhere($queryFind->expr()->eq('poll_option', $text));
355
			->where('poll_id = "' . $pollId . '"')
356
			->andWhere('poll_option_text ="' . $text . '"');
357
358
		$resultFind = $queryFind->execute();
359
		$row = $resultFind->fetch();
360
		return $row['id'];
361
362
	}
363
	
364
	protected function translateVoteTypeToAnswer($voteType) {
365
		switch ($voteType) {
366
			case 0:
367
				$answer = "no";
368
				break;
369
			case 1:
370
				$answer = "yes";
371
				break;
372
			case 2:
373
				$answer = "maybe";
374
				break;
375
			default:
376
				$answer = "no";
377
		}
378
		return $answer;
379
	}
380
}
381