1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicklas\Comment; |
4
|
|
|
|
5
|
|
|
use \Nicklas\Comment\HTMLForm\Comment\CreateQuestionForm; |
6
|
|
|
use \Nicklas\Comment\HTMLForm\Comment\CreateAnswerForm; |
7
|
|
|
use \Nicklas\Comment\HTMLForm\Comment\EditCommentForm; |
8
|
|
|
|
9
|
|
|
use \Nicklas\Comment\Modules\Question; |
10
|
|
|
use \Nicklas\Comment\Modules\Comment; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Extends the UserController, for comments |
14
|
|
|
*/ |
15
|
|
|
class QuestionController extends AdminController |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Show all items. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
1 |
View Code Duplication |
public function getIndex() |
|
|
|
|
24
|
|
|
{ |
25
|
1 |
|
$question = new Question($this->di->get("db")); |
26
|
|
|
|
27
|
|
|
$views = [ |
28
|
1 |
|
["comment/question/view-all", ["questions" => $question->getQuestions()], "main"] |
29
|
1 |
|
]; |
30
|
|
|
|
31
|
1 |
|
$this->di->get("pageRenderComment")->renderPage([ |
32
|
1 |
|
"views" => $views, |
33
|
|
|
"title" => "All questions" |
34
|
1 |
|
]); |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* View specific question and create answer form |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function getPostQuestionAnswer($id, $sort = null) |
43
|
|
|
{ |
44
|
|
|
$question = new Question($this->di->get("db")); |
45
|
|
|
$question = $question->getQuestion($id); |
46
|
|
|
|
47
|
|
|
// Get query for up or down |
48
|
|
|
$order = isset($_GET["order"]) ? $_GET["order"] : "up"; |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
if ($sort == "points") { |
52
|
|
|
usort($question->answers, function ($current, $next) { |
53
|
|
|
return $current->vote->score < $next->vote->score; |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($sort == "vote") { |
58
|
|
|
usort($question->answers, function ($current, $next) { |
59
|
|
|
return count($current->vote->likes) > count($next->vote->likes); |
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
if ($order == "down") { |
65
|
|
|
asort($question->answers); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($order == "up") { |
69
|
|
|
arsort($question->answers); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
$form = new CreateAnswerForm($this->di, $id); |
77
|
|
|
$form->check(); |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
|
81
|
|
|
$views = [ |
82
|
|
|
["comment/question/view/view-question", ["question" => $question], "question"], |
83
|
|
|
["comment/question/view/view-answers", |
84
|
|
|
["answers" => $question->answers, "questionId" => $question->question->questionId], "question"], |
85
|
|
|
["comment/question/view/post-answer", ["form" => $form->getHTML()], "form"], |
86
|
|
|
["comment/question/view/wrappedField", ["question" => $question], "main"] |
87
|
|
|
]; |
88
|
|
|
$this->di->get("pageRenderComment")->renderPage([ |
89
|
|
|
"views" => $views, |
90
|
|
|
"title" => "Create your question" |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Show all items. |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public function getTaggedQuestions($tag) |
102
|
|
|
{ |
103
|
|
|
$question = new Question($this->di->get("db")); |
104
|
|
|
|
105
|
|
|
$questions = $question->getQuestions(); |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
|
|
$filteredQuestions = array_filter($questions, function ($value) use ($tag) { |
110
|
|
|
return in_array($tag, $value->tags); |
111
|
|
|
}); |
112
|
|
|
|
113
|
|
|
$views = [ |
114
|
|
|
["comment/question/view-all", ["questions" => $filteredQuestions], "main"] |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
$this->di->get("pageRenderComment")->renderPage([ |
118
|
|
|
"views" => $views, |
119
|
|
|
"title" => "Questions | $tag" |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* View all comments and create question form |
125
|
|
|
* |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
public function getPostCreateQuestion() |
129
|
|
|
{ |
130
|
|
|
$question = new Question($this->di->get("db")); |
131
|
|
|
|
132
|
|
|
$form = new CreateQuestionForm($this->di); |
133
|
|
|
$form->check(); |
134
|
|
|
|
135
|
|
|
$views = [ |
136
|
|
|
["comment/question/create-question", ["form" => $form->getHTML()], "main"], |
137
|
|
|
["comment/question/view-all", ["questions" => $question->getQuestions()], "main"] |
138
|
|
|
]; |
139
|
|
|
|
140
|
|
|
// If not logged in, render other views |
141
|
|
|
if (!$this->di->get("session")->has("user")) { |
142
|
|
|
$views = [ |
143
|
|
|
["comment/loginComment", [], "main"] |
144
|
|
|
]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->di->get("pageRenderComment")->renderPage([ |
148
|
|
|
"views" => $views, |
149
|
|
|
"title" => "Create your question" |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.