1 | <?php |
||||
2 | |||||
3 | namespace Pamo\Question; |
||||
4 | |||||
5 | use Anax\Commons\ContainerInjectableInterface; |
||||
6 | use Anax\Commons\ContainerInjectableTrait; |
||||
7 | |||||
8 | /** |
||||
9 | * Question controller. |
||||
10 | */ |
||||
11 | class QuestionController implements ContainerInjectableInterface |
||||
12 | { |
||||
13 | use ContainerInjectableTrait; |
||||
14 | |||||
15 | |||||
16 | |||||
17 | /** |
||||
18 | * The initialize method is optional and will always be called before the |
||||
19 | * target method/action. This is a convienient method where you could |
||||
20 | * setup internal properties that are commonly used by several methods. |
||||
21 | * |
||||
22 | * @return void |
||||
23 | */ |
||||
24 | 7 | public function initialize() : void |
|||
25 | { |
||||
26 | 7 | $this->base = "question"; |
|||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
27 | 7 | $this->title = "Questions"; |
|||
0 ignored issues
–
show
|
|||||
28 | 7 | $this->game = $this->di->get("game"); |
|||
0 ignored issues
–
show
|
|||||
29 | 7 | $this->sortBy = $this->game->getQSort(); |
|||
0 ignored issues
–
show
|
|||||
30 | 7 | $this->sortType = $this->game->getQSortType(); |
|||
0 ignored issues
–
show
|
|||||
31 | 7 | $this->page = $this->di->get("page"); |
|||
0 ignored issues
–
show
|
|||||
32 | 7 | $this->page->add("block/nav-admin", $this->game->getNav()); |
|||
33 | 7 | } |
|||
34 | |||||
35 | |||||
36 | |||||
37 | /** |
||||
38 | * Show all questions. |
||||
39 | * |
||||
40 | * @return object as a response object |
||||
41 | */ |
||||
42 | 1 | public function indexActionGet() : object |
|||
43 | { |
||||
44 | 1 | $groupConcat = "( |
|||
45 | SELECT GROUP_CONCAT(tagname, ',') |
||||
46 | FROM 'Tag2Question' |
||||
47 | WHERE Tag2Question.questionid = Question.id) AS tags"; |
||||
48 | 1 | $this->page->add($this->base . "/crud/view-all", [ |
|||
49 | 1 | "title" => "All Questions", |
|||
50 | 1 | "question" => $this->game->tagQuestion->joinConcatOrder("Question", "questionid = Question.id", $groupConcat, "Question.id", "$this->sortBy $this->sortType"), |
|||
51 | 1 | "answer" => $this->game->answer, |
|||
52 | 1 | "activeSort" => "$this->sortBy $this->sortType" |
|||
53 | ]); |
||||
54 | |||||
55 | 1 | return $this->page->render([ |
|||
56 | 1 | "title" => $this->title, |
|||
57 | ]); |
||||
58 | } |
||||
59 | |||||
60 | |||||
61 | |||||
62 | /** |
||||
63 | * Show all questions. |
||||
64 | * |
||||
65 | * @return object as a response object |
||||
66 | */ |
||||
67 | 1 | public function tagActionGet($tag) : object |
|||
68 | { |
||||
69 | 1 | $tag = str_replace("-", " ", $tag); |
|||
70 | 1 | $groupConcat = "( |
|||
71 | SELECT GROUP_CONCAT(tagname, ',') |
||||
72 | FROM 'Tag2Question' |
||||
73 | WHERE Tag2Question.questionid = Question.id) AS tags"; |
||||
74 | 1 | $this->page->add($this->base . "/crud/view-all", [ |
|||
75 | 1 | "title" => "Questions for $tag", |
|||
76 | 1 | "question" => $this->game->tagQuestion->joinWhereConcatOrder("tagname = ?", $tag, "Question", "questionid = Question.id", $groupConcat, "Question.id", "$this->sortBy $this->sortType"), |
|||
77 | 1 | "answer" => $this->game->answer, |
|||
78 | 1 | "activeSort" => "$this->sortBy $this->sortType" |
|||
79 | ]); |
||||
80 | |||||
81 | 1 | return $this->page->render([ |
|||
82 | 1 | "title" => "$this->title | Tags", |
|||
83 | ]); |
||||
84 | } |
||||
85 | |||||
86 | |||||
87 | |||||
88 | /** |
||||
89 | * Handler with form to create a new item. |
||||
90 | * |
||||
91 | * @return object as a response object |
||||
92 | */ |
||||
93 | 1 | public function createAction() : object |
|||
94 | { |
||||
95 | 1 | if (!$this->game->activeUser()) { |
|||
96 | 1 | return $this->di->get("response")->redirect("user/login"); |
|||
97 | } |
||||
98 | |||||
99 | 1 | $form = $this->game->getQuestionForm("create"); |
|||
100 | 1 | $form->check(); |
|||
101 | |||||
102 | 1 | $this->page->add($this->base . "/crud/create", [ |
|||
103 | 1 | "form" => $form->getHTML(), |
|||
104 | ]); |
||||
105 | |||||
106 | 1 | return $this->page->render([ |
|||
107 | 1 | "title" => "$this->title | Ask", |
|||
108 | ]); |
||||
109 | } |
||||
110 | |||||
111 | |||||
112 | |||||
113 | /** |
||||
114 | * Handler with form to update an item. |
||||
115 | * |
||||
116 | * @param int $id the id to update. |
||||
117 | * |
||||
118 | * @return null |
||||
119 | */ |
||||
120 | 1 | public function acceptAction(int $id, int $answerId) |
|||
121 | { |
||||
122 | 1 | if (!$this->game->activeUser()) { |
|||
123 | 1 | return $this->di->get("response")->redirect("user/login"); |
|||
124 | } |
||||
125 | |||||
126 | 1 | $question = $this->game->question; |
|||
127 | 1 | $question->find("id", $id); |
|||
128 | 1 | $question->accepted = $answerId; |
|||
129 | |||||
130 | 1 | $question->save(); |
|||
131 | |||||
132 | 1 | return $this->di->get("response")->redirect("game/question/$id")->send(); |
|||
133 | } |
||||
134 | |||||
135 | |||||
136 | |||||
137 | /** |
||||
138 | * Handler with form to update an item. |
||||
139 | * |
||||
140 | * @param int $id the id to update. |
||||
141 | * |
||||
142 | * @return null |
||||
143 | */ |
||||
144 | 1 | public function unacceptAction(int $id) |
|||
145 | { |
||||
146 | 1 | if (!$this->game->activeUser()) { |
|||
147 | 1 | return $this->di->get("response")->redirect("user/login"); |
|||
148 | } |
||||
149 | |||||
150 | 1 | $question = $this->game->question; |
|||
151 | 1 | $question->find("id", $id); |
|||
152 | 1 | $question->accepted = null; |
|||
153 | |||||
154 | 1 | $question->save(); |
|||
155 | |||||
156 | 1 | return $this->di->get("response")->redirect("game/question/$id")->send(); |
|||
157 | } |
||||
158 | |||||
159 | |||||
160 | |||||
161 | /** |
||||
162 | * This sample method dumps the content of $di. |
||||
163 | * GET mountpoint/dump-app |
||||
164 | * |
||||
165 | * @return string |
||||
166 | */ |
||||
167 | 1 | public function dumpDiActionGet() : string |
|||
168 | { |
||||
169 | // Deal with the action and return a response. |
||||
170 | 1 | $services = implode(", ", $this->di->getServices()); |
|||
171 | 1 | return __METHOD__ . "<p>\$di contains: $services"; |
|||
172 | } |
||||
173 | |||||
174 | |||||
175 | |||||
176 | /** |
||||
177 | * Adding an optional catchAll() method will catch all actions sent to the |
||||
178 | * router. You can then reply with an actual response or return void to |
||||
179 | * allow for the router to move on to next handler. |
||||
180 | * A catchAll() handles the following, if a specific action method is not |
||||
181 | * created: |
||||
182 | * ANY METHOD mountpoint/** |
||||
183 | * |
||||
184 | * @param array $args as a variadic parameter. |
||||
185 | * |
||||
186 | * @return mixed |
||||
187 | * |
||||
188 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||||
189 | */ |
||||
190 | 1 | public function catchAll(...$args) |
|||
0 ignored issues
–
show
The parameter
$args is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
191 | { |
||||
192 | // Deal with the request and send an actual response, or not. |
||||
193 | //return __METHOD__ . ", \$db is {$this->db}, got '" . count($args) . "' arguments: " . implode(", ", $args); |
||||
194 | 1 | return; |
|||
195 | } |
||||
196 | } |
||||
197 |