1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicklas\Comment\Modules; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A database driven model. |
7
|
|
|
*/ |
8
|
|
|
class User extends ActiveRecordModelExtender |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string $tableName name of the database table. |
13
|
|
|
*/ |
14
|
|
|
protected $tableName = "coffee_users"; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Columns in the table. |
18
|
|
|
* |
19
|
|
|
* @var integer $id primary key auto incremented. |
20
|
|
|
*/ |
21
|
|
|
public $id; |
22
|
|
|
public $name; |
23
|
|
|
public $email; |
24
|
|
|
public $pass; |
25
|
|
|
public $authority = "user"; |
26
|
|
|
public $question; |
27
|
|
|
|
28
|
7 |
|
public function getReputation($user) |
29
|
|
|
{ |
30
|
|
|
// Get all the vote->scores connected to users posts |
31
|
|
|
$questionPoints = array_reduce($user->questions, function ($carry, $item) { |
32
|
5 |
|
$carry += $item->question->vote->score; |
33
|
5 |
|
return $carry; |
34
|
7 |
|
}); |
35
|
|
|
|
36
|
|
|
$postPoints = array_reduce($user->posts, function ($carry, $item) { |
37
|
5 |
|
$carry += $item->vote->score; |
38
|
5 |
|
return $carry; |
39
|
7 |
|
}); |
40
|
|
|
|
41
|
|
|
$commentPoints = array_reduce($user->comments, function ($carry, $item) { |
42
|
5 |
|
$carry += $item->vote->score; |
43
|
5 |
|
return $carry; |
44
|
7 |
|
}); |
45
|
|
|
|
46
|
|
|
$points = |
47
|
7 |
|
($questionPoints * 2) + |
48
|
7 |
|
($postPoints * 3) + |
49
|
7 |
|
($commentPoints * 1); |
50
|
|
|
|
51
|
|
|
// Algorithm for reputation points |
52
|
|
|
$reputation = |
53
|
7 |
|
(count($user->questions) * 2) + |
54
|
7 |
|
(count($user->posts) * 3) + |
55
|
7 |
|
(count($user->comments) * 1) + $points; |
56
|
|
|
|
57
|
7 |
|
return $reputation; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Sets up user |
63
|
|
|
* @param object $user |
64
|
|
|
* |
65
|
|
|
* @return object |
66
|
|
|
*/ |
67
|
7 |
|
public function setupUser($user) |
68
|
|
|
{ |
69
|
7 |
|
$question = new Question($this->db); |
70
|
7 |
|
$post = new Post($this->db); |
71
|
7 |
|
$comment = new Comment($this->db); |
72
|
7 |
|
$vote = new Vote($this->db); |
73
|
|
|
|
74
|
|
|
// Basic setup for user |
75
|
7 |
|
$user->img = $this->getGravatar($user->name); |
76
|
|
|
|
77
|
|
|
// Get all posts/votes user made |
78
|
7 |
|
$sqlAccept = "user = ? AND type = ? AND accepted = ?"; |
79
|
7 |
|
$user->acceptedAnswers = count($post->getAllPosts($sqlAccept, [$user->name, "answer", "yes"])); |
80
|
7 |
|
$user->questions = $question->getQuestions("user = ?", $user->name); |
81
|
7 |
|
$user->comments = $comment->getComments("user = ?", $user->name); |
82
|
7 |
|
$user->posts = $post->getAllPosts("user = ? AND type = ?", [$user->name, "answer"]); |
83
|
7 |
|
$user->votes = $vote->getVote("user = ?", [$user->name]); |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
// Amount of posts |
87
|
7 |
|
$user->postAmount = count($user->questions) + count($user->posts) + count($user->comments); |
88
|
|
|
|
89
|
|
|
// Algorithm for reputation points |
90
|
7 |
|
$user->reputation = $this->getReputation($user); |
91
|
|
|
|
92
|
|
|
// Get all the questions connected to answers |
93
|
7 |
|
$user->answeredQuestions = array_map(function ($answer) { |
94
|
5 |
|
$question = new Question($this->db); |
95
|
5 |
|
return $question->getQuestion($answer->questionId); |
96
|
7 |
|
}, $user->posts); |
97
|
|
|
|
98
|
7 |
|
return $user; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns post with markdown and gravatar |
103
|
|
|
* @param string $sql |
104
|
|
|
* @param array $param |
|
|
|
|
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
3 |
View Code Duplication |
public function getAllUsers($sql = null, $params = null) |
|
|
|
|
109
|
|
|
{ |
110
|
3 |
|
if ($sql == null) { |
|
|
|
|
111
|
3 |
|
$users = $this->findAll(); |
112
|
3 |
|
} |
113
|
3 |
|
if ($sql != null) { |
|
|
|
|
114
|
1 |
|
$users = $this->findAllWhere($sql, $params); |
115
|
1 |
|
} |
116
|
|
|
|
117
|
3 |
|
return array_map(array($this, 'setupUser'), $users); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* return question/answer, three attributes are set, comments connected to them is an array. |
122
|
|
|
* @param string $name |
123
|
|
|
* |
124
|
|
|
* @return object |
125
|
|
|
*/ |
126
|
4 |
|
public function getUser($name) |
127
|
|
|
{ |
128
|
4 |
|
$user = $this->find("name", $name); |
129
|
4 |
|
$user = $this->setupUser($user); |
|
|
|
|
130
|
4 |
|
return $user; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Check if user exists |
136
|
|
|
* |
137
|
|
|
* @param string $name |
138
|
|
|
* |
139
|
|
|
* @return boolean true if user exists in database else false |
140
|
|
|
*/ |
141
|
1 |
|
public function userExists($name) |
142
|
|
|
{ |
143
|
1 |
|
$user = $this->find("name", $name); |
144
|
1 |
|
return !$user ? false : true; |
145
|
|
|
} |
146
|
|
|
/** |
147
|
|
|
* Returns gravatar link |
148
|
|
|
* |
149
|
|
|
* @param string $email |
|
|
|
|
150
|
|
|
* |
151
|
|
|
* @return string as gravatar link |
152
|
|
|
*/ |
153
|
1 |
|
public function setGravatar() |
154
|
|
|
{ |
155
|
1 |
|
$this->img = $this->gravatar($this->email); |
|
|
|
|
156
|
1 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns gravatar link |
160
|
|
|
* |
161
|
|
|
* @param string $name |
162
|
|
|
* |
163
|
|
|
* @return string as gravatar link |
164
|
|
|
*/ |
165
|
10 |
|
public function getGravatar($name) |
166
|
|
|
{ |
167
|
10 |
|
$this->find("name", $name); |
168
|
10 |
|
return $this->gravatar($this->email); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set the pass. |
173
|
|
|
* |
174
|
|
|
* @param string $pass the pass to use. |
175
|
|
|
* |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
4 |
|
public function setPassword($pass) |
179
|
|
|
{ |
180
|
4 |
|
$this->pass = password_hash($pass, PASSWORD_DEFAULT); |
181
|
4 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Verify the name and the pass, if successful the object contains |
185
|
|
|
* all details from the database row. |
186
|
|
|
* |
187
|
|
|
* @param string $name name to check. |
188
|
|
|
* @param string $pass the pass to use. |
189
|
|
|
* |
190
|
|
|
* @return boolean true if name and pass matches, else false. |
191
|
|
|
*/ |
192
|
3 |
|
public function verifyPassword($name, $pass) |
193
|
|
|
{ |
194
|
3 |
|
$this->find("name", $name); |
195
|
3 |
|
return password_verify($pass, $this->pass); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Verify the name and the anaswer, if successful the object contains |
200
|
|
|
* all details from the database row. |
201
|
|
|
* |
202
|
|
|
* @param string $name name to check. |
203
|
|
|
* @param string $answer the answer. |
204
|
|
|
* |
205
|
|
|
* @return boolean true if name and pass matches, else false. |
206
|
|
|
*/ |
207
|
1 |
|
public function verifyQuestion($name, $answer) |
208
|
|
|
{ |
209
|
1 |
|
$this->find("name", $name); |
210
|
1 |
|
return ($this->question == $answer); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Check if a post belongs to user |
216
|
|
|
* |
217
|
|
|
* |
218
|
|
|
* @return boolean |
219
|
|
|
*/ |
220
|
2 |
|
public function controlAuthority($loggedUser, $name) |
221
|
|
|
{ |
222
|
2 |
|
$this->find("name", $loggedUser); |
223
|
|
|
// IF AUTHORITY == admin, then continue |
224
|
2 |
|
if ($this->authority != "admin") { |
225
|
2 |
|
return ($this->name == $name); |
226
|
|
|
} |
227
|
2 |
|
return true; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.