1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Seb\Answers\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Seb\Answers\Answers; |
8
|
|
|
use Seb\User\User; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Form to create an item. |
12
|
|
|
*/ |
13
|
|
|
class CreateAnswersForm extends FormModel |
14
|
|
|
{ |
15
|
|
|
public $qid; |
16
|
|
|
/** |
17
|
|
|
* Constructor injects with DI container. |
18
|
|
|
* |
19
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
20
|
|
|
*/ |
21
|
2 |
|
public function __construct(ContainerInterface $di, int $id) |
22
|
|
|
{ |
23
|
2 |
|
$this->qid = $id; |
24
|
2 |
|
parent::__construct($di); |
25
|
2 |
|
$this->form->create( |
26
|
|
|
[ |
27
|
2 |
|
"id" => __CLASS__, |
28
|
|
|
"legend" => "Create Answer", |
29
|
|
|
], |
30
|
|
|
[ |
31
|
|
|
|
32
|
|
|
"questionid" => [ |
33
|
2 |
|
"type" => "hidden", |
34
|
|
|
"validation" => ["not_empty"], |
35
|
|
|
"readonly" => true, |
36
|
2 |
|
"value" => $id, |
37
|
|
|
], |
38
|
|
|
|
39
|
|
|
"answer" => [ |
40
|
|
|
"type" => "textarea", |
41
|
|
|
"validation" => ["not_empty"], |
42
|
|
|
], |
43
|
|
|
|
44
|
|
|
"submit" => [ |
45
|
2 |
|
"type" => "submit", |
46
|
2 |
|
"value" => "Create item", |
47
|
2 |
|
"callback" => [$this, "callbackSubmit"] |
48
|
|
|
], |
49
|
|
|
] |
50
|
|
|
); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Callback for submit-button which should return true if it could |
55
|
|
|
* carry out its work and false if something failed. |
56
|
|
|
* |
57
|
|
|
* @return bool true if okey, false if something went wrong. |
58
|
|
|
*/ |
59
|
|
|
public function callbackSubmit() : bool |
60
|
|
|
{ |
61
|
|
|
$session = $this->di->get("session"); |
62
|
|
|
$answers = new Answers(); |
63
|
|
|
$answers->setDb($this->di->get("dbqb")); |
64
|
|
|
$answers->acronym = $session->get("acronym"); |
65
|
|
|
$answers->userid = $session->get("userid"); |
66
|
|
|
$answers->answer = $this->form->value("answer"); |
67
|
|
|
$answers->questionid = $this->form->value("questionid"); |
68
|
|
|
$answers->save(); |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Callback what to do if the form was successfully submitted, this |
74
|
|
|
* happen when the submit callback method returns true. This method |
75
|
|
|
* can/should be implemented by the subclass for a different behaviour. |
76
|
|
|
*/ |
77
|
|
|
public function callbackSuccess() |
78
|
|
|
{ |
79
|
|
|
$session = $this->di->get("session"); |
80
|
|
|
$userid = $session->get("userid"); |
81
|
|
|
$user = new User(); |
82
|
|
|
$user->setDb($this->di->get("dbqb")); |
83
|
|
|
$user->find("id", $userid); |
84
|
|
|
$user->score += 1; |
85
|
|
|
$user->save(); |
86
|
|
|
$this->di->get("response")->redirect("forum/topic/{$this->qid}")->send(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|