1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Questions; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A controller for "Questions". |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class QuestionsController implements \Anax\DI\IInjectionAware |
10
|
|
|
{ |
11
|
|
|
use \Anax\DI\TInjectable, |
12
|
|
|
\Anax\MVC\TRedirectHelpers; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Initialize the controller. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public function initialize() |
20
|
|
|
{ |
21
|
|
|
$this->questions = new \Anax\Questions\CQuestions(); |
|
|
|
|
22
|
|
|
$this->questions->setDI($this->di); |
|
|
|
|
23
|
|
|
$this->tags = new \Anax\Tags\CTags(); |
|
|
|
|
24
|
|
|
$this->tags->setDI($this->di); |
|
|
|
|
25
|
|
|
$this->tags2Questions = new \Anax\Tags\CTags2Question(); |
|
|
|
|
26
|
|
|
$this->tags2Questions->setDI($this->di); |
|
|
|
|
27
|
|
|
$this->users = new \Anax\Users\User(); |
|
|
|
|
28
|
|
|
$this->users->setDI($this->di); |
|
|
|
|
29
|
|
|
$this->answers = new \Anax\Answers\CAnswers(); |
|
|
|
|
30
|
|
|
$this->answers->setDI($this->di); |
|
|
|
|
31
|
|
|
$this->comments = new \Anax\CommentDb\CommentsInDb(); |
|
|
|
|
32
|
|
|
$this->comments->setDI($this->di); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Setup initial table for users. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function setupAction() |
41
|
|
|
{ |
42
|
|
|
$this->questions->init(); |
43
|
|
|
// $this->redirectTo('questions/'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* List recent questions. |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function recentquestionsAction($count) |
52
|
|
|
{ |
53
|
|
|
$this->views->add('default/page', [ |
|
|
|
|
54
|
|
|
'title' => 'Senaste frågorna', |
55
|
|
|
'content' => 'Här hittar du de senast ställda frågorna', |
56
|
|
|
]); |
57
|
|
|
$all = $this->questions->findRecent($count); |
58
|
|
|
foreach ($all as $question) { |
59
|
|
|
// TODO: Add more stuff into view? User, tags, ? |
60
|
|
|
// or shorten down and place in grid-box? |
61
|
|
|
// Add headline in view, "Senaste frågorna", or in index.php |
62
|
|
|
$this->views->add('questions/short', [ |
|
|
|
|
63
|
|
|
'title' => 'Senaste frågorna', |
64
|
|
|
'question' => $question->getProperties(), |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* List all questions. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function listAction($what = null, $id = null) |
75
|
|
|
{ |
76
|
|
|
// Collect all questions in $questions array. |
77
|
|
|
$questions = array(); |
78
|
|
|
switch ($what) { |
79
|
|
|
case 'tags': |
80
|
|
|
$tag = $this->tags->find($id)->getProperties(); |
81
|
|
|
$title = "Kategori {$tag['name']}"; |
82
|
|
|
$content = "Frågor i kategori {$tag['name']}"; |
83
|
|
|
$qids = $this->tags2Questions->query() |
|
|
|
|
84
|
|
|
->where("tag_id = $id") |
85
|
|
|
->execute(); |
86
|
|
|
foreach ($qids as $qid) { |
87
|
|
|
$question = $this->questions->find($qid->question_id); |
88
|
|
|
$questions[] = $question->getProperties(); |
89
|
|
|
} |
90
|
|
|
break; |
91
|
|
|
case 'answers': |
92
|
|
|
$user = $this->users->query() |
93
|
|
|
->where('acronym = ' . "'$id'") |
94
|
|
|
->execute()[0]; |
95
|
|
|
$answers = $this->answers->query() |
96
|
|
|
->where('user_id = ' . "'$user->id'") |
97
|
|
|
->execute(); |
98
|
|
|
foreach ($answers as $answer) { |
99
|
|
|
$questions[] = $this->questions->query() |
100
|
|
|
->where('id = ' . "'$answer->q_id'") |
101
|
|
|
->execute()[0]->getProperties(); |
102
|
|
|
} |
103
|
|
|
// Remove duplicate questions. E.g. same user answered multiple times on same question. |
104
|
|
|
$questions = array_unique($questions, SORT_REGULAR); |
105
|
|
|
$title = "Frågor svarade av {$user->name}"; |
106
|
|
|
$content = "Frågor besvarade av {$user->name}"; |
107
|
|
|
break; |
108
|
|
|
case 'questions': |
109
|
|
|
$user = $this->users->query() |
110
|
|
|
->where('acronym = ' . "'$id'") |
111
|
|
|
->execute()[0]; |
112
|
|
|
$all = $this->questions->query() |
113
|
|
|
->where('user_id = ' . "'$user->id'") |
114
|
|
|
->execute(); |
115
|
|
|
foreach ($all as $question) { |
116
|
|
|
$questions[] = $question->getProperties(); |
117
|
|
|
} |
118
|
|
|
$title = "Frågor från {$user->name}"; |
119
|
|
|
$content = "Frågor ställda av {$user->name}"; |
120
|
|
|
break; |
121
|
|
|
|
122
|
|
|
default: |
123
|
|
|
$title = "Alla frågor"; |
124
|
|
|
$content = "Visa alla frågor"; |
125
|
|
|
$all = $this->questions->findAll(); |
126
|
|
|
foreach ($all as $question) { |
127
|
|
|
$questions[] = $question->getProperties(); |
128
|
|
|
} |
129
|
|
|
break; |
130
|
|
|
} |
131
|
|
|
// Display questions with tags and user. |
132
|
|
|
$this->theme->setTitle("Frågor"); |
|
|
|
|
133
|
|
|
$this->views->add('default/page', [ |
|
|
|
|
134
|
|
|
'title' => $title, |
135
|
|
|
'content' => $content, |
136
|
|
|
]); |
137
|
|
|
foreach ($questions as $question) { |
138
|
|
|
$allTagsId = $this->tags2Questions->query() |
|
|
|
|
139
|
|
|
->where('question_id = ' . "'{$question['id']}'") |
140
|
|
|
->execute(); |
141
|
|
|
|
142
|
|
|
// TODO: is below with $allTags needed? |
143
|
|
|
$allTags = array(); |
144
|
|
|
foreach ($allTagsId as $tid) { |
145
|
|
|
$allTags[] = $this->tags->find($tid->tag_id)->getProperties(); |
146
|
|
|
} |
147
|
|
|
$this->views->add('questions/short', [ |
|
|
|
|
148
|
|
|
'question' => $question, |
149
|
|
|
]); |
150
|
|
|
// Add view with the questions tags |
151
|
|
|
$this->dispatcher->forward([ |
|
|
|
|
152
|
|
|
'controller' => 'tags', |
153
|
|
|
'action' => 'question', |
154
|
|
|
'params' => [$question['id'], ], |
155
|
|
|
]); |
156
|
|
|
// Add view with users card |
157
|
|
|
$this->dispatcher->forward([ |
|
|
|
|
158
|
|
|
'controller' => 'users', |
159
|
|
|
'action' => 'card', |
160
|
|
|
'params' => [ |
161
|
|
|
$question['user_id'], |
162
|
|
|
], |
163
|
|
|
]); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function acceptAnswer($answer, $question) |
168
|
|
|
{ |
169
|
|
|
if ($this->users->loggedIn() && |
170
|
|
|
($this->users->loggedInUser()->id == $question->user_id) && |
171
|
|
|
!$answer->accepted && |
172
|
|
|
!$question->accepted_answer) { |
173
|
|
|
$acceptUrl['href'] = $this->url->create('answers/accept/' . $answer->id); |
|
|
|
|
174
|
|
|
$acceptUrl['text'] = '| Acceptera svar'; |
175
|
|
|
$this->views->add('default/link', [ |
|
|
|
|
176
|
|
|
'link' => $acceptUrl, |
177
|
|
|
]); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Route to enable showing of comment or answer form |
183
|
|
|
* This is used when displaying single question |
184
|
|
|
* |
185
|
|
|
* @param int $form answer, comment_question, comment_answer |
186
|
|
|
* |
187
|
|
|
* @return void |
188
|
|
|
*/ |
189
|
|
|
public function showformAction($form) |
190
|
|
|
{ |
191
|
|
|
$this->session->set('ShowFormCorA', $form); |
|
|
|
|
192
|
|
|
$this->redirectTo($_SERVER['HTTP_REFERER']); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Route to show link to or comment or answer form |
197
|
|
|
* |
198
|
|
|
* @param string $form {answer, comment_question, comment_answer} |
199
|
|
|
* string $controller to handle form |
200
|
|
|
* int $id of question or answer |
201
|
|
|
* string $linkText to display on linkText |
202
|
|
|
* string 'q' or 'a' on comment on question or comment on answer |
203
|
|
|
* |
204
|
|
|
* @return void |
205
|
|
|
*/ |
206
|
|
|
private function showFormCommentOrAnswer($form, $controller, $id, $linkText, $qora = null) |
207
|
|
|
{ |
208
|
|
|
// Add view to comment question |
209
|
|
|
if ($this->session->get('ShowFormCorA')==$form) { |
|
|
|
|
210
|
|
|
$this->dispatcher->forward([ |
|
|
|
|
211
|
|
|
'controller' => $controller, |
212
|
|
|
'action' => 'add', |
213
|
|
|
'params' => [$id, $qora, ], |
214
|
|
|
]); |
215
|
|
|
} else { |
216
|
|
|
if ($this->users->loggedIn()) { |
217
|
|
|
$route = "questions/showform/$form"; |
218
|
|
|
} else { |
219
|
|
|
$route = 'users/login'; |
220
|
|
|
} |
221
|
|
|
$commentFormUrl['href'] = $this->url->create($route); |
|
|
|
|
222
|
|
|
$commentFormUrl['text'] = $linkText; |
223
|
|
|
$this->views->add('default/link', [ |
|
|
|
|
224
|
|
|
'link' => $commentFormUrl, |
225
|
|
|
]); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
/** |
229
|
|
|
* List single question |
230
|
|
|
* |
231
|
|
|
* @param int $id of question to display |
232
|
|
|
* |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
|
public function singleAction($id = null) |
236
|
|
|
{ |
237
|
|
|
$question = $this->questions->query() |
238
|
|
|
->where('id = ' . "'$id'") |
239
|
|
|
->execute()[0]; |
240
|
|
|
|
241
|
|
|
// Add view with the question. |
242
|
|
|
$this->theme->setTitle("$question->headline"); |
|
|
|
|
243
|
|
|
$this->views->add('questions/single', [ |
|
|
|
|
244
|
|
|
'question' => $question, |
245
|
|
|
]); |
246
|
|
|
|
247
|
|
|
// Add view with the questions tags |
248
|
|
|
$this->dispatcher->forward([ |
|
|
|
|
249
|
|
|
'controller' => 'tags', |
250
|
|
|
'action' => 'question', |
251
|
|
|
'params' => [$question->id, ], |
252
|
|
|
]); |
253
|
|
|
|
254
|
|
|
// Add view with user card. |
255
|
|
|
$humanTime = \Anax\CommentDb\CommentsInDb::humanTiming($question->created); |
256
|
|
|
$text = "Frågade för $humanTime sedan"; |
257
|
|
|
$this->dispatcher->forward([ |
|
|
|
|
258
|
|
|
'controller' => 'users', |
259
|
|
|
'action' => 'card', |
260
|
|
|
'params' => [$question->user_id, 'q', $text, ], |
261
|
|
|
]); |
262
|
|
|
|
263
|
|
|
// Add view to comment question, form visible when logged in and clicking link. |
264
|
|
|
$this->showFormCommentOrAnswer('comment_question', 'comment', $question->id, 'Kommentera ', 'q'); |
265
|
|
|
|
266
|
|
|
// Add view to answer question, form visible when logged in and clicking link. |
267
|
|
|
$this->showFormCommentOrAnswer('answer', 'answers', $question->id, '| Besvara '); |
268
|
|
|
|
269
|
|
|
// Add view with Comments to question |
270
|
|
|
$allComments = $this->comments->query() |
271
|
|
|
->where("q_or_a = 'q'") |
272
|
|
|
->andwhere("q_or_a_id = $id") |
273
|
|
|
->execute(); |
274
|
|
|
$nrOfComments = sizeof($allComments); |
275
|
|
|
$commentsListData = array(); |
276
|
|
|
for ($j=0; $j < $nrOfComments; $j++) { |
277
|
|
|
$commentsListData[$j]['comment'] = $allComments[$j]->getProperties(); |
278
|
|
|
// Get user of comment |
279
|
|
|
$user = $this->users->find($allComments[$j]->user_id); |
280
|
|
|
$commentsListData[$j]['user'] = $user->getProperties(); |
281
|
|
|
} |
282
|
|
|
$this->views->add('questions/comments', [ |
|
|
|
|
283
|
|
|
'comments' => $commentsListData, |
284
|
|
|
]); |
285
|
|
|
|
286
|
|
|
// Display all answers and comments to each answer. |
287
|
|
|
// Display all answers to question. |
288
|
|
|
$allAnswers = $this->answers->query() |
289
|
|
|
->where("q_id = $question->id") |
290
|
|
|
->execute(); |
291
|
|
|
$this->views->add('questions/answersheading', [ |
|
|
|
|
292
|
|
|
'title' => sizeof($allAnswers) . " svar", |
293
|
|
|
]); |
294
|
|
|
foreach ($allAnswers as $answer) { |
295
|
|
|
$user = $this->users->find($answer->user_id); |
296
|
|
|
// Add view to display all answer. |
297
|
|
|
$this->views->add('questions/answer', [ |
|
|
|
|
298
|
|
|
'answer' => $answer->getProperties(), |
299
|
|
|
'user' => $user->getProperties(), |
300
|
|
|
]); |
301
|
|
|
// Display link or comment form for commenting the answer |
302
|
|
|
$this->showFormCommentOrAnswer( |
303
|
|
|
'comment_answer_' . $answer->id, |
304
|
|
|
'comment', |
305
|
|
|
$answer->id, |
306
|
|
|
'Kommentera ', |
307
|
|
|
'a' |
308
|
|
|
); |
309
|
|
|
$this->acceptAnswer($answer, $question); |
310
|
|
|
|
311
|
|
|
// Get all comments to answer |
312
|
|
|
$allComments = $this->comments->query() |
313
|
|
|
->where("q_or_a = 'a'") |
314
|
|
|
->andwhere("q_or_a_id = $answer->id") |
315
|
|
|
->execute(); |
316
|
|
|
$commentsListData = array(); |
317
|
|
|
foreach ($allComments as $comment) { |
318
|
|
|
$user = $this->users->find($comment->user_id); |
319
|
|
|
$commentsListData[] = [ |
320
|
|
|
'comment' => $comment->getProperties(), |
321
|
|
|
'user' => $user->getProperties(), |
322
|
|
|
]; |
323
|
|
|
} |
324
|
|
|
$this->views->add('questions/comments', [ |
|
|
|
|
325
|
|
|
'comments' => $commentsListData, |
326
|
|
|
]); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
$this->views->add('default/page', [ |
|
|
|
|
330
|
|
|
'content' => ' ', |
331
|
|
|
'links' => [ |
332
|
|
|
[ |
333
|
|
|
'href' => $this->url->create('questions/ask'), |
|
|
|
|
334
|
|
|
'text' => "Fråga en fråga", |
335
|
|
|
], |
336
|
|
|
], |
337
|
|
|
]); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Ask new question |
342
|
|
|
* |
343
|
|
|
* @param string $route to page for comment flow. |
|
|
|
|
344
|
|
|
* |
345
|
|
|
* @return void |
346
|
|
|
*/ |
347
|
|
View Code Duplication |
public function askAction() |
|
|
|
|
348
|
|
|
{ |
349
|
|
|
if ($this->users->loggedIn()) { |
350
|
|
|
$this->di->session(); // Will load the session service which also starts the session |
351
|
|
|
$form = $this->createAddQuestionForm(); |
352
|
|
|
$form->check([$this, 'callbackSuccess'], [$this, 'callbackFail']); |
353
|
|
|
$this->di->theme->setTitle("Fråga"); |
354
|
|
|
$this->di->views->add('default/page', [ |
355
|
|
|
'title' => "Ställ en fråga", |
356
|
|
|
'content' => $form->getHTML() |
357
|
|
|
]); |
358
|
|
|
} else { |
359
|
|
|
$this->redirectTo($this->url->create('users/login')); |
|
|
|
|
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
private function createAddQuestionForm() |
363
|
|
|
{ |
364
|
|
|
$tags = $this->tags->findAll(); |
365
|
|
|
foreach ($tags as $tag) { |
366
|
|
|
$tagArray[] = $tag->name; |
|
|
|
|
367
|
|
|
} |
368
|
|
|
return $this->di->form->create([], [ |
369
|
|
|
'headline' => [ |
370
|
|
|
'type' => 'text', |
371
|
|
|
'label' => 'Rubrik:', |
372
|
|
|
'required' => true, |
373
|
|
|
'validation' => ['not_empty'], |
374
|
|
|
], |
375
|
|
|
'content' => [ |
376
|
|
|
'type' => 'textarea', |
377
|
|
|
'label' => 'Fråga:', |
378
|
|
|
'required' => true, |
379
|
|
|
'validation' => ['not_empty'], |
380
|
|
|
], |
381
|
|
|
"tags" => [ |
382
|
|
|
"type" => "checkbox-multiple", |
383
|
|
|
'label' => 'Kategorier:', |
384
|
|
|
"values" => $tagArray, |
|
|
|
|
385
|
|
|
], |
386
|
|
|
'submit' => [ |
387
|
|
|
'type' => 'submit', |
388
|
|
|
'callback' => [$this, 'callbackSubmitAddQuestion'], |
389
|
|
|
], |
390
|
|
|
// 'submit-fail' => [ |
391
|
|
|
// 'type' => 'submit', |
392
|
|
|
// 'callback' => [$this, 'callbackSubmitFailAddQuestion'], |
393
|
|
|
// ], |
394
|
|
|
]); |
395
|
|
|
} |
396
|
|
|
/** |
397
|
|
|
* Callback for submit-button. |
398
|
|
|
* |
399
|
|
|
*/ |
400
|
|
|
public function callbackSubmitAddQuestion($form) |
401
|
|
|
{ |
402
|
|
|
// $form->AddOutput("<p>DoSubmit(): Form was submitted.<p>"); |
403
|
|
|
// $form->AddOutput("<p>Do stuff (save to database) and return true (success) or false (failed processing)</p>"); |
404
|
|
|
|
405
|
|
|
// Save comment to database |
406
|
|
|
$now = time(); |
407
|
|
|
$this->questions->save([ |
408
|
|
|
'headline' => $form->Value('headline'), |
409
|
|
|
'content' => $form->Value('content'), |
410
|
|
|
'user_id' => $this->users->loggedInUser()->id, |
411
|
|
|
'vote' => 0, |
412
|
|
|
'created' => $now, |
413
|
|
|
]); |
414
|
|
|
// Pull out tags and store them in tags2question model |
415
|
|
|
$qid = $this->questions->id; |
416
|
|
|
$tags = $form->Value('tags'); |
417
|
|
|
foreach ($tags as $tag) { |
418
|
|
|
$tag_id = $this->tags->findTag($tag)->getProperties()['id']; |
419
|
|
|
unset($this->tags2Questions->id); |
420
|
|
|
$this->tags2Questions->save([ |
|
|
|
|
421
|
|
|
'tag_id' => $tag_id, |
422
|
|
|
'question_id' => $qid, |
423
|
|
|
]); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
// $form->AddOutput("<p><b>Name: " . $form->Value('name') . "</b></p>"); |
427
|
|
|
// $form->AddOutput("<p><b>Email: " . $form->Value('mail') . "</b></p>"); |
428
|
|
|
$form->saveInSession = false; |
429
|
|
|
return true; |
430
|
|
|
} |
431
|
|
|
/** |
432
|
|
|
* Callback for submit-button. |
433
|
|
|
* |
434
|
|
|
*/ |
435
|
|
|
public function callbackSubmitFailAddQuestion($form) |
436
|
|
|
{ |
437
|
|
|
// TODO: Remove this? |
438
|
|
|
$form->AddOutput("<p><i>DoSubmitFail(): Form was submitted but I failed to process/save/validate it</i></p>"); |
439
|
|
|
return false; |
440
|
|
|
} |
441
|
|
|
/** |
442
|
|
|
* Callback What to do if the form was submitted? |
443
|
|
|
* |
444
|
|
|
*/ |
445
|
|
|
public function callbackSuccess($form) |
446
|
|
|
{ |
447
|
|
|
$form->AddOUtput("<p><i>Form was submitted and the callback method returned true.</i></p>"); |
448
|
|
|
// Redirect to page displaying the new question. |
449
|
|
|
$this->redirectTo($this->url->create('questions/single/' . $this->questions->id)); |
|
|
|
|
450
|
|
|
} |
451
|
|
|
/** |
452
|
|
|
* Callback What to do when form could not be processed? |
453
|
|
|
* |
454
|
|
|
*/ |
455
|
|
|
public function callbackFail($form) |
456
|
|
|
{ |
457
|
|
|
$form->AddOutput("<p><i>Form was submitted and the Check() method returned false.</i></p>"); |
458
|
|
|
// Redirect to comment form. |
459
|
|
|
$this->redirectTo(); |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: