|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[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
|
|
|
|
|
25
|
|
|
namespace OCA\Polls\Service; |
|
26
|
|
|
|
|
27
|
|
|
use OCA\Polls\Db\Vote; |
|
28
|
|
|
use OCA\Polls\Db\VoteMapper; |
|
29
|
|
|
use OCA\Polls\Db\Comment; |
|
30
|
|
|
use OCA\Polls\Db\CommentMapper; |
|
31
|
|
|
|
|
32
|
|
|
class AnonymizeService { |
|
33
|
|
|
|
|
34
|
|
|
/** @var VoteMapper */ |
|
35
|
|
|
private $voteMapper; |
|
36
|
|
|
|
|
37
|
|
|
/** @var CommentMapper */ |
|
38
|
|
|
private $commentMapper; |
|
39
|
|
|
|
|
40
|
|
|
/** @var array */ |
|
41
|
|
|
private $anonList = []; |
|
42
|
|
|
|
|
43
|
|
|
/** @var string */ |
|
44
|
|
|
private $userId; |
|
45
|
|
|
|
|
46
|
|
|
/** @var int */ |
|
47
|
|
|
private $pollId; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct( |
|
50
|
|
|
VoteMapper $voteMapper, |
|
51
|
|
|
CommentMapper $commentMapper |
|
52
|
|
|
) { |
|
53
|
|
|
$this->voteMapper = $voteMapper; |
|
54
|
|
|
$this->commentMapper = $commentMapper; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Anonymizes the participants of a poll |
|
59
|
|
|
* $array Input list which should be anonymized must be a collection of Vote or Comment |
|
60
|
|
|
* Returns the original array with anonymized user names |
|
61
|
|
|
*/ |
|
62
|
|
|
private function anonymize(array $array): array { |
|
63
|
|
|
// get mapping for the complete poll |
|
64
|
|
|
foreach ($array as &$element) { |
|
65
|
|
|
if ($element->getUserId() === $this->userId) { |
|
66
|
|
|
// skip current user |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
$element->setUserId($this->anonList[$element->getUserId()] ?? 'Unknown user'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $array; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Initialize anonymizer with pollId and userId |
|
77
|
|
|
* Creates a mapping list with unique Anonymous strings based on the partcipants of a poll |
|
78
|
|
|
* $userId - usernames, which will not be anonymized |
|
79
|
|
|
*/ |
|
80
|
|
|
public function set(int $pollId, string $userId): void { |
|
81
|
|
|
$this->pollId = $pollId; |
|
82
|
|
|
$this->userId = $userId; |
|
83
|
|
|
$votes = $this->voteMapper->findByPoll($pollId); |
|
84
|
|
|
$comments = $this->commentMapper->findByPoll($pollId); |
|
85
|
|
|
$i = 0; |
|
86
|
|
|
|
|
87
|
|
|
foreach ($votes as $element) { |
|
88
|
|
|
if (!array_key_exists($element->getUserId(), $this->anonList) && $element->getUserId() !== $userId) { |
|
89
|
|
|
$this->anonList[$element->getUserId()] = 'Anonymous ' . ++$i; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
foreach ($comments as $element) { |
|
94
|
|
|
if (!array_key_exists($element->getUserId(), $this->anonList) && $element->getUserId() !== $userId) { |
|
95
|
|
|
$this->anonList[$element->getUserId()] = 'Anonymous ' . ++$i; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
return; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Anonymizes the comments of a poll |
|
103
|
|
|
* Returns anonymized comments |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getComments(): array { |
|
106
|
|
|
return $this->anonymize($this->commentMapper->findByPoll($this->pollId)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Anonymizes the participants of a poll |
|
111
|
|
|
* Returns anonymized votes |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getVotes(): array { |
|
114
|
|
|
return $this->anonymize($this->voteMapper->findByPoll($this->pollId)); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|