|
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 = "ramverk1_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
|
|
|
* @param array $param |
|
|
|
|
|
|
65
|
|
|
* |
|
66
|
|
|
* @return object |
|
67
|
|
|
*/ |
|
68
|
7 |
|
public function setupUser($user) |
|
69
|
|
|
{ |
|
70
|
7 |
|
$question = new Question($this->db); |
|
71
|
7 |
|
$post = new Post($this->db); |
|
72
|
7 |
|
$comment = new Comment($this->db); |
|
73
|
7 |
|
$vote = new Vote($this->db); |
|
74
|
|
|
|
|
75
|
|
|
// Basic setup for user |
|
76
|
7 |
|
$user->img = $this->getGravatar($user->name); |
|
77
|
|
|
|
|
78
|
|
|
// Get all posts/votes user made |
|
79
|
7 |
|
$user->questions = $question->getQuestions("user = ?", $user->name); |
|
80
|
7 |
|
$user->comments = $comment->getComments("user = ?", $user->name); |
|
81
|
7 |
|
$user->posts = $post->getAllPosts("user = ? AND type = ?", [$user->name, "answer"]); |
|
82
|
7 |
|
$user->votes = $vote->getVote("user = ?", [$user->name]); |
|
83
|
|
|
|
|
84
|
|
|
// Amount of posts |
|
85
|
7 |
|
$user->postAmount = count($user->questions) + count($user->posts) + count($user->comments); |
|
86
|
|
|
|
|
87
|
|
|
// Algorithm for reputation points |
|
88
|
7 |
|
$user->reputation = $this->getReputation($user); |
|
89
|
|
|
|
|
90
|
|
|
// Get all the questions connected to answers |
|
91
|
7 |
|
$user->answeredQuestions = array_map(function ($answer) { |
|
92
|
5 |
|
$question = new Question($this->db); |
|
93
|
5 |
|
return $question->getQuestion($answer->questionId); |
|
94
|
7 |
|
}, $user->posts); |
|
95
|
|
|
|
|
96
|
7 |
|
return $user; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns post with markdown and gravatar |
|
101
|
|
|
* @param string $sql |
|
102
|
|
|
* @param array $param |
|
|
|
|
|
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
3 |
View Code Duplication |
public function getAllUsers($sql = null, $params = null) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
3 |
|
if ($sql == null) { |
|
|
|
|
|
|
109
|
3 |
|
$users = $this->findAll(); |
|
110
|
3 |
|
} |
|
111
|
3 |
|
if ($sql != null) { |
|
|
|
|
|
|
112
|
1 |
|
$users = $this->findAllWhere($sql, $params); |
|
113
|
1 |
|
} |
|
114
|
|
|
|
|
115
|
3 |
|
return array_map(array($this, 'setupUser'), $users); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* return question/answer, three attributes are set, comments connected to them is an array. |
|
120
|
|
|
* |
|
121
|
|
|
* @return object |
|
122
|
|
|
*/ |
|
123
|
4 |
|
public function getUser($name) |
|
124
|
|
|
{ |
|
125
|
4 |
|
$user = $this->find("name", $name); |
|
126
|
4 |
|
$user = $this->setupUser($user); |
|
|
|
|
|
|
127
|
4 |
|
return $user; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Check if user exists |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $name |
|
135
|
|
|
* |
|
136
|
|
|
* @return boolean true if user exists in database else false |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public function userExists($name) |
|
139
|
|
|
{ |
|
140
|
1 |
|
$user = $this->find("name", $name); |
|
141
|
1 |
|
return !$user ? false : true; |
|
142
|
|
|
} |
|
143
|
|
|
/** |
|
144
|
|
|
* Returns gravatar link |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $email |
|
|
|
|
|
|
147
|
|
|
* |
|
148
|
|
|
* @return string as gravatar link |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function setGravatar() |
|
151
|
|
|
{ |
|
152
|
1 |
|
$this->img = $this->gravatar($this->email); |
|
|
|
|
|
|
153
|
1 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Returns gravatar link |
|
157
|
|
|
* |
|
158
|
|
|
* @param string $name |
|
159
|
|
|
* |
|
160
|
|
|
* @return string as gravatar link |
|
161
|
|
|
*/ |
|
162
|
10 |
|
public function getGravatar($name) |
|
163
|
|
|
{ |
|
164
|
10 |
|
$this->find("name", $name); |
|
165
|
10 |
|
return $this->gravatar($this->email); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Set the pass. |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $pass the pass to use. |
|
172
|
|
|
* |
|
173
|
|
|
* @return void |
|
174
|
|
|
*/ |
|
175
|
4 |
|
public function setPassword($pass) |
|
176
|
|
|
{ |
|
177
|
4 |
|
$this->pass = password_hash($pass, PASSWORD_DEFAULT); |
|
178
|
4 |
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Verify the name and the pass, if successful the object contains |
|
182
|
|
|
* all details from the database row. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $name name to check. |
|
185
|
|
|
* @param string $pass the pass to use. |
|
186
|
|
|
* |
|
187
|
|
|
* @return boolean true if name and pass matches, else false. |
|
188
|
|
|
*/ |
|
189
|
3 |
|
public function verifyPassword($name, $pass) |
|
190
|
|
|
{ |
|
191
|
3 |
|
$this->find("name", $name); |
|
192
|
3 |
|
return password_verify($pass, $this->pass); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Verify the name and the anaswer, if successful the object contains |
|
197
|
|
|
* all details from the database row. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $name name to check. |
|
200
|
|
|
* @param string $answer the answer. |
|
201
|
|
|
* |
|
202
|
|
|
* @return boolean true if name and pass matches, else false. |
|
203
|
|
|
*/ |
|
204
|
1 |
|
public function verifyQuestion($name, $answer) |
|
205
|
|
|
{ |
|
206
|
1 |
|
$this->find("name", $name); |
|
207
|
1 |
|
return ($this->question == $answer); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Check if a post belongs to user |
|
213
|
|
|
* |
|
214
|
|
|
* |
|
215
|
|
|
* @return boolean |
|
216
|
|
|
*/ |
|
217
|
2 |
|
public function controlAuthority($loggedUser, $name) |
|
218
|
|
|
{ |
|
219
|
2 |
|
$this->find("name", $loggedUser); |
|
220
|
|
|
// IF AUTHORITY == admin, then continue |
|
221
|
2 |
|
if ($this->authority != "admin") { |
|
222
|
2 |
|
return ($this->name == $name); |
|
223
|
|
|
} |
|
224
|
2 |
|
return true; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.