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 |
|
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
|
|
|
if ($question->find("id", $id) == null) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
$question = $question->getQuestion($id); |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
// This is a bad practice and should be in the module, (fix this after kmom10 future) |
52
|
|
|
// getting actual user Objects for posts for reputation show in view. |
53
|
|
|
$question->question->userObj = $user->getUser($question->user); |
|
|
|
|
54
|
|
|
$question->userObj = $user->getUser($question->user); |
55
|
|
|
|
56
|
|
|
$question->answers = array_map(function ($answer) { |
57
|
|
|
$user = new User($this->di->get("db")); |
58
|
|
|
$answer->userObj = $user->getUser($answer->user); |
59
|
|
|
$answer->vote->score = $answer->vote->score == null ? 0 : $answer->vote->score; |
60
|
|
|
return $answer; |
61
|
|
|
}, $question->answers); |
62
|
|
|
|
63
|
|
|
// Get query for up or down |
64
|
|
|
$order = isset($_GET["order"]) ? $_GET["order"] : "up"; |
65
|
|
|
|
66
|
|
|
// Highest points |
67
|
|
|
if ($sort == "points") { |
68
|
|
|
usort($question->answers, function ($current, $next) { |
69
|
|
|
return $current->vote->score > $next->vote->score; |
70
|
|
|
}); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Accepted top |
74
|
|
|
if ($sort == "accepted") { |
75
|
|
|
usort($question->answers, function ($current, $next) { |
|
|
|
|
76
|
|
|
return $current->accepted == "yes" ? 1 : -1; |
77
|
|
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
// Highest votes |
82
|
|
|
if ($sort == "vote") { |
83
|
|
|
usort($question->answers, function ($current, $next) { |
84
|
|
|
return count($current->vote->likes) > count($next->vote->likes); |
85
|
|
|
}); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// If up array_reverse, "down" is default from module |
89
|
|
|
if ($order == "up") { |
90
|
|
|
$question->answers = array_reverse($question->answers); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$form = new CreateAnswerForm($this->di, $id); |
94
|
|
|
$form->check(); |
95
|
|
|
|
96
|
|
|
$views = [ |
97
|
|
|
["comment/question/view/view-question", ["question" => $question], "question"], |
98
|
|
|
["comment/question/view/view-answers", |
99
|
|
|
["answers" => $question->answers, "questionId" => $question->question->questionId], "question"], |
100
|
|
|
["comment/question/view/post-answer", ["form" => $form->getHTML()], "form"], |
101
|
|
|
["comment/question/view/wrappedField", ["question" => $question], "main"] |
102
|
|
|
]; |
103
|
|
|
$this->di->get("pageRenderComment")->renderPage([ |
104
|
|
|
"views" => $views, |
105
|
|
|
"title" => "Fråga $question->id" |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Show all items. |
113
|
|
|
* |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public function getTaggedQuestions($tag) |
117
|
|
|
{ |
118
|
|
|
$question = new Question($this->di->get("db")); |
119
|
|
|
|
120
|
|
|
$questions = $question->getQuestions(); |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
|
124
|
|
|
$filteredQuestions = array_filter($questions, function ($value) use ($tag) { |
125
|
|
|
return in_array($tag, $value->tags); |
126
|
|
|
}); |
127
|
|
|
|
128
|
|
|
$views = [ |
129
|
|
|
["comment/question/view-all", ["questions" => $filteredQuestions], "main"] |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
$this->di->get("pageRenderComment")->renderPage([ |
133
|
|
|
"views" => $views, |
134
|
|
|
"title" => "Questions | $tag" |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* View all comments and create question form |
140
|
|
|
* |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
public function getPostCreateQuestion() |
144
|
|
|
{ |
145
|
|
|
$question = new Question($this->di->get("db")); |
146
|
|
|
|
147
|
|
|
$form = new CreateQuestionForm($this->di); |
148
|
|
|
$form->check(); |
149
|
|
|
|
150
|
|
|
$views = [ |
151
|
|
|
["comment/question/create-question", ["form" => $form->getHTML()], "main"], |
152
|
|
|
["comment/question/view-all", ["questions" => $question->getQuestions()], "main"] |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
// If not logged in, render other views |
156
|
|
|
if (!$this->di->get("session")->has("user")) { |
157
|
|
|
$views = [ |
158
|
|
|
["comment/loginComment", [], "main"] |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->di->get("pageRenderComment")->renderPage([ |
163
|
|
|
"views" => $views, |
164
|
|
|
"title" => "Create your question" |
165
|
|
|
]); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.