Passed
Push — master ( 0259d4...eb77b7 )
by René
04:31 queued 10s
created

Poll::getProposalsExpired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author Vinzenz Rosenkranz <[email protected]>
6
 * @author Kai Schröer <[email protected]>
7
 * @author René Gieling <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 *  This program is free software: you can redistribute it and/or modify
12
 *  it under the terms of the GNU Affero General Public License as
13
 *  published by the Free Software Foundation, either version 3 of the
14
 *  License, or (at your option) any later version.
15
 *
16
 *  This program is distributed in the hope that it will be useful,
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *  GNU Affero General Public License for more details.
20
 *
21
 *  You should have received a copy of the GNU Affero General Public License
22
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OCA\Polls\Db;
27
28
use JsonSerializable;
29
30
use OCP\IUser;
31
use OCP\AppFramework\Db\Entity;
32
use OCA\Polls\Model\User;
33
34
/**
35
 * @method string getType()
36
 * @method void setType(string $value)
37
 * @method string getTitle()
38
 * @method void setTitle(string $value)
39
 * @method string getDescription()
40
 * @method void setDescription(string $value)
41
 * @method string getOwner()
42
 * @method void setOwner(string $value)
43
 * @method int getCreated()
44
 * @method void setCreated(integer $value)
45
 * @method int getExpire()
46
 * @method void setExpire(integer $value)
47
 * @method int getDeleted()
48
 * @method void setDeleted(integer $value)
49
 * @method string getAccess()
50
 * @method void setAccess(string $value)
51
 * @method int getAnonymous()
52
 * @method void setAnonymous(integer $value)
53
 * @method int getFullAnonymous()
54
 * @method void setFullAnonymous(integer $value)
55
 * @method int getallowComment()
56
 * @method void setallowComment(integer $value)
57
 * @method int getAllowMaybe()
58
 * @method void setAllowMaybe(integer $value)
59
 * @method string getAllowProposals()
60
 * @method void setAllowProposals(string $value)
61
 * @method int getProposalsExpire()
62
 * @method void setProposalsExpire(integer $value)
63
 * @method string getOptions()
64
 * @method void setOptions(string $value)
65
 * @method string getSettings()
66
 * @method void setSettings(string $value)
67
 * @method int getVoteLimit()
68
 * @method void setVoteLimit(integer $value)
69
 * @method int getOptionLimit()
70
 * @method void setOptionLimit(integer $value)
71
 * @method string getShowResults()
72
 * @method void setShowResults(string $value)
73
 * @method int getAdminAccess()
74
 * @method void setAdminAccess(integer $value)
75
 * @method int getImportant()
76
 * @method void setImportant(integer $value)
77
 * @method int getHideBookedUp()
78
 * @method void setHideBookedUp(integer $value)
79
 */
80
class Poll extends Entity implements JsonSerializable {
81
	public const TYPE_DATE = 'datePoll';
82
	public const TYPE_TEXT = 'textPoll';
83
	public const ACCESS_HIDDEN = 'hidden';
84
	public const ACCESS_PUBLIC = 'public';
85
	public const SHOW_RESULTS_ALWAYS = 'always';
86
	public const SHOW_RESULTS_CLOSED = 'closed';
87
	public const SHOW_RESULTS_NEVER = 'never';
88
	public const PROPOSAL_DISALLOW = 'disallow';
89
	public const PROPOSAL_ALLOW = 'allow';
90
	public const PROPOSAL_REVIEW = 'review';
91
92
	/** @var string $type */
93
	protected $type;
94
95
	/** @var string $title */
96
	protected $title;
97
98
	/** @var string $description */
99
	protected $description;
100
101
	/** @var string $owner */
102
	protected $owner;
103
104
	/** @var int $created */
105
	protected $created;
106
107
	/** @var int $expire */
108
	protected $expire;
109
110
	/** @var int $deleted */
111
	protected $deleted;
112
113
	/** @var string $access */
114
	protected $access;
115
116
	/** @var int $anonymous */
117
	protected $anonymous;
118
119
	/** @var int $fullAnonymous */
120
	protected $fullAnonymous;
121
122
	/** @var int $allowMaybe */
123
	protected $allowMaybe;
124
125
	/** @var string $allowProposals */
126
	protected $allowProposals;
127
128
	/** @var string $proposalsExpire */
129
	protected $proposalsExpire;
130
131
	/** @var string $options */
132
	protected $options;
133
134
	/** @var string $settings*/
135
	protected $settings;
136
137
	/** @var int $voteLimit*/
138
	protected $voteLimit;
139
140
	/** @var int $optionLimit*/
141
	protected $optionLimit;
142
143
	/** @var string $showResults */
144
	protected $showResults;
145
146
	/** @var int $adminAccess*/
147
	protected $adminAccess;
148
149
	/** @var int $important*/
150
	protected $important;
151
152
	/** @var int $allowComment*/
153
	protected $allowComment;
154
155
	/** @var int $hideBookedUp*/
156
	protected $hideBookedUp;
157
158
	public function __construct() {
159
		$this->addType('created', 'integer');
160
		$this->addType('expire', 'integer');
161
		$this->addType('deleted', 'integer');
162
		$this->addType('anonymous', 'integer');
163
		$this->addType('allowComment', 'integer');
164
		$this->addType('allowMaybe', 'integer');
165
		$this->addType('proposalsExpire', 'integer');
166
		$this->addType('voteLimit', 'integer');
167
		$this->addType('optionLimit', 'integer');
168
		$this->addType('adminAccess', 'integer');
169
		$this->addType('important', 'integer');
170
		$this->addType('hideBookedUp', 'integer');
171
	}
172
173
	public function jsonSerialize() {
174
		return [
175
			'id' => $this->id,
176
			'type' => $this->type,
177
			'title' => $this->title,
178
			'description' => $this->description,
179
			'descriptionSafe' => $this->getDescriptionSafe(),
180
			'owner' => $this->owner,
181
			'created' => $this->created,
182
			'expire' => $this->expire,
183
			'deleted' => $this->deleted,
184
			'access' => $this->access,
185
			'anonymous' => $this->anonymous,
186
			'allowComment' => $this->allowComment,
187
			'allowMaybe' => $this->allowMaybe,
188
			'allowProposals' => $this->allowProposals,
189
			'proposalsExpire' => $this->proposalsExpire,
190
			'settings' => $this->settings,
191
			'voteLimit' => $this->voteLimit,
192
			'optionLimit' => $this->optionLimit,
193
			'showResults' => $this->showResults === 'expired' ? Poll::SHOW_RESULTS_CLOSED : $this->showResults,
194
			'adminAccess' => $this->adminAccess,
195
			'ownerDisplayName' => $this->getDisplayName(),
196
			'important' => $this->important,
197
			'hideBookedUp' => $this->hideBookedUp
198
		];
199
	}
200
201
	/**
202
	 * @return static
203
	 */
204
	public function deserializeArray(array $array): self {
205
		$this->setTitle($array['title'] ?? $this->getTitle());
206
		$this->setDescription($array['description'] ?? $this->getDescription());
207
		$this->setAccess($array['access'] ?? $this->getAccess());
208
		$this->setExpire($array['expire'] ?? $this->getExpire());
209
		$this->setAnonymous($array['anonymous'] ?? $this->getAnonymous());
210
		$this->setallowComment($array['allowComment'] ?? $this->getallowComment());
211
		$this->setAllowMaybe($array['allowMaybe'] ?? $this->getAllowMaybe());
212
		$this->setAllowProposals($array['allowProposals'] ?? $this->getAllowProposals());
213
		$this->setProposalsExpire($array['proposalsExpire'] ?? $this->getProposalsExpire());
214
		$this->setVoteLimit($array['voteLimit'] ?? $this->getVoteLimit());
215
		$this->setOptionLimit($array['optionLimit'] ?? $this->getOptionLimit());
216
		$this->setShowResults($array['showResults'] ?? $this->getShowResults());
217
		$this->setDeleted($array['deleted'] ?? $this->getDeleted());
218
		$this->setAdminAccess($array['adminAccess'] ?? $this->getAdminAccess());
219
		$this->setImportant($array['important'] ?? $this->getImportant());
220
		$this->setHideBookedUp($array['hideBookedUp'] ?? $this->getHideBookedUp());
221
		return $this;
222
	}
223
224
	public function getExpired(): bool {
225
		return (
226
			   $this->getExpire() > 0
227
			&& $this->getExpire() < time()
228
		);
229
	}
230
231
	public function getProposalsExpired(): bool {
232
		return (
233
			   $this->getProposalsExpire() > 0
234
			&& $this->getProposalsExpire() < time()
235
		);
236
	}
237
238
	public function getDescriptionSafe() {
239
		return htmlspecialchars($this->description);
240
	}
241
242
	private function getDisplayName(): string {
243
		return \OC::$server->getUserManager()->get($this->owner) instanceof IUser
244
			? \OC::$server->getUserManager()->get($this->owner)->getDisplayName()
245
			: $this->owner;
246
	}
247
248
	public function getOwnerUserObject(): User {
249
		return new User($this->owner);
250
	}
251
}
252