1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pamo\Game; |
4
|
|
|
|
5
|
|
|
use Pamo\Question\HTMLForm\CreateQuestionForm; |
6
|
|
|
use Pamo\Question\HTMLForm\DeleteQuestionForm; |
7
|
|
|
use Pamo\Question\HTMLForm\UpdateQuestionForm; |
8
|
|
|
use Pamo\Answer\HTMLForm\CreateAnswerForm; |
9
|
|
|
use Pamo\Answer\HTMLForm\DeleteAnswerForm; |
10
|
|
|
use Pamo\Answer\HTMLForm\UpdateAnswerForm; |
11
|
|
|
use Pamo\Comment\HTMLForm\CreateCommentForm; |
12
|
|
|
use Pamo\Comment\HTMLForm\DeleteCommentForm; |
13
|
|
|
use Pamo\Comment\HTMLForm\UpdateCommentForm; |
14
|
|
|
use Pamo\User\HTMLForm\CreateUserForm; |
15
|
|
|
use Pamo\User\HTMLForm\UpdateUserForm; |
16
|
|
|
use Pamo\User\HTMLForm\DeleteUserForm; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Game |
20
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity) |
21
|
|
|
*/ |
22
|
|
|
class Game |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var object $di. |
26
|
|
|
*/ |
27
|
|
|
private $di; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Game class |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
36 |
|
public function init($di) |
36
|
|
|
{ |
37
|
36 |
|
$this->di = $di; |
38
|
36 |
|
$this->game = []; |
|
|
|
|
39
|
36 |
|
$this->session = $this->di->get("session"); |
|
|
|
|
40
|
36 |
|
$this->request = $this->di->get("request"); |
|
|
|
|
41
|
36 |
|
$this->gravatar = $this->di->get("gravatar"); |
|
|
|
|
42
|
36 |
|
$this->sortQBy = $this->session->get("sortQBy", "created"); |
|
|
|
|
43
|
36 |
|
$this->sortQType = $this->session->get("sortQType", "DESC"); |
|
|
|
|
44
|
36 |
|
$this->sortABy = $this->session->get("sortABy", "created"); |
|
|
|
|
45
|
36 |
|
$this->sortAType = $this->session->get("sortAType", "ASC"); |
|
|
|
|
46
|
36 |
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get and setup all databases. |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
36 |
|
public function getDatabases() |
55
|
|
|
{ |
56
|
36 |
|
$this->question = $this->di->get("question"); |
|
|
|
|
57
|
36 |
|
$this->question->setDb($this->di->get("dbqb")); |
58
|
|
|
|
59
|
36 |
|
$this->answer = $this->di->get("answer"); |
|
|
|
|
60
|
36 |
|
$this->answer->setDb($this->di->get("dbqb")); |
61
|
|
|
|
62
|
36 |
|
$this->user = $this->di->get("user"); |
|
|
|
|
63
|
36 |
|
$this->user->setDb($this->di->get("dbqb")); |
64
|
|
|
|
65
|
36 |
|
$this->commentQuestion = $this->di->get("comment-question"); |
|
|
|
|
66
|
36 |
|
$this->commentQuestion->setDb($this->di->get("dbqb")); |
67
|
|
|
|
68
|
36 |
|
$this->commentAnswer = $this->di->get("comment-answer"); |
|
|
|
|
69
|
36 |
|
$this->commentAnswer->setDb($this->di->get("dbqb")); |
70
|
|
|
|
71
|
36 |
|
$this->tag = $this->di->get("tag"); |
|
|
|
|
72
|
36 |
|
$this->tag->setDb($this->di->get("dbqb")); |
73
|
|
|
|
74
|
36 |
|
$this->tagQuestion = $this->di->get("tag-question"); |
|
|
|
|
75
|
36 |
|
$this->tagQuestion->setDb($this->di->get("dbqb")); |
76
|
36 |
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Vote |
82
|
|
|
* |
83
|
|
|
* @return object |
84
|
|
|
*/ |
85
|
1 |
|
public function vote($type, $id, $vote) |
86
|
|
|
{ |
87
|
1 |
|
$currentUser = $this->session->get("user", null); |
88
|
|
|
|
89
|
1 |
|
switch ($type) { |
90
|
1 |
|
case "question": |
91
|
1 |
|
$table = $this->question; |
92
|
1 |
|
break; |
93
|
1 |
|
case "answer": |
94
|
1 |
|
$table = $this->answer; |
95
|
1 |
|
break; |
96
|
1 |
|
case "comment-question": |
97
|
1 |
|
$table = $this->commentQuestion; |
98
|
1 |
|
break; |
99
|
1 |
|
case "comment-answer": |
100
|
1 |
|
$table = $this->commentAnswer; |
101
|
1 |
|
break; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
$table->find("id", $id); |
|
|
|
|
105
|
|
|
|
106
|
1 |
|
$eligible = $this->canUserVote($table->user); |
|
|
|
|
107
|
|
|
|
108
|
1 |
|
if (!$eligible) { |
|
|
|
|
109
|
1 |
|
return false; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
if ($vote === "up") { |
113
|
1 |
|
$table->vote += 1; |
114
|
1 |
|
$this->updateRank($table->user, $table->vote); |
115
|
1 |
|
} else if ($vote === "down") { |
116
|
1 |
|
$table->vote -= 1; |
117
|
|
|
} else { |
118
|
1 |
|
$table->vote = $table->vote; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
$table->save(); |
122
|
|
|
|
123
|
1 |
|
if ($type === "question") { |
124
|
1 |
|
$questionId = $table->id; |
125
|
|
|
} else { |
126
|
1 |
|
$questionId = $table->questionid; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
$user = $this->user; |
130
|
1 |
|
$user->find("username", $currentUser["username"]); |
131
|
1 |
|
$user->voted += 1; |
132
|
1 |
|
$user->save(); |
133
|
1 |
|
$this->updateRank($currentUser["username"], 1); |
134
|
|
|
|
135
|
1 |
|
return $questionId; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Rank |
140
|
|
|
* |
141
|
|
|
* @return null |
142
|
|
|
*/ |
143
|
5 |
|
public function updateRank($username, $rank) |
144
|
|
|
{ |
145
|
|
|
switch (true) { |
146
|
5 |
|
case $rank <= 1: |
147
|
1 |
|
$value = 1; |
148
|
1 |
|
break; |
149
|
5 |
|
case $rank <= 5: |
150
|
5 |
|
$value = 5; |
151
|
5 |
|
break; |
152
|
1 |
|
case $rank <= 10: |
153
|
1 |
|
$value = 10; |
154
|
1 |
|
break; |
155
|
1 |
|
case $rank <= 20: |
156
|
1 |
|
$value = 20; |
157
|
1 |
|
break; |
158
|
1 |
|
case $rank <= 30: |
159
|
1 |
|
$value = 30; |
160
|
1 |
|
break; |
161
|
|
|
default: |
162
|
1 |
|
$value = 50; |
163
|
|
|
} |
164
|
|
|
|
165
|
5 |
|
$user = $this->user; |
166
|
5 |
|
$user->find("username", $username); |
167
|
5 |
|
$user->rank += $value; |
168
|
5 |
|
$user->save(); |
169
|
5 |
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Return question form |
175
|
|
|
* |
176
|
|
|
* @return object |
177
|
|
|
*/ |
178
|
3 |
|
public function getUserForm($adminType, $username = null) |
179
|
|
|
{ |
180
|
3 |
|
switch ($adminType) { |
181
|
3 |
|
case "create": |
182
|
1 |
|
$userForm = new CreateUserForm($this->di); |
183
|
1 |
|
break; |
184
|
2 |
|
case "edit": |
185
|
1 |
|
$userForm = new UpdateUserForm($this->di, $username); |
186
|
1 |
|
break; |
187
|
1 |
|
case "delete": |
188
|
1 |
|
$userForm = new DeleteUserForm($this->di, $username); |
189
|
1 |
|
break; |
190
|
|
|
} |
191
|
3 |
|
return isset($userForm) ? $userForm : null; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Return question form |
198
|
|
|
* |
199
|
|
|
* @return object |
200
|
|
|
*/ |
201
|
4 |
|
public function getQuestionForm($adminType, $questionId = null) |
202
|
|
|
{ |
203
|
4 |
|
switch ($adminType) { |
204
|
4 |
|
case "create": |
205
|
1 |
|
$questionForm = new CreateQuestionForm($this->di); |
206
|
1 |
|
break; |
207
|
3 |
|
case "edit": |
208
|
2 |
|
$questionForm = new UpdateQuestionForm($this->di, $questionId); |
209
|
2 |
|
break; |
210
|
2 |
|
case "delete": |
211
|
1 |
|
$questionForm = new DeleteQuestionForm($this->di, $questionId); |
212
|
1 |
|
break; |
213
|
|
|
default: |
214
|
1 |
|
$questionForm = null; |
215
|
|
|
} |
216
|
4 |
|
return $questionForm; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Return answer form |
223
|
|
|
* |
224
|
|
|
* @return object |
225
|
|
|
*/ |
226
|
7 |
|
public function getAnswerForm($adminType, $questionId) |
227
|
|
|
{ |
228
|
7 |
|
$request = $this->di->get("request"); |
229
|
7 |
|
$adminId = $request->getGet("adminId", null); |
230
|
|
|
|
231
|
7 |
|
switch ($adminType) { |
232
|
7 |
|
case "create": |
233
|
7 |
|
$answerForm = new CreateAnswerForm($this->di, $questionId, $this->activeUser("username")); |
|
|
|
|
234
|
7 |
|
break; |
235
|
2 |
|
case "edit": |
236
|
1 |
|
$answerForm = new UpdateAnswerForm($this->di, $adminId); |
237
|
1 |
|
break; |
238
|
2 |
|
case "delete": |
239
|
1 |
|
$answerForm = new DeleteAnswerForm($this->di, $adminId); |
240
|
1 |
|
break; |
241
|
|
|
default: |
242
|
1 |
|
$answerForm = null; |
243
|
|
|
} |
244
|
7 |
|
return $answerForm; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Return comment form |
251
|
|
|
* |
252
|
|
|
* @return object |
253
|
|
|
*/ |
254
|
3 |
|
public function getCommentForm($adminType, $questionId, $table) |
255
|
|
|
{ |
256
|
3 |
|
$request = $this->di->get("request"); |
257
|
3 |
|
$adminId = $request->getGet("adminId", null); |
258
|
3 |
|
$answerId = $request->getGet("answerId", null); |
259
|
|
|
|
260
|
3 |
|
switch ($adminType) { |
261
|
3 |
|
case "create": |
262
|
2 |
|
$commentForm = new CreateCommentForm($this->di, $table, $questionId, $answerId); |
263
|
2 |
|
break; |
264
|
3 |
|
case "edit": |
265
|
2 |
|
$commentForm = new UpdateCommentForm($this->di, $table, $adminId); |
266
|
2 |
|
break; |
267
|
3 |
|
case "delete": |
268
|
2 |
|
$commentForm = new DeleteCommentForm($this->di, $table, $adminId); |
269
|
2 |
|
break; |
270
|
|
|
default: |
271
|
1 |
|
$commentForm = null; |
272
|
|
|
} |
273
|
3 |
|
return $commentForm; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
|
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Return nav |
280
|
|
|
* |
281
|
|
|
* @return array |
282
|
|
|
*/ |
283
|
34 |
|
public function getNav() |
284
|
|
|
{ |
285
|
34 |
|
$navData = []; |
286
|
|
|
|
287
|
34 |
|
if ($this->activeUser()) { |
288
|
|
|
$navData = [ |
289
|
27 |
|
"activeUser" => $this->activeUser(), |
290
|
27 |
|
"gravatar" => $this->gravatar->get($this->activeUser("email")) |
291
|
|
|
]; |
292
|
|
|
} |
293
|
|
|
|
294
|
34 |
|
return $navData; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
|
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Return form settings |
301
|
|
|
* |
302
|
|
|
* @return array |
303
|
|
|
*/ |
304
|
7 |
|
public function getFormSettings() |
305
|
|
|
{ |
306
|
|
|
$formSettings = [ |
307
|
7 |
|
"use_fieldset" => false |
308
|
|
|
]; |
309
|
|
|
|
310
|
7 |
|
return $formSettings; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
|
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Return current user |
317
|
|
|
* |
318
|
|
|
* @return array |
319
|
|
|
*/ |
320
|
34 |
|
public function activeUser($item = null) |
321
|
|
|
{ |
322
|
34 |
|
$user = $this->session->get("user", null); |
323
|
|
|
|
324
|
34 |
|
if ($item && $user) { |
325
|
30 |
|
return $user[$item]; |
326
|
|
|
} |
327
|
|
|
|
328
|
34 |
|
return $user; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
|
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Is the logged in user the content owner. |
335
|
|
|
* |
336
|
|
|
* @return bool |
337
|
|
|
*/ |
338
|
3 |
|
public function validUser($username) |
339
|
|
|
{ |
340
|
3 |
|
if ($this->activeUser("username") != $username) { |
341
|
2 |
|
return false; |
342
|
|
|
} |
343
|
1 |
|
return true; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
|
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Is the logged in user the content owner. |
350
|
|
|
* |
351
|
|
|
* @return null |
352
|
|
|
*/ |
353
|
1 |
|
public function canUserVote($username) |
354
|
|
|
{ |
355
|
1 |
|
return $this->activeUser("username") != $username; |
|
|
|
|
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
|
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Return question sort |
362
|
|
|
* |
363
|
|
|
* @return string |
364
|
|
|
*/ |
365
|
7 |
|
public function getQSort() |
366
|
|
|
{ |
367
|
7 |
|
$sort = $this->request->getGet("sortQBy", null); |
368
|
|
|
|
369
|
7 |
|
if ($sort) { |
370
|
7 |
|
$this->session->set("sortQBy", $sort); |
371
|
7 |
|
$this->sortQBy = $sort; |
|
|
|
|
372
|
|
|
} |
373
|
|
|
|
374
|
7 |
|
return $this->sortQBy; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
|
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Return question sort type |
381
|
|
|
* |
382
|
|
|
* @return string |
383
|
|
|
*/ |
384
|
7 |
|
public function getQSortType() |
385
|
|
|
{ |
386
|
7 |
|
$sortType = $this->request->getGet("sortQType", null); |
387
|
|
|
|
388
|
7 |
|
if ($sortType) { |
389
|
7 |
|
$this->session->set("sortQType", $sortType); |
390
|
7 |
|
$this->sortQType = $sortType; |
|
|
|
|
391
|
|
|
} |
392
|
|
|
|
393
|
7 |
|
return $this->sortQType; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
|
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Return answer sort |
400
|
|
|
* |
401
|
|
|
* @return string |
402
|
|
|
*/ |
403
|
10 |
|
public function getASort() |
404
|
|
|
{ |
405
|
10 |
|
$sort = $this->request->getGet("sortABy", null); |
406
|
|
|
|
407
|
10 |
|
if ($sort) { |
408
|
10 |
|
$this->session->set("sortABy", $sort); |
409
|
10 |
|
$this->sortABy = $sort; |
|
|
|
|
410
|
|
|
} |
411
|
|
|
|
412
|
10 |
|
return $this->sortABy; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
|
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Return answer sort type |
419
|
|
|
* |
420
|
|
|
* @return string |
421
|
|
|
*/ |
422
|
10 |
|
public function getASortType() |
423
|
|
|
{ |
424
|
10 |
|
$sortType = $this->request->getGet("sortAType", null); |
425
|
|
|
|
426
|
10 |
|
if ($sortType) { |
427
|
10 |
|
$this->session->set("sortAType", $sortType); |
428
|
10 |
|
$this->sortAType = $sortType; |
|
|
|
|
429
|
|
|
} |
430
|
|
|
|
431
|
10 |
|
return $this->sortAType; |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|