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

Poll.php « Db « lib - github.com/nextcloud/polls.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b60161ccfe6b052edb61434e96e7a42ee5537c9 (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
<?php
/**
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
 *
 * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
 * @author Kai Schröer <git@schroeer.co>
 * @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\Db;

use JsonSerializable;
use OCA\Polls\Exceptions\NoDeadLineException;
use OCA\Polls\Helper\Container;
use OCP\IURLGenerator;

/**
 * @method string getType()
 * @method void setType(string $value)
 * @method string getTitle()
 * @method void setTitle(string $value)
 * @method string getDescription()
 * @method void setDescription(string $value)
 * @method string getOwner()
 * @method void setOwner(string $value)
 * @method int getCreated()
 * @method void setCreated(integer $value)
 * @method int getExpire()
 * @method void setExpire(integer $value)
 * @method int getDeleted()
 * @method void setDeleted(integer $value)
 * @method string getAccess()
 * @method void setAccess(string $value)
 * @method int getAnonymous()
 * @method void setAnonymous(integer $value)
 * @method int getallowComment()
 * @method void setallowComment(integer $value)
 * @method int getAllowMaybe()
 * @method void setAllowMaybe(integer $value)
 * @method string getAllowProposals()
 * @method void setAllowProposals(string $value)
 * @method int getProposalsExpire()
 * @method void setProposalsExpire(integer $value)
 * @method int getVoteLimit()
 * @method void setVoteLimit(integer $value)
 * @method int getOptionLimit()
 * @method void setOptionLimit(integer $value)
 * @method string getShowResults()
 * @method void setShowResults(string $value)
 * @method int getAdminAccess()
 * @method void setAdminAccess(integer $value)
 * @method int getImportant()
 * @method void setImportant(integer $value)
 * @method int getHideBookedUp()
 * @method void setHideBookedUp(integer $value)
 * @method int getUseNo()
 * @method void setUseNo(integer $value)
 * @method string getMiscSettings()
 * @method void setMiscSettings(string $value)
 */

class Poll extends EntityWithUser implements JsonSerializable {
	public const TABLE = 'polls_polls';
	public const TYPE_DATE = 'datePoll';
	public const TYPE_TEXT = 'textPoll';
	public const ACCESS_HIDDEN = 'hidden';
	public const ACCESS_PUBLIC = 'public';
	public const ACCESS_PRIVATE = 'private';
	public const ACCESS_OPEN = 'open';
	public const SHOW_RESULTS_ALWAYS = 'always';
	public const SHOW_RESULTS_CLOSED = 'closed';
	public const SHOW_RESULTS_NEVER = 'never';
	public const PROPOSAL_DISALLOW = 'disallow';
	public const PROPOSAL_ALLOW = 'allow';
	public const PROPOSAL_REVIEW = 'review';
	public const URI_PREFIX = 'poll/';
	public const FIVE_DAYS = 432000;
	public const FOUR_DAYS = 345600;
	public const THREE_DAYS = 259200;
	public const TWO_DAYS = 172800;
	public const ONE_AND_HALF_DAY = 129600;

	/** @var string $type */
	protected $type;

	/** @var string $title */
	protected $title;

	/** @var string $description */
	protected $description;

	/** @var string $owner */
	protected $owner;

	/** @var int $created */
	protected $created;

	/** @var int $expire */
	protected $expire;

	/** @var int $deleted */
	protected $deleted;

	/** @var string $access */
	protected $access;

	/** @var int $anonymous */
	protected $anonymous;

	/** @var int $allowMaybe */
	protected $allowMaybe;

	/** @var string $allowProposals */
	protected $allowProposals;

	/** @var string $proposalsExpire */
	protected $proposalsExpire;

	/** @var int $voteLimit*/
	protected $voteLimit;

	/** @var int $optionLimit*/
	protected $optionLimit;

	/** @var string $showResults */
	protected $showResults;

	/** @var int $adminAccess*/
	protected $adminAccess;

	/** @var int $important*/
	protected $important;

	/** @var int $allowComment*/
	protected $allowComment;

	/** @var int $hideBookedUp*/
	protected $hideBookedUp;

	/** @var int $useNo*/
	protected $useNo;

	/** @var string $miscSettings*/
	protected $miscSettings;

	/** @var IURLGenerator */
	private $urlGenerator;

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

	public function __construct() {
		$this->addType('created', 'int');
		$this->addType('expire', 'int');
		$this->addType('deleted', 'int');
		$this->addType('anonymous', 'int');
		$this->addType('allowComment', 'int');
		$this->addType('allowMaybe', 'int');
		$this->addType('proposalsExpire', 'int');
		$this->addType('voteLimit', 'int');
		$this->addType('optionLimit', 'int');
		$this->addType('adminAccess', 'int');
		$this->addType('important', 'int');
		$this->addType('hideBookedUp', 'int');
		$this->addType('useNo', 'int');
		$this->optionMapper = Container::queryClass(OptionMapper::class);
		$this->urlGenerator = Container::queryClass(IURLGenerator::class);
	}

	/**
	 * @return array
	 */
	public function jsonSerialize(): array {
		return [
			'id' => $this->getId(),
			'type' => $this->getType(),
			'title' => $this->getTitle(),
			'description' => $this->getDescription(),
			'descriptionSafe' => $this->getDescriptionSafe(),
			'created' => $this->getCreated(),
			'expire' => $this->getExpire(),
			'deleted' => $this->getDeleted(),
			'access' => $this->getAccess(),
			'anonymous' => $this->getAnonymous(),
			'allowComment' => $this->getAllowComment(),
			'allowMaybe' => $this->getAllowMaybe(),
			'allowProposals' => $this->getAllowProposals(),
			'proposalsExpire' => $this->getProposalsExpire(),
			'voteLimit' => $this->getVoteLimit(),
			'optionLimit' => $this->getOptionLimit(),
			'showResults' => $this->getShowResults() === 'expired' ? Poll::SHOW_RESULTS_CLOSED : $this->getShowResults(),
			'adminAccess' => $this->getAdminAccess(),
			'important' => $this->getImportant(),
			'hideBookedUp' => $this->getHideBookedUp(),
			'useNo' => $this->getUseNo(),
			'autoReminder' => $this->getAutoReminder(),
			'owner' => $this->getUser(),
		];
	}

	/**
	 * @return static
	 */
	public function deserializeArray(array $array): self {
		$this->setTitle($array['title'] ?? $this->getTitle());
		$this->setDescription($array['description'] ?? $this->getDescription());
		$this->setAccess($array['access'] ?? $this->getAccess());
		$this->setExpire($array['expire'] ?? $this->getExpire());
		$this->setAnonymous($array['anonymous'] ?? $this->getAnonymous());
		$this->setallowComment($array['allowComment'] ?? $this->getallowComment());
		$this->setAllowMaybe($array['allowMaybe'] ?? $this->getAllowMaybe());
		$this->setAllowProposals($array['allowProposals'] ?? $this->getAllowProposals());
		$this->setProposalsExpire($array['proposalsExpire'] ?? $this->getProposalsExpire());
		$this->setVoteLimit($array['voteLimit'] ?? $this->getVoteLimit());
		$this->setOptionLimit($array['optionLimit'] ?? $this->getOptionLimit());
		$this->setShowResults($array['showResults'] ?? $this->getShowResults());
		$this->setDeleted($array['deleted'] ?? $this->getDeleted());
		$this->setAdminAccess($array['adminAccess'] ?? $this->getAdminAccess());
		$this->setImportant($array['important'] ?? $this->getImportant());
		$this->setHideBookedUp($array['hideBookedUp'] ?? $this->getHideBookedUp());
		$this->setUseNo($array['useNo'] ?? $this->getUseNo());
		$this->setAutoReminder($array['autoReminder'] ?? $this->getAutoReminder());
		return $this;
	}

	public function getExpired(): bool {
		return (
			$this->getExpire() > 0
			&& $this->getExpire() < time()
		);
	}

	public function getUri(): string {
		return self::URI_PREFIX . $this->getId();
	}

	public function getVoteUrl() : string {
		return $this->urlGenerator->linkToRouteAbsolute(
			'polls.page.vote',
			['id' => $this->getId()]
		);
	}

	/**
	 * Keep for compatibilty reasons
	 * TODO: remove this later
	 * OCA\Polls\Db\Share::getDefaultPublicPollEmail() depends on this
	 * @deprecated
	 */

	public function getPublicPollEmail(): string {
		return $this->getMiscSettingsArray()['publicPollEmail'] ?? 'optional';
	}

	public function setAutoReminder(bool $value) : void {
		$this->setMiscSettingsByKey('autoReminder', $value);
	}

	public function getAutoReminder(): bool {
		return $this->getMiscSettingsArray()['autoReminder'] ?? false;
	}

	// alias of getId()
	public function getPollId(): int {
		return $this->getId();
	}

	// alias of getOwner()
	public function getUserId() : string {
		return $this->getOwner();
	}

	// alias of setOwner($value)
	public function setUserId(string $userId) : void {
		$this->setOwner($userId);
	}

	public function getAccess() {
		if ($this->access === self::ACCESS_PUBLIC) {
			return self::ACCESS_OPEN;
		}
		if ($this->access === self::ACCESS_HIDDEN) {
			return self::ACCESS_PRIVATE;
		}
		return $this->access;
	}

	public function getProposalsExpired(): bool {
		return (
			$this->getProposalsExpire() > 0
			&& $this->getProposalsExpire() < time()
		);
	}

	public function getDescriptionSafe(): string {
		return htmlspecialchars($this->description);
	}

	private function setMiscSettingsArray(array $value) : void {
		$this->setMiscSettings(json_encode($value));
	}

	private function getMiscSettingsArray() : array {
		if ($this->getMiscSettings()) {
			return json_decode($this->getMiscSettings(), true);
		}
		
		return [];
	}

	public function getTimeToDeadline(int $time = 0): ?int {
		if ($time === 0) {
			$time = time();
		}
		$deadline = $this->getDeadline();
		if (
			$deadline - $this->getCreated() > self::FIVE_DAYS
			&& $deadline - $time < self::TWO_DAYS
			&& $deadline > $time
		) {
			return self::TWO_DAYS;
		}

		if (
			$deadline - $this->getCreated() > self::TWO_DAYS
			&& $deadline - $time < self::ONE_AND_HALF_DAY
			&& $deadline > $time
		) {
			return self::ONE_AND_HALF_DAY;
		}
		throw new NoDeadLineException();
	}

	public function getDeadline(): ?int {
		if ($this->getExpire()) {
			return $this->getExpire();
		}

		if ($this->getType() === Poll::TYPE_DATE) {
			// use first date option as reminder deadline
			$options = $this->optionMapper->findByPoll($this->getId());
			return $options[0]->getTimestamp();
		}
		throw new NoDeadLineException();
	}

	/**
	 * @param bool|string|int|array $value
	 */
	private function setMiscSettingsByKey(string $key, $value): void {
		$miscSettings = $this->getMiscSettingsArray();
		$miscSettings[$key] = $value;
		$this->setMiscSettingsArray($miscSettings);
	}
}