Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OptionService.php « Service « lib - github.com/nextcloud/polls.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9db06f2cc3df462586adf647c9b5745dbe3a8637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<?php
/**
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
 *
 * @author René Gieling <github@dartcafe.de>
 *
 * @license GNU AGPL version 3 or any later version
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OCA\Polls\Service;

use DateTime;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Psr\Log\LoggerInterface;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\Exception;
use OCP\EventDispatcher\IEventDispatcher;

use OCA\Polls\Exceptions\DuplicateEntryException;
use OCA\Polls\Exceptions\InvalidPollTypeException;
use OCA\Polls\Exceptions\InvalidOptionPropertyException;

use OCA\Polls\Db\OptionMapper;
use OCA\Polls\Db\VoteMapper;
use OCA\Polls\Db\Vote;
use OCA\Polls\Db\Option;
use OCA\Polls\Db\Poll;
use OCA\Polls\Event\OptionEvent;
use OCA\Polls\Event\OptionConfirmedEvent;
use OCA\Polls\Event\OptionCreatedEvent;
use OCA\Polls\Event\OptionDeletedEvent;
use OCA\Polls\Model\Acl;

class OptionService {

	/** @var IEventDispatcher */
	private $eventDispatcher;

	/** @var LoggerInterface */
	private $logger;

	/** @var string */
	private $appName;

	/** @var Acl */
	private $acl;

	/** @var Option */
	private $option;

	/** @var Poll */
	private $poll;

	/** @var Option[] */
	private $options;

	/** @var Vote[] */
	private $votes;

	/** @var OptionMapper */
	private $optionMapper;

	/** @var VoteMapper */
	private $voteMapper;

	public function __construct(
		string $AppName,
		Acl $acl,
		IEventDispatcher $eventDispatcher,
		LoggerInterface $logger,
		Option $option,
		OptionMapper $optionMapper,
		VoteMapper $voteMapper
	) {
		$this->appName = $AppName;
		$this->acl = $acl;
		$this->eventDispatcher = $eventDispatcher;
		$this->logger = $logger;
		$this->option = $option;
		$this->optionMapper = $optionMapper;
		$this->voteMapper = $voteMapper;
	}

	/**
	 * Get all options of given poll
	 *
	 * @return Option[]
	 *
	 * @psalm-return array<array-key, Option>
	 */
	public function list(int $pollId = 0, string $token = ''): array {
		if ($token) {
			$this->acl->setToken($token);
		} else {
			$this->acl->setPollId($pollId);
		}

		try {
			$this->options = $this->optionMapper->findByPoll($this->acl->getPollId());
			$this->votes = $this->voteMapper->findByPoll($this->acl->getPollId());

			$this->calculateVotes();

			if ($this->acl->getPoll()->getHideBookedUp() && !$this->acl->getIsAllowed(Acl::PERMISSION_POLL_EDIT)) {
				// hide booked up options except the user has edit permission
				$this->filterBookedUp();
			} elseif ($this->acl->getIsAllowed(Acl::PERMISSION_POLL_RESULTS_VIEW)) {
				$this->calculateRanks();
			}

			return array_values($this->options);
		} catch (DoesNotExistException $e) {
			return [];
		}
	}

	/**
	 * Get option
	 *
	 * @return Option
	 */
	public function get(int $optionId): Option {
		$option = $this->optionMapper->find($optionId);
		$this->acl->setPollId($option->getPollId());
		return $option;
	}


	/**
	 * Add a new option
	 *
	 * @return Option
	 */
	public function add(int $pollId, int $timestamp = 0, string $pollOptionText = '', ?int $duration = 0, string $token = ''): Option {
		if ($token) {
			$this->acl->setToken($token, Acl::PERMISSION_OPTIONS_ADD);
		} else {
			$this->acl->setPollId($pollId, Acl::PERMISSION_OPTIONS_ADD);
		}

		$this->option = new Option();
		$this->option->setPollId($this->acl->getPollId());
		$this->option->setOrder($this->getHighestOrder($this->acl->getPollId()) + 1);
		$this->setOption($timestamp, $pollOptionText, $duration);

		if (!$this->acl->getIsOwner()) {
			$this->option->setOwner($this->acl->getUserId());
		}

		try {
			$this->option = $this->optionMapper->insert($this->option);
		} catch (UniqueConstraintViolationException $e) {
			// deprecated NC22
			throw new DuplicateEntryException('This option already exists');
		} catch (Exception $e) {
			if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
				throw new DuplicateEntryException('This option already exists');
			}
			throw $e;
		}

		$this->eventDispatcher->dispatchTyped(new OptionCreatedEvent($this->option));

		return $this->option;
	}

	/**
	 * Update option
	 *
	 * @return Option
	 */
	public function update(int $optionId, int $timestamp = 0, ?string $pollOptionText = '', ?int $duration = 0): Option {
		$this->option = $this->optionMapper->find($optionId);
		$this->acl->setPollId($this->option->getPollId(), Acl::PERMISSION_POLL_EDIT);

		$this->setOption($timestamp, $pollOptionText, $duration);

		$this->option = $this->optionMapper->update($this->option);
		$this->eventDispatcher->dispatchTyped(new OptionEvent($this->option));

		return $this->option;
	}

	/**
	 * Delete option
	 *
	 * @return Option
	 */
	public function delete(int $optionId, string $token = ''): Option {
		$this->option = $this->optionMapper->find($optionId);

		if ($token) {
			$this->acl->setToken($token,Acl::PERMISSION_POLL_VIEW, $this->option->getPollId());
		} else {
			$this->acl->setPollId($this->option->getPollId());
		}

		if ($this->option->getOwner() !== $this->acl->getUserId()) {
			$this->acl->request(Acl::PERMISSION_POLL_EDIT);
		}

		$this->optionMapper->delete($this->option);
		$this->eventDispatcher->dispatchTyped(new OptionDeletedEvent($this->option));

		return $this->option;
	}

	/**
	 * Switch option confirmation
	 *
	 * @return Option
	 */
	public function confirm(int $optionId): Option {
		$this->option = $this->optionMapper->find($optionId);
		$this->acl->setPollId($this->option->getPollId(), Acl::PERMISSION_POLL_EDIT);

		$this->option->setConfirmed($this->option->getConfirmed() ? 0 : time());
		$this->option = $this->optionMapper->update($this->option);

		$this->eventDispatcher->dispatchTyped(new OptionConfirmedEvent($this->option));

		return $this->option;
	}

	/**
	 * Make a sequence of date poll options
	 * @param int $optionId
	 * @param int $step - The step for creating the sequence
	 * @param string $unit - The timeunit (year, month, ...)
	 * @param int $amount - Number of sequence items to create
	 *
	 * @return Option[]
	 *
	 * @psalm-return array<array-key, Option>
	 */
	public function sequence(int $optionId, int $step, string $unit, int $amount): array {
		$this->option = $this->optionMapper->find($optionId);
		$this->acl->setPollId($this->option->getPollId(), Acl::PERMISSION_POLL_EDIT);

		if ($this->acl->getPoll()->getType() !== Poll::TYPE_DATE) {
			throw new InvalidPollTypeException('Sequences are only available in date polls');
		}

		if ($step === 0) {
			return $this->optionMapper->findByPoll($this->acl->getPollId());
		}

		$baseDate = new DateTime;
		$baseDate->setTimestamp($this->option->getTimestamp());

		for ($i = 0; $i < $amount; $i++) {
			$clonedOption = new Option();
			$clonedOption->setPollId($this->acl->getPollId());
			$clonedOption->setDuration($this->option->getDuration());
			$clonedOption->setConfirmed(0);
			$clonedOption->setTimestamp($baseDate->modify($step . ' ' . $unit)->getTimestamp());
			$clonedOption->setOrder($clonedOption->getTimestamp());
			$clonedOption->setPollOptionText($baseDate->format('c'));

			try {
				$this->optionMapper->insert($clonedOption);
			} catch (UniqueConstraintViolationException $e) {
				// deprecated NC22
				$this->logger->warning('skip adding ' . $baseDate->format('c') . 'for pollId' . $this->option->getPollId() . '. Option already exists.');
			} catch (Exception $e) {
				if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
					$this->logger->warning('skip adding ' . $baseDate->format('c') . 'for pollId' . $this->option->getPollId() . '. Option already exists.');
				}
				throw $e;
			}
		}

		$this->eventDispatcher->dispatchTyped(new OptionCreatedEvent($this->option));

		return $this->optionMapper->findByPoll($this->acl->getPollId());
	}

	/**
	 * Shift all date options
	 * @param int $pollId
	 * @param int $step - The step for creating the sequence
	 * @param string $unit - The timeunit (year, month, ...)
	 *
	 * @return Option[]
	 *
	 * @psalm-return array<array-key, Option>
	 */
	public function shift(int $pollId, int $step, string $unit): array {
		$this->acl->setPollId($pollId, Acl::PERMISSION_POLL_EDIT);

		if ($this->acl->getPoll()->getType() !== Poll::TYPE_DATE) {
			throw new InvalidPollTypeException('Shifting is only available in date polls');
		}

		$this->options = $this->optionMapper->findByPoll($this->acl->getPollId());

		if ($step > 0) {
			// avoid UniqueConstraintViolationException
			// start from last item
			$this->options = array_reverse($this->options);
		}

		$shiftedDate = new DateTime;
		foreach ($this->options as $option) {
			$shiftedDate->setTimestamp($option->getTimestamp());
			$option->setTimestamp($shiftedDate->modify($step . ' ' . $unit)->getTimestamp());
			$this->optionMapper->update($option);
		}

		return $this->optionMapper->findByPoll($this->acl->getPollId());
	}

	/**
	 * Copy options from $fromPoll to $toPoll
	 *
	 * @return Option[]
	 *
	 * @psalm-return array<array-key, Option>
	 */
	public function clone(int $fromPollId, int $toPollId): array {
		$this->acl->setPollId($fromPollId);

		foreach ($this->optionMapper->findByPoll($fromPollId) as $origin) {
			$option = new Option();
			$option->setPollId($toPollId);
			$option->setConfirmed(0);
			$option->setPollOptionText($origin->getPollOptionText());
			$option->setTimestamp($origin->getTimestamp());
			$option->setDuration($origin->getDuration());
			$option->setOrder($origin->getOrder());
			$this->optionMapper->insert($option);
		}

		return $this->optionMapper->findByPoll($toPollId);
	}

	/**
	 * Reorder options with the order specified by $options
	 *
	 * @return Option[]
	 *
	 * @psalm-return array<array-key, Option>
	 */
	public function reorder(int $pollId, array $options): array {
		$this->acl->setPollId($pollId, Acl::PERMISSION_POLL_EDIT);

		if ($this->acl->getPoll()->getType() === Poll::TYPE_DATE) {
			throw new InvalidPollTypeException('Not allowed in date polls');
		}

		$i = 0;
		foreach ($options as $option) {
			$this->option = $this->optionMapper->find($option['id']);
			if ($this->acl->getPollId() === intval($this->option->getPollId())) {
				$this->option->setOrder(++$i);
				$this->optionMapper->update($this->option);
			}
		}

		$this->eventDispatcher->dispatchTyped(new OptionEvent($this->option));

		return $this->optionMapper->findByPoll($this->acl->getPollId());
	}

	/**
	 * Change order for $optionId and reorder the options
	 *
	 * @return Option[]
	 *
	 * @psalm-return array<array-key, Option>
	 */
	public function setOrder(int $optionId, int $newOrder): array {
		$this->option = $this->optionMapper->find($optionId);
		$this->acl->setPollId($this->option->getPollId(), Acl::PERMISSION_POLL_EDIT);

		if ($this->acl->getPoll()->getType() === Poll::TYPE_DATE) {
			throw new InvalidPollTypeException('Not allowed in date polls');
		}

		if ($newOrder < 1) {
			$newOrder = 1;
		} elseif ($newOrder > $this->getHighestOrder($this->acl->getPollId())) {
			$newOrder = $this->getHighestOrder($this->acl->getPollId());
		}

		foreach ($this->optionMapper->findByPoll($this->acl->getPollId()) as $option) {
			$option->setOrder($this->moveModifier($this->option->getOrder(), $newOrder, $option->getOrder()));
			$this->optionMapper->update($option);
		}

		$this->eventDispatcher->dispatchTyped(new OptionEvent($this->option));

		return $this->optionMapper->findByPoll($this->acl->getPollId());
	}

	/**
	 * moveModifier - evaluate new order depending on the old and
	 * the new position of a moved array item
	 * @param int $moveFrom - old position of the moved item
	 * @param int $moveTo - target posotion of the moved item
	 * @param int $currentPosition - current position of the current item
	 *
	 * @return int - The modified new new position of the current item
	 */
	private function moveModifier(int $moveFrom, int $moveTo, int $currentPosition): int {
		$moveModifier = 0;
		if ($moveFrom < $currentPosition && $currentPosition <= $moveTo) {
			// moving forward
			$moveModifier = -1;
		} elseif ($moveTo <= $currentPosition && $currentPosition < $moveFrom) {
			//moving backwards
			$moveModifier = 1;
		} elseif ($moveFrom === $currentPosition) {
			return $moveTo;
		}
		return $currentPosition + $moveModifier;
	}

	/**
	 * Set option entities validated
	 *
	 * @return void
	 */
	private function setOption(int $timestamp = 0, ?string $pollOptionText = '', ?int $duration = 0): void {
		if ($timestamp) {
			$this->option->setTimestamp($timestamp);
			$this->option->setOrder($timestamp);
			$this->option->setDuration($duration ?? 0);
			if ($duration > 0) {
				$this->option->setPollOptionText(date('c', $timestamp) . ' - ' . date('c', $timestamp + $duration));
			} else {
				$this->option->setPollOptionText(date('c', $timestamp));
			}
		} elseif ($pollOptionText) {
			$this->option->setPollOptionText($pollOptionText);
		} else {
			throw new InvalidOptionPropertyException('Option must have a value');
		}
	}

	/**
	 * Get all voteOptionTexts of the options, the user opted in
	 * with yes or maybe
	 *
	 * @return array
	 */
	private function getUsersVotes() {
		$userVotes = [];
		foreach ($this->votes as $vote) {
			if ($vote->getUserId() === $this->acl->getUserId() && in_array($vote->getVoteAnswer(), ['yes', 'maybe'])) {
				$userVotes[] = $vote->getVoteOptionText();
			}
		}
		return $userVotes;
	}

	/**
	 * Remove booked up options, because they are not votable
	 *
	 * @return void
	 */
	private function filterBookedUp() {
		$exceptVotes = $this->getUsersVotes();
		$this->options = array_filter($this->options, function ($option) use ($exceptVotes) {
			return (!$option->isBookedUp || in_array($option->getPollOptionText(), $exceptVotes));
		});
	}

	/**
	 * Calculate the votes of each option and determines if the option is booked up
	 * - unvoted counts as no
	 * - realNo reports the actually opted out votes
	 *
	 * @return void
	 */
	private function calculateVotes() {
		foreach ($this->options as $option) {
			$option->yes = count(
				array_filter($this->votes, function ($vote) use ($option) {
					return ($vote->getVoteOptionText() === $option->getPollOptionText()
						&& $vote->getVoteAnswer() === 'yes') ;
				})
			);

			$option->realNo = count(
				array_filter($this->votes, function ($vote) use ($option) {
					return ($vote->getVoteOptionText() === $option->getPollOptionText()
						&& $vote->getVoteAnswer() === 'no');
				})
			);

			$option->maybe = count(
				array_filter($this->votes, function ($vote) use ($option) {
					return ($vote->getVoteOptionText() === $option->getPollOptionText()
						&& $vote->getVoteAnswer() === 'maybe');
				})
			);

			$option->isBookedUp = $this->acl->getPoll()->getOptionLimit() ? $this->acl->getPoll()->getOptionLimit() <= $option->yes : false;

			// remove details, if the results shall be hidden
			if (!$this->acl->getIsAllowed(Acl::PERMISSION_POLL_RESULTS_VIEW)) {
				$option->yes = 0;
				$option->no = 0;
				$option->maybe = 0;
				$option->realNo = 0;
			} else {
				$option->no = count($this->voteMapper->findParticipantsByPoll($this->acl->getPollId())) - $option->maybe - $option->yes;
				$option->no = $this->countParticipants() - $option->maybe - $option->yes;
			}
		}
	}

	private function countParticipants(): int {
		return count($this->voteMapper->findParticipantsByPoll($this->acl->getPollId()));
	}

	/**
	 * Calculate the rank of each option based on the
	 * yes and maybe votes and recognize equal ranked options
	 *
	 * @return void
	 */
	private function calculateRanks() {
		// sort array by yes and maybe votes
		usort($this->options, function (Option $a, Option $b):int {
			$diff = $b->yes - $a->yes;
			return ($diff !== 0) ? $diff : $b->maybe - $a->maybe;
		});

		// calculate the rank
		$count = count($this->options);
		for ($i = 0; $i < $count; $i++) {
			if ($i > 0 && $this->options[$i]->yes === $this->options[$i - 1]->yes && $this->options[$i]->maybe === $this->options[$i - 1]->maybe) {
				$this->options[$i]->rank = $this->options[$i - 1]->rank;
			} else {
				$this->options[$i]->rank = $i + 1;
			}
		}

		// restore original order
		usort($this->options, function (Option $a, Option $b):int {
			return $a->getOrder() - $b->getOrder();
		});
	}

	/**
	 * Get the highest order number in $pollId
	 * Return Highest order number
	 *
	 * @return int
	 */
	private function getHighestOrder(int $pollId): int {
		$highestOrder = 0;
		foreach ($this->optionMapper->findByPoll($pollId) as $option) {
			$highestOrder = ($option->getOrder() > $highestOrder) ? $option->getOrder() : $highestOrder;
		}
		return $highestOrder;
	}
}